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