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 package org.castor.anttask;
46
47 import java.io.File;
48 import java.io.FileInputStream;
49 import java.io.FileNotFoundException;
50 import java.io.IOException;
51 import java.lang.reflect.InvocationTargetException;
52 import java.lang.reflect.Method;
53 import java.util.Properties;
54 import java.util.Vector;
55
56 import org.apache.tools.ant.BuildException;
57 import org.apache.tools.ant.DirectoryScanner;
58 import org.apache.tools.ant.taskdefs.MatchingTask;
59 import org.apache.tools.ant.types.FileSet;
60 import org.castor.xml.BackwardCompatibilityContext;
61 import org.castor.xml.InternalContext;
62 import org.exolab.castor.builder.SourceGenerator;
63 import org.exolab.castor.builder.binding.ExtendedBinding;
64 import org.exolab.castor.builder.factory.FieldInfoFactory;
65 import org.exolab.castor.xml.XMLException;
66 import org.exolab.castor.xml.schema.Schema;
67 import org.exolab.castor.xml.schema.SchemaContext;
68 import org.exolab.castor.xml.schema.SchemaContextImpl;
69 import org.exolab.castor.xml.schema.reader.Sax2ComponentReader;
70 import org.exolab.castor.xml.schema.reader.SchemaUnmarshaller;
71 import org.exolab.javasource.JClass;
72 import org.xml.sax.InputSource;
73 import org.xml.sax.Parser;
74 import org.xml.sax.SAXException;
75
76
77
78
79
80
81
82
83
84
85
86 public final class CastorCodeGenTask extends MatchingTask {
87
88
89 private static final String PROBLEM_SETTING_JDO_DESC =
90 "Problem calling SourceGenerator.setJdoDescriptorCreation: ";
91
92
93 private static final String DISABLE_DESCRIPTORS_MSG =
94 "Disabling generation of Class descriptors";
95
96
97 private static final String DISABLE_MARSHAL_MSG =
98 "Disabling generation of Marshaling framework methods (marshal, unmarshal, validate).";
99
100
101 private static final String CASTOR_TESTABLE_MSG =
102 "The generated classes will implement org.exolab.castor.tests.CastorTestable";
103
104
105 private static final String INVALID_LINESEP_MSG =
106 "Invalid value for lineseparator, must be win, unix, or mac.";
107
108
109 private static final String NO_SCHEMA_MSG =
110 "At least one of the file, url or dir attributes, or a fileset element, must be set.";
111
112
113 private InternalContext _internalContext;
114
115
116 private File _schemaFile = null;
117
118
119 private String _schemaURL = null;
120
121
122 private File _schemaDir = null;
123
124
125 private Vector < FileSet > _schemaFilesets = new Vector < FileSet > ();
126
127
128
129 private String _srcpackage;
130
131
132 private String _todir;
133
134
135 private String _resourcesDirectory;
136
137
138 private String _bindingfile;
139
140
141 private String _types;
142
143
144 private String _lineseparator;
145
146
147 private boolean _verbose;
148
149
150
151 private boolean _warnings = true;
152
153
154 private boolean _nodesc;
155
156
157 private boolean _nomarshal;
158
159
160 private boolean _useOldFieldNaming = true;
161
162
163
164
165 private boolean _generateMapping;
166
167
168 private boolean _testable;
169
170
171 private boolean _generateImportedSchemas;
172
173
174 private boolean _sax1;
175
176
177 private boolean _caseInsensitive;
178
179
180 private String _properties;
181
182
183 private String _nameConflictStrategy = "warnViaConsoleDialog";
184
185
186 private String _automaticConflictStrategy = "xpath";
187
188
189 private boolean _generateJdoDescriptors;
190
191
192
193
194 private String _jclassPrinterType = "standard";
195
196
197 private SourceGenerator _sgen;
198
199
200
201
202 public CastorCodeGenTask() {
203 super();
204 _internalContext = new BackwardCompatibilityContext();
205 }
206
207
208
209
210
211
212 public void setFile(final File file) {
213 _schemaFile = file;
214 }
215
216
217
218
219
220
221 public void setSchemaURL(final String schemaURL) {
222 _schemaURL = schemaURL;
223 }
224
225
226
227
228
229
230
231
232 public void setDir(final File dir) {
233 _schemaDir = dir;
234 }
235
236
237
238
239
240
241 public void addFileset(final FileSet set) {
242 _schemaFilesets.addElement(set);
243 }
244
245
246
247
248
249
250 public void setPackage(final String pack) {
251 _srcpackage = pack;
252 }
253
254
255
256
257
258
259 public void setTodir(final String dest) {
260 _todir = dest;
261 }
262
263
264
265
266
267
268
269
270 public void setResourcesDirectory(final String destination) {
271 _resourcesDirectory = destination;
272 }
273
274
275
276
277
278
279 public void setBindingfile(final String bindingfile) {
280 _bindingfile = bindingfile;
281 }
282
283
284
285
286
287
288 public void setLineseparator(final String ls) {
289 _lineseparator = ls;
290 }
291
292
293
294
295
296
297 public void setTypes(final String tf) {
298 _types = (tf.equals("j2")) ? "arraylist" : tf;
299 }
300
301
302
303
304
305
306 public void setVerbose(final boolean b) {
307 _verbose = b;
308 }
309
310
311
312
313
314
315 public void setNameConflictStrategy(final String nameConflictStrategy) {
316 _nameConflictStrategy = nameConflictStrategy;
317 }
318
319
320
321
322
323
324 public void setAutomaticConflictStrategy(final String automaticConflictStrategy) {
325 _automaticConflictStrategy = automaticConflictStrategy;
326 }
327
328
329
330
331
332
333
334
335 public void setWarnings(final boolean b) {
336 _warnings = b;
337 }
338
339
340
341
342
343
344 public void setUseOldFieldNaming(final boolean useOldFieldNaming) {
345 _useOldFieldNaming = useOldFieldNaming;
346 }
347
348
349
350
351
352
353 public void setNodesc(final boolean b) {
354 _nodesc = b;
355 }
356
357
358
359
360
361
362 public void setNomarshal(final boolean b) {
363 _nomarshal = b;
364 }
365
366
367
368
369
370
371 public void setTestable(final boolean b) {
372 _testable = b;
373 }
374
375
376
377
378
379
380 public void setGenerateImportedSchemas(final boolean generateImportedSchemas) {
381 _generateImportedSchemas = generateImportedSchemas;
382 }
383
384
385
386
387
388
389 public void setGenerateJdoDescriptors(final boolean generateJdoDescriptors) {
390 _generateJdoDescriptors = generateJdoDescriptors;
391 }
392
393
394
395
396
397
398
399 public void setSAX1(final boolean sax1) {
400 _sax1 = sax1;
401 }
402
403
404
405
406
407
408
409
410 public void setCaseInsensitive(final boolean caseInsensitive) {
411 _caseInsensitive = caseInsensitive;
412 }
413
414
415
416
417
418
419 public void setProperties(final String properties) {
420 _properties = properties;
421 }
422
423
424
425
426
427 public void setJClassPrinterType(final String jclassPrinterType) {
428 _jclassPrinterType = jclassPrinterType;
429 }
430
431
432
433
434
435
436 public void setGenerateMapping(final boolean generateMapping) {
437 _generateMapping = generateMapping;
438 }
439
440
441
442
443
444 private void config() {
445
446 if (_types != null) {
447 FieldInfoFactory factory;
448 try {
449 if (_useOldFieldNaming) {
450 factory = new FieldInfoFactory(_types);
451 } else {
452 factory = new FieldInfoFactory(_types, false);
453 }
454
455 _sgen = new CastorSourceGeneratorWrapper(factory);
456 } catch (Exception e) {
457 try {
458 factory = (FieldInfoFactory) Class.forName(_types).newInstance();
459 _sgen = new CastorSourceGeneratorWrapper(factory);
460 } catch (Exception e2) {
461 throw new BuildException("Invalid types \"" + _types + "\": " + e.getMessage());
462 }
463 }
464 } else {
465
466 _sgen = new CastorSourceGeneratorWrapper();
467 }
468
469
470 String lineSep = System.getProperty("line.separator");
471 if (_lineseparator != null) {
472 if ("win".equals(_lineseparator)) {
473 log("Using Windows style line separation.");
474 lineSep = "\r\n";
475 } else if ("unix".equals(_lineseparator)) {
476 log("Using UNIX style line separation.");
477 lineSep = "\n";
478 } else if ("mac".equals(_lineseparator)) {
479 log("Using Macintosh style line separation.");
480 lineSep = "\r";
481 } else {
482 throw new BuildException(INVALID_LINESEP_MSG);
483 }
484 }
485 _sgen.setLineSeparator(lineSep);
486
487 _sgen.setDestDir(_todir);
488
489
490 if (_resourcesDirectory != null && _resourcesDirectory.length() > 0) {
491 _sgen.setResourceDestination(_resourcesDirectory);
492 }
493
494 if (_bindingfile != null) { _sgen.setBinding(_bindingfile); }
495
496 _sgen.setVerbose(_verbose);
497 _sgen.setSuppressNonFatalWarnings(!_warnings);
498
499 _sgen.setDescriptorCreation(!_nodesc);
500 if (_nodesc) { log(DISABLE_DESCRIPTORS_MSG); }
501
502 _sgen.setCreateMarshalMethods(!_nomarshal);
503 if (_nomarshal) { log(DISABLE_MARSHAL_MSG); }
504
505 _sgen.setGenerateImportedSchemas(_generateImportedSchemas);
506
507 _sgen.setSAX1(_sax1);
508
509 _sgen.setCaseInsensitive(_caseInsensitive);
510
511 _sgen.setNameConflictStrategy(_nameConflictStrategy);
512
513 _sgen.setClassNameConflictResolver(_automaticConflictStrategy);
514
515 _sgen.setJClassPrinterType(_jclassPrinterType);
516
517 _sgen.setGenerateMappingFile(_generateMapping);
518
519 if (_generateJdoDescriptors) {
520 callSetterMethodUsingReflection(_sgen, "setJdoDescriptorCreation",
521 boolean.class, new Boolean(_generateJdoDescriptors));
522 }
523
524 _sgen.setTestable(_testable);
525 if (this._testable) { log(CASTOR_TESTABLE_MSG); }
526
527
528 if (_properties != null) {
529 String filePath = new File(_properties).getAbsolutePath();
530 Properties customProperties = new Properties();
531 try {
532 customProperties.load(new FileInputStream(filePath));
533 } catch (FileNotFoundException e) {
534 throw new BuildException("Properties file \"" + filePath + "\" not found");
535 } catch (IOException e) {
536 throw new BuildException("Can't read properties file \"" + filePath + "\": " + e);
537 }
538 _sgen.setDefaultProperties(customProperties);
539 }
540 }
541
542
543
544
545
546
547
548 private void processFile(final String filePath) {
549 log("Processing " + filePath);
550 try {
551 _sgen.generateSource(filePath, _srcpackage);
552 } catch (FileNotFoundException e) {
553 String message = "XML Schema file \"" + _schemaFile.getAbsolutePath() + "\" not found.";
554 log(message);
555 throw new BuildException(message);
556 } catch (Exception iox) {
557 throw new BuildException(iox);
558 }
559 }
560
561
562
563
564
565
566
567 private void processURL(final String schemaURL) {
568 log("Processing " + schemaURL);
569 try {
570 InputSource inputSource = new InputSource(schemaURL);
571 _sgen.generateSource(inputSource, _srcpackage);
572 } catch (FileNotFoundException e) {
573 String message = "XML Schema file \"" + _schemaURL + "\" not found.";
574 log(message);
575 throw new BuildException(message);
576 } catch (Exception iox) {
577 throw new BuildException(iox);
578 }
579 }
580
581
582
583
584
585
586
587
588
589 public void execute() {
590
591 if (_schemaFile == null && _schemaDir == null && _schemaFilesets.size() == 0
592 && _schemaURL == null) {
593 throw new BuildException(NO_SCHEMA_MSG);
594 }
595
596 try {
597 config();
598
599
600 if (_schemaFile != null) {
601 processFile(_schemaFile.getAbsolutePath());
602 }
603
604
605 if (_schemaDir != null && _schemaDir.exists() && _schemaDir.isDirectory()) {
606 DirectoryScanner ds = this.getDirectoryScanner(_schemaDir);
607
608 String[] files = ds.getIncludedFiles();
609 for (int i = 0; i < files.length; i++) {
610 String filePath = _schemaDir.getAbsolutePath() + File.separator + files[i];
611 processFile(filePath);
612 }
613 }
614
615
616 for (int i = 0; i < _schemaFilesets.size(); i++) {
617 FileSet fs = (FileSet) _schemaFilesets.elementAt(i);
618 DirectoryScanner ds = fs.getDirectoryScanner(getProject());
619 File subdir = fs.getDir(getProject());
620
621 String[] files = ds.getIncludedFiles();
622 for (int j = 0; j < files.length; j++) {
623 String filePath = subdir.getAbsolutePath() + File.separator + files[j];
624 processFile(filePath);
625 }
626 }
627
628
629 if (_schemaURL != null) {
630 processURL(_schemaURL);
631 }
632 } finally {
633 _sgen = null;
634 }
635 }
636
637
638
639
640
641 private final class CastorSourceGeneratorWrapper extends SourceGenerator {
642
643
644
645 public CastorSourceGeneratorWrapper() {
646 super();
647 }
648
649
650
651
652
653
654 public CastorSourceGeneratorWrapper(final FieldInfoFactory fieldInfoFactory) {
655 super(fieldInfoFactory);
656 }
657
658
659
660
661
662
663
664
665 public CastorSourceGeneratorWrapper(
666 final FieldInfoFactory fieldInfoFactory, final ExtendedBinding extendedBinding) {
667 super(fieldInfoFactory, extendedBinding);
668 }
669
670
671
672
673
674
675
676
677
678
679
680 public void generateSource(final InputSource source, final String packageName) {
681 Parser parser = null;
682 try {
683 parser = _internalContext.getParser();
684 } catch (RuntimeException e) {
685 throw new BuildException("Unable to create SAX parser.", e);
686 }
687 if (parser == null) {
688 throw new BuildException("Unable to create SAX parser.");
689 }
690
691 SchemaContext schemaContext = new SchemaContextImpl();
692 SchemaUnmarshaller schemaUnmarshaller = null;
693 try {
694 schemaUnmarshaller = new SchemaUnmarshaller(schemaContext);
695 } catch (XMLException e) {
696 throw new BuildException("Unable to create schema unmarshaller.", e);
697 }
698
699 Sax2ComponentReader handler = new Sax2ComponentReader(schemaUnmarshaller);
700 parser.setDocumentHandler(handler);
701 parser.setErrorHandler(handler);
702 try {
703 parser.parse(source);
704 } catch (IOException e) {
705 String msg = "Can't read input file " + source.getSystemId() + ".\n" + e;
706 throw new BuildException(msg, e);
707 } catch (SAXException e) {
708 String msg = "Can't parse input file " + source.getSystemId() + ".\n" + e;
709 throw new BuildException(msg, e);
710 }
711 Schema schema = schemaUnmarshaller.getSchema();
712 try {
713 generateSource(schema, packageName);
714 } catch (Exception iox) {
715 throw new BuildException(iox);
716 }
717 }
718 }
719
720
721
722
723
724
725
726
727
728
729
730 private void callSetterMethodUsingReflection(final SourceGenerator sgen,
731 final String methodName, final Class parameterType,
732 final Object parameterValue) throws BuildException {
733 try {
734 Method method = sgen.getClass().getMethod(methodName,
735 new Class[] {parameterType});
736 method.invoke(sgen, new Object[] {parameterValue});
737 } catch (NoSuchMethodException e) {
738
739 } catch (IllegalArgumentException e) {
740 throw new BuildException(PROBLEM_SETTING_JDO_DESC, e);
741 } catch (IllegalAccessException e) {
742 throw new BuildException(PROBLEM_SETTING_JDO_DESC, e);
743 } catch (InvocationTargetException e) {
744 throw new BuildException(PROBLEM_SETTING_JDO_DESC, e);
745 }
746 }
747
748
749
750 }