src/share/jaxws_classes/com/sun/tools/internal/jxc/ConfigReader.java

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 368
0989ad8c0860
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.tools.internal.jxc;
aoqi@0 27
aoqi@0 28 import com.sun.tools.internal.jxc.ap.Options;
aoqi@0 29 import java.io.File;
aoqi@0 30 import java.io.IOException;
aoqi@0 31 import java.util.Collection;
aoqi@0 32 import java.util.HashMap;
aoqi@0 33 import java.util.HashSet;
aoqi@0 34 import java.util.List;
aoqi@0 35 import java.util.Map;
aoqi@0 36 import java.util.Set;
aoqi@0 37 import java.util.regex.Matcher;
aoqi@0 38 import java.util.regex.Pattern;
aoqi@0 39
aoqi@0 40 import javax.xml.bind.SchemaOutputResolver;
aoqi@0 41 import javax.xml.parsers.ParserConfigurationException;
aoqi@0 42 import javax.xml.parsers.SAXParserFactory;
aoqi@0 43 import javax.xml.transform.Result;
aoqi@0 44 import javax.xml.transform.stream.StreamResult;
aoqi@0 45 import javax.xml.validation.ValidatorHandler;
aoqi@0 46
aoqi@0 47 import javax.annotation.processing.ProcessingEnvironment;
aoqi@0 48 import javax.lang.model.element.TypeElement;
aoqi@0 49 import com.sun.tools.internal.jxc.gen.config.Config;
aoqi@0 50 import com.sun.tools.internal.jxc.gen.config.Schema;
aoqi@0 51 import com.sun.tools.internal.xjc.SchemaCache;
aoqi@0 52 import com.sun.tools.internal.xjc.api.Reference;
aoqi@0 53 import com.sun.tools.internal.xjc.util.ForkContentHandler;
aoqi@0 54
aoqi@0 55 import com.sun.xml.internal.bind.v2.util.XmlFactory;
aoqi@0 56 import org.xml.sax.ErrorHandler;
aoqi@0 57 import org.xml.sax.InputSource;
aoqi@0 58 import org.xml.sax.SAXException;
aoqi@0 59 import org.xml.sax.XMLReader;
aoqi@0 60
aoqi@0 61
aoqi@0 62 /**
aoqi@0 63 * This reads the config files passed by the user to annotation processing
aoqi@0 64 * and obtains a list of classes that need to be included
aoqi@0 65 * for a particular config from the set of classes passed
aoqi@0 66 * by the user to annotation processing.
aoqi@0 67 *
aoqi@0 68 * @author Bhakti Mehta (bhakti.mehta@sun.com)
aoqi@0 69 */
aoqi@0 70 public final class ConfigReader {
aoqi@0 71
aoqi@0 72 /**
aoqi@0 73 * The set of classes to be passed to XJC
aoqi@0 74 *
aoqi@0 75 */
aoqi@0 76 private final Set<Reference> classesToBeIncluded = new HashSet<Reference>();
aoqi@0 77
aoqi@0 78
aoqi@0 79 /**
aoqi@0 80 * The SchemaOutputResolver used to generate the schemas
aoqi@0 81 */
aoqi@0 82 private final SchemaOutputResolver schemaOutputResolver;
aoqi@0 83
aoqi@0 84 private final ProcessingEnvironment env;
aoqi@0 85
aoqi@0 86 /**
aoqi@0 87 *
aoqi@0 88 * @param classes
aoqi@0 89 * The set of classes passed to the AnnotationProcessor
aoqi@0 90 * @param xmlFile
aoqi@0 91 * The configuration file.
aoqi@0 92 * @throws SAXException
aoqi@0 93 * If this is thrown, the error has already been reported.
aoqi@0 94 * @throws IOException
aoqi@0 95 * If any IO errors occur.
aoqi@0 96 */
aoqi@0 97 public ConfigReader(ProcessingEnvironment env, Collection<? extends TypeElement> classes, File xmlFile, ErrorHandler errorHandler) throws SAXException, IOException {
aoqi@0 98 this.env = env;
aoqi@0 99 Config config = parseAndGetConfig(xmlFile, errorHandler, env.getOptions().containsKey(Options.DISABLE_XML_SECURITY));
aoqi@0 100 checkAllClasses(config,classes);
aoqi@0 101 String path = xmlFile.getAbsolutePath();
aoqi@0 102 String xmlPath = path.substring(0,path.lastIndexOf(File.separatorChar));
aoqi@0 103 schemaOutputResolver = createSchemaOutputResolver(config,xmlPath);
aoqi@0 104
aoqi@0 105 }
aoqi@0 106
aoqi@0 107
aoqi@0 108 /**
aoqi@0 109 * This creates a regular expression
aoqi@0 110 * for the user pattern , matches the input classes
aoqi@0 111 * passed by the user and returns the final
aoqi@0 112 * list of classes that need to be included for a config file
aoqi@0 113 * after applying those patterns
aoqi@0 114 *
aoqi@0 115 */
aoqi@0 116 public Collection<Reference> getClassesToBeIncluded() {
aoqi@0 117 return classesToBeIncluded;
aoqi@0 118 }
aoqi@0 119
aoqi@0 120 private void checkAllClasses(Config config, Collection<? extends TypeElement> rootClasses) {
aoqi@0 121
aoqi@0 122 List<Pattern> includeRegexList = config.getClasses().getIncludes();
aoqi@0 123 List<Pattern> excludeRegexList = config.getClasses().getExcludes();
aoqi@0 124
aoqi@0 125 OUTER:
aoqi@0 126 for (TypeElement typeDecl : rootClasses) {
aoqi@0 127
aoqi@0 128 String qualifiedName = typeDecl.getQualifiedName().toString();
aoqi@0 129
aoqi@0 130 for (Pattern pattern : excludeRegexList) {
aoqi@0 131 boolean match = checkPatternMatch(qualifiedName, pattern);
aoqi@0 132 if (match)
aoqi@0 133 continue OUTER; // excluded
aoqi@0 134 }
aoqi@0 135
aoqi@0 136 for (Pattern pattern : includeRegexList) {
aoqi@0 137 boolean match = checkPatternMatch(qualifiedName, pattern);
aoqi@0 138 if (match) {
aoqi@0 139 classesToBeIncluded.add(new Reference(typeDecl,env));
aoqi@0 140 break;
aoqi@0 141 }
aoqi@0 142 }
aoqi@0 143 }
aoqi@0 144 }
aoqi@0 145
aoqi@0 146 /**
aoqi@0 147 * This returns the SchemaOutputResolver to generate the schemas
aoqi@0 148 */
aoqi@0 149 public SchemaOutputResolver getSchemaOutputResolver(){
aoqi@0 150 return schemaOutputResolver;
aoqi@0 151 }
aoqi@0 152
aoqi@0 153 private SchemaOutputResolver createSchemaOutputResolver(Config config, String xmlpath) {
aoqi@0 154 File baseDir = new File(xmlpath, config.getBaseDir().getPath());
aoqi@0 155 SchemaOutputResolverImpl outResolver = new SchemaOutputResolverImpl (baseDir);
aoqi@0 156
aoqi@0 157 for( Schema schema : (List<Schema>)config.getSchema() ) {
aoqi@0 158 String namespace = schema.getNamespace();
aoqi@0 159 File location = schema.getLocation();
aoqi@0 160 outResolver.addSchemaInfo(namespace,location);
aoqi@0 161 }
aoqi@0 162 return outResolver;
aoqi@0 163 }
aoqi@0 164
aoqi@0 165 /**
aoqi@0 166 * This will check if the qualified name matches the pattern
aoqi@0 167 *
aoqi@0 168 * @param qualifiedName
aoqi@0 169 * The qualified name of the TypeDeclaration
aoqi@0 170 * @param pattern
aoqi@0 171 * The pattern obtained from the users input
aoqi@0 172 *
aoqi@0 173 */
aoqi@0 174 private boolean checkPatternMatch(String qualifiedName, Pattern pattern) {
aoqi@0 175 Matcher matcher = pattern.matcher(qualifiedName);
aoqi@0 176 return matcher.matches();
aoqi@0 177 }
aoqi@0 178
aoqi@0 179
aoqi@0 180
aoqi@0 181 /**
aoqi@0 182 * Lazily parsed schema for the binding file.
aoqi@0 183 */
aoqi@0 184 private static SchemaCache configSchema = new SchemaCache(Config.class.getResource("config.xsd"));
aoqi@0 185
aoqi@0 186
aoqi@0 187 /**
aoqi@0 188 * Parses an xml config file and returns a Config object.
aoqi@0 189 *
aoqi@0 190 * @param xmlFile
aoqi@0 191 * The xml config file which is passed by the user to annotation processing
aoqi@0 192 * @return
aoqi@0 193 * A non null Config object
aoqi@0 194 */
aoqi@0 195 private Config parseAndGetConfig (File xmlFile, ErrorHandler errorHandler, boolean disableSecureProcessing) throws SAXException, IOException {
aoqi@0 196 XMLReader reader;
aoqi@0 197 try {
aoqi@0 198 SAXParserFactory factory = XmlFactory.createParserFactory(disableSecureProcessing);
aoqi@0 199 reader = factory.newSAXParser().getXMLReader();
aoqi@0 200 } catch (ParserConfigurationException e) {
aoqi@0 201 // in practice this will never happen
aoqi@0 202 throw new Error(e);
aoqi@0 203 }
aoqi@0 204 NGCCRuntimeEx runtime = new NGCCRuntimeEx(errorHandler);
aoqi@0 205
aoqi@0 206 // set up validator
aoqi@0 207 ValidatorHandler validator = configSchema.newValidator();
aoqi@0 208 validator.setErrorHandler(errorHandler);
aoqi@0 209
aoqi@0 210 // the validator will receive events first, then the parser.
aoqi@0 211 reader.setContentHandler(new ForkContentHandler(validator,runtime));
aoqi@0 212
aoqi@0 213 reader.setErrorHandler(errorHandler);
aoqi@0 214 Config config = new Config(runtime);
aoqi@0 215 runtime.setRootHandler(config);
aoqi@0 216 reader.parse(new InputSource(xmlFile.toURL().toExternalForm()));
aoqi@0 217 runtime.reset();
aoqi@0 218
aoqi@0 219 return config;
aoqi@0 220 }
aoqi@0 221 /**
aoqi@0 222 * Controls where the JAXB RI puts the generates
aoqi@0 223 * schema files.
aoqi@0 224 * @author
aoqi@0 225 * Bhakti Mehta (bhakti.mehta@sun.com)
aoqi@0 226 */
aoqi@0 227 private static final class SchemaOutputResolverImpl extends SchemaOutputResolver{
aoqi@0 228
aoqi@0 229 /**
aoqi@0 230 * Directory to which we put the rest of the files.
aoqi@0 231 * Never be null.
aoqi@0 232 */
aoqi@0 233 private final File baseDir;
aoqi@0 234
aoqi@0 235 /**
aoqi@0 236 * Namespace URI to the location of the schema.
aoqi@0 237 * This captures what the user specifies.
aoqi@0 238 */
aoqi@0 239 private final Map<String,File> schemas = new HashMap<String,File>();
aoqi@0 240
aoqi@0 241
aoqi@0 242 /**
aoqi@0 243 * Decides where the schema file (of the given namespace URI)
aoqi@0 244 * will be written, and return it as a {@link Result} object.
aoqi@0 245 *
aoqi@0 246 */
aoqi@0 247 public Result createOutput( String namespaceUri, String suggestedFileName ) {
aoqi@0 248
aoqi@0 249 // the user's preference takes a precedence
aoqi@0 250 if(schemas.containsKey(namespaceUri)) {
aoqi@0 251 File loc = schemas.get(namespaceUri);
aoqi@0 252 if(loc==null) return null; // specifically not to generate a schema
aoqi@0 253
aoqi@0 254 // create directories if necessary. we've already checked that the baseDir
aoqi@0 255 // exists, so this should be no surprise to users.
aoqi@0 256 loc.getParentFile().mkdirs();
aoqi@0 257
aoqi@0 258 return new StreamResult(loc); // generate into a file the user specified.
aoqi@0 259 }
aoqi@0 260
aoqi@0 261 // if the user didn't say anything about this namespace,
aoqi@0 262 // generate it into the default directory with a default name.
aoqi@0 263
aoqi@0 264 File schemaFile = new File (baseDir, suggestedFileName);
aoqi@0 265 // The systemId for the result will be schemaFile
aoqi@0 266 return new StreamResult(schemaFile);
aoqi@0 267 }
aoqi@0 268
aoqi@0 269
aoqi@0 270 public SchemaOutputResolverImpl(File baseDir) {
aoqi@0 271 assert baseDir!=null;
aoqi@0 272 this.baseDir = baseDir;
aoqi@0 273 }
aoqi@0 274
aoqi@0 275 public void addSchemaInfo(String namespaceUri, File location) {
aoqi@0 276 if (namespaceUri == null )
aoqi@0 277 //generate elements in no namespace
aoqi@0 278 namespaceUri = "";
aoqi@0 279 schemas.put(namespaceUri, location);
aoqi@0 280
aoqi@0 281 }
aoqi@0 282
aoqi@0 283 }
aoqi@0 284 }

mercurial