1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 package org.exolab.castor.builder;
51
52 import java.io.PrintWriter;
53 import java.util.Properties;
54
55 import org.exolab.castor.builder.binding.BindingException;
56 import org.exolab.castor.builder.binding.BindingLoader;
57 import org.exolab.castor.builder.factory.FieldInfoFactory;
58 import org.exolab.castor.util.CommandLineOptions;
59 import org.xml.sax.InputSource;
60
61
62
63
64
65
66
67
68
69
70
71
72 public final class SourceGeneratorMain {
73
74
75
76
77 private static final String ARGUMENT_BINDING_FILENAME = "binding-file";
78 private static final String ARGUMENT_CASE_INSENSITIVE = "case-insensitive";
79 private static final String ARGUMENT_DESTINATION_DIR = "dest";
80 private static final String ARGUMENT_DISABLE_DESCRIPTORS = "nodesc";
81 private static final String ARGUMENT_FORCE = "f";
82 private static final String ARGUMENT_GENERATE_IMPORTED_SCHEMAS = "generateImportedSchemas";
83 private static final String ARGUMENT_GENERATE_MAPPING = "gen-mapping";
84 private static final String ARGUMENT_HELP = "h";
85 private static final String ARGUMENT_INPUT = "i";
86 private static final String ARGUMENT_INPUT_SOURCE = "is";
87 private static final String ARGUMENT_LINE_SEPARATOR = "line-separator";
88 private static final String ARGUMENT_NOMARSHALL = "nomarshall";
89 private static final String ARGUMENT_PACKAGE = "package";
90 private static final String ARGUMENT_RESOURCES_DESTINATION_DIR = "resourcesDestination";
91 private static final String ARGUMENT_SAX1 = "sax1";
92 private static final String ARGUMENT_TESTABLE = "testable";
93 private static final String ARGUMENT_TYPES = "types";
94 private static final String ARGUMENT_TYPES_DEPRECATED = "type-factory";
95 private static final String ARGUMENT_TYPES_JAVA2 = "j2";
96 private static final String ARGUMENT_VERBOSE = "verbose";
97 private static final String ARGUMENT_FAIL_ON_ERROR = "fail";
98 private static final String ARGUMENT_NAME_CONFLICT_STRATEGY = "nameConflictStrategy";
99 private static final String ARGUMENT_NAME_JCLASSPRINTER = "classPrinter";
100 private static final String ARGUMENT_USE_OLD_FIELD_NAMING = "useOldFieldNaming";
101
102 private static final String ARG_VALUE_LINE_SEPARATION_MAC = "mac";
103 private static final String ARG_VALUE_LINE_SEPARATION_UNIX = "unix";
104 private static final String ARG_VALUE_LINE_SEPARATION_WIN = "win";
105
106
107
108
109
110
111 private static final String DISABLE_DESCRIPTORS_MSG =
112 "Disabling generation of Class descriptors.";
113
114
115 private static final String DISABLE_MARSHALL_MSG =
116 "Disabling generation of Marshaling framework methods (marshal, unmarshal, validate).";
117
118
119 private static final String CASTOR_TESTABLE_MSG =
120 "The generated classes will implement org.castor.xmlctf.CastorTestable.";
121
122
123 private static final String SAX1_MSG =
124 "The generated classes will use SAX 1.";
125
126 private static final String GENERATE_IMPORT_MSG =
127 "Imported XML Schemas will be processed automatically.";
128
129 private static final String CASE_INSENSITIVE_MSG =
130 "The generated classes will use a case insensitive method "
131 + "for looking up enumerated type values.";
132
133 private static final String TYPE_FACTORY_ARG_MSG =
134 "The argument '-type-factory' is deprecated; please use '-types' in its place.";
135
136 private static final String SUPPRESS_NON_FATAL_WARN_MSG =
137 "Suppressing non fatal warnings.";
138
139 private static final String GENERATING_MAPPING_FILE_MSG =
140 "Generating mapping file: ";
141
142 private static final String BINDING_FILE_ERROR1_MSG =
143 "Unable to load binding file ";
144
145 private static final String BINDING_FILE_ERROR2_MSG =
146 " due to the following Exception:";
147
148 private static final String BINDING_FILE_ERROR3_MSG =
149 "No binding file will be used.";
150
151 private static final String INVALID_TYPES_OPTION_MSG =
152 "Invalid option for '-types': ";
153
154 private static final String DEFAULT_FIELD_INFO_MSG =
155 "Using default source generator types.";
156
157 private static final String LINE_SEPARATION_WIN_MSG =
158 "Using Windows style line separation.";
159
160 private static final String LINE_SEPARATION_UNIX_MSG =
161 "Using UNIX style line separation.";
162
163 private static final String LINE_SEPARATION_MAC_MSG =
164 "Using Macintosh style line separation.";
165
166 private static final String DEFAULT_LINE_SEPARATOR_MSG =
167 "Using default line separator for this platform";
168
169 private static final String INVALID_LINE_SEPARATOR_MSG =
170 "Invalid option for line-separator: ";
171
172 private static final String NAME_CONFLICT_STRATEGY_MSG =
173 "Using name conflict strategy ";
174
175 private static final String JCLASSPRINTER_TYPE_MSG =
176 "Using JClass printing type ";
177
178 private static final String USING_SEPARATE_RESOURCES_DIRECTORY =
179 "Using a separate destination for resources.";
180
181 private static final String USE_OLD_FIELD_NAMING_MSG =
182 "Using old Java field naming conventions";
183
184 private static final String USE_NEW_FIELD_NAMING_MSG =
185 "Using new Java field naming conventions";
186
187
188 private static final CommandLineOptions ALL_OPTIONS = setupCommandLineOptions();
189
190
191
192
193 private SourceGeneratorMain() {
194
195 }
196
197
198
199
200
201
202
203
204
205
206
207 public static void main(final String[] args) {
208
209 Properties options = ALL_OPTIONS.getOptions(args);
210
211
212 if (options.getProperty(ARGUMENT_HELP) != null) {
213 PrintWriter pw = new PrintWriter(System.out, true);
214 ALL_OPTIONS.printHelp(pw);
215 return;
216 }
217
218
219 String schemaFilename = options.getProperty(ARGUMENT_INPUT);
220 String schemaURL = options.getProperty(ARGUMENT_INPUT_SOURCE);
221
222 if (schemaFilename == null && schemaURL == null) {
223 System.out.println(SourceGenerator.APP_NAME);
224 ALL_OPTIONS.printUsage(new PrintWriter(System.out));
225 return;
226 }
227
228
229 FieldInfoFactory factory = getTypeFactory(options);
230 SourceGenerator sgen = (factory == null) ? new SourceGenerator()
231 : new SourceGenerator(factory);
232
233
234
235 sgen.setLineSeparator(getLineSeparator(options.getProperty(ARGUMENT_LINE_SEPARATOR)));
236 sgen.setDestDir(options.getProperty(ARGUMENT_DESTINATION_DIR));
237
238
239 String resourcesDestination = options.getProperty(ARGUMENT_RESOURCES_DESTINATION_DIR);
240 if (resourcesDestination != null) {
241 sgen.setResourceDestination(resourcesDestination);
242 System.out.print("-- ");
243 System.out.println(USING_SEPARATE_RESOURCES_DIRECTORY);
244 }
245
246 sgen.setVerbose(options.getProperty(ARGUMENT_VERBOSE) != null);
247 sgen.setFailOnFirstError(options.getProperty(ARGUMENT_FAIL_ON_ERROR) != null);
248
249 boolean force = (options.getProperty(ARGUMENT_FORCE) != null);
250 sgen.setSuppressNonFatalWarnings(force);
251 if (force) {
252 System.out.print("-- ");
253 System.out.println(SUPPRESS_NON_FATAL_WARN_MSG);
254 }
255
256 if (options.getProperty(ARGUMENT_DISABLE_DESCRIPTORS) != null) {
257 sgen.setDescriptorCreation(false);
258 System.out.print("-- ");
259 System.out.println(DISABLE_DESCRIPTORS_MSG);
260 }
261
262 String mappingFilename = options.getProperty(ARGUMENT_GENERATE_MAPPING);
263 if (mappingFilename != null) {
264 sgen.setGenerateMappingFile(true);
265 if (mappingFilename.length() > 0) {
266 sgen.setMappingFilename(mappingFilename);
267 }
268 System.out.print("-- ");
269 System.out.println(GENERATING_MAPPING_FILE_MSG + "'" + mappingFilename + "'");
270 }
271
272 if (options.getProperty(ARGUMENT_NOMARSHALL) != null) {
273 sgen.setCreateMarshalMethods(false);
274 System.out.print("-- ");
275 System.out.println(DISABLE_MARSHALL_MSG);
276 }
277
278 if (options.getProperty(ARGUMENT_TESTABLE) != null) {
279 sgen.setTestable(true);
280 System.out.print("-- ");
281 System.out.println(CASTOR_TESTABLE_MSG);
282 }
283
284 if (options.getProperty(ARGUMENT_SAX1) != null) {
285 sgen.setSAX1(true);
286 System.out.print("-- ");
287 System.out.println(SAX1_MSG);
288 }
289
290 if (options.getProperty(ARGUMENT_CASE_INSENSITIVE) != null) {
291 sgen.setCaseInsensitive(true);
292 System.out.print("-- ");
293 System.out.println(CASE_INSENSITIVE_MSG);
294 }
295
296 String nameConflictStrategy = options.getProperty(ARGUMENT_NAME_CONFLICT_STRATEGY);
297 if (nameConflictStrategy != null) {
298 sgen.setNameConflictStrategy(nameConflictStrategy);
299 System.out.print("-- ");
300 System.out.println(CASE_INSENSITIVE_MSG + nameConflictStrategy);
301 }
302
303 String jClassPrinterType = options.getProperty(ARGUMENT_NAME_JCLASSPRINTER);
304 if (jClassPrinterType != null) {
305 sgen.setJClassPrinterType(jClassPrinterType);
306 System.out.print("-- ");
307 System.out.println(JCLASSPRINTER_TYPE_MSG + jClassPrinterType);
308 }
309
310 String bindingFilename = options.getProperty(ARGUMENT_BINDING_FILENAME);
311 if (bindingFilename != null) {
312 try {
313 sgen.setBinding(BindingLoader.createBinding(bindingFilename));
314 } catch (BindingException e) {
315 System.out.print("--");
316 System.out.println(BINDING_FILE_ERROR1_MSG + "'" + bindingFilename + "'"
317 + BINDING_FILE_ERROR2_MSG);
318 e.printStackTrace();
319 System.out.print("--");
320 System.out.println(BINDING_FILE_ERROR3_MSG);
321 }
322 }
323
324 if (options.getProperty(ARGUMENT_GENERATE_IMPORTED_SCHEMAS) != null) {
325 sgen.setGenerateImportedSchemas(true);
326 System.out.print("-- ");
327 System.out.println(GENERATE_IMPORT_MSG);
328 }
329
330 try {
331 if (schemaFilename != null) {
332 sgen.generateSource(schemaFilename, options.getProperty(ARGUMENT_PACKAGE));
333 } else if (schemaURL != null) {
334 sgen.generateSource(new InputSource(schemaURL), options.getProperty(ARGUMENT_PACKAGE));
335 }
336 } catch (Exception ex) {
337 ex.printStackTrace();
338 }
339 }
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363 private static FieldInfoFactory getTypeFactory(final Properties options) {
364 String typeFactory = options.getProperty(ARGUMENT_TYPES);
365 if (typeFactory == null) {
366
367 typeFactory = options.getProperty(ARGUMENT_TYPES_DEPRECATED);
368 if (typeFactory != null) {
369 System.out.print("-- ");
370 System.out.println(TYPE_FACTORY_ARG_MSG);
371 }
372 }
373
374
375 if (typeFactory != null && typeFactory.equals(ARGUMENT_TYPES_JAVA2)) {
376 typeFactory = "arraylist";
377 }
378
379
380 if (typeFactory == null) {
381 return null;
382 }
383
384 FieldInfoFactory factory = null;
385 try {
386 System.out.print("-- ");
387 if (Boolean.valueOf(options.getProperty(ARGUMENT_USE_OLD_FIELD_NAMING, "true"))) {
388 System.out.println(USE_OLD_FIELD_NAMING_MSG);
389 factory = new FieldInfoFactory(typeFactory);
390 } else {
391 System.out.println(USE_NEW_FIELD_NAMING_MSG);
392 factory = new FieldInfoFactory(typeFactory, false);
393 }
394 } catch (IllegalArgumentException e) {
395 try {
396
397 ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
398 factory = (FieldInfoFactory) classLoader.loadClass(typeFactory).newInstance();
399 } catch (Exception e2) {
400 System.out.println("-- " + INVALID_TYPES_OPTION_MSG + typeFactory);
401 System.out.println("-- " + e.getMessage());
402 System.out.println("-- " + DEFAULT_FIELD_INFO_MSG);
403 }
404 }
405
406 return factory;
407 }
408
409
410
411
412
413
414
415
416 private static String getLineSeparator(final String lineSepStyle) {
417 String lineSep = System.getProperty("line.separator");
418 if (lineSepStyle != null) {
419 if (ARG_VALUE_LINE_SEPARATION_WIN.equals(lineSepStyle)) {
420 System.out.println("-- " + LINE_SEPARATION_WIN_MSG);
421 lineSep = "\r\n";
422 } else if (ARG_VALUE_LINE_SEPARATION_UNIX.equals(lineSepStyle)) {
423 System.out.println("-- " + LINE_SEPARATION_UNIX_MSG);
424 lineSep = "\n";
425 } else if (ARG_VALUE_LINE_SEPARATION_MAC.equals(lineSepStyle)) {
426 System.out.println("-- " + LINE_SEPARATION_MAC_MSG);
427 lineSep = "\r";
428 } else {
429 System.out.println("-- " + INVALID_LINE_SEPARATOR_MSG + "'" + lineSepStyle + "'");
430 System.out.println("-- " + DEFAULT_LINE_SEPARATOR_MSG);
431 }
432 }
433 return lineSep;
434 }
435
436
437
438
439
440
441
442
443 private static CommandLineOptions setupCommandLineOptions() {
444 CommandLineOptions allOptions = new CommandLineOptions();
445 String desc;
446
447
448 desc = "Sets the filename for the schema used as input.";
449 allOptions.addFlag(ARGUMENT_INPUT, "schema filename", desc);
450
451
452 desc = "Sets the input source for the schema used as input.";
453 allOptions.addFlag(ARGUMENT_INPUT_SOURCE, "input source for XML schema", desc);
454
455
456 desc = "Sets the package name for generated code.";
457 allOptions.addFlag(ARGUMENT_PACKAGE, "package name", desc, true);
458
459
460 desc = "Sets the destination output directory.";
461 allOptions.addFlag(ARGUMENT_DESTINATION_DIR, "destination directory", desc, true);
462
463
464 desc = "Sets the destination output directory for resources.";
465 allOptions.addFlag(ARGUMENT_RESOURCES_DESTINATION_DIR, "resources destination directory", desc, true);
466
467
468 desc = "Sets the line separator style for the desired platform.";
469 allOptions.addFlag(ARGUMENT_LINE_SEPARATOR, "(unix | mac | win)", desc, true);
470
471
472 desc = "Suppresses non fatal warnings, such as overwriting files.";
473 allOptions.addFlag(ARGUMENT_FORCE, "", desc, true);
474
475
476 desc = "Displays this help screen.";
477 allOptions.addFlag(ARGUMENT_HELP, "", desc, true);
478
479
480 desc = "Prints out additional messages when creating source.";
481 allOptions.addFlag(ARGUMENT_VERBOSE, "", desc, true);
482
483
484 desc = "Causes source generation to fail on the first error encountered.";
485 allOptions.addFlag(ARGUMENT_FAIL_ON_ERROR, "", desc, true);
486
487
488 desc = "Disables the generation of the Class descriptors.";
489 allOptions.addFlag(ARGUMENT_DISABLE_DESCRIPTORS, "", desc, true);
490
491
492 desc = "Indicates that a mapping file should be generated.";
493 allOptions.addFlag(ARGUMENT_GENERATE_MAPPING, "mapping filename", desc, true);
494
495
496 desc = "Sets the source generator types name (SGTypeFactory).";
497 allOptions.addFlag(ARGUMENT_TYPES, "types", desc, true);
498
499
500 desc = "";
501 allOptions.addFlag(ARGUMENT_TYPES_DEPRECATED, "collections class name", desc, true);
502
503
504 desc = "Disables the generation of the methods specific to the XML marshaling framework.";
505 allOptions.addFlag(ARGUMENT_NOMARSHALL, "", desc, true);
506
507
508 desc = "Implements some specific methods to allow the generated classes"
509 + " to be used with Castor Testing Framework.";
510 allOptions.addFlag(ARGUMENT_TESTABLE, "", desc, true);
511
512
513 desc = "Uses SAX 1 in the generated code.";
514 allOptions.addFlag(ARGUMENT_SAX1, "", desc, true);
515
516
517 desc = "Sets the Source Generator Binding File name.";
518 allOptions.addFlag(ARGUMENT_BINDING_FILENAME, "filename", desc, true);
519
520
521 desc = "Generates sources for imported XML schemas.";
522 allOptions.addFlag(ARGUMENT_GENERATE_IMPORTED_SCHEMAS, "", desc, true);
523
524
525 desc = "Sets enumerated types to use a case insensitive lookup.";
526 allOptions.addFlag(ARGUMENT_CASE_INSENSITIVE, "", desc);
527
528
529 desc = "Sets name conflict strategy to use (possible values are "
530 + "'informViaLog', 'warnViaConsoleDialog').";
531 allOptions.addFlag(ARGUMENT_NAME_CONFLICT_STRATEGY, "", desc);
532
533 desc = "Selects the JClass printer type (default 'standard')";
534 allOptions.addFlag(ARGUMENT_NAME_JCLASSPRINTER, "<mode>", desc, true);
535
536 desc = "Whether to use old Java field naming conventions (default to 'true')";
537 allOptions.addFlag(ARGUMENT_USE_OLD_FIELD_NAMING, "", desc, true);
538
539 return allOptions;
540 }
541
542 }