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