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 package org.jomc.ant;
32
33 import java.io.IOException;
34 import java.net.URL;
35 import java.util.ArrayList;
36 import java.util.Iterator;
37 import java.util.LinkedList;
38 import java.util.List;
39 import java.util.Locale;
40 import java.util.Map;
41 import java.util.logging.Level;
42 import org.apache.commons.lang.StringEscapeUtils;
43 import org.apache.commons.lang.StringUtils;
44 import org.apache.tools.ant.BuildException;
45 import org.apache.tools.ant.Project;
46 import org.jomc.ant.types.KeyValueType;
47 import org.jomc.ant.types.LocaleType;
48 import org.jomc.ant.types.PropertiesResourceType;
49 import org.jomc.model.Implementation;
50 import org.jomc.model.Module;
51 import org.jomc.model.Modules;
52 import org.jomc.model.Specification;
53 import org.jomc.model.modlet.ModelHelper;
54 import org.jomc.modlet.Model;
55 import org.jomc.tools.JomcTool;
56
57
58
59
60
61
62
63 public class JomcToolTask extends JomcModelTask
64 {
65
66
67 private String defaultTemplateProfile;
68
69
70 private String inputEncoding;
71
72
73 private String outputEncoding;
74
75
76 private String templateEncoding;
77
78
79 private String templateLocation;
80
81
82 private String templateProfile;
83
84
85 private String indentation;
86
87
88 private String lineSeparator;
89
90
91 private LocaleType locale;
92
93
94 private String specification;
95
96
97 private String implementation;
98
99
100 private String module;
101
102
103 private List<KeyValueType> velocityProperties;
104
105
106 private List<PropertiesResourceType> velocityPropertyResources;
107
108
109 private List<KeyValueType> templateParameters;
110
111
112 private List<PropertiesResourceType> templateParameterResources;
113
114
115 public JomcToolTask()
116 {
117 super();
118 }
119
120
121
122
123
124
125
126
127 public final String getInputEncoding()
128 {
129 return this.inputEncoding;
130 }
131
132
133
134
135
136
137
138
139 public final void setInputEncoding( final String value )
140 {
141 this.inputEncoding = value;
142 }
143
144
145
146
147
148
149
150
151 public final String getOutputEncoding()
152 {
153 return this.outputEncoding;
154 }
155
156
157
158
159
160
161
162
163 public final void setOutputEncoding( final String value )
164 {
165 this.outputEncoding = value;
166 }
167
168
169
170
171
172
173
174
175 public final String getTemplateEncoding()
176 {
177 return this.templateEncoding;
178 }
179
180
181
182
183
184
185
186
187 public final void setTemplateEncoding( final String value )
188 {
189 this.templateEncoding = value;
190 }
191
192
193
194
195
196
197
198
199 public final String getTemplateLocation()
200 {
201 return this.templateLocation;
202 }
203
204
205
206
207
208
209
210
211
212 public final void setTemplateLocation( final String value )
213 {
214 this.templateLocation = value;
215 }
216
217
218
219
220
221
222
223
224 public final String getDefaultTemplateProfile()
225 {
226 return this.defaultTemplateProfile;
227 }
228
229
230
231
232
233
234
235
236 public final void setDefaultTemplateProfile( final String value )
237 {
238 this.defaultTemplateProfile = value;
239 }
240
241
242
243
244
245
246
247
248 public final String getTemplateProfile()
249 {
250 return this.templateProfile;
251 }
252
253
254
255
256
257
258
259
260 public final void setTemplateProfile( final String value )
261 {
262 this.templateProfile = value;
263 }
264
265
266
267
268
269
270
271
272 public final String getIndentation()
273 {
274 return this.indentation;
275 }
276
277
278
279
280
281
282
283
284 public final void setIndentation( final String value )
285 {
286 this.indentation = value;
287 }
288
289
290
291
292
293
294
295
296 public final String getLineSeparator()
297 {
298 return this.lineSeparator;
299 }
300
301
302
303
304
305
306
307
308 public final void setLineSeparator( final String value )
309 {
310 this.lineSeparator = value;
311 }
312
313
314
315
316
317
318
319
320 public final LocaleType getLocale()
321 {
322 return this.locale;
323 }
324
325
326
327
328
329
330
331
332
333
334 public LocaleType createLocale()
335 {
336 if ( this.locale != null )
337 {
338 throw new BuildException( Messages.getMessage( "multipleElements", "locale" ), this.getLocation() );
339 }
340
341 this.locale = new LocaleType();
342 return this.locale;
343 }
344
345
346
347
348
349
350
351
352 public final String getSpecification()
353 {
354 return this.specification;
355 }
356
357
358
359
360
361
362
363
364 public final void setSpecification( final String value )
365 {
366 this.specification = value;
367 }
368
369
370
371
372
373
374
375
376
377
378
379
380 public final Specification getSpecification( final Model model )
381 {
382 if ( model == null )
383 {
384 throw new NullPointerException( "model" );
385 }
386
387 Specification s = null;
388
389 if ( this.getSpecification() != null )
390 {
391 final Modules modules = ModelHelper.getModules( model );
392
393 if ( modules != null )
394 {
395 s = modules.getSpecification( this.getSpecification() );
396 }
397
398 if ( s == null )
399 {
400 this.log( Messages.getMessage( "specificationNotFound", this.getSpecification() ), Project.MSG_WARN );
401 }
402 }
403
404 return s;
405 }
406
407
408
409
410
411
412
413
414 public final String getImplementation()
415 {
416 return this.implementation;
417 }
418
419
420
421
422
423
424
425
426 public final void setImplementation( final String value )
427 {
428 this.implementation = value;
429 }
430
431
432
433
434
435
436
437
438
439
440
441
442 public final Implementation getImplementation( final Model model )
443 {
444 if ( model == null )
445 {
446 throw new NullPointerException( "model" );
447 }
448
449 Implementation i = null;
450
451 if ( this.getImplementation() != null )
452 {
453 final Modules modules = ModelHelper.getModules( model );
454
455 if ( modules != null )
456 {
457 i = modules.getImplementation( this.getImplementation() );
458 }
459
460 if ( i == null )
461 {
462 this.log( Messages.getMessage( "implementationNotFound", this.getImplementation() ), Project.MSG_WARN );
463 }
464 }
465
466 return i;
467 }
468
469
470
471
472
473
474
475
476 public final String getModule()
477 {
478 return this.module;
479 }
480
481
482
483
484
485
486
487
488 public final void setModule( final String value )
489 {
490 this.module = value;
491 }
492
493
494
495
496
497
498
499
500
501
502
503
504 public final Module getModule( final Model model )
505 {
506 if ( model == null )
507 {
508 throw new NullPointerException( "model" );
509 }
510
511 Module m = null;
512
513 if ( this.getModule() != null )
514 {
515 final Modules modules = ModelHelper.getModules( model );
516
517 if ( modules != null )
518 {
519 m = modules.getModule( this.getModule() );
520 }
521
522 if ( m == null )
523 {
524 this.log( Messages.getMessage( "moduleNotFound", this.getModule() ), Project.MSG_WARN );
525 }
526 }
527
528 return m;
529 }
530
531
532
533
534
535
536
537
538
539
540 public boolean isModulesProcessingRequested()
541 {
542 return this.getSpecification() == null && this.getImplementation() == null && this.getModule() == null;
543 }
544
545
546
547
548
549
550
551
552
553
554
555 public final List<KeyValueType> getVelocityProperties()
556 {
557 if ( this.velocityProperties == null )
558 {
559 this.velocityProperties = new LinkedList<KeyValueType>();
560 }
561
562 return this.velocityProperties;
563 }
564
565
566
567
568
569
570
571
572 public KeyValueType createVelocityProperty()
573 {
574 final KeyValueType velocityProperty = new KeyValueType();
575 this.getVelocityProperties().add( velocityProperty );
576 return velocityProperty;
577 }
578
579
580
581
582
583
584
585
586
587
588
589 public final List<PropertiesResourceType> getVelocityPropertyResources()
590 {
591 if ( this.velocityPropertyResources == null )
592 {
593 this.velocityPropertyResources = new LinkedList<PropertiesResourceType>();
594 }
595
596 return this.velocityPropertyResources;
597 }
598
599
600
601
602
603
604
605
606 public PropertiesResourceType createVelocityPropertyResource()
607 {
608 final PropertiesResourceType velocityPropertyResource = new PropertiesResourceType();
609 this.getVelocityPropertyResources().add( velocityPropertyResource );
610 return velocityPropertyResource;
611 }
612
613
614
615
616
617
618
619
620
621
622
623 public final List<KeyValueType> getTemplateParameters()
624 {
625 if ( this.templateParameters == null )
626 {
627 this.templateParameters = new LinkedList<KeyValueType>();
628 }
629
630 return this.templateParameters;
631 }
632
633
634
635
636
637
638
639
640 public KeyValueType createTemplateParameter()
641 {
642 final KeyValueType templateParameter = new KeyValueType();
643 this.getTemplateParameters().add( templateParameter );
644 return templateParameter;
645 }
646
647
648
649
650
651
652
653
654
655
656
657 public final List<PropertiesResourceType> getTemplateParameterResources()
658 {
659 if ( this.templateParameterResources == null )
660 {
661 this.templateParameterResources = new LinkedList<PropertiesResourceType>();
662 }
663
664 return this.templateParameterResources;
665 }
666
667
668
669
670
671
672
673
674 public PropertiesResourceType createTemplateParameterResource()
675 {
676 final PropertiesResourceType templateParameterResource = new PropertiesResourceType();
677 this.getTemplateParameterResources().add( templateParameterResource );
678 return templateParameterResource;
679 }
680
681
682 @Override
683 public void preExecuteTask() throws BuildException
684 {
685 super.preExecuteTask();
686
687 this.assertKeysNotNull( this.getVelocityProperties() );
688 this.assertKeysNotNull( this.getTemplateParameters() );
689 this.assertLocationsNotNull( this.getTemplateParameterResources() );
690 this.assertLocationsNotNull( this.getVelocityPropertyResources() );
691 }
692
693
694 @Override
695 public void postExecuteTask() throws BuildException
696 {
697 JomcTool.setDefaultTemplateProfile( null );
698
699 super.postExecuteTask();
700 }
701
702
703
704
705
706
707
708
709
710 public void configureJomcTool( final JomcTool tool ) throws BuildException
711 {
712 if ( tool == null )
713 {
714 throw new NullPointerException( "tool" );
715 }
716
717 try
718 {
719 tool.setLogLevel( Level.ALL );
720 tool.setIndentation( StringEscapeUtils.unescapeJava( this.getIndentation() ) );
721 tool.setInputEncoding( this.getInputEncoding() );
722 tool.setLineSeparator( StringEscapeUtils.unescapeJava( this.getLineSeparator() ) );
723 tool.setOutputEncoding( this.getOutputEncoding() );
724 tool.setTemplateEncoding( this.getTemplateEncoding() );
725 tool.setDefaultTemplateProfile( this.getDefaultTemplateProfile() );
726 tool.setTemplateProfile( this.getTemplateProfile() );
727 tool.getListeners().add( new JomcTool.Listener()
728 {
729
730 @Override
731 public void onLog( final Level level, final String message, final Throwable throwable )
732 {
733 super.onLog( level, message, throwable );
734
735 if ( level.intValue() >= Level.SEVERE.intValue() )
736 {
737 log( message, throwable, Project.MSG_ERR );
738 }
739 else if ( level.intValue() >= Level.WARNING.intValue() )
740 {
741 log( message, throwable, Project.MSG_WARN );
742 }
743 else if ( level.intValue() >= Level.INFO.intValue() )
744 {
745 log( message, throwable, Project.MSG_INFO );
746 }
747 else
748 {
749 log( message, throwable, Project.MSG_DEBUG );
750 }
751 }
752
753 } );
754
755 for ( int i = 0, s0 = this.getVelocityPropertyResources().size(); i < s0; i++ )
756 {
757 for ( Map.Entry<Object, Object> e :
758 this.getProperties( this.getVelocityPropertyResources().get( i ) ).entrySet() )
759 {
760 if ( e.getValue() != null )
761 {
762 tool.getVelocityEngine().setProperty( e.getKey().toString(), e.getValue() );
763 }
764 else
765 {
766 tool.getVelocityEngine().clearProperty( e.getKey().toString() );
767 }
768 }
769 }
770
771 for ( int i = 0, s0 = this.getVelocityProperties().size(); i < s0; i++ )
772 {
773 final KeyValueType p = this.getVelocityProperties().get( i );
774 final Object object = p.getObject( this.getLocation() );
775
776 if ( object != null )
777 {
778 tool.getVelocityEngine().setProperty( p.getKey(), object );
779 }
780 else
781 {
782 tool.getVelocityEngine().clearProperty( p.getKey() );
783 }
784 }
785
786 for ( Map.Entry<Object, Object> e : System.getProperties().entrySet() )
787 {
788 tool.getTemplateParameters().put( e.getKey().toString(), e.getValue() );
789 }
790
791 for ( final Iterator<Map.Entry<?, ?>> it = this.getProject().getProperties().entrySet().iterator();
792 it.hasNext(); )
793 {
794 final Map.Entry<?, ?> e = it.next();
795 tool.getTemplateParameters().put( e.getKey().toString(), e.getValue() );
796 }
797
798 for ( int i = 0, s0 = this.getTemplateParameterResources().size(); i < s0; i++ )
799 {
800 for ( Map.Entry<Object, Object> e :
801 this.getProperties( this.getTemplateParameterResources().get( i ) ).entrySet() )
802 {
803 if ( e.getValue() != null )
804 {
805 tool.getTemplateParameters().put( e.getKey().toString(), e.getValue() );
806 }
807 else
808 {
809 tool.getTemplateParameters().remove( e.getKey().toString() );
810 }
811 }
812 }
813
814 for ( int i = 0, s0 = this.getTemplateParameters().size(); i < s0; i++ )
815 {
816 final KeyValueType p = this.getTemplateParameters().get( i );
817 final Object object = p.getObject( this.getLocation() );
818
819 if ( object != null )
820 {
821 tool.getTemplateParameters().put( p.getKey(), object );
822 }
823 else
824 {
825 tool.getTemplateParameters().remove( p.getKey() );
826 }
827 }
828
829 if ( this.getTemplateLocation() != null )
830 {
831 final URL url = this.getDirectory( this.getTemplateLocation() );
832 tool.setTemplateLocation( url );
833
834 if ( url == null )
835 {
836 this.log( Messages.getMessage( "templateLocationNotFound", this.getTemplateLocation() ),
837 Project.MSG_WARN );
838
839 }
840 }
841
842 if ( this.getLocale() != null )
843 {
844 tool.setLocale( new Locale( StringUtils.defaultString( this.getLocale().getLanguage() ),
845 StringUtils.defaultString( this.getLocale().getCountry() ),
846 StringUtils.defaultString( this.getLocale().getVariant() ) ) );
847
848 }
849 }
850 catch ( final IOException e )
851 {
852 throw new BuildException( Messages.getMessage( e ), e, this.getLocation() );
853 }
854 }
855
856
857 @Override
858 public JomcToolTask clone()
859 {
860 final JomcToolTask clone = (JomcToolTask) super.clone();
861
862 if ( this.locale != null )
863 {
864 clone.locale = this.locale.clone();
865 }
866
867 if ( this.velocityPropertyResources != null )
868 {
869 clone.velocityPropertyResources =
870 new ArrayList<PropertiesResourceType>( this.velocityPropertyResources.size() );
871
872 for ( PropertiesResourceType e : this.velocityPropertyResources )
873 {
874 clone.velocityPropertyResources.add( e.clone() );
875 }
876 }
877
878 if ( this.velocityProperties != null )
879 {
880 clone.velocityProperties = new ArrayList<KeyValueType>( this.velocityProperties.size() );
881
882 for ( KeyValueType e : this.velocityProperties )
883 {
884 clone.velocityProperties.add( e.clone() );
885 }
886 }
887
888 if ( this.velocityPropertyResources != null )
889 {
890 clone.velocityPropertyResources =
891 new ArrayList<PropertiesResourceType>( this.velocityPropertyResources.size() );
892
893 for ( PropertiesResourceType e : this.velocityPropertyResources )
894 {
895 clone.velocityPropertyResources.add( e.clone() );
896 }
897 }
898
899 if ( this.templateParameters != null )
900 {
901 clone.templateParameters = new ArrayList<KeyValueType>( this.templateParameters.size() );
902
903 for ( KeyValueType e : this.templateParameters )
904 {
905 clone.templateParameters.add( e.clone() );
906 }
907 }
908
909 if ( this.templateParameterResources != null )
910 {
911 clone.templateParameterResources =
912 new ArrayList<PropertiesResourceType>( this.templateParameterResources.size() );
913
914 for ( PropertiesResourceType e : this.templateParameterResources )
915 {
916 clone.templateParameterResources.add( e.clone() );
917 }
918 }
919
920 return clone;
921 }
922
923 }