KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.axis.wsdl.gen;
17
18 import org.apache.axis.utils.CLArgsParser;
19 import org.apache.axis.utils.CLOption;
20 import org.apache.axis.utils.CLOptionDescriptor;
21 import org.apache.axis.utils.CLUtil;
22 import org.apache.axis.utils.DefaultAuthenticator;
23 import org.apache.axis.utils.Messages;
24
25 import java.net.Authenticator JavaDoc;
26 import java.net.MalformedURLException JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.util.List JavaDoc;
29
30 /**
31  * Class WSDL2
32  *
33  * @version %I%, %G%
34  */

35 public class WSDL2 {
36
37     /** Field DEBUG_OPT */
38     protected static final int DEBUG_OPT = 'D';
39
40     /** Field HELP_OPT */
41     protected static final int HELP_OPT = 'h';
42
43     /** Field NETWORK_TIMEOUT_OPT */
44     protected static final int NETWORK_TIMEOUT_OPT = 'O';
45
46     /** Field NOIMPORTS_OPT */
47     protected static final int NOIMPORTS_OPT = 'n';
48
49     /** Field VERBOSE_OPT */
50     protected static final int VERBOSE_OPT = 'v';
51
52     /** Field NOWRAP_OPT */
53     protected static final int NOWRAP_OPT = 'W';
54
55     /** Filed quiet */
56     protected static final int QUIET_OPT = 'q';
57
58     /** Field options */
59     protected CLOptionDescriptor[] options = new CLOptionDescriptor[]{
60         new CLOptionDescriptor("help", CLOptionDescriptor.ARGUMENT_DISALLOWED,
61                 HELP_OPT, Messages.getMessage("optionHelp00")),
62         new CLOptionDescriptor("verbose",
63                 CLOptionDescriptor.ARGUMENT_DISALLOWED,
64                 VERBOSE_OPT,
65                 Messages.getMessage("optionVerbose00")),
66         new CLOptionDescriptor("noImports",
67                 CLOptionDescriptor.ARGUMENT_DISALLOWED,
68                 NOIMPORTS_OPT,
69                 Messages.getMessage("optionImport00")),
70         new CLOptionDescriptor("timeout", CLOptionDescriptor.ARGUMENT_REQUIRED,
71                 NETWORK_TIMEOUT_OPT,
72                 Messages.getMessage("optionTimeout00")),
73         new CLOptionDescriptor("Debug", CLOptionDescriptor.ARGUMENT_DISALLOWED,
74                 DEBUG_OPT, Messages.getMessage("optionDebug00")),
75         new CLOptionDescriptor("noWrapped",
76                 CLOptionDescriptor.ARGUMENT_DISALLOWED,
77                 NOWRAP_OPT,
78                 Messages.getMessage("optionNoWrap00")),
79         new CLOptionDescriptor("quiet",
80                 CLOptionDescriptor.ARGUMENT_DISALLOWED,
81                 QUIET_OPT,
82                 Messages.getMessage("optionQuiet"))
83     };
84
85     /** Field wsdlURI */
86     protected String JavaDoc wsdlURI = null;
87
88     /** Field parser */
89     protected Parser parser;
90
91     /**
92      * Constructor
93      * Used by extended classes to construct an instance of WSDL2
94      */

95     protected WSDL2() {
96         parser = createParser();
97     } // ctor
98

99     /**
100      * createParser
101      * Used by extended classes to construct an instance of the Parser
102      *
103      * @return
104      */

105     protected Parser createParser() {
106         return new Parser();
107     } // createParser
108

109     /**
110      * getParser
111      * get the Parser object
112      *
113      * @return
114      */

115     protected Parser getParser() {
116         return parser;
117     } // getParser
118

119     /**
120      * addOptions
121      * Add option descriptions to the tool.
122      *
123      * @param newOptions CLOptionDescriptor[] the options
124      */

125     protected void addOptions(CLOptionDescriptor[] newOptions) {
126
127         if ((newOptions != null) && (newOptions.length > 0)) {
128             CLOptionDescriptor[] allOptions =
129                     new CLOptionDescriptor[options.length + newOptions.length];
130
131             System.arraycopy(options, 0, allOptions, 0, options.length);
132             System.arraycopy(newOptions, 0, allOptions, options.length,
133                     newOptions.length);
134
135             options = allOptions;
136         }
137     } // addOptions
138

139     /**
140      * removeOption
141      * Remove an option description from the tool.
142      *
143      * @param name the name of the CLOptionDescriptor to remove
144      */

145     protected void removeOption(String JavaDoc name) {
146
147         int foundOptionIndex = -1;
148
149         for (int i = 0; i < options.length; i++) {
150             if (options[i].getName().equals(name)) {
151                 foundOptionIndex = i;
152
153                 break;
154             }
155         }
156
157         if (foundOptionIndex != -1) {
158             CLOptionDescriptor[] newOptions =
159                     new CLOptionDescriptor[options.length - 1];
160
161             System.arraycopy(options, 0, newOptions, 0, foundOptionIndex);
162
163             if (foundOptionIndex < newOptions.length) {
164                 System.arraycopy(options, foundOptionIndex + 1, newOptions,
165                         foundOptionIndex,
166                         newOptions.length - foundOptionIndex);
167             }
168
169             options = newOptions;
170         }
171     } // removeOption
172

173     /**
174      * Parse an option
175      *
176      * @param option CLOption is the option
177      */

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

232     /**
233      * validateOptions
234      * This method is invoked after the options are set to validate and default the options
235      * the option settings.
236      */

237     protected void validateOptions() {
238
239         if (wsdlURI == null) {
240             System.out.println(Messages.getMessage("w2jMissingWSDLURI00"));
241             printUsage();
242         }
243
244         if (parser.isQuiet()) {
245             if (parser.isVerbose()) {
246                 System.out.println(Messages.getMessage("exclusiveQuietVerbose"));
247                 printUsage();
248             }
249             if (parser.isDebug()) {
250                 System.out.println(Messages.getMessage("exclusiveQuietDebug"));
251                 printUsage();
252             }
253         }
254
255         // Set username and password if provided in URL
256
checkForAuthInfo(wsdlURI);
257         Authenticator.setDefault(new DefaultAuthenticator(parser.getUsername(),
258                 parser.getPassword()));
259     } // validateOptions
260

261     /**
262      * checkForAuthInfo
263      * set user and password information
264      *
265      * @param uri
266      */

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

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

311     /**
312      * run
313      * checkes the command-line arguments and runs the tool.
314      *
315      * @param args String[] command-line arguments.
316      */

317     protected void run(String JavaDoc[] args) {
318
319         // Parse the arguments
320
CLArgsParser argsParser = new CLArgsParser(args, options);
321
322         // Print parser errors, if any
323
if (null != argsParser.getErrorString()) {
324             System.err.println(
325                     Messages.getMessage("error01", argsParser.getErrorString()));
326             printUsage();
327         }
328
329         // Get a list of parsed options
330
List JavaDoc clOptions = argsParser.getArguments();
331         int size = clOptions.size();
332
333         try {
334
335             // Parse the options and configure the emitter as appropriate.
336
for (int i = 0; i < size; i++) {
337                 parseOption((CLOption) clOptions.get(i));
338             }
339
340             // validate argument combinations
341
//
342
validateOptions();
343             parser.run(wsdlURI);
344
345             // everything is good
346
System.exit(0);
347         } catch (Throwable JavaDoc t) {
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
363         wsdl2.run(args);
364     } // main
365
} // class WSDL2
366
Popular Tags