1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/tools/internal/jxc/ConfigReader.java Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,283 @@ 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.io.IOException; 1.33 +import java.util.Collection; 1.34 +import java.util.HashMap; 1.35 +import java.util.HashSet; 1.36 +import java.util.List; 1.37 +import java.util.Map; 1.38 +import java.util.Set; 1.39 +import java.util.regex.Matcher; 1.40 +import java.util.regex.Pattern; 1.41 + 1.42 +import javax.xml.bind.SchemaOutputResolver; 1.43 +import javax.xml.parsers.ParserConfigurationException; 1.44 +import javax.xml.parsers.SAXParserFactory; 1.45 +import javax.xml.transform.Result; 1.46 +import javax.xml.transform.stream.StreamResult; 1.47 +import javax.xml.validation.ValidatorHandler; 1.48 + 1.49 +import javax.annotation.processing.ProcessingEnvironment; 1.50 +import javax.lang.model.element.TypeElement; 1.51 +import com.sun.tools.internal.jxc.gen.config.Config; 1.52 +import com.sun.tools.internal.jxc.gen.config.Schema; 1.53 +import com.sun.tools.internal.xjc.SchemaCache; 1.54 +import com.sun.tools.internal.xjc.api.Reference; 1.55 +import com.sun.tools.internal.xjc.util.ForkContentHandler; 1.56 + 1.57 +import org.xml.sax.ErrorHandler; 1.58 +import org.xml.sax.InputSource; 1.59 +import org.xml.sax.SAXException; 1.60 +import org.xml.sax.XMLReader; 1.61 + 1.62 + 1.63 +/** 1.64 + * This reads the config files passed by the user to annotation processing 1.65 + * and obtains a list of classes that need to be included 1.66 + * for a particular config from the set of classes passed 1.67 + * by the user to annotation processing. 1.68 + * 1.69 + * @author Bhakti Mehta (bhakti.mehta@sun.com) 1.70 + */ 1.71 +public final class ConfigReader { 1.72 + 1.73 + /** 1.74 + * The set of classes to be passed to XJC 1.75 + * 1.76 + */ 1.77 + private final Set<Reference> classesToBeIncluded = new HashSet<Reference>(); 1.78 + 1.79 + 1.80 + /** 1.81 + * The SchemaOutputResolver used to generate the schemas 1.82 + */ 1.83 + private final SchemaOutputResolver schemaOutputResolver; 1.84 + 1.85 + private final ProcessingEnvironment env; 1.86 + 1.87 + /** 1.88 + * 1.89 + * @param classes 1.90 + * The set of classes passed to the AnnotationProcessor 1.91 + * @param xmlFile 1.92 + * The configuration file. 1.93 + * @throws SAXException 1.94 + * If this is thrown, the error has already been reported. 1.95 + * @throws IOException 1.96 + * If any IO errors occur. 1.97 + */ 1.98 + public ConfigReader(ProcessingEnvironment env, Collection<? extends TypeElement> classes, File xmlFile, ErrorHandler errorHandler) throws SAXException, IOException { 1.99 + this.env = env; 1.100 + Config config = parseAndGetConfig(xmlFile,errorHandler); 1.101 + checkAllClasses(config,classes); 1.102 + String path = xmlFile.getAbsolutePath(); 1.103 + String xmlPath = path.substring(0,path.lastIndexOf(File.separatorChar)); 1.104 + schemaOutputResolver = createSchemaOutputResolver(config,xmlPath); 1.105 + 1.106 + } 1.107 + 1.108 + 1.109 + /** 1.110 + * This creates a regular expression 1.111 + * for the user pattern , matches the input classes 1.112 + * passed by the user and returns the final 1.113 + * list of classes that need to be included for a config file 1.114 + * after applying those patterns 1.115 + * 1.116 + */ 1.117 + public Collection<Reference> getClassesToBeIncluded() { 1.118 + return classesToBeIncluded; 1.119 + } 1.120 + 1.121 + private void checkAllClasses(Config config, Collection<? extends TypeElement> rootClasses) { 1.122 + 1.123 + List<Pattern> includeRegexList = config.getClasses().getIncludes(); 1.124 + List<Pattern> excludeRegexList = config.getClasses().getExcludes(); 1.125 + 1.126 + OUTER: 1.127 + for (TypeElement typeDecl : rootClasses) { 1.128 + 1.129 + String qualifiedName = typeDecl.getQualifiedName().toString(); 1.130 + 1.131 + for (Pattern pattern : excludeRegexList) { 1.132 + boolean match = checkPatternMatch(qualifiedName, pattern); 1.133 + if (match) 1.134 + continue OUTER; // excluded 1.135 + } 1.136 + 1.137 + for (Pattern pattern : includeRegexList) { 1.138 + boolean match = checkPatternMatch(qualifiedName, pattern); 1.139 + if (match) { 1.140 + classesToBeIncluded.add(new Reference(typeDecl,env)); 1.141 + break; 1.142 + } 1.143 + } 1.144 + } 1.145 + } 1.146 + 1.147 + /** 1.148 + * This returns the SchemaOutputResolver to generate the schemas 1.149 + */ 1.150 + public SchemaOutputResolver getSchemaOutputResolver(){ 1.151 + return schemaOutputResolver; 1.152 + } 1.153 + 1.154 + private SchemaOutputResolver createSchemaOutputResolver(Config config, String xmlpath) { 1.155 + File baseDir = new File(xmlpath, config.getBaseDir().getPath()); 1.156 + SchemaOutputResolverImpl schemaOutputResolver = new SchemaOutputResolverImpl (baseDir); 1.157 + 1.158 + for( Schema schema : (List<Schema>)config.getSchema() ) { 1.159 + String namespace = schema.getNamespace(); 1.160 + File location = schema.getLocation(); 1.161 + schemaOutputResolver.addSchemaInfo(namespace,location); 1.162 + } 1.163 + return schemaOutputResolver; 1.164 + } 1.165 + 1.166 + /** 1.167 + * This will check if the qualified name matches the pattern 1.168 + * 1.169 + * @param qualifiedName 1.170 + * The qualified name of the TypeDeclaration 1.171 + * @param pattern 1.172 + * The pattern obtained from the users input 1.173 + * 1.174 + */ 1.175 + private boolean checkPatternMatch(String qualifiedName, Pattern pattern) { 1.176 + Matcher matcher = pattern.matcher(qualifiedName); 1.177 + return matcher.matches(); 1.178 + } 1.179 + 1.180 + 1.181 + 1.182 + /** 1.183 + * Lazily parsed schema for the binding file. 1.184 + */ 1.185 + private static SchemaCache configSchema = new SchemaCache(Config.class.getResource("config.xsd")); 1.186 + 1.187 + 1.188 + /** 1.189 + * Parses an xml config file and returns a Config object. 1.190 + * 1.191 + * @param xmlFile 1.192 + * The xml config file which is passed by the user to annotation processing 1.193 + * @return 1.194 + * A non null Config object 1.195 + */ 1.196 + private Config parseAndGetConfig (File xmlFile, ErrorHandler errorHandler) throws SAXException, IOException { 1.197 + XMLReader reader; 1.198 + try { 1.199 + SAXParserFactory factory = SAXParserFactory.newInstance(); 1.200 + factory.setNamespaceAware(true); 1.201 + reader = factory.newSAXParser().getXMLReader(); 1.202 + } catch (ParserConfigurationException e) { 1.203 + // in practice this will never happen 1.204 + throw new Error(e); 1.205 + } 1.206 + NGCCRuntimeEx runtime = new NGCCRuntimeEx(errorHandler); 1.207 + 1.208 + // set up validator 1.209 + ValidatorHandler validator = configSchema.newValidator(); 1.210 + validator.setErrorHandler(errorHandler); 1.211 + 1.212 + // the validator will receive events first, then the parser. 1.213 + reader.setContentHandler(new ForkContentHandler(validator,runtime)); 1.214 + 1.215 + reader.setErrorHandler(errorHandler); 1.216 + Config config = new Config(runtime); 1.217 + runtime.setRootHandler(config); 1.218 + reader.parse(new InputSource(xmlFile.toURL().toExternalForm())); 1.219 + runtime.reset(); 1.220 + 1.221 + return config; 1.222 + } 1.223 + /** 1.224 + * Controls where the JAXB RI puts the generates 1.225 + * schema files. 1.226 + * @author 1.227 + * Bhakti Mehta (bhakti.mehta@sun.com) 1.228 + */ 1.229 + private static final class SchemaOutputResolverImpl extends SchemaOutputResolver{ 1.230 + 1.231 + /** 1.232 + * Directory to which we put the rest of the files. 1.233 + * Never be null. 1.234 + */ 1.235 + private final File baseDir; 1.236 + 1.237 + /** 1.238 + * Namespace URI to the location of the schema. 1.239 + * This captures what the user specifies. 1.240 + */ 1.241 + private final Map<String,File> schemas = new HashMap<String,File>(); 1.242 + 1.243 + 1.244 + /** 1.245 + * Decides where the schema file (of the given namespace URI) 1.246 + * will be written, and return it as a {@link Result} object. 1.247 + * 1.248 + */ 1.249 + public Result createOutput( String namespaceUri, String suggestedFileName ) { 1.250 + 1.251 + // the user's preference takes a precedence 1.252 + if(schemas.containsKey(namespaceUri)) { 1.253 + File loc = schemas.get(namespaceUri); 1.254 + if(loc==null) return null; // specifically not to generate a schema 1.255 + 1.256 + // create directories if necessary. we've already checked that the baseDir 1.257 + // exists, so this should be no surprise to users. 1.258 + loc.getParentFile().mkdirs(); 1.259 + 1.260 + return new StreamResult(loc); // generate into a file the user specified. 1.261 + } 1.262 + 1.263 + // if the user didn't say anything about this namespace, 1.264 + // generate it into the default directory with a default name. 1.265 + 1.266 + File schemaFile = new File (baseDir, suggestedFileName); 1.267 + // The systemId for the result will be schemaFile 1.268 + return new StreamResult(schemaFile); 1.269 + } 1.270 + 1.271 + 1.272 + public SchemaOutputResolverImpl(File baseDir) { 1.273 + assert baseDir!=null; 1.274 + this.baseDir = baseDir; 1.275 + } 1.276 + 1.277 + public void addSchemaInfo(String namespaceUri, File location) { 1.278 + if (namespaceUri == null ) 1.279 + //generate elements in no namespace 1.280 + namespaceUri = ""; 1.281 + schemas.put(namespaceUri, location); 1.282 + 1.283 + } 1.284 + 1.285 + } 1.286 +}