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