src/share/jaxws_classes/com/sun/tools/internal/ws/wscompile/Options.java

Tue, 09 Apr 2013 14:51:13 +0100

author
alanb
date
Tue, 09 Apr 2013 14:51:13 +0100
changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 408
b0610cd08440
permissions
-rw-r--r--

8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com

ohair@286 1 /*
alanb@368 2 * Copyright (c) 1997, 2013, 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.ws.wscompile;
ohair@286 27
ohair@286 28 import com.sun.tools.internal.ws.resources.WscompileMessages;
ohair@286 29 import com.sun.tools.internal.ws.Invoker;
ohair@286 30
ohair@286 31 import javax.annotation.processing.Filer;
ohair@286 32 import java.io.File;
ohair@286 33 import java.io.IOException;
ohair@286 34 import java.net.MalformedURLException;
ohair@286 35 import java.net.URL;
ohair@286 36 import java.net.URLClassLoader;
ohair@286 37 import java.nio.charset.Charset;
ohair@286 38 import java.nio.charset.IllegalCharsetNameException;
alanb@368 39 import java.text.MessageFormat;
ohair@286 40 import java.util.ArrayList;
ohair@286 41 import java.util.List;
ohair@286 42 import java.util.StringTokenizer;
ohair@286 43
ohair@286 44 /**
ohair@286 45 * Provide common jaxws tool options.
ohair@286 46 *
ohair@286 47 * @author Vivek Pandey
ohair@286 48 */
ohair@286 49 public class Options {
ohair@286 50 /**
ohair@286 51 * -verbose
ohair@286 52 */
ohair@286 53 public boolean verbose;
ohair@286 54
ohair@286 55 /**
ohair@286 56 * - quite
ohair@286 57 */
ohair@286 58 public boolean quiet;
ohair@286 59
ohair@286 60 /**
ohair@286 61 * -keep
ohair@286 62 */
ohair@286 63 public boolean keep;
ohair@286 64
ohair@286 65
ohair@286 66
ohair@286 67 /**
ohair@286 68 * -d
ohair@286 69 */
ohair@286 70 public File destDir = new File(".");
ohair@286 71
ohair@286 72
ohair@286 73 /**
ohair@286 74 * -s
ohair@286 75 */
ohair@286 76 public File sourceDir;
ohair@286 77
ohair@286 78 /**
ohair@286 79 * The filer that can use used to write out the generated files
ohair@286 80 */
ohair@286 81 public Filer filer;
ohair@286 82
ohair@286 83 /**
ohair@286 84 * -encoding
ohair@286 85 */
ohair@286 86 public String encoding;
ohair@286 87
ohair@286 88 public String classpath = System.getProperty("java.class.path");
ohair@286 89
ohair@286 90
ohair@286 91 /**
ohair@286 92 * -Xnocompile
ohair@286 93 */
ohair@286 94 public boolean nocompile;
ohair@286 95
alanb@368 96 /**
alanb@368 97 * Disable secure xml processing.
alanb@368 98 * -XdisableSecureXmlProcessing
alanb@368 99 */
alanb@368 100 public boolean disableSecureXmlProcessing = false;
alanb@368 101
ohair@286 102 public enum Target {
ohair@286 103 V2_0, V2_1, V2_2;
ohair@286 104
ohair@286 105 /**
ohair@286 106 * Returns true if this version is equal or later than the given one.
ohair@286 107 */
ohair@286 108 public boolean isLaterThan(Target t) {
ohair@286 109 return this.ordinal() >= t.ordinal();
ohair@286 110 }
ohair@286 111
ohair@286 112 /**
ohair@286 113 * Parses "2.0" and "2.1" into the {@link Target} object.
ohair@286 114 *
ohair@286 115 * @return null for parsing failure.
ohair@286 116 */
ohair@286 117 public static Target parse(String token) {
ohair@286 118 if (token.equals("2.0"))
ohair@286 119 return Target.V2_0;
ohair@286 120 else if (token.equals("2.1"))
ohair@286 121 return Target.V2_1;
ohair@286 122 else if (token.equals("2.2"))
ohair@286 123 return Target.V2_2;
ohair@286 124 return null;
ohair@286 125 }
ohair@286 126
ohair@286 127 /**
ohair@286 128 * Gives the String representation of the {@link Target}
ohair@286 129 */
ohair@286 130 public String getVersion(){
ohair@286 131 switch(this){
ohair@286 132 case V2_0:
ohair@286 133 return "2.0";
ohair@286 134 case V2_1:
ohair@286 135 return "2.1";
ohair@286 136 case V2_2:
ohair@286 137 return "2.2";
ohair@286 138 default:
ohair@286 139 return null;
ohair@286 140 }
ohair@286 141 }
ohair@286 142
ohair@286 143 public static Target getDefault() {
ohair@286 144 return V2_2;
ohair@286 145 }
ohair@286 146
ohair@286 147 public static Target getLoadedAPIVersion() {
ohair@286 148 return LOADED_API_VERSION;
ohair@286 149 }
ohair@286 150
ohair@286 151 private static final Target LOADED_API_VERSION;
ohair@286 152
ohair@286 153 static {
ohair@286 154 // check if we are indeed loading JAX-WS 2.2 API
ohair@286 155 if (Invoker.checkIfLoading22API()) {
ohair@286 156 LOADED_API_VERSION = Target.V2_2;
ohair@286 157 } // check if we are indeed loading JAX-WS 2.1 API
ohair@286 158 else if (Invoker.checkIfLoading21API()) {
ohair@286 159 LOADED_API_VERSION = Target.V2_1;
ohair@286 160 } else {
ohair@286 161 LOADED_API_VERSION = Target.V2_0;
ohair@286 162 }
ohair@286 163 }
ohair@286 164 }
ohair@286 165
ohair@286 166 public Target target = Target.V2_2;
ohair@286 167
ohair@286 168 /**
ohair@286 169 * strictly follow the compatibility rules specified in JAXWS spec
ohair@286 170 */
ohair@286 171 public static final int STRICT = 1;
ohair@286 172
ohair@286 173 /**
ohair@286 174 * loosely follow the compatibility rules and allow the use of vendor
ohair@286 175 * binding extensions
ohair@286 176 */
ohair@286 177 public static final int EXTENSION = 2;
ohair@286 178
ohair@286 179 /**
ohair@286 180 * this switch determines how carefully the compiler will follow
ohair@286 181 * the compatibility rules in the spec. Either <code>STRICT</code>
ohair@286 182 * or <code>EXTENSION</code>.
ohair@286 183 */
ohair@286 184 public int compatibilityMode = STRICT;
ohair@286 185
ohair@286 186 public boolean isExtensionMode() {
ohair@286 187 return compatibilityMode == EXTENSION;
ohair@286 188 }
ohair@286 189
ohair@286 190 public boolean debug = false;
ohair@286 191
ohair@286 192 /**
ohair@286 193 * -Xdebug - gives complete stack trace
ohair@286 194 */
ohair@286 195 public boolean debugMode = false;
ohair@286 196
ohair@286 197
ohair@286 198 private final List<File> generatedFiles = new ArrayList<File>();
ohair@286 199 private ClassLoader classLoader;
ohair@286 200
ohair@286 201
ohair@286 202 /**
ohair@286 203 * Remember info on generated source file generated so that it
ohair@286 204 * can be removed later, if appropriate.
ohair@286 205 */
ohair@286 206 public void addGeneratedFile(File file) {
ohair@286 207 generatedFiles.add(file);
ohair@286 208 }
ohair@286 209
ohair@286 210 /**
ohair@286 211 * Remove generated files
ohair@286 212 */
ohair@286 213 public void removeGeneratedFiles(){
ohair@286 214 for(File file : generatedFiles){
ohair@286 215 if (file.getName().endsWith(".java")) {
alanb@368 216 boolean deleted = file.delete();
alanb@368 217 if (verbose && !deleted) {
alanb@368 218 System.out.println(MessageFormat.format("{0} could not be deleted.", file));
alanb@368 219 }
ohair@286 220 }
ohair@286 221 }
ohair@286 222 generatedFiles.clear();
ohair@286 223 }
ohair@286 224
ohair@286 225 /**
ohair@286 226 * Return all the generated files and its types.
ohair@286 227 */
ohair@286 228 public Iterable<File> getGeneratedFiles() {
ohair@286 229 return generatedFiles;
ohair@286 230 }
ohair@286 231
ohair@286 232 /**
ohair@286 233 * Delete all the generated source files made during the execution
ohair@286 234 * of this environment (those that have been registered with the
ohair@286 235 * "addGeneratedFile" method).
ohair@286 236 */
ohair@286 237 public void deleteGeneratedFiles() {
ohair@286 238 synchronized (generatedFiles) {
ohair@286 239 for (File file : generatedFiles) {
ohair@286 240 if (file.getName().endsWith(".java")) {
alanb@368 241 boolean deleted = file.delete();
alanb@368 242 if (verbose && !deleted) {
alanb@368 243 System.out.println(MessageFormat.format("{0} could not be deleted.", file));
alanb@368 244 }
ohair@286 245 }
ohair@286 246 }
ohair@286 247 generatedFiles.clear();
ohair@286 248 }
ohair@286 249 }
ohair@286 250
ohair@286 251 /**
ohair@286 252 * Parses arguments and fill fields of this object.
ohair@286 253 *
ohair@286 254 * @exception BadCommandLineException
ohair@286 255 * thrown when there's a problem in the command-line arguments
ohair@286 256 */
ohair@286 257 public void parseArguments( String[] args ) throws BadCommandLineException {
ohair@286 258
ohair@286 259 for (int i = 0; i < args.length; i++) {
ohair@286 260 if(args[i].length()==0)
ohair@286 261 throw new BadCommandLineException();
ohair@286 262 if (args[i].charAt(0) == '-') {
ohair@286 263 int j = parseArguments(args,i);
ohair@286 264 if(j==0)
ohair@286 265 throw new BadCommandLineException(WscompileMessages.WSCOMPILE_INVALID_OPTION(args[i]));
ohair@286 266 i += (j-1);
ohair@286 267 } else {
ohair@286 268 addFile(args[i]);
ohair@286 269 }
ohair@286 270 }
ohair@286 271 if(destDir == null)
ohair@286 272 destDir = new File(".");
ohair@286 273 if(sourceDir == null)
ohair@286 274 sourceDir = destDir;
ohair@286 275 }
ohair@286 276
ohair@286 277
ohair@286 278 /**
ohair@286 279 * Adds a file from the argume
ohair@286 280 *
ohair@286 281 * @param arg a file, could be a wsdl or xsd or a Class
ohair@286 282 */
ohair@286 283 protected void addFile(String arg) throws BadCommandLineException {}
ohair@286 284
ohair@286 285 /**
ohair@286 286 * Parses an option <code>args[i]</code> and return
ohair@286 287 * the number of tokens consumed.
ohair@286 288 *
ohair@286 289 * @return
ohair@286 290 * 0 if the argument is not understood. Returning 0
ohair@286 291 * will let the caller report an error.
ohair@286 292 * @exception BadCommandLineException
ohair@286 293 * If the callee wants to provide a custom message for an error.
ohair@286 294 */
ohair@286 295 protected int parseArguments(String[] args, int i) throws BadCommandLineException {
ohair@286 296 if (args[i].equals("-g")) {
ohair@286 297 debug = true;
ohair@286 298 return 1;
ohair@286 299 } else if (args[i].equals("-Xdebug")) {
ohair@286 300 debugMode = true;
ohair@286 301 return 1;
ohair@286 302 } else if (args[i].equals("-Xendorsed")) {
ohair@286 303 // this option is processed much earlier, so just ignore.
ohair@286 304 return 1;
ohair@286 305 } else if (args[i].equals("-verbose")) {
ohair@286 306 verbose = true;
ohair@286 307 return 1;
ohair@286 308 } else if (args[i].equals("-quiet")) {
ohair@286 309 quiet = true;
ohair@286 310 return 1;
ohair@286 311 } else if (args[i].equals("-keep")) {
ohair@286 312 keep = true;
ohair@286 313 return 1;
ohair@286 314 } else if (args[i].equals("-target")) {
ohair@286 315 String token = requireArgument("-target", args, ++i);
ohair@286 316 target = Target.parse(token);
ohair@286 317 if(target == null)
ohair@286 318 throw new BadCommandLineException(WscompileMessages.WSIMPORT_ILLEGAL_TARGET_VERSION(token));
ohair@286 319 return 2;
ohair@286 320 } else if (args[i].equals("-classpath") || args[i].equals("-cp")) {
ohair@286 321 classpath = requireArgument("-classpath", args, ++i) + File.pathSeparator + System.getProperty("java.class.path");
ohair@286 322 return 2;
ohair@286 323 } else if (args[i].equals("-d")) {
ohair@286 324 destDir = new File(requireArgument("-d", args, ++i));
ohair@286 325 if (!destDir.exists())
ohair@286 326 throw new BadCommandLineException(WscompileMessages.WSCOMPILE_NO_SUCH_DIRECTORY(destDir.getPath()));
ohair@286 327 return 2;
ohair@286 328 } else if (args[i].equals("-s")) {
ohair@286 329 sourceDir = new File(requireArgument("-s", args, ++i));
ohair@286 330 keep = true;
ohair@286 331 if (!sourceDir.exists()) {
ohair@286 332 throw new BadCommandLineException(WscompileMessages.WSCOMPILE_NO_SUCH_DIRECTORY(sourceDir.getPath()));
ohair@286 333 }
ohair@286 334 return 2;
ohair@286 335 } else if (args[i].equals("-extension")) {
ohair@286 336 compatibilityMode = EXTENSION;
ohair@286 337 return 1;
ohair@286 338 } else if (args[i].startsWith("-help")) {
ohair@286 339 WeAreDone done = new WeAreDone();
ohair@286 340 done.initOptions(this);
ohair@286 341 throw done;
ohair@286 342 } else if (args[i].equals("-Xnocompile")) {
ohair@286 343 // -nocompile implies -keep. this is undocumented switch.
ohair@286 344 nocompile = true;
ohair@286 345 keep = true;
ohair@286 346 return 1;
ohair@286 347 } else if (args[i].equals("-encoding")) {
ohair@286 348 encoding = requireArgument("-encoding", args, ++i);
ohair@286 349 try {
ohair@286 350 if (!Charset.isSupported(encoding)) {
ohair@286 351 throw new BadCommandLineException(WscompileMessages.WSCOMPILE_UNSUPPORTED_ENCODING(encoding));
ohair@286 352 }
ohair@286 353 } catch (IllegalCharsetNameException icne) {
ohair@286 354 throw new BadCommandLineException(WscompileMessages.WSCOMPILE_UNSUPPORTED_ENCODING(encoding));
ohair@286 355 }
ohair@286 356 return 2;
alanb@368 357 } else if (args[i].equals("-XdisableSecureXmlProcessing")) {
alanb@368 358 disableSecureXmlProcessing= true;
alanb@368 359 return 1;
ohair@286 360 }
ohair@286 361 return 0;
ohair@286 362 }
ohair@286 363
ohair@286 364 /**
ohair@286 365 * Obtains an operand and reports an error if it's not there.
ohair@286 366 */
ohair@286 367 public String requireArgument(String optionName, String[] args, int i) throws BadCommandLineException {
ohair@286 368 //if (i == args.length || args[i].startsWith("-")) {
ohair@286 369 if (args[i].startsWith("-")) {
ohair@286 370 throw new BadCommandLineException(WscompileMessages.WSCOMPILE_MISSING_OPTION_ARGUMENT(optionName));
ohair@286 371 }
ohair@286 372 return args[i];
ohair@286 373 }
ohair@286 374
ohair@286 375
ohair@286 376
ohair@286 377 /**
ohair@286 378 * Used to signal that we've finished processing.
ohair@286 379 */
ohair@286 380 public static final class WeAreDone extends BadCommandLineException {}
ohair@286 381
ohair@286 382 /**
ohair@286 383 * Get a URLClassLoader from using the classpath
ohair@286 384 */
ohair@286 385 public ClassLoader getClassLoader() {
ohair@286 386 if (classLoader == null) {
ohair@286 387 classLoader =
ohair@286 388 new URLClassLoader(pathToURLs(classpath),
ohair@286 389 this.getClass().getClassLoader());
ohair@286 390 }
ohair@286 391 return classLoader;
ohair@286 392 }
ohair@286 393
ohair@286 394 /**
ohair@286 395 * Utility method for converting a search path string to an array
ohair@286 396 * of directory and JAR file URLs.
ohair@286 397 *
ohair@286 398 * @param path the search path string
ohair@286 399 * @return the resulting array of directory and JAR file URLs
ohair@286 400 */
ohair@286 401 public static URL[] pathToURLs(String path) {
ohair@286 402 StringTokenizer st = new StringTokenizer(path, File.pathSeparator);
ohair@286 403 URL[] urls = new URL[st.countTokens()];
ohair@286 404 int count = 0;
ohair@286 405 while (st.hasMoreTokens()) {
ohair@286 406 URL url = fileToURL(new File(st.nextToken()));
ohair@286 407 if (url != null) {
ohair@286 408 urls[count++] = url;
ohair@286 409 }
ohair@286 410 }
ohair@286 411 if (urls.length != count) {
ohair@286 412 URL[] tmp = new URL[count];
ohair@286 413 System.arraycopy(urls, 0, tmp, 0, count);
ohair@286 414 urls = tmp;
ohair@286 415 }
ohair@286 416 return urls;
ohair@286 417 }
ohair@286 418
ohair@286 419 /**
ohair@286 420 * Returns the directory or JAR file URL corresponding to the specified
ohair@286 421 * local file name.
ohair@286 422 *
ohair@286 423 * @param file the File object
ohair@286 424 * @return the resulting directory or JAR file URL, or null if unknown
ohair@286 425 */
ohair@286 426 public static URL fileToURL(File file) {
ohair@286 427 String name;
ohair@286 428 try {
ohair@286 429 name = file.getCanonicalPath();
ohair@286 430 } catch (IOException e) {
ohair@286 431 name = file.getAbsolutePath();
ohair@286 432 }
ohair@286 433 name = name.replace(File.separatorChar, '/');
ohair@286 434 if (!name.startsWith("/")) {
ohair@286 435 name = "/" + name;
ohair@286 436 }
ohair@286 437
ohair@286 438 // If the file does not exist, then assume that it's a directory
ohair@286 439 if (!file.isFile()) {
ohair@286 440 name = name + "/";
ohair@286 441 }
ohair@286 442 try {
ohair@286 443 return new URL("file", "", name);
ohair@286 444 } catch (MalformedURLException e) {
ohair@286 445 throw new IllegalArgumentException("file");
ohair@286 446 }
ohair@286 447 }
ohair@286 448
ohair@286 449 }

mercurial