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