1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/tools/internal/jxc/NGCCRuntimeEx.java Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,168 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.tools.internal.jxc; 1.30 + 1.31 +import java.io.File; 1.32 +import java.util.ArrayList; 1.33 +import java.util.List; 1.34 +import java.util.regex.Pattern; 1.35 + 1.36 +import com.sun.tools.internal.jxc.gen.config.NGCCRuntime; 1.37 + 1.38 +import org.xml.sax.ErrorHandler; 1.39 +import org.xml.sax.SAXException; 1.40 +import org.xml.sax.SAXParseException; 1.41 + 1.42 + 1.43 +/** 1.44 + * Controls the validating and converting of values obtained 1.45 + * from the config file. 1.46 + * 1.47 + * @author 1.48 + * Bhakti Mehta (bhakti.mehta@sun.com) 1.49 + */ 1.50 +public final class NGCCRuntimeEx extends NGCCRuntime { 1.51 + /** 1.52 + * All the errors shall be sent to this object. 1.53 + */ 1.54 + private final ErrorHandler errorHandler; 1.55 + 1.56 + public NGCCRuntimeEx(ErrorHandler errorHandler) { 1.57 + this.errorHandler = errorHandler; 1.58 + } 1.59 + 1.60 + /** 1.61 + * This will check if the baseDir provided by the user 1.62 + * in the config file exists. If not it throws an error 1.63 + * @param baseDir 1.64 + * The baseDir attribute passed by the user in the xml config file as a path 1.65 + * @return 1.66 + * The file representation of the path name 1.67 + */ 1.68 + public File getBaseDir(String baseDir) throws SAXException { 1.69 + File dir = new File(baseDir); 1.70 + if (dir.exists()) { 1.71 + return dir; 1.72 + } else { 1.73 + SAXParseException e = new SAXParseException( 1.74 + Messages.BASEDIR_DOESNT_EXIST.format(dir.getAbsolutePath()), 1.75 + getLocator()); 1.76 + errorHandler.error(e); 1.77 + throw e; // we can't recover from this error 1.78 + } 1.79 + } 1.80 + 1.81 + /** 1.82 + * This takes the include list provided by the user in the config file 1.83 + * It converts the user values to {@link Pattern} 1.84 + * @param includeContent 1.85 + * The include list specified by the user 1.86 + * @return 1.87 + * A list of regular expression patterns {@link Pattern} 1.88 + */ 1.89 + public List<Pattern> getIncludePatterns(List includeContent ) { 1.90 + List<Pattern> includeRegexList = new ArrayList<Pattern>(); 1.91 + for (int i = 0 ; i < includeContent.size(); i ++){ 1.92 + String includes = (String)includeContent.get(i); 1.93 + String regex = convertToRegex(includes); 1.94 + Pattern pattern = Pattern.compile(regex); 1.95 + includeRegexList.add(pattern); 1.96 + } 1.97 + return includeRegexList; 1.98 + } 1.99 + 1.100 + 1.101 + /** 1.102 + * This takes the exclude list provided by the user in the config file 1.103 + * It converts the user values to {@link Pattern} 1.104 + * @param excludeContent 1.105 + * The exclude list specified by the user 1.106 + * @return 1.107 + * A list of regular expression patterns {@link Pattern} 1.108 + */ 1.109 + public List getExcludePatterns(List excludeContent ) { 1.110 + List excludeRegexList = new ArrayList(); 1.111 + for (int i = 0 ; i < excludeContent.size(); i ++){ 1.112 + String excludes = (String)excludeContent.get(i); 1.113 + String regex = convertToRegex(excludes); 1.114 + Pattern pattern = Pattern.compile(regex); 1.115 + excludeRegexList.add(pattern); 1.116 + } 1.117 + return excludeRegexList; 1.118 + } 1.119 + 1.120 + 1.121 + /** 1.122 + * This will tokenize the pattern and convert it into a regular expression 1.123 + * @param pattern 1.124 + */ 1.125 + private String convertToRegex(String pattern) { 1.126 + StringBuilder regex = new StringBuilder(); 1.127 + char nc = ' '; 1.128 + if (pattern.length() >0 ) { 1.129 + 1.130 + for ( int i = 0 ; i < pattern.length(); i ++ ) { 1.131 + char c = pattern.charAt(i); 1.132 + int j = i; 1.133 + nc = ' '; 1.134 + if ((j+1) != pattern.length()) { 1.135 + nc = pattern.charAt(j+1); 1.136 + } 1.137 + //escape single '.' 1.138 + if ((c=='.') && ( nc !='.')){ 1.139 + regex.append('\\'); 1.140 + regex.append('.'); 1.141 + //do not allow patterns like a..b 1.142 + } else if ((c=='.') && ( nc =='.')){ 1.143 + continue; 1.144 + // "**" gets replaced by ".*" 1.145 + } else if ((c=='*') && (nc == '*')) { 1.146 + regex.append(".*"); 1.147 + break; 1.148 + //'*' replaced by anything but '.' i.e [^\\.]+ 1.149 + } else if (c=='*') { 1.150 + regex.append("[^\\.]+"); 1.151 + continue; 1.152 + //'?' replaced by anything but '.' i.e [^\\.] 1.153 + } else if (c=='?') { 1.154 + regex.append("[^\\.]"); 1.155 + //else leave the chars as they occur in the pattern 1.156 + } else 1.157 + regex.append(c); 1.158 + } 1.159 + 1.160 + } 1.161 + 1.162 + return regex.toString(); 1.163 + } 1.164 + 1.165 + protected void unexpectedX(String token) throws SAXException { 1.166 + errorHandler.error( 1.167 + new SAXParseException(Messages.UNEXPECTED_NGCC_TOKEN.format( 1.168 + token, getLocator().getLineNumber(), getLocator().getColumnNumber()), 1.169 + getLocator())); 1.170 + } 1.171 +}