001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.tools.ws.wscompile;
038:
039: import com.sun.tools.ws.resources.WscompileMessages;
040:
041: import java.io.File;
042: import java.io.IOException;
043: import java.net.MalformedURLException;
044: import java.net.URL;
045: import java.net.URLClassLoader;
046: import java.util.ArrayList;
047: import java.util.List;
048: import java.util.StringTokenizer;
049:
050: /**
051: * Provide common jaxws tool options.
052: *
053: * @author Vivek Pandey
054: */
055: public class Options {
056: /**
057: * -verbose
058: */
059: public boolean verbose;
060:
061: /**
062: * - quite
063: */
064: public boolean quiet;
065:
066: /**
067: * -keep
068: */
069: public boolean keep;
070:
071: /**
072: * -d
073: */
074: public File destDir = new File(".");
075:
076: /**
077: * -s
078: */
079: public File sourceDir;
080:
081: public String classpath = System.getProperty("java.class.path");
082:
083: /**
084: * -Xnocompile
085: */
086: public boolean nocompile;
087:
088: public enum Target {
089: V2_0, V2_1;
090:
091: /**
092: * Returns true if this version is equal or later than the given one.
093: */
094: public boolean isLaterThan(Target t) {
095: return this .ordinal() >= t.ordinal();
096: }
097:
098: /**
099: * Parses "2.0" and "2.1" into the {@link Target} object.
100: *
101: * @return null for parsing failure.
102: */
103: public static Target parse(String token) {
104: if (token.equals("2.0"))
105: return Target.V2_0;
106: else if (token.equals("2.1"))
107: return Target.V2_1;
108: return null;
109: }
110:
111: /**
112: * Gives the String representation of the {@link Target}
113: */
114: public String getVersion() {
115: switch (this ) {
116: case V2_0:
117: return "2.0";
118: case V2_1:
119: return "2.1";
120: default:
121: return null;
122: }
123: }
124: }
125:
126: public Target target = Target.V2_1;
127:
128: /**
129: * strictly follow the compatibility rules specified in JAXWS spec
130: */
131: public static final int STRICT = 1;
132:
133: /**
134: * loosely follow the compatibility rules and allow the use of vendor
135: * binding extensions
136: */
137: public static final int EXTENSION = 2;
138:
139: /**
140: * this switch determines how carefully the compiler will follow
141: * the compatibility rules in the spec. Either <code>STRICT</code>
142: * or <code>EXTENSION</code>.
143: */
144: public int compatibilityMode = STRICT;
145:
146: public boolean isExtensionMode() {
147: return compatibilityMode == EXTENSION;
148: }
149:
150: /**
151: * Target direcoty when producing files.
152: */
153: public File targetDir = new File(".");
154:
155: public boolean debug = false;
156: public boolean debugMode = false;
157:
158: private final List<File> generatedFiles = new ArrayList<File>();
159: private ClassLoader classLoader;
160:
161: /**
162: * Remember info on generated source file generated so that it
163: * can be removed later, if appropriate.
164: */
165: public void addGeneratedFile(File file) {
166: generatedFiles.add(file);
167: }
168:
169: /**
170: * Remove generated files
171: */
172: public void removeGeneratedFiles() {
173: for (File file : generatedFiles) {
174: if (file.getName().endsWith(".java")) {
175: file.delete();
176: }
177: }
178: generatedFiles.clear();
179: }
180:
181: /**
182: * Return all the generated files and its types.
183: */
184: public Iterable<File> getGeneratedFiles() {
185: return generatedFiles;
186: }
187:
188: /**
189: * Delete all the generated source files made during the execution
190: * of this environment (those that have been registered with the
191: * "addGeneratedFile" method).
192: */
193: public void deleteGeneratedFiles() {
194: synchronized (generatedFiles) {
195: for (File file : generatedFiles) {
196: if (file.getName().endsWith(".java")) {
197: file.delete();
198: }
199: }
200: generatedFiles.clear();
201: }
202: }
203:
204: /**
205: * Parses arguments and fill fields of this object.
206: *
207: * @exception BadCommandLineException
208: * thrown when there's a problem in the command-line arguments
209: */
210: public final void parseArguments(String[] args)
211: throws BadCommandLineException {
212:
213: for (int i = 0; i < args.length; i++) {
214: if (args[i].length() == 0)
215: throw new BadCommandLineException();
216: if (args[i].charAt(0) == '-') {
217: int j = parseArguments(args, i);
218: if (j == 0)
219: throw new BadCommandLineException(WscompileMessages
220: .WSCOMPILE_INVALID_OPTION(args[i]));
221: i += (j - 1);
222: } else {
223: addFile(args[i]);
224: }
225: }
226: if (destDir == null)
227: destDir = new File(".");
228: if (sourceDir == null)
229: sourceDir = destDir;
230: }
231:
232: /**
233: * Adds a file from the argume
234: *
235: * @param arg a file, could be a wsdl or xsd or a Class
236: */
237: protected void addFile(String arg) throws BadCommandLineException {
238: }
239:
240: /**
241: * Parses an option <code>args[i]</code> and return
242: * the number of tokens consumed.
243: *
244: * @return
245: * 0 if the argument is not understood. Returning 0
246: * will let the caller report an error.
247: * @exception BadCommandLineException
248: * If the callee wants to provide a custom message for an error.
249: */
250: protected int parseArguments(String[] args, int i)
251: throws BadCommandLineException {
252: if (args[i].equals("-g")) {
253: debug = true;
254: return 1;
255: } else if (args[i].equals("-Xdebug")) {
256: debugMode = true;
257: return 1;
258: } else if (args[i].equals("-Xendorsed")) {
259: // this option is processed much earlier, so just ignore.
260: return 1;
261: } else if (args[i].equals("-verbose")) {
262: verbose = true;
263: return 1;
264: } else if (args[i].equals("-quiet")) {
265: quiet = true;
266: return 1;
267: } else if (args[i].equals("-keep")) {
268: keep = true;
269: return 1;
270: } else if (args[i].equals("-target")) {
271: String token = requireArgument("-target", args, ++i);
272: target = Target.parse(token);
273: if (target == null)
274: throw new BadCommandLineException(WscompileMessages
275: .WSIMPORT_ILLEGAL_TARGET_VERSION(token));
276: return 2;
277: } else if (args[i].equals("-d")) {
278: destDir = new File(requireArgument("-d", args, ++i));
279: if (!destDir.exists())
280: throw new BadCommandLineException(WscompileMessages
281: .WSCOMPILE_NO_SUCH_DIRECTORY(destDir.getPath()));
282: return 2;
283: } else if (args[i].equals("-s")) {
284: sourceDir = new File(requireArgument("-s", args, ++i));
285: keep = true;
286: if (!sourceDir.exists()) {
287: throw new BadCommandLineException(WscompileMessages
288: .WSCOMPILE_NO_SUCH_DIRECTORY(sourceDir
289: .getPath()));
290: }
291: return 2;
292: } else if (args[i].equals("-extension")) {
293: compatibilityMode = EXTENSION;
294: return 1;
295: } else if (args[i].startsWith("-help")) {
296: WeAreDone done = new WeAreDone();
297: done.initOptions(this );
298: throw done;
299: } else if (args[i].equals("-Xnocompile")) {
300: // -nocompile implies -keep. this is undocumented switch.
301: nocompile = true;
302: keep = true;
303: return 1;
304: }
305: return 0;
306: }
307:
308: /**
309: * Obtains an operand and reports an error if it's not there.
310: */
311: public String requireArgument(String optionName, String[] args,
312: int i) throws BadCommandLineException {
313: //if (i == args.length || args[i].startsWith("-")) {
314: if (args[i].startsWith("-")) {
315: throw new BadCommandLineException(WscompileMessages
316: .WSCOMPILE_MISSING_OPTION_ARGUMENT(optionName));
317: }
318: return args[i];
319: }
320:
321: /**
322: * Used to signal that we've finished processing.
323: */
324: public static final class WeAreDone extends BadCommandLineException {
325: }
326:
327: /**
328: * Get a URLClassLoader from using the classpath
329: */
330: public ClassLoader getClassLoader() {
331: if (classLoader == null) {
332: classLoader = new URLClassLoader(pathToURLs(classpath),
333: this .getClass().getClassLoader());
334: }
335: return classLoader;
336: }
337:
338: /**
339: * Utility method for converting a search path string to an array
340: * of directory and JAR file URLs.
341: *
342: * @param path the search path string
343: * @return the resulting array of directory and JAR file URLs
344: */
345: public static URL[] pathToURLs(String path) {
346: StringTokenizer st = new StringTokenizer(path,
347: File.pathSeparator);
348: URL[] urls = new URL[st.countTokens()];
349: int count = 0;
350: while (st.hasMoreTokens()) {
351: URL url = fileToURL(new File(st.nextToken()));
352: if (url != null) {
353: urls[count++] = url;
354: }
355: }
356: if (urls.length != count) {
357: URL[] tmp = new URL[count];
358: System.arraycopy(urls, 0, tmp, 0, count);
359: urls = tmp;
360: }
361: return urls;
362: }
363:
364: /**
365: * Returns the directory or JAR file URL corresponding to the specified
366: * local file name.
367: *
368: * @param file the File object
369: * @return the resulting directory or JAR file URL, or null if unknown
370: */
371: public static URL fileToURL(File file) {
372: String name;
373: try {
374: name = file.getCanonicalPath();
375: } catch (IOException e) {
376: name = file.getAbsolutePath();
377: }
378: name = name.replace(File.separatorChar, '/');
379: if (!name.startsWith("/")) {
380: name = "/" + name;
381: }
382:
383: // If the file does not exist, then assume that it's a directory
384: if (!file.isFile()) {
385: name = name + "/";
386: }
387: try {
388: return new URL("file", "", name);
389: } catch (MalformedURLException e) {
390: throw new IllegalArgumentException("file");
391: }
392: }
393:
394: }
|