Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/apache/axis/wsdl/WSDL2Java.java


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;
17  
18  import org.apache.axis.constants.Scope;
19  import org.apache.axis.utils.CLOption;
20  import org.apache.axis.utils.CLOptionDescriptor;
21  import org.apache.axis.utils.ClassUtils;
22  import org.apache.axis.utils.JavaUtils;
23  import org.apache.axis.utils.Messages;
24  import org.apache.axis.wsdl.gen.Parser;
25  import org.apache.axis.wsdl.gen.WSDL2;
26  import org.apache.axis.wsdl.toJava.Emitter;
27  import org.apache.axis.wsdl.toJava.NamespaceSelector;
28  
29  /**
30   * Command line interface to the WSDL2Java utility
31   */
32  public class WSDL2Java extends WSDL2 {
33  
34      // Define our short one-letter option identifiers.
35  
36      /** Field SERVER_OPT */
37      protected static final int SERVER_OPT = 's';
38  
39      /** Field SKELETON_DEPLOY_OPT */
40      protected static final int SKELETON_DEPLOY_OPT = 'S';
41  
42      /** Field NAMESPACE_OPT */
43      protected static final int NAMESPACE_OPT = 'N';
44  
45      /** Field NAMESPACE_FILE_OPT */
46      protected static final int NAMESPACE_FILE_OPT = 'f';
47  
48      /** Field OUTPUT_OPT */
49      protected static final int OUTPUT_OPT = 'o';
50  
51      /** Field SCOPE_OPT */
52      protected static final int SCOPE_OPT = 'd';
53  
54      /** Field TEST_OPT */
55      protected static final int TEST_OPT = 't';
56      /** Field BUILDFILE_OPT */
57      protected static final int BUILDFILE_OPT = 'B';
58      /** Field PACKAGE_OPT */
59      protected static final int PACKAGE_OPT = 'p';
60  
61      /** Field ALL_OPT */
62      protected static final int ALL_OPT = 'a';
63  
64      /** Field TYPEMAPPING_OPT */
65      protected static final int TYPEMAPPING_OPT = 'T';
66  
67      /** Field FACTORY_CLASS_OPT */
68      protected static final int FACTORY_CLASS_OPT = 'F';
69  
70      /** Field HELPER_CLASS_OPT */
71      protected static final int HELPER_CLASS_OPT = 'H';
72  
73      /** Field USERNAME_OPT */
74      protected static final int USERNAME_OPT = 'U';
75  
76      /** Field PASSWORD_OPT */
77      protected static final int PASSWORD_OPT = 'P';
78  
79      protected static final int CLASSPATH_OPT = 'X';
80  
81      /** Field bPackageOpt */
82      protected boolean bPackageOpt = false;
83  
84      /** Field namespace include */
85      protected static final int NS_INCLUDE_OPT = 'i';
86      
87    /** Filed namespace exclude */
88    protected static final int NS_EXCLUDE_OPT = 'x';
89    
90    /** Field IMPL_CLASS_OPT */
91    protected static final int IMPL_CLASS_OPT = 'c';
92  
93      /** Field ALLOW_INVALID_URL_OPT */
94      protected static final int ALLOW_INVALID_URL_OPT = 'u';
95      
96      /** Wrap arrays option */
97      protected static final int WRAP_ARRAYS_OPT = 'w';
98  
99      /** Field emitter */
100     private Emitter emitter;
101     
102     /**
103      * Define the understood options. Each CLOptionDescriptor contains:
104      * - The "long" version of the option. Eg, "help" means that "--help" will
105      * be recognised.
106      * - The option flags, governing the option's argument(s).
107      * - The "short" version of the option. Eg, 'h' means that "-h" will be
108      * recognised.
109      * - A description of the option for the usage message
110      */
111 
112     protected static final CLOptionDescriptor[] options =
113             new CLOptionDescriptor[]{
114                 new CLOptionDescriptor("server-side",
115                         CLOptionDescriptor.ARGUMENT_DISALLOWED,
116                         SERVER_OPT, Messages.getMessage("optionSkel00")),
117                 new CLOptionDescriptor("skeletonDeploy",
118                         CLOptionDescriptor.ARGUMENT_REQUIRED,
119                         SKELETON_DEPLOY_OPT,
120                         Messages.getMessage("optionSkeletonDeploy00")),
121                 new CLOptionDescriptor("NStoPkg",
122                         CLOptionDescriptor.DUPLICATES_ALLOWED
123                         + CLOptionDescriptor.ARGUMENTS_REQUIRED_2,
124                         NAMESPACE_OPT,
125                         Messages.getMessage("optionNStoPkg00")),
126                 new CLOptionDescriptor("fileNStoPkg",
127                         CLOptionDescriptor.ARGUMENT_REQUIRED,
128                         NAMESPACE_FILE_OPT,
129                         Messages.getMessage("optionFileNStoPkg00")),
130                 new CLOptionDescriptor("package", CLOptionDescriptor.ARGUMENT_REQUIRED,
131                         PACKAGE_OPT,
132                         Messages.getMessage("optionPackage00")),
133                 new CLOptionDescriptor("output", CLOptionDescriptor.ARGUMENT_REQUIRED,
134                         OUTPUT_OPT,
135                         Messages.getMessage("optionOutput00")),
136                 new CLOptionDescriptor("deployScope",
137                         CLOptionDescriptor.ARGUMENT_REQUIRED, SCOPE_OPT,
138                         Messages.getMessage("optionScope00")),
139                 new CLOptionDescriptor("testCase",
140                         CLOptionDescriptor.ARGUMENT_DISALLOWED,
141                         TEST_OPT, Messages.getMessage("optionTest00")),
142                 new CLOptionDescriptor("all", CLOptionDescriptor.ARGUMENT_DISALLOWED,
143                         ALL_OPT, Messages.getMessage("optionAll00")),
144                 new CLOptionDescriptor("typeMappingVersion",
145                         CLOptionDescriptor.ARGUMENT_REQUIRED,
146                         TYPEMAPPING_OPT,
147                         Messages.getMessage("optionTypeMapping00")),
148                 new CLOptionDescriptor("factory", CLOptionDescriptor.ARGUMENT_REQUIRED,
149                         FACTORY_CLASS_OPT,
150                         Messages.getMessage("optionFactory00")),
151                 new CLOptionDescriptor("helperGen",
152                         CLOptionDescriptor.ARGUMENT_DISALLOWED,
153                         HELPER_CLASS_OPT,
154                         Messages.getMessage("optionHelper00")),
155                 new CLOptionDescriptor("buildFile", CLOptionDescriptor.ARGUMENT_DISALLOWED,
156                         BUILDFILE_OPT,
157                         Messages.getMessage("optionBuildFile00")),        
158                 new CLOptionDescriptor("user", CLOptionDescriptor.ARGUMENT_REQUIRED,
159                         USERNAME_OPT,
160                         Messages.getMessage("optionUsername")),
161                 new CLOptionDescriptor("password",
162                         CLOptionDescriptor.ARGUMENT_REQUIRED,
163                         PASSWORD_OPT,
164                         Messages.getMessage("optionPassword")),
165                 new CLOptionDescriptor("classpath",
166                         CLOptionDescriptor.ARGUMENT_OPTIONAL,
167                         CLASSPATH_OPT,
168                         Messages.getMessage("optionClasspath")),
169                 new CLOptionDescriptor("nsInclude",
170                         CLOptionDescriptor.DUPLICATES_ALLOWED
171                         + CLOptionDescriptor.ARGUMENT_REQUIRED,
172                         NS_INCLUDE_OPT,
173                         Messages.getMessage("optionNSInclude")),
174         new CLOptionDescriptor("nsExclude",
175             CLOptionDescriptor.DUPLICATES_ALLOWED
176             + CLOptionDescriptor.ARGUMENT_REQUIRED,
177             NS_EXCLUDE_OPT,
178             Messages.getMessage("optionNSExclude")),
179         new CLOptionDescriptor("implementationClassName",
180             CLOptionDescriptor.ARGUMENT_REQUIRED,
181             IMPL_CLASS_OPT,
182             Messages.getMessage("implementationClassName")),
183                 new CLOptionDescriptor("allowInvalidURL", CLOptionDescriptor.ARGUMENT_DISALLOWED,
184                         ALLOW_INVALID_URL_OPT, Messages.getMessage("optionAllowInvalidURL")),
185                 new CLOptionDescriptor("wrapArrays",
186                                        CLOptionDescriptor.ARGUMENT_OPTIONAL,
187                                        WRAP_ARRAYS_OPT,
188                                        Messages.getMessage("optionWrapArrays")),
189                 };
190 
191     /**
192      * Instantiate a WSDL2Java emitter.
193      */
194     protected WSDL2Java() {
195 
196         // emitter is the same as the parent's parser variable.  Just cast it
197         // here once so we don't have to cast it every time we use it.
198         emitter = (Emitter) parser;
199 
200         addOptions(options);
201     }    // ctor
202 
203     /**
204      * Instantiate an extension of the Parser
205      * 
206      * @return 
207      */
208     protected Parser createParser() {
209         return new Emitter();
210     }    // createParser
211     
212     /**
213      * Parse an option
214      * 
215      * @param option is the option
216      */
217     protected void parseOption(CLOption option) {
218 
219         switch (option.getId()) {
220 
221             case FACTORY_CLASS_OPT:
222                 emitter.setFactory(option.getArgument());
223                 break;
224 
225             case HELPER_CLASS_OPT:
226                 emitter.setHelperWanted(true);
227                 break;
228 
229             case SKELETON_DEPLOY_OPT:
230                 emitter.setSkeletonWanted(
231                         JavaUtils.isTrueExplicitly(option.getArgument(0)));
232 
233                 // --skeletonDeploy assumes --server-side, so fall thru
234             case SERVER_OPT:
235                 emitter.setServerSide(true);
236                 break;
237 
238             case NAMESPACE_OPT:
239                 String namespace = option.getArgument(0);
240                 String packageName = option.getArgument(1);
241 
242                 emitter.getNamespaceMap().put(namespace, packageName);
243                 break;
244 
245             case NAMESPACE_FILE_OPT:
246                 emitter.setNStoPkg(option.getArgument());
247                 break;
248 
249             case PACKAGE_OPT:
250                 bPackageOpt = true;
251 
252                 emitter.setPackageName(option.getArgument());
253                 break;
254 
255             case OUTPUT_OPT:
256                 emitter.setOutputDir(option.getArgument());
257                 break;
258 
259             case SCOPE_OPT:
260                 String arg = option.getArgument();
261 
262                 // Provide 'null' default, prevents logging internal error.
263                 // we have something different to report here.
264                 Scope scope = Scope.getScope(arg, null);
265 
266                 if (scope != null) {
267                     emitter.setScope(scope);
268                 } else {
269                     System.err.println(Messages.getMessage("badScope00", arg));
270                 }
271                 break;
272 
273             case TEST_OPT:
274                 emitter.setTestCaseWanted(true);
275                 break;
276             case BUILDFILE_OPT:
277                 emitter.setBuildFileWanted(true);
278                 break;
279             case ALL_OPT:
280                 emitter.setAllWanted(true);
281                 break;
282 
283             case TYPEMAPPING_OPT:
284                 String tmValue = option.getArgument();
285 
286                 if (tmValue.equals("1.0")) {
287                     emitter.setTypeMappingVersion("1.0");
288                 } else if (tmValue.equals("1.1")) {
289                         emitter.setTypeMappingVersion("1.1");
290                 } else if (tmValue.equals("1.2")) {
291                     emitter.setTypeMappingVersion("1.2");
292                 } else if (tmValue.equals("1.3")) {
293                     emitter.setTypeMappingVersion("1.3");
294                 } else {
295                     System.out.println(
296                             Messages.getMessage("badTypeMappingOption00"));
297                 }
298                 break;
299 
300             case USERNAME_OPT:
301                 emitter.setUsername(option.getArgument());
302                 break;
303 
304             case PASSWORD_OPT:
305                 emitter.setPassword(option.getArgument());
306                 break;
307 
308             case CLASSPATH_OPT:
309                 ClassUtils.setDefaultClassLoader(ClassUtils.createClassLoader(
310                         option.getArgument(),
311                         this.getClass().getClassLoader()));
312                 break;
313 
314             case NS_INCLUDE_OPT:
315                 NamespaceSelector include = new NamespaceSelector();
316                 include.setNamespace(option.getArgument());
317                 emitter.getNamespaceIncludes().add(include);
318                 break;
319             case NS_EXCLUDE_OPT:
320                 NamespaceSelector exclude = new NamespaceSelector();
321                 exclude.setNamespace(option.getArgument());
322                 emitter.getNamespaceExcludes().add(exclude);
323                 break;
324 
325       case IMPL_CLASS_OPT:
326         emitter.setImplementationClassName(option.getArgument());
327         break;
328 
329             case ALLOW_INVALID_URL_OPT:
330                 emitter.setAllowInvalidURL(true);
331                 break;
332 
333             case WRAP_ARRAYS_OPT:
334                 emitter.setWrapArrays(true);
335                 break;
336 
337             default :
338                 super.parseOption(option);
339         }
340     }    // parseOption
341 
342     /**
343      * validateOptions
344      * This method is invoked after the options are set to validate
345      * the option settings.
346      */
347     protected void validateOptions() {
348 
349         super.validateOptions();
350 
351         // validate argument combinations
352         if (emitter.isSkeletonWanted() && !emitter.isServerSide()) {
353             System.out.println(Messages.getMessage("badSkeleton00"));
354             printUsage();
355         }
356 
357         if (!emitter.getNamespaceMap().isEmpty() && bPackageOpt) {
358             System.out.println(Messages.getMessage("badpackage00"));
359             printUsage();
360         }
361     }    // validateOptions
362 
363     /**
364      * Main
365      * Run the WSDL2Java emitter with the specified command-line arguments
366      * 
367      * @param args command-line arguments
368      */
369     public static void main(String args[]) {
370 
371         WSDL2Java wsdl2java = new WSDL2Java();
372 
373         wsdl2java.run(args);
374     }
375 }