001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036: package com.sun.tools.jxc;
037:
038: import java.io.File;
039: import java.util.ArrayList;
040: import java.util.List;
041: import java.util.regex.Pattern;
042:
043: import com.sun.tools.jxc.gen.config.NGCCRuntime;
044:
045: import org.xml.sax.ErrorHandler;
046: import org.xml.sax.SAXException;
047: import org.xml.sax.SAXParseException;
048:
049: /**
050: * Controls the validating and converting of values obtained
051: * from the config file.
052: *
053: * @author
054: * Bhakti Mehta (bhakti.mehta@sun.com)
055: */
056: public final class NGCCRuntimeEx extends NGCCRuntime {
057: /**
058: * All the errors shall be sent to this object.
059: */
060: private final ErrorHandler errorHandler;
061:
062: public NGCCRuntimeEx(ErrorHandler errorHandler) {
063: this .errorHandler = errorHandler;
064: }
065:
066: /**
067: * This will check if the baseDir provided by the user
068: * in the config file exists. If not it throws an error
069: * @param baseDir
070: * The baseDir attribute passed by the user in the xml config file as a path
071: * @return
072: * The file representation of the path name
073: */
074: public File getBaseDir(String baseDir) throws SAXException {
075: File dir = new File(baseDir);
076: if (dir.exists()) {
077: return dir;
078: } else {
079: SAXParseException e = new SAXParseException(
080: Messages.BASEDIR_DOESNT_EXIST.format(dir
081: .getAbsolutePath()), getLocator());
082: errorHandler.error(e);
083: throw e; // we can't recover from this error
084: }
085: }
086:
087: /**
088: * This takes the include list provided by the user in the config file
089: * It converts the user values to {@link Pattern}
090: * @param includeContent
091: * The include list specified by the user
092: * @return
093: * A list of regular expression patterns {@link Pattern}
094: */
095: public List<Pattern> getIncludePatterns(List includeContent) {
096: List<Pattern> includeRegexList = new ArrayList<Pattern>();
097: for (int i = 0; i < includeContent.size(); i++) {
098: String includes = (String) includeContent.get(i);
099: String regex = convertToRegex(includes);
100: Pattern pattern = Pattern.compile(regex);
101: includeRegexList.add(pattern);
102: }
103: return includeRegexList;
104: }
105:
106: /**
107: * This takes the exclude list provided by the user in the config file
108: * It converts the user values to {@link Pattern}
109: * @param excludeContent
110: * The exclude list specified by the user
111: * @return
112: * A list of regular expression patterns {@link Pattern}
113: */
114: public List getExcludePatterns(List excludeContent) {
115: List excludeRegexList = new ArrayList();
116: for (int i = 0; i < excludeContent.size(); i++) {
117: String excludes = (String) excludeContent.get(i);
118: String regex = convertToRegex(excludes);
119: Pattern pattern = Pattern.compile(regex);
120: excludeRegexList.add(pattern);
121: }
122: return excludeRegexList;
123: }
124:
125: /**
126: * This will tokenize the pattern and convert it into a regular expression
127: * @param pattern
128: */
129: private String convertToRegex(String pattern) {
130: StringBuilder regex = new StringBuilder();
131: char nc = ' ';
132: if (pattern.length() > 0) {
133:
134: for (int i = 0; i < pattern.length(); i++) {
135: char c = pattern.charAt(i);
136: int j = i;
137: nc = ' ';
138: if ((j + 1) != pattern.length()) {
139: nc = pattern.charAt(j + 1);
140: }
141: //escape single '.'
142: if ((c == '.') && (nc != '.')) {
143: regex.append('\\');
144: regex.append('.');
145: //do not allow patterns like a..b
146: } else if ((c == '.') && (nc == '.')) {
147: continue;
148: // "**" gets replaced by ".*"
149: } else if ((c == '*') && (nc == '*')) {
150: regex.append(".*");
151: break;
152: //'*' replaced by anything but '.' i.e [^\\.]+
153: } else if (c == '*') {
154: regex.append("[^\\.]+");
155: continue;
156: //'?' replaced by anything but '.' i.e [^\\.]
157: } else if (c == '?') {
158: regex.append("[^\\.]");
159: //else leave the chars as they occur in the pattern
160: } else
161: regex.append(c);
162: }
163:
164: }
165:
166: return regex.toString();
167: }
168:
169: protected void unexpectedX(String token) throws SAXException {
170: errorHandler.error(new SAXParseException(
171: Messages.UNEXPECTED_NGCC_TOKEN.format(token,
172: getLocator().getLineNumber(), getLocator()
173: .getColumnNumber()), getLocator()));
174: }
175: }
|