1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/tools/internal/xjc/Options.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,1053 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2013, 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.xjc; 1.30 + 1.31 +import java.io.BufferedReader; 1.32 +import java.io.File; 1.33 +import java.io.FileInputStream; 1.34 +import java.io.IOException; 1.35 +import java.io.InputStreamReader; 1.36 +import java.io.PrintWriter; 1.37 +import java.io.StringWriter; 1.38 +import java.lang.reflect.Array; 1.39 +import java.lang.reflect.InvocationTargetException; 1.40 +import java.net.MalformedURLException; 1.41 +import java.net.URL; 1.42 +import java.net.URLClassLoader; 1.43 +import java.text.SimpleDateFormat; 1.44 +import java.util.ArrayList; 1.45 +import java.util.Arrays; 1.46 +import java.util.Date; 1.47 +import java.util.Enumeration; 1.48 +import java.util.HashSet; 1.49 +import java.util.List; 1.50 +import java.util.Set; 1.51 + 1.52 +import com.sun.codemodel.internal.CodeWriter; 1.53 +import com.sun.codemodel.internal.JPackage; 1.54 +import com.sun.codemodel.internal.JResourceFile; 1.55 +import com.sun.codemodel.internal.writer.FileCodeWriter; 1.56 +import com.sun.codemodel.internal.writer.PrologCodeWriter; 1.57 +import com.sun.istack.internal.tools.DefaultAuthenticator; 1.58 +import com.sun.org.apache.xml.internal.resolver.CatalogManager; 1.59 +import com.sun.org.apache.xml.internal.resolver.tools.CatalogResolver; 1.60 +import com.sun.tools.internal.xjc.api.ClassNameAllocator; 1.61 +import com.sun.tools.internal.xjc.api.SpecVersion; 1.62 +import com.sun.tools.internal.xjc.generator.bean.field.FieldRendererFactory; 1.63 +import com.sun.tools.internal.xjc.model.Model; 1.64 +import com.sun.tools.internal.xjc.reader.Util; 1.65 +import com.sun.xml.internal.bind.api.impl.NameConverter; 1.66 +import java.net.URI; 1.67 +import java.net.URISyntaxException; 1.68 +import java.nio.charset.Charset; 1.69 +import java.nio.charset.IllegalCharsetNameException; 1.70 +import java.util.Locale; 1.71 +import java.util.logging.Level; 1.72 +import java.util.logging.Logger; 1.73 + 1.74 +import org.xml.sax.EntityResolver; 1.75 +import org.xml.sax.InputSource; 1.76 + 1.77 +/** 1.78 + * Global options. 1.79 + * 1.80 + * <p> 1.81 + * This class stores invocation configuration for XJC. 1.82 + * The configuration in this class should be abstract enough so that 1.83 + * it could be parsed from both command-line or Ant. 1.84 + */ 1.85 +public class Options 1.86 +{ 1.87 + /** If "-debug" is specified. */ 1.88 + public boolean debugMode; 1.89 + 1.90 + /** If the "-verbose" option is specified. */ 1.91 + public boolean verbose; 1.92 + 1.93 + /** If the "-quiet" option is specified. */ 1.94 + public boolean quiet; 1.95 + 1.96 + /** If the -readOnly option is specified. */ 1.97 + public boolean readOnly; 1.98 + 1.99 + /** No file header comment (to be more friendly with diff.) */ 1.100 + public boolean noFileHeader; 1.101 + 1.102 + /** When on, fixes getter/setter generation to match the Bean Introspection API */ 1.103 + public boolean enableIntrospection; 1.104 + 1.105 + /** When on, generates content property for types with multiple xs:any derived elements (which is supposed to be correct behaviour) */ 1.106 + public boolean contentForWildcard; 1.107 + 1.108 + /** Encoding to be used by generated java sources, null for platform default. */ 1.109 + public String encoding; 1.110 + 1.111 + /** 1.112 + * If true XML security features when parsing XML documents will be disabled. 1.113 + * The default value is false. 1.114 + * 1.115 + * Boolean 1.116 + * @since 2.2.6 1.117 + */ 1.118 + public boolean disableXmlSecurity; 1.119 + 1.120 + /** 1.121 + * Check the source schemas with extra scrutiny. 1.122 + * The exact meaning depends on the schema language. 1.123 + */ 1.124 + public boolean strictCheck =true; 1.125 + 1.126 + /** 1.127 + * If -explicit-annotation option is specified. 1.128 + * <p> 1.129 + * This generates code that works around issues specific to 1.4 runtime. 1.130 + */ 1.131 + public boolean runtime14 = false; 1.132 + 1.133 + /** 1.134 + * If true, try to resolve name conflicts automatically by assigning mechanical numbers. 1.135 + */ 1.136 + public boolean automaticNameConflictResolution = false; 1.137 + 1.138 + /** 1.139 + * strictly follow the compatibility rules and reject schemas that 1.140 + * contain features from App. E.2, use vendor binding extensions 1.141 + */ 1.142 + public static final int STRICT = 1; 1.143 + /** 1.144 + * loosely follow the compatibility rules and allow the use of vendor 1.145 + * binding extensions 1.146 + */ 1.147 + public static final int EXTENSION = 2; 1.148 + 1.149 + /** 1.150 + * this switch determines how carefully the compiler will follow 1.151 + * the compatibility rules in the spec. Either <code>STRICT</code> 1.152 + * or <code>EXTENSION</code>. 1.153 + */ 1.154 + public int compatibilityMode = STRICT; 1.155 + 1.156 + public boolean isExtensionMode() { 1.157 + return compatibilityMode==EXTENSION; 1.158 + } 1.159 + 1.160 + private static final Logger logger = com.sun.xml.internal.bind.Util.getClassLogger(); 1.161 + 1.162 + /** 1.163 + * Generates output for the specified version of the runtime. 1.164 + */ 1.165 + public SpecVersion target = SpecVersion.LATEST; 1.166 + 1.167 + 1.168 + public Options() { 1.169 + try { 1.170 + Class.forName("javax.xml.bind.JAXBPermission"); 1.171 + } catch (ClassNotFoundException cnfe) { 1.172 + target = SpecVersion.V2_1; 1.173 + } 1.174 + } 1.175 + 1.176 + /** 1.177 + * Target directory when producing files. 1.178 + * <p> 1.179 + * This field is not used when XJC is driven through the XJC API. 1.180 + * Plugins that need to generate extra files should do so by using 1.181 + * {@link JPackage#addResourceFile(JResourceFile)}. 1.182 + */ 1.183 + public File targetDir = new File("."); 1.184 + 1.185 + /** 1.186 + * Actually stores {@link CatalogResolver}, but the field 1.187 + * type is made to {@link EntityResolver} so that XJC can be 1.188 + * used even if resolver.jar is not available in the classpath. 1.189 + */ 1.190 + public EntityResolver entityResolver = null; 1.191 + 1.192 + /** 1.193 + * Type of input schema language. One of the <code>SCHEMA_XXX</code> 1.194 + * constants. 1.195 + */ 1.196 + private Language schemaLanguage = null; 1.197 + 1.198 + /** 1.199 + * The -p option that should control the default Java package that 1.200 + * will contain the generated code. Null if unspecified. 1.201 + */ 1.202 + public String defaultPackage = null; 1.203 + 1.204 + /** 1.205 + * Similar to the -p option, but this one works with a lower priority, 1.206 + * and customizations overrides this. Used by JAX-RPC. 1.207 + */ 1.208 + public String defaultPackage2 = null; 1.209 + 1.210 + /** 1.211 + * Input schema files as a list of {@link InputSource}s. 1.212 + */ 1.213 + private final List<InputSource> grammars = new ArrayList<InputSource>(); 1.214 + 1.215 + private final List<InputSource> bindFiles = new ArrayList<InputSource>(); 1.216 + 1.217 + // Proxy setting. 1.218 + private String proxyHost = null; 1.219 + private String proxyPort = null; 1.220 + public String proxyAuth = null; 1.221 + 1.222 + /** 1.223 + * {@link Plugin}s that are enabled in this compilation. 1.224 + */ 1.225 + public final List<Plugin> activePlugins = new ArrayList<Plugin>(); 1.226 + 1.227 + /** 1.228 + * All discovered {@link Plugin}s. 1.229 + * This is lazily parsed, so that we can take '-cp' option into account. 1.230 + * 1.231 + * @see #getAllPlugins() 1.232 + */ 1.233 + private List<Plugin> allPlugins; 1.234 + 1.235 + /** 1.236 + * Set of URIs that plug-ins recognize as extension bindings. 1.237 + */ 1.238 + public final Set<String> pluginURIs = new HashSet<String>(); 1.239 + 1.240 + /** 1.241 + * This allocator has the final say on deciding the class name. 1.242 + */ 1.243 + public ClassNameAllocator classNameAllocator; 1.244 + 1.245 + /** 1.246 + * This switch controls whether or not xjc will generate package level annotations 1.247 + */ 1.248 + public boolean packageLevelAnnotations = true; 1.249 + 1.250 + /** 1.251 + * This {@link FieldRendererFactory} determines how the fields are generated. 1.252 + */ 1.253 + private FieldRendererFactory fieldRendererFactory = new FieldRendererFactory(); 1.254 + /** 1.255 + * Used to detect if two {@link Plugin}s try to overwrite {@link #fieldRendererFactory}. 1.256 + */ 1.257 + private Plugin fieldRendererFactoryOwner = null; 1.258 + 1.259 + /** 1.260 + * If this is non-null, we use this {@link NameConverter} over the one 1.261 + * given in the schema/binding. 1.262 + */ 1.263 + private NameConverter nameConverter = null; 1.264 + /** 1.265 + * Used to detect if two {@link Plugin}s try to overwrite {@link #nameConverter}. 1.266 + */ 1.267 + private Plugin nameConverterOwner = null; 1.268 + 1.269 + /** 1.270 + * Gets the active {@link FieldRendererFactory} that shall be used to build {@link Model}. 1.271 + * 1.272 + * @return always non-null. 1.273 + */ 1.274 + public FieldRendererFactory getFieldRendererFactory() { 1.275 + return fieldRendererFactory; 1.276 + } 1.277 + 1.278 + /** 1.279 + * Sets the {@link FieldRendererFactory}. 1.280 + * 1.281 + * <p> 1.282 + * This method is for plugins to call to set a custom {@link FieldRendererFactory}. 1.283 + * 1.284 + * @param frf 1.285 + * The {@link FieldRendererFactory} to be installed. Must not be null. 1.286 + * @param owner 1.287 + * Identifies the plugin that owns this {@link FieldRendererFactory}. 1.288 + * When two {@link Plugin}s try to call this method, this allows XJC 1.289 + * to report it as a user-friendly error message. 1.290 + * 1.291 + * @throws BadCommandLineException 1.292 + * If a conflit happens, this exception carries a user-friendly error 1.293 + * message, indicating a conflict. 1.294 + */ 1.295 + public void setFieldRendererFactory(FieldRendererFactory frf, Plugin owner) throws BadCommandLineException { 1.296 + // since this method is for plugins, make it bit more fool-proof than usual 1.297 + if(frf==null) 1.298 + throw new IllegalArgumentException(); 1.299 + if(fieldRendererFactoryOwner!=null) { 1.300 + throw new BadCommandLineException( 1.301 + Messages.format(Messages.FIELD_RENDERER_CONFLICT, 1.302 + fieldRendererFactoryOwner.getOptionName(), 1.303 + owner.getOptionName() )); 1.304 + } 1.305 + this.fieldRendererFactoryOwner = owner; 1.306 + this.fieldRendererFactory = frf; 1.307 + } 1.308 + 1.309 + 1.310 + /** 1.311 + * Gets the active {@link NameConverter} that shall be used to build {@link Model}. 1.312 + * 1.313 + * @return can be null, in which case it's up to the binding. 1.314 + */ 1.315 + public NameConverter getNameConverter() { 1.316 + return nameConverter; 1.317 + } 1.318 + 1.319 + /** 1.320 + * Sets the {@link NameConverter}. 1.321 + * 1.322 + * <p> 1.323 + * This method is for plugins to call to set a custom {@link NameConverter}. 1.324 + * 1.325 + * @param nc 1.326 + * The {@link NameConverter} to be installed. Must not be null. 1.327 + * @param owner 1.328 + * Identifies the plugin that owns this {@link NameConverter}. 1.329 + * When two {@link Plugin}s try to call this method, this allows XJC 1.330 + * to report it as a user-friendly error message. 1.331 + * 1.332 + * @throws BadCommandLineException 1.333 + * If a conflit happens, this exception carries a user-friendly error 1.334 + * message, indicating a conflict. 1.335 + */ 1.336 + public void setNameConverter(NameConverter nc, Plugin owner) throws BadCommandLineException { 1.337 + // since this method is for plugins, make it bit more fool-proof than usual 1.338 + if(nc==null) 1.339 + throw new IllegalArgumentException(); 1.340 + if(nameConverter!=null) { 1.341 + throw new BadCommandLineException( 1.342 + Messages.format(Messages.NAME_CONVERTER_CONFLICT, 1.343 + nameConverterOwner.getOptionName(), 1.344 + owner.getOptionName() )); 1.345 + } 1.346 + this.nameConverterOwner = owner; 1.347 + this.nameConverter = nc; 1.348 + } 1.349 + 1.350 + /** 1.351 + * Gets all the {@link Plugin}s discovered so far. 1.352 + * 1.353 + * <p> 1.354 + * A plugins are enumerated when this method is called for the first time, 1.355 + * by taking {@link #classpaths} into account. That means 1.356 + * "-cp plugin.jar" has to come before you specify options to enable it. 1.357 + */ 1.358 + public List<Plugin> getAllPlugins() { 1.359 + if(allPlugins==null) { 1.360 + allPlugins = new ArrayList<Plugin>(); 1.361 + ClassLoader ucl = getUserClassLoader(SecureLoader.getClassClassLoader(getClass())); 1.362 + allPlugins.addAll(Arrays.asList(findServices(Plugin.class,ucl))); 1.363 + } 1.364 + 1.365 + return allPlugins; 1.366 + } 1.367 + 1.368 + public Language getSchemaLanguage() { 1.369 + if( schemaLanguage==null) 1.370 + schemaLanguage = guessSchemaLanguage(); 1.371 + return schemaLanguage; 1.372 + } 1.373 + public void setSchemaLanguage(Language _schemaLanguage) { 1.374 + this.schemaLanguage = _schemaLanguage; 1.375 + } 1.376 + 1.377 + /** Input schema files. */ 1.378 + public InputSource[] getGrammars() { 1.379 + return grammars.toArray(new InputSource[grammars.size()]); 1.380 + } 1.381 + 1.382 + /** 1.383 + * Adds a new input schema. 1.384 + */ 1.385 + public void addGrammar( InputSource is ) { 1.386 + grammars.add(absolutize(is)); 1.387 + } 1.388 + 1.389 + private InputSource fileToInputSource( File source ) { 1.390 + try { 1.391 + String url = source.toURL().toExternalForm(); 1.392 + return new InputSource(Util.escapeSpace(url)); 1.393 + } catch (MalformedURLException e) { 1.394 + return new InputSource(source.getPath()); 1.395 + } 1.396 + } 1.397 + 1.398 + public void addGrammar( File source ) { 1.399 + addGrammar(fileToInputSource(source)); 1.400 + } 1.401 + 1.402 + /** 1.403 + * Recursively scan directories and add all XSD files in it. 1.404 + */ 1.405 + public void addGrammarRecursive( File dir ) { 1.406 + addRecursive(dir,".xsd",grammars); 1.407 + } 1.408 + 1.409 + private void addRecursive( File dir, String suffix, List<InputSource> result ) { 1.410 + File[] files = dir.listFiles(); 1.411 + if(files==null) return; // work defensively 1.412 + 1.413 + for( File f : files ) { 1.414 + if(f.isDirectory()) 1.415 + addRecursive(f,suffix,result); 1.416 + else 1.417 + if(f.getPath().endsWith(suffix)) 1.418 + result.add(absolutize(fileToInputSource(f))); 1.419 + } 1.420 + } 1.421 + 1.422 + 1.423 + private InputSource absolutize(InputSource is) { 1.424 + // absolutize all the system IDs in the input, so that we can map system IDs to DOM trees. 1.425 + try { 1.426 + URL baseURL = new File(".").getCanonicalFile().toURL(); 1.427 + is.setSystemId( new URL(baseURL,is.getSystemId()).toExternalForm() ); 1.428 + } catch( IOException e ) { 1.429 + logger.log(Level.FINE, "{0}, {1}", new Object[]{is.getSystemId(), e.getLocalizedMessage()}); 1.430 + } 1.431 + return is; 1.432 + } 1.433 + 1.434 + /** Input external binding files. */ 1.435 + public InputSource[] getBindFiles() { 1.436 + return bindFiles.toArray(new InputSource[bindFiles.size()]); 1.437 + } 1.438 + 1.439 + /** 1.440 + * Adds a new binding file. 1.441 + */ 1.442 + public void addBindFile( InputSource is ) { 1.443 + bindFiles.add(absolutize(is)); 1.444 + } 1.445 + 1.446 + /** 1.447 + * Adds a new binding file. 1.448 + */ 1.449 + public void addBindFile( File bindFile ) { 1.450 + bindFiles.add(fileToInputSource(bindFile)); 1.451 + } 1.452 + 1.453 + /** 1.454 + * Recursively scan directories and add all ".xjb" files in it. 1.455 + */ 1.456 + public void addBindFileRecursive( File dir ) { 1.457 + addRecursive(dir,".xjb",bindFiles); 1.458 + } 1.459 + 1.460 + public final List<URL> classpaths = new ArrayList<URL>(); 1.461 + /** 1.462 + * Gets a classLoader that can load classes specified via the 1.463 + * -classpath option. 1.464 + */ 1.465 + public ClassLoader getUserClassLoader( ClassLoader parent ) { 1.466 + if (classpaths.isEmpty()) 1.467 + return parent; 1.468 + return new URLClassLoader( 1.469 + classpaths.toArray(new URL[classpaths.size()]),parent); 1.470 + } 1.471 + 1.472 + 1.473 + /** 1.474 + * Parses an option <code>args[i]</code> and return 1.475 + * the number of tokens consumed. 1.476 + * 1.477 + * @return 1.478 + * 0 if the argument is not understood. Returning 0 1.479 + * will let the caller report an error. 1.480 + * @exception BadCommandLineException 1.481 + * If the callee wants to provide a custom message for an error. 1.482 + */ 1.483 + public int parseArgument( String[] args, int i ) throws BadCommandLineException { 1.484 + if (args[i].equals("-classpath") || args[i].equals("-cp")) { 1.485 + String a = requireArgument(args[i], args, ++i); 1.486 + for (String p : a.split(File.pathSeparator)) { 1.487 + File file = new File(p); 1.488 + try { 1.489 + classpaths.add(file.toURL()); 1.490 + } catch (MalformedURLException e) { 1.491 + throw new BadCommandLineException( 1.492 + Messages.format(Messages.NOT_A_VALID_FILENAME,file),e); 1.493 + } 1.494 + } 1.495 + return 2; 1.496 + } 1.497 + if (args[i].equals("-d")) { 1.498 + targetDir = new File(requireArgument("-d",args,++i)); 1.499 + if( !targetDir.exists() ) 1.500 + throw new BadCommandLineException( 1.501 + Messages.format(Messages.NON_EXISTENT_DIR,targetDir)); 1.502 + return 2; 1.503 + } 1.504 + if (args[i].equals("-readOnly")) { 1.505 + readOnly = true; 1.506 + return 1; 1.507 + } 1.508 + if (args[i].equals("-p")) { 1.509 + defaultPackage = requireArgument("-p",args,++i); 1.510 + if(defaultPackage.length()==0) { // user specified default package 1.511 + // there won't be any package to annotate, so disable them 1.512 + // automatically as a usability feature 1.513 + packageLevelAnnotations = false; 1.514 + } 1.515 + return 2; 1.516 + } 1.517 + if (args[i].equals("-debug")) { 1.518 + debugMode = true; 1.519 + verbose = true; 1.520 + return 1; 1.521 + } 1.522 + if (args[i].equals("-nv")) { 1.523 + strictCheck = false; 1.524 + return 1; 1.525 + } 1.526 + if( args[i].equals("-npa")) { 1.527 + packageLevelAnnotations = false; 1.528 + return 1; 1.529 + } 1.530 + if( args[i].equals("-no-header")) { 1.531 + noFileHeader = true; 1.532 + return 1; 1.533 + } 1.534 + if (args[i].equals("-verbose")) { 1.535 + verbose = true; 1.536 + return 1; 1.537 + } 1.538 + if (args[i].equals("-quiet")) { 1.539 + quiet = true; 1.540 + return 1; 1.541 + } 1.542 + if (args[i].equals("-XexplicitAnnotation")) { 1.543 + runtime14 = true; 1.544 + return 1; 1.545 + } 1.546 + if (args[i].equals("-enableIntrospection")) { 1.547 + enableIntrospection = true; 1.548 + return 1; 1.549 + } 1.550 + if (args[i].equals("-disableXmlSecurity")) { 1.551 + disableXmlSecurity = true; 1.552 + return 1; 1.553 + } 1.554 + if (args[i].equals("-contentForWildcard")) { 1.555 + contentForWildcard = true; 1.556 + return 1; 1.557 + } 1.558 + if (args[i].equals("-XautoNameResolution")) { 1.559 + automaticNameConflictResolution = true; 1.560 + return 1; 1.561 + } 1.562 + if (args[i].equals("-b")) { 1.563 + addFile(requireArgument("-b",args,++i),bindFiles,".xjb"); 1.564 + return 2; 1.565 + } 1.566 + if (args[i].equals("-dtd")) { 1.567 + schemaLanguage = Language.DTD; 1.568 + return 1; 1.569 + } 1.570 + if (args[i].equals("-relaxng")) { 1.571 + schemaLanguage = Language.RELAXNG; 1.572 + return 1; 1.573 + } 1.574 + if (args[i].equals("-relaxng-compact")) { 1.575 + schemaLanguage = Language.RELAXNG_COMPACT; 1.576 + return 1; 1.577 + } 1.578 + if (args[i].equals("-xmlschema")) { 1.579 + schemaLanguage = Language.XMLSCHEMA; 1.580 + return 1; 1.581 + } 1.582 + if (args[i].equals("-wsdl")) { 1.583 + schemaLanguage = Language.WSDL; 1.584 + return 1; 1.585 + } 1.586 + if (args[i].equals("-extension")) { 1.587 + compatibilityMode = EXTENSION; 1.588 + return 1; 1.589 + } 1.590 + if (args[i].equals("-target")) { 1.591 + String token = requireArgument("-target",args,++i); 1.592 + target = SpecVersion.parse(token); 1.593 + if(target==null) 1.594 + throw new BadCommandLineException(Messages.format(Messages.ILLEGAL_TARGET_VERSION,token)); 1.595 + return 2; 1.596 + } 1.597 + if (args[i].equals("-httpproxyfile")) { 1.598 + if (i == args.length - 1 || args[i + 1].startsWith("-")) { 1.599 + throw new BadCommandLineException( 1.600 + Messages.format(Messages.MISSING_PROXYFILE)); 1.601 + } 1.602 + 1.603 + File file = new File(args[++i]); 1.604 + if(!file.exists()) { 1.605 + throw new BadCommandLineException( 1.606 + Messages.format(Messages.NO_SUCH_FILE,file)); 1.607 + } 1.608 + 1.609 + try { 1.610 + BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF-8")); 1.611 + parseProxy(in.readLine()); 1.612 + in.close(); 1.613 + } catch (IOException e) { 1.614 + throw new BadCommandLineException( 1.615 + Messages.format(Messages.FAILED_TO_PARSE,file,e.getMessage()),e); 1.616 + } 1.617 + 1.618 + return 2; 1.619 + } 1.620 + if (args[i].equals("-httpproxy")) { 1.621 + if (i == args.length - 1 || args[i + 1].startsWith("-")) { 1.622 + throw new BadCommandLineException( 1.623 + Messages.format(Messages.MISSING_PROXY)); 1.624 + } 1.625 + 1.626 + parseProxy(args[++i]); 1.627 + return 2; 1.628 + } 1.629 + if (args[i].equals("-host")) { 1.630 + proxyHost = requireArgument("-host",args,++i); 1.631 + return 2; 1.632 + } 1.633 + if (args[i].equals("-port")) { 1.634 + proxyPort = requireArgument("-port",args,++i); 1.635 + return 2; 1.636 + } 1.637 + if( args[i].equals("-catalog") ) { 1.638 + // use Sun's "XML Entity and URI Resolvers" by Norman Walsh 1.639 + // to resolve external entities. 1.640 + // http://www.sun.com/xml/developers/resolver/ 1.641 + 1.642 + File catalogFile = new File(requireArgument("-catalog",args,++i)); 1.643 + try { 1.644 + addCatalog(catalogFile); 1.645 + } catch (IOException e) { 1.646 + throw new BadCommandLineException( 1.647 + Messages.format(Messages.FAILED_TO_PARSE,catalogFile,e.getMessage()),e); 1.648 + } 1.649 + return 2; 1.650 + } 1.651 + if( args[i].equals("-Xtest-class-name-allocator") ) { 1.652 + classNameAllocator = new ClassNameAllocator() { 1.653 + public String assignClassName(String packageName, String className) { 1.654 + System.out.printf("assignClassName(%s,%s)\n",packageName,className); 1.655 + return className+"_Type"; 1.656 + } 1.657 + }; 1.658 + return 1; 1.659 + } 1.660 + 1.661 + if (args[i].equals("-encoding")) { 1.662 + encoding = requireArgument("-encoding", args, ++i); 1.663 + try { 1.664 + if (!Charset.isSupported(encoding)) { 1.665 + throw new BadCommandLineException( 1.666 + Messages.format(Messages.UNSUPPORTED_ENCODING, encoding)); 1.667 + } 1.668 + } catch (IllegalCharsetNameException icne) { 1.669 + throw new BadCommandLineException( 1.670 + Messages.format(Messages.UNSUPPORTED_ENCODING, encoding)); 1.671 + } 1.672 + return 2; 1.673 + } 1.674 + 1.675 + // see if this is one of the extensions 1.676 + for( Plugin plugin : getAllPlugins() ) { 1.677 + try { 1.678 + if( ('-'+plugin.getOptionName()).equals(args[i]) ) { 1.679 + activePlugins.add(plugin); 1.680 + plugin.onActivated(this); 1.681 + pluginURIs.addAll(plugin.getCustomizationURIs()); 1.682 + 1.683 + // give the plugin a chance to parse arguments to this option. 1.684 + // this is new in 2.1, and due to the backward compatibility reason, 1.685 + // if plugin didn't understand it, we still return 1 to indicate 1.686 + // that this option is consumed. 1.687 + int r = plugin.parseArgument(this,args,i); 1.688 + if(r!=0) 1.689 + return r; 1.690 + else 1.691 + return 1; 1.692 + } 1.693 + 1.694 + int r = plugin.parseArgument(this,args,i); 1.695 + if(r!=0) return r; 1.696 + } catch (IOException e) { 1.697 + throw new BadCommandLineException(e.getMessage(),e); 1.698 + } 1.699 + } 1.700 + 1.701 + return 0; // unrecognized 1.702 + } 1.703 + 1.704 + private void parseProxy(String text) throws BadCommandLineException { 1.705 + int i = text.lastIndexOf('@'); 1.706 + int j = text.lastIndexOf(':'); 1.707 + 1.708 + if (i > 0) { 1.709 + proxyAuth = text.substring(0, i); 1.710 + if (j > i) { 1.711 + proxyHost = text.substring(i + 1, j); 1.712 + proxyPort = text.substring(j + 1); 1.713 + } else { 1.714 + proxyHost = text.substring(i + 1); 1.715 + proxyPort = "80"; 1.716 + } 1.717 + } else { 1.718 + //no auth info 1.719 + if (j < 0) { 1.720 + //no port 1.721 + proxyHost = text; 1.722 + proxyPort = "80"; 1.723 + } else { 1.724 + proxyHost = text.substring(0, j); 1.725 + proxyPort = text.substring(j + 1); 1.726 + } 1.727 + } 1.728 + try { 1.729 + Integer.valueOf(proxyPort); 1.730 + } catch (NumberFormatException e) { 1.731 + throw new BadCommandLineException(Messages.format(Messages.ILLEGAL_PROXY,text)); 1.732 + } 1.733 + } 1.734 + 1.735 + /** 1.736 + * Obtains an operand and reports an error if it's not there. 1.737 + */ 1.738 + public String requireArgument(String optionName, String[] args, int i) throws BadCommandLineException { 1.739 + if (i == args.length || args[i].startsWith("-")) { 1.740 + throw new BadCommandLineException( 1.741 + Messages.format(Messages.MISSING_OPERAND,optionName)); 1.742 + } 1.743 + return args[i]; 1.744 + } 1.745 + 1.746 + /** 1.747 + * Parses a token to a file (or a set of files) 1.748 + * and add them as {@link InputSource} to the specified list. 1.749 + * 1.750 + * @param suffix 1.751 + * If the given token is a directory name, we do a recusive search 1.752 + * and find all files that have the given suffix. 1.753 + */ 1.754 + private void addFile(String name, List<InputSource> target, String suffix) throws BadCommandLineException { 1.755 + Object src; 1.756 + try { 1.757 + src = Util.getFileOrURL(name); 1.758 + } catch (IOException e) { 1.759 + throw new BadCommandLineException( 1.760 + Messages.format(Messages.NOT_A_FILE_NOR_URL,name)); 1.761 + } 1.762 + if(src instanceof URL) { 1.763 + target.add(absolutize(new InputSource(Util.escapeSpace(((URL)src).toExternalForm())))); 1.764 + } else { 1.765 + File fsrc = (File)src; 1.766 + if(fsrc.isDirectory()) { 1.767 + addRecursive(fsrc,suffix,target); 1.768 + } else { 1.769 + target.add(absolutize(fileToInputSource(fsrc))); 1.770 + } 1.771 + } 1.772 + } 1.773 + 1.774 + /** 1.775 + * Adds a new catalog file. 1.776 + */ 1.777 + public void addCatalog(File catalogFile) throws IOException { 1.778 + if(entityResolver==null) { 1.779 + CatalogManager.getStaticManager().setIgnoreMissingProperties(true); 1.780 + entityResolver = new CatalogResolver(true); 1.781 + } 1.782 + ((CatalogResolver)entityResolver).getCatalog().parseCatalog(catalogFile.getPath()); 1.783 + } 1.784 + 1.785 + /** 1.786 + * Parses arguments and fill fields of this object. 1.787 + * 1.788 + * @exception BadCommandLineException 1.789 + * thrown when there's a problem in the command-line arguments 1.790 + */ 1.791 + public void parseArguments( String[] args ) throws BadCommandLineException { 1.792 + 1.793 + for (int i = 0; i < args.length; i++) { 1.794 + if(args[i].length()==0) 1.795 + throw new BadCommandLineException(); 1.796 + if (args[i].charAt(0) == '-') { 1.797 + int j = parseArgument(args,i); 1.798 + if(j==0) 1.799 + throw new BadCommandLineException( 1.800 + Messages.format(Messages.UNRECOGNIZED_PARAMETER, args[i])); 1.801 + i += (j-1); 1.802 + } else { 1.803 + if(args[i].endsWith(".jar")) 1.804 + scanEpisodeFile(new File(args[i])); 1.805 + else 1.806 + addFile(args[i],grammars,".xsd"); 1.807 + } 1.808 + } 1.809 + 1.810 + // configure proxy 1.811 + if (proxyHost != null || proxyPort != null) { 1.812 + if (proxyHost != null && proxyPort != null) { 1.813 + System.setProperty("http.proxyHost", proxyHost); 1.814 + System.setProperty("http.proxyPort", proxyPort); 1.815 + System.setProperty("https.proxyHost", proxyHost); 1.816 + System.setProperty("https.proxyPort", proxyPort); 1.817 + } else if (proxyHost == null) { 1.818 + throw new BadCommandLineException( 1.819 + Messages.format(Messages.MISSING_PROXYHOST)); 1.820 + } else { 1.821 + throw new BadCommandLineException( 1.822 + Messages.format(Messages.MISSING_PROXYPORT)); 1.823 + } 1.824 + if (proxyAuth != null) { 1.825 + DefaultAuthenticator.getAuthenticator().setProxyAuth(proxyAuth); 1.826 + } 1.827 + } 1.828 + 1.829 + if (grammars.isEmpty()) 1.830 + throw new BadCommandLineException( 1.831 + Messages.format(Messages.MISSING_GRAMMAR)); 1.832 + 1.833 + if( schemaLanguage==null ) 1.834 + schemaLanguage = guessSchemaLanguage(); 1.835 + 1.836 +// if(target==SpecVersion.V2_2 && !isExtensionMode()) 1.837 +// throw new BadCommandLineException( 1.838 +// "Currently 2.2 is still not finalized yet, so using it requires the -extension switch." + 1.839 +// "NOTE THAT 2.2 SPEC MAY CHANGE BEFORE IT BECOMES FINAL."); 1.840 + 1.841 + if(pluginLoadFailure!=null) 1.842 + throw new BadCommandLineException( 1.843 + Messages.format(Messages.PLUGIN_LOAD_FAILURE,pluginLoadFailure)); 1.844 + } 1.845 + 1.846 + /** 1.847 + * Finds the <tt>META-INF/sun-jaxb.episode</tt> file to add as a binding customization. 1.848 + */ 1.849 + public void scanEpisodeFile(File jar) throws BadCommandLineException { 1.850 + try { 1.851 + URLClassLoader ucl = new URLClassLoader(new URL[]{jar.toURL()}); 1.852 + Enumeration<URL> resources = ucl.findResources("META-INF/sun-jaxb.episode"); 1.853 + while (resources.hasMoreElements()) { 1.854 + URL url = resources.nextElement(); 1.855 + addBindFile(new InputSource(url.toExternalForm())); 1.856 + } 1.857 + } catch (IOException e) { 1.858 + throw new BadCommandLineException( 1.859 + Messages.format(Messages.FAILED_TO_LOAD,jar,e.getMessage()), e); 1.860 + } 1.861 + } 1.862 + 1.863 + 1.864 + /** 1.865 + * Guesses the schema language. 1.866 + */ 1.867 + public Language guessSchemaLanguage() { 1.868 + 1.869 + // otherwise, use the file extension. 1.870 + // not a good solution, but very easy. 1.871 + if ((grammars != null) && (grammars.size() > 0)) { 1.872 + String name = grammars.get(0).getSystemId().toLowerCase(); 1.873 + 1.874 + if (name.endsWith(".rng")) 1.875 + return Language.RELAXNG; 1.876 + if (name.endsWith(".rnc")) 1.877 + return Language.RELAXNG_COMPACT; 1.878 + if (name.endsWith(".dtd")) 1.879 + return Language.DTD; 1.880 + if (name.endsWith(".wsdl")) 1.881 + return Language.WSDL; 1.882 + } 1.883 + 1.884 + // by default, assume XML Schema 1.885 + return Language.XMLSCHEMA; 1.886 + } 1.887 + 1.888 + /** 1.889 + * Creates a configured CodeWriter that produces files into the specified directory. 1.890 + */ 1.891 + public CodeWriter createCodeWriter() throws IOException { 1.892 + return createCodeWriter(new FileCodeWriter( targetDir, readOnly, encoding )); 1.893 + } 1.894 + 1.895 + /** 1.896 + * Creates a configured CodeWriter that produces files into the specified directory. 1.897 + */ 1.898 + public CodeWriter createCodeWriter( CodeWriter core ) { 1.899 + if(noFileHeader) 1.900 + return core; 1.901 + 1.902 + return new PrologCodeWriter( core,getPrologComment() ); 1.903 + } 1.904 + 1.905 + /** 1.906 + * Gets the string suitable to be used as the prolog comment baked into artifacts. 1.907 + * This is the string like "This file was generated by the JAXB RI on YYYY/mm/dd..." 1.908 + */ 1.909 + public String getPrologComment() { 1.910 + // generate format syntax: <date> 'at' <time> 1.911 + String format = 1.912 + Messages.format(Messages.DATE_FORMAT) 1.913 + + " '" 1.914 + + Messages.format(Messages.AT) 1.915 + + "' " 1.916 + + Messages.format(Messages.TIME_FORMAT); 1.917 + SimpleDateFormat dateFormat = new SimpleDateFormat(format, Locale.ENGLISH); 1.918 + 1.919 + return Messages.format( 1.920 + Messages.FILE_PROLOG_COMMENT, 1.921 + dateFormat.format(new Date())); 1.922 + } 1.923 + 1.924 + /** 1.925 + * If a plugin failed to load, report. 1.926 + */ 1.927 + private static String pluginLoadFailure; 1.928 + 1.929 + /** 1.930 + * Looks for all "META-INF/services/[className]" files and 1.931 + * create one instance for each class name found inside this file. 1.932 + */ 1.933 + private static <T> T[] findServices( Class<T> clazz, ClassLoader classLoader ) { 1.934 + // if true, print debug output 1.935 + final boolean debug = com.sun.tools.internal.xjc.util.Util.getSystemProperty(Options.class,"findServices")!=null; 1.936 + 1.937 + // if we are running on Mustang or Dolphin, use ServiceLoader 1.938 + // so that we can take advantage of JSR-277 module system. 1.939 + try { 1.940 + Class<?> serviceLoader = Class.forName("java.util.ServiceLoader"); 1.941 + if(debug) 1.942 + System.out.println("Using java.util.ServiceLoader"); 1.943 + Iterable<T> itr = (Iterable<T>)serviceLoader.getMethod("load",Class.class,ClassLoader.class).invoke(null,clazz,classLoader); 1.944 + List<T> r = new ArrayList<T>(); 1.945 + for (T t : itr) 1.946 + r.add(t); 1.947 + return r.toArray((T[])Array.newInstance(clazz,r.size())); 1.948 + } catch (ClassNotFoundException e) { 1.949 + // fall through 1.950 + } catch (IllegalAccessException e) { 1.951 + Error x = new IllegalAccessError(); 1.952 + x.initCause(e); 1.953 + throw x; 1.954 + } catch (InvocationTargetException e) { 1.955 + Throwable x = e.getTargetException(); 1.956 + if (x instanceof RuntimeException) 1.957 + throw (RuntimeException) x; 1.958 + if (x instanceof Error) 1.959 + throw (Error) x; 1.960 + throw new Error(x); 1.961 + } catch (NoSuchMethodException e) { 1.962 + Error x = new NoSuchMethodError(); 1.963 + x.initCause(e); 1.964 + throw x; 1.965 + } 1.966 + 1.967 + String serviceId = "META-INF/services/" + clazz.getName(); 1.968 + 1.969 + // used to avoid creating the same instance twice 1.970 + Set<String> classNames = new HashSet<String>(); 1.971 + 1.972 + if(debug) { 1.973 + System.out.println("Looking for "+serviceId+" for add-ons"); 1.974 + } 1.975 + 1.976 + // try to find services in CLASSPATH 1.977 + try { 1.978 + Enumeration<URL> e = classLoader.getResources(serviceId); 1.979 + if(e==null) return (T[])Array.newInstance(clazz,0); 1.980 + 1.981 + ArrayList<T> a = new ArrayList<T>(); 1.982 + while(e.hasMoreElements()) { 1.983 + URL url = e.nextElement(); 1.984 + BufferedReader reader=null; 1.985 + 1.986 + if(debug) { 1.987 + System.out.println("Checking "+url+" for an add-on"); 1.988 + } 1.989 + 1.990 + try { 1.991 + reader = new BufferedReader(new InputStreamReader(url.openStream())); 1.992 + String impl; 1.993 + while((impl = reader.readLine())!=null ) { 1.994 + // try to instanciate the object 1.995 + impl = impl.trim(); 1.996 + if(classNames.add(impl)) { 1.997 + Class implClass = classLoader.loadClass(impl); 1.998 + if(!clazz.isAssignableFrom(implClass)) { 1.999 + pluginLoadFailure = impl+" is not a subclass of "+clazz+". Skipping"; 1.1000 + if(debug) 1.1001 + System.out.println(pluginLoadFailure); 1.1002 + continue; 1.1003 + } 1.1004 + if(debug) { 1.1005 + System.out.println("Attempting to instanciate "+impl); 1.1006 + } 1.1007 + a.add(clazz.cast(implClass.newInstance())); 1.1008 + } 1.1009 + } 1.1010 + reader.close(); 1.1011 + } catch( Exception ex ) { 1.1012 + // let it go. 1.1013 + StringWriter w = new StringWriter(); 1.1014 + ex.printStackTrace(new PrintWriter(w)); 1.1015 + pluginLoadFailure = w.toString(); 1.1016 + if(debug) { 1.1017 + System.out.println(pluginLoadFailure); 1.1018 + } 1.1019 + if( reader!=null ) { 1.1020 + try { 1.1021 + reader.close(); 1.1022 + } catch( IOException ex2 ) { 1.1023 + // ignore 1.1024 + } 1.1025 + } 1.1026 + } 1.1027 + } 1.1028 + 1.1029 + return a.toArray((T[])Array.newInstance(clazz,a.size())); 1.1030 + } catch( Throwable e ) { 1.1031 + // ignore any error 1.1032 + StringWriter w = new StringWriter(); 1.1033 + e.printStackTrace(new PrintWriter(w)); 1.1034 + pluginLoadFailure = w.toString(); 1.1035 + if(debug) { 1.1036 + System.out.println(pluginLoadFailure); 1.1037 + } 1.1038 + return (T[])Array.newInstance(clazz,0); 1.1039 + } 1.1040 + } 1.1041 + 1.1042 + // this is a convenient place to expose the build version to xjc plugins 1.1043 + public static String getBuildID() { 1.1044 + return Messages.format(Messages.BUILD_ID); 1.1045 + } 1.1046 + 1.1047 + public static String normalizeSystemId(String systemId) { 1.1048 + try { 1.1049 + systemId = new URI(systemId).normalize().toString(); 1.1050 + } catch (URISyntaxException e) { 1.1051 + // leave the system ID untouched. In my experience URI is often too strict 1.1052 + } 1.1053 + return systemId; 1.1054 + } 1.1055 + 1.1056 +}