KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > wsdl > gen > WSDL2


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Axis" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation. For more
52  * information on the Apache Software Foundation, please see
53  * <http://www.apache.org/>.
54  */

55 package org.jboss.axis.wsdl.gen;
56
57 import org.jboss.axis.utils.CLArgsParser;
58 import org.jboss.axis.utils.CLOption;
59 import org.jboss.axis.utils.CLOptionDescriptor;
60 import org.jboss.axis.utils.CLUtil;
61 import org.jboss.axis.utils.DefaultAuthenticator;
62 import org.jboss.axis.utils.Messages;
63
64 import java.net.Authenticator JavaDoc;
65 import java.net.MalformedURLException JavaDoc;
66 import java.net.URL JavaDoc;
67 import java.util.List JavaDoc;
68
69 public class WSDL2
70 {
71
72    protected static final int DEBUG_OPT = 'D';
73    protected static final int HELP_OPT = 'h';
74    protected static final int NETWORK_TIMEOUT_OPT = 'O';
75    protected static final int NOIMPORTS_OPT = 'n';
76    protected static final int VERBOSE_OPT = 'v';
77    protected static final int NOWRAP_OPT = 'W';
78
79    protected CLOptionDescriptor[] options = new CLOptionDescriptor[]{
80       new CLOptionDescriptor("help",
81               CLOptionDescriptor.ARGUMENT_DISALLOWED,
82               HELP_OPT,
83               Messages.getMessage("optionHelp00")),
84       new CLOptionDescriptor("verbose",
85               CLOptionDescriptor.ARGUMENT_DISALLOWED,
86               VERBOSE_OPT,
87               Messages.getMessage("optionVerbose00")),
88       new CLOptionDescriptor("noImports",
89               CLOptionDescriptor.ARGUMENT_DISALLOWED,
90               NOIMPORTS_OPT,
91               Messages.getMessage("optionImport00")),
92       new CLOptionDescriptor("timeout",
93               CLOptionDescriptor.ARGUMENT_REQUIRED,
94               NETWORK_TIMEOUT_OPT,
95               Messages.getMessage("optionTimeout00")),
96       new CLOptionDescriptor("Debug",
97               CLOptionDescriptor.ARGUMENT_DISALLOWED,
98               DEBUG_OPT,
99               Messages.getMessage("optionDebug00")),
100       new CLOptionDescriptor("noWrapped",
101               CLOptionDescriptor.ARGUMENT_DISALLOWED,
102               NOWRAP_OPT,
103               Messages.getMessage("optionNoWrap00"))
104    };
105
106    protected String JavaDoc wsdlURI = null;
107    protected Parser parser;
108
109    /**
110     * Constructor
111     * Used by extended classes to construct an instance of WSDL2
112     */

113    protected WSDL2()
114    {
115       parser = createParser();
116    } // ctor
117

118    /**
119     * createParser
120     * Used by extended classes to construct an instance of the Parser
121     */

122    protected Parser createParser()
123    {
124       return new Parser();
125    } // createParser
126

127    /**
128     * getParser
129     * get the Parser object
130     */

131    protected Parser getParser()
132    {
133       return parser;
134    } // getParser
135

136    /**
137     * addOptions
138     * Add option descriptions to the tool.
139     *
140     * @param newOptions CLOptionDescriptor[] the options
141     */

142    protected void addOptions(CLOptionDescriptor[] newOptions)
143    {
144       if (newOptions != null && newOptions.length > 0)
145       {
146          CLOptionDescriptor[] allOptions = new CLOptionDescriptor[
147                  options.length + newOptions.length];
148          System.arraycopy(options, 0, allOptions, 0, options.length);
149          System.arraycopy(newOptions, 0, allOptions, options.length, newOptions.length);
150          options = allOptions;
151       }
152    } // addOptions
153

154    /**
155     * removeOption
156     * Remove an option description from the tool.
157     *
158     * @param name the name of the CLOptionDescriptor to remove
159     */

160    protected void removeOption(String JavaDoc name)
161    {
162       int foundOptionIndex = -1;
163       for (int i = 0; i < options.length; i++)
164       {
165          if (options[i].getName().equals(name))
166          {
167             foundOptionIndex = i;
168             break;
169          }
170       }
171       if (foundOptionIndex != -1)
172       {
173          CLOptionDescriptor[] newOptions =
174                  new CLOptionDescriptor[options.length - 1];
175          System.arraycopy(options, 0, newOptions, 0, foundOptionIndex);
176          if (foundOptionIndex < newOptions.length)
177          {
178             System.arraycopy(options,
179                     foundOptionIndex + 1,
180                     newOptions,
181                     foundOptionIndex,
182                     newOptions.length - foundOptionIndex);
183          }
184          options = newOptions;
185       }
186    } // removeOption
187

188    /**
189     * Parse an option
190     *
191     * @param option CLOption is the option
192     */

193    protected void parseOption(CLOption option)
194    {
195       switch (option.getId())
196       {
197          case CLOption.TEXT_ARGUMENT:
198             if (wsdlURI != null)
199             {
200                System.out.println(Messages.getMessage("w2jDuplicateWSDLURI00",
201                        wsdlURI,
202                        option.getArgument()));
203                printUsage();
204             }
205             wsdlURI = option.getArgument();
206             break;
207
208          case HELP_OPT:
209             printUsage();
210             break;
211
212          case NOIMPORTS_OPT:
213             parser.setImports(false);
214             break;
215
216          case NETWORK_TIMEOUT_OPT:
217             String JavaDoc timeoutValue = option.getArgument();
218             long timeout = Long.parseLong(timeoutValue);
219             // Convert seconds to milliseconds.
220
if (timeout > 0)
221                timeout = timeout * 1000;
222             parser.setTimeout(timeout);
223             break;
224
225          case VERBOSE_OPT:
226             parser.setVerbose(true);
227             break;
228
229          case DEBUG_OPT:
230             parser.setDebug(true);
231             break;
232
233          case NOWRAP_OPT:
234             parser.setNowrap(true);
235             break;
236       }
237    } // parseOption
238

239    /**
240     * validateOptions
241     * This method is invoked after the options are set to validate and default the options
242     * the option settings.
243     */

244    protected void validateOptions()
245    {
246       if (wsdlURI == null)
247       {
248          System.out.println(Messages.getMessage("w2jMissingWSDLURI00"));
249          printUsage();
250       }
251
252       // Set username and password if provided in URL
253
checkForAuthInfo(wsdlURI);
254       Authenticator.setDefault(new DefaultAuthenticator(parser.getUsername(), parser.getPassword()));
255    } // validateOptions
256

257
258    /**
259     * checkForAuthInfo
260     * set user and password information
261     */

262    private void checkForAuthInfo(String JavaDoc uri)
263    {
264       URL JavaDoc url = null;
265       try
266       {
267          url = new URL JavaDoc(uri);
268       }
269       catch (MalformedURLException JavaDoc e)
270       {
271          // not going to have userInfo
272
return;
273       }
274       String JavaDoc userInfo = url.getUserInfo();
275       if (userInfo != null)
276       {
277          int i = userInfo.indexOf(':');
278          if (i >= 0)
279          {
280             parser.setUsername(userInfo.substring(0, i));
281             parser.setPassword(userInfo.substring(i + 1));
282          }
283          else
284          {
285             parser.setUsername(userInfo);
286          }
287       }
288    }
289
290    /**
291     * printUsage
292     * print usage information and quit.
293     */

294    protected void printUsage()
295    {
296       String JavaDoc lSep = System.getProperty("line.separator");
297       StringBuffer JavaDoc msg = new StringBuffer JavaDoc();
298       msg.append(Messages.getMessage("usage00",
299               "java " + getClass().getName() + " [options] WSDL-URI"))
300               .append(lSep);
301       msg.append(Messages.getMessage("options00")).append(lSep);
302       msg.append(CLUtil.describeOptions(options).toString());
303       System.out.println(msg.toString());
304       System.exit(1);
305    } // printUsage
306

307    /**
308     * run
309     * checkes the command-line arguments and runs the tool.
310     *
311     * @param args String[] command-line arguments.
312     */

313    protected void run(String JavaDoc[] args)
314    {
315       // Parse the arguments
316
CLArgsParser argsParser = new CLArgsParser(args, options);
317
318       // Print parser errors, if any
319
if (null != argsParser.getErrorString())
320       {
321          System.err.println(Messages.getMessage("error01", argsParser.getErrorString()));
322          printUsage();
323       }
324
325       // Get a list of parsed options
326
List JavaDoc clOptions = argsParser.getArguments();
327       int size = clOptions.size();
328
329       try
330       {
331          // Parse the options and configure the emitter as appropriate.
332
for (int i = 0; i < size; i++)
333          {
334             parseOption((CLOption)clOptions.get(i));
335          }
336
337          // validate argument combinations
338
//
339
validateOptions();
340
341          parser.run(wsdlURI);
342
343          // everything is good
344
System.exit(0);
345       }
346       catch (Throwable JavaDoc t)
347       {
348          t.printStackTrace();
349          System.exit(1);
350       }
351    } // run
352

353    /**
354     * Main
355     * Run the tool with the specified command-line arguments
356     *
357     * @param args String[] command-line arguments
358     */

359    public static void main(String JavaDoc[] args)
360    {
361       WSDL2 wsdl2 = new WSDL2();
362       wsdl2.run(args);
363    } // main
364
} // class WSDL2
365
Popular Tags