ohair@286: /* ohair@286: * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. ohair@286: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ohair@286: * ohair@286: * This code is free software; you can redistribute it and/or modify it ohair@286: * under the terms of the GNU General Public License version 2 only, as ohair@286: * published by the Free Software Foundation. Oracle designates this ohair@286: * particular file as subject to the "Classpath" exception as provided ohair@286: * by Oracle in the LICENSE file that accompanied this code. ohair@286: * ohair@286: * This code is distributed in the hope that it will be useful, but WITHOUT ohair@286: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ohair@286: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ohair@286: * version 2 for more details (a copy is included in the LICENSE file that ohair@286: * accompanied this code). ohair@286: * ohair@286: * You should have received a copy of the GNU General Public License version ohair@286: * 2 along with this work; if not, write to the Free Software Foundation, ohair@286: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ohair@286: * ohair@286: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@286: * or visit www.oracle.com if you need additional information or have any ohair@286: * questions. ohair@286: */ ohair@286: ohair@286: package com.sun.tools.internal.jxc; ohair@286: ohair@286: import java.io.File; ohair@286: import java.util.ArrayList; ohair@286: import java.util.List; ohair@286: import java.util.regex.Pattern; ohair@286: ohair@286: import com.sun.tools.internal.jxc.gen.config.NGCCRuntime; ohair@286: ohair@286: import org.xml.sax.ErrorHandler; ohair@286: import org.xml.sax.SAXException; ohair@286: import org.xml.sax.SAXParseException; ohair@286: ohair@286: ohair@286: /** ohair@286: * Controls the validating and converting of values obtained ohair@286: * from the config file. ohair@286: * ohair@286: * @author ohair@286: * Bhakti Mehta (bhakti.mehta@sun.com) ohair@286: */ ohair@286: public final class NGCCRuntimeEx extends NGCCRuntime { ohair@286: /** ohair@286: * All the errors shall be sent to this object. ohair@286: */ ohair@286: private final ErrorHandler errorHandler; ohair@286: ohair@286: public NGCCRuntimeEx(ErrorHandler errorHandler) { ohair@286: this.errorHandler = errorHandler; ohair@286: } ohair@286: ohair@286: /** ohair@286: * This will check if the baseDir provided by the user ohair@286: * in the config file exists. If not it throws an error ohair@286: * @param baseDir ohair@286: * The baseDir attribute passed by the user in the xml config file as a path ohair@286: * @return ohair@286: * The file representation of the path name ohair@286: */ ohair@286: public File getBaseDir(String baseDir) throws SAXException { ohair@286: File dir = new File(baseDir); ohair@286: if (dir.exists()) { ohair@286: return dir; ohair@286: } else { ohair@286: SAXParseException e = new SAXParseException( ohair@286: Messages.BASEDIR_DOESNT_EXIST.format(dir.getAbsolutePath()), ohair@286: getLocator()); ohair@286: errorHandler.error(e); ohair@286: throw e; // we can't recover from this error ohair@286: } ohair@286: } ohair@286: ohair@286: /** ohair@286: * This takes the include list provided by the user in the config file ohair@286: * It converts the user values to {@link Pattern} ohair@286: * @param includeContent ohair@286: * The include list specified by the user ohair@286: * @return ohair@286: * A list of regular expression patterns {@link Pattern} ohair@286: */ alanb@368: public List getIncludePatterns(List includeContent ) { ohair@286: List includeRegexList = new ArrayList(); alanb@368: for (String includes : includeContent) { ohair@286: String regex = convertToRegex(includes); ohair@286: Pattern pattern = Pattern.compile(regex); ohair@286: includeRegexList.add(pattern); ohair@286: } ohair@286: return includeRegexList; ohair@286: } ohair@286: ohair@286: ohair@286: /** ohair@286: * This takes the exclude list provided by the user in the config file ohair@286: * It converts the user values to {@link Pattern} ohair@286: * @param excludeContent ohair@286: * The exclude list specified by the user ohair@286: * @return ohair@286: * A list of regular expression patterns {@link Pattern} ohair@286: */ alanb@368: public List getExcludePatterns(List excludeContent ) { alanb@368: List excludeRegexList = new ArrayList(); alanb@368: for (String excludes : excludeContent) { ohair@286: String regex = convertToRegex(excludes); ohair@286: Pattern pattern = Pattern.compile(regex); ohair@286: excludeRegexList.add(pattern); ohair@286: } ohair@286: return excludeRegexList; ohair@286: } ohair@286: ohair@286: ohair@286: /** ohair@286: * This will tokenize the pattern and convert it into a regular expression ohair@286: * @param pattern ohair@286: */ ohair@286: private String convertToRegex(String pattern) { ohair@286: StringBuilder regex = new StringBuilder(); ohair@286: char nc = ' '; ohair@286: if (pattern.length() >0 ) { ohair@286: ohair@286: for ( int i = 0 ; i < pattern.length(); i ++ ) { ohair@286: char c = pattern.charAt(i); ohair@286: nc = ' '; alanb@368: if ((i +1) != pattern.length()) { alanb@368: nc = pattern.charAt(i +1); ohair@286: } ohair@286: //escape single '.' alanb@368: if (c == '.' && nc != '.'){ ohair@286: regex.append('\\'); ohair@286: regex.append('.'); ohair@286: //do not allow patterns like a..b alanb@368: } else if (c == '.'){ ohair@286: continue; ohair@286: // "**" gets replaced by ".*" ohair@286: } else if ((c=='*') && (nc == '*')) { ohair@286: regex.append(".*"); ohair@286: break; ohair@286: //'*' replaced by anything but '.' i.e [^\\.]+ ohair@286: } else if (c=='*') { ohair@286: regex.append("[^\\.]+"); ohair@286: continue; ohair@286: //'?' replaced by anything but '.' i.e [^\\.] ohair@286: } else if (c=='?') { ohair@286: regex.append("[^\\.]"); ohair@286: //else leave the chars as they occur in the pattern ohair@286: } else ohair@286: regex.append(c); ohair@286: } ohair@286: ohair@286: } ohair@286: ohair@286: return regex.toString(); ohair@286: } ohair@286: ohair@286: protected void unexpectedX(String token) throws SAXException { ohair@286: errorHandler.error( ohair@286: new SAXParseException(Messages.UNEXPECTED_NGCC_TOKEN.format( ohair@286: token, getLocator().getLineNumber(), getLocator().getColumnNumber()), ohair@286: getLocator())); ohair@286: } ohair@286: }