1 /* 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.tools.internal.jxc; 27 28 import java.io.File; 29 import java.util.ArrayList; 30 import java.util.List; 31 import java.util.regex.Pattern; 32 33 import com.sun.tools.internal.jxc.gen.config.NGCCRuntime; 34 35 import org.xml.sax.ErrorHandler; 36 import org.xml.sax.SAXException; 37 import org.xml.sax.SAXParseException; 38 39 40 /** 41 * Controls the validating and converting of values obtained 42 * from the config file. 43 * 44 * @author 45 * Bhakti Mehta (bhakti.mehta@sun.com) 46 */ 47 public final class NGCCRuntimeEx extends NGCCRuntime { 48 /** 49 * All the errors shall be sent to this object. 50 */ 51 private final ErrorHandler errorHandler; 52 53 public NGCCRuntimeEx(ErrorHandler errorHandler) { 54 this.errorHandler = errorHandler; 55 } 56 57 /** 58 * This will check if the baseDir provided by the user 59 * in the config file exists. If not it throws an error 60 * @param baseDir 61 * The baseDir attribute passed by the user in the xml config file as a path 62 * @return 63 * The file representation of the path name 64 */ 65 public File getBaseDir(String baseDir) throws SAXException { 66 File dir = new File(baseDir); 67 if (dir.exists()) { 68 return dir; 69 } else { 70 SAXParseException e = new SAXParseException( 71 Messages.BASEDIR_DOESNT_EXIST.format(dir.getAbsolutePath()), 72 getLocator()); 73 errorHandler.error(e); 74 throw e; // we can't recover from this error 75 } 76 } 77 78 /** 79 * This takes the include list provided by the user in the config file 80 * It converts the user values to {@link Pattern} 81 * @param includeContent 82 * The include list specified by the user 83 * @return 84 * A list of regular expression patterns {@link Pattern} 85 */ 86 public List<Pattern> getIncludePatterns(List<String> includeContent ) { 87 List<Pattern> includeRegexList = new ArrayList<Pattern>(); 88 for (String includes : includeContent) { 89 String regex = convertToRegex(includes); 90 Pattern pattern = Pattern.compile(regex); 91 includeRegexList.add(pattern); 92 } 93 return includeRegexList; 94 } 95 96 97 /** 98 * This takes the exclude list provided by the user in the config file 99 * It converts the user values to {@link Pattern} 100 * @param excludeContent 101 * The exclude list specified by the user 102 * @return 103 * A list of regular expression patterns {@link Pattern} 104 */ 105 public List getExcludePatterns(List<String> excludeContent ) { 106 List<Pattern> excludeRegexList = new ArrayList<Pattern>(); 107 for (String excludes : excludeContent) { 108 String regex = convertToRegex(excludes); 109 Pattern pattern = Pattern.compile(regex); 110 excludeRegexList.add(pattern); 111 } 112 return excludeRegexList; 113 } 114 115 116 /** 117 * This will tokenize the pattern and convert it into a regular expression 118 * @param pattern 119 */ 120 private String convertToRegex(String pattern) { 121 StringBuilder regex = new StringBuilder(); 122 char nc = ' '; 123 if (pattern.length() >0 ) { 124 125 for ( int i = 0 ; i < pattern.length(); i ++ ) { 126 char c = pattern.charAt(i); 127 nc = ' '; 128 if ((i +1) != pattern.length()) { 129 nc = pattern.charAt(i +1); 130 } 131 //escape single '.' 132 if (c == '.' && nc != '.'){ 133 regex.append('\\'); 134 regex.append('.'); 135 //do not allow patterns like a..b 136 } else if (c == '.'){ 137 continue; 138 // "**" gets replaced by ".*" 139 } else if ((c=='*') && (nc == '*')) { 140 regex.append(".*"); 141 break; 142 //'*' replaced by anything but '.' i.e [^\\.]+ 143 } else if (c=='*') { 144 regex.append("[^\\.]+"); 145 continue; 146 //'?' replaced by anything but '.' i.e [^\\.] 147 } else if (c=='?') { 148 regex.append("[^\\.]"); 149 //else leave the chars as they occur in the pattern 150 } else 151 regex.append(c); 152 } 153 154 } 155 156 return regex.toString(); 157 } 158 159 protected void unexpectedX(String token) throws SAXException { 160 errorHandler.error( 161 new SAXParseException(Messages.UNEXPECTED_NGCC_TOKEN.format( 162 token, getLocator().getLineNumber(), getLocator().getColumnNumber()), 163 getLocator())); 164 } 165 }