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.tools;
32
33 import java.io.BufferedReader;
34 import java.io.ByteArrayInputStream;
35 import java.io.ByteArrayOutputStream;
36 import java.io.FileNotFoundException;
37 import java.io.IOException;
38 import java.io.InputStream;
39 import java.io.InputStreamReader;
40 import java.io.OutputStreamWriter;
41 import java.io.Reader;
42 import java.io.StringReader;
43 import java.lang.ref.Reference;
44 import java.lang.ref.SoftReference;
45 import java.lang.reflect.InvocationTargetException;
46 import java.net.URL;
47 import java.text.DateFormat;
48 import java.text.Format;
49 import java.text.MessageFormat;
50 import java.text.ParseException;
51 import java.text.SimpleDateFormat;
52 import java.util.ArrayList;
53 import java.util.Calendar;
54 import java.util.Collections;
55 import java.util.Enumeration;
56 import java.util.HashMap;
57 import java.util.List;
58 import java.util.Locale;
59 import java.util.Map;
60 import java.util.ResourceBundle;
61 import java.util.Set;
62 import java.util.concurrent.ConcurrentHashMap;
63 import java.util.concurrent.CopyOnWriteArrayList;
64 import java.util.concurrent.CopyOnWriteArraySet;
65 import java.util.logging.Level;
66 import javax.activation.MimeTypeParseException;
67 import org.apache.commons.io.IOUtils;
68 import org.apache.commons.lang.StringEscapeUtils;
69 import org.apache.commons.lang.StringUtils;
70 import org.apache.velocity.Template;
71 import org.apache.velocity.VelocityContext;
72 import org.apache.velocity.app.VelocityEngine;
73 import org.apache.velocity.exception.ParseErrorException;
74 import org.apache.velocity.exception.ResourceNotFoundException;
75 import org.apache.velocity.exception.VelocityException;
76 import org.apache.velocity.runtime.RuntimeConstants;
77 import org.apache.velocity.runtime.RuntimeServices;
78 import org.apache.velocity.runtime.log.LogChute;
79 import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
80 import org.apache.velocity.runtime.resource.loader.URLResourceLoader;
81 import org.jomc.model.Argument;
82 import org.jomc.model.Dependency;
83 import org.jomc.model.Implementation;
84 import org.jomc.model.InheritanceModel;
85 import org.jomc.model.JavaIdentifier;
86 import org.jomc.model.JavaTypeName;
87 import org.jomc.model.Message;
88 import org.jomc.model.ModelObject;
89 import org.jomc.model.ModelObjectException;
90 import org.jomc.model.Modules;
91 import org.jomc.model.Multiplicity;
92 import org.jomc.model.Property;
93 import org.jomc.model.Specification;
94 import org.jomc.model.SpecificationReference;
95 import org.jomc.model.Text;
96 import org.jomc.model.Texts;
97 import org.jomc.model.modlet.ModelHelper;
98 import org.jomc.modlet.Model;
99
100
101
102
103
104
105
106 public class JomcTool
107 {
108
109
110 public abstract static class Listener
111 {
112
113
114 public Listener()
115 {
116 super();
117 }
118
119
120
121
122
123
124
125
126
127
128 public void onLog( final Level level, final String message, final Throwable throwable )
129 {
130 if ( level == null )
131 {
132 throw new NullPointerException( "level" );
133 }
134 }
135
136 }
137
138
139 private static final byte[] NO_BYTES =
140 {
141 };
142
143
144 private static final String TEMPLATE_PREFIX =
145 JomcTool.class.getPackage().getName().replace( '.', '/' ) + "/templates/";
146
147
148 private static final String DEFAULT_TEMPLATE_PROFILE = "jomc-java";
149
150
151
152
153
154 private static final String PARENT_TEMPLATE_PROFILE_PROPERTY_NAME = "parent-template-profile";
155
156
157
158
159
160 private static final String TEMPLATE_ENCODING_PROFILE_PROPERTY_NAME = "template-encoding";
161
162
163
164
165
166 private String defaultTemplateEncoding;
167
168
169 private static volatile String defaultTemplateProfile;
170
171
172
173
174
175 private static final Level DEFAULT_LOG_LEVEL = Level.WARNING;
176
177
178 private static volatile Level defaultLogLevel;
179
180
181 private Model model;
182
183
184 private VelocityEngine velocityEngine;
185
186
187
188
189
190 private boolean defaultVelocityEngine;
191
192
193
194
195
196 private URL templateLocation;
197
198
199 private String inputEncoding;
200
201
202 private String outputEncoding;
203
204
205
206
207
208 private Map<String, Object> templateParameters;
209
210
211 private String templateProfile;
212
213
214 private String indentation;
215
216
217 private String lineSeparator;
218
219
220 private List<Listener> listeners;
221
222
223 private Level logLevel;
224
225
226
227
228
229 private Locale locale;
230
231
232 private volatile Reference<Map<String, String>> indentationCache;
233
234
235
236
237
238 private volatile Reference<Map<String, TemplateData>> templateCache;
239
240
241
242
243
244 private volatile Reference<Map<String, java.util.Properties>> templateProfileContextPropertiesCache;
245
246
247
248
249
250 private volatile Reference<Map<String, java.util.Properties>> templateProfilePropertiesCache;
251
252
253 private volatile Reference<Set<String>> javaKeywordsCache;
254
255
256 public JomcTool()
257 {
258 super();
259 }
260
261
262
263
264
265
266
267
268
269 public JomcTool( final JomcTool tool ) throws IOException
270 {
271 this();
272
273 if ( tool == null )
274 {
275 throw new NullPointerException( "tool" );
276 }
277
278 this.indentation = tool.indentation;
279 this.inputEncoding = tool.inputEncoding;
280 this.lineSeparator = tool.lineSeparator;
281 this.listeners = tool.listeners != null ? new CopyOnWriteArrayList<Listener>( tool.listeners ) : null;
282 this.logLevel = tool.logLevel;
283 this.model = tool.model != null ? tool.model.clone() : null;
284 this.outputEncoding = tool.outputEncoding;
285 this.defaultTemplateEncoding = tool.defaultTemplateEncoding;
286 this.templateProfile = tool.templateProfile;
287 this.velocityEngine = tool.velocityEngine;
288 this.defaultVelocityEngine = tool.defaultVelocityEngine;
289 this.locale = tool.locale;
290 this.templateParameters =
291 tool.templateParameters != null
292 ? Collections.synchronizedMap( new HashMap<String, Object>( tool.templateParameters ) )
293 : null;
294
295 this.templateLocation =
296 tool.templateLocation != null ? new URL( tool.templateLocation.toExternalForm() ) : null;
297
298 }
299
300
301
302
303
304
305
306
307
308
309
310 public List<Listener> getListeners()
311 {
312 if ( this.listeners == null )
313 {
314 this.listeners = new CopyOnWriteArrayList<Listener>();
315 }
316
317 return this.listeners;
318 }
319
320
321
322
323
324
325
326
327
328
329
330
331 public static Level getDefaultLogLevel()
332 {
333 if ( defaultLogLevel == null )
334 {
335 defaultLogLevel = Level.parse( System.getProperty( "org.jomc.tools.JomcTool.defaultLogLevel",
336 DEFAULT_LOG_LEVEL.getName() ) );
337
338 }
339
340 return defaultLogLevel;
341 }
342
343
344
345
346
347
348
349
350 public static void setDefaultLogLevel( final Level value )
351 {
352 defaultLogLevel = value;
353 }
354
355
356
357
358
359
360
361
362
363
364 public final Level getLogLevel()
365 {
366 if ( this.logLevel == null )
367 {
368 this.logLevel = getDefaultLogLevel();
369
370 if ( this.isLoggable( Level.CONFIG ) )
371 {
372 this.log( Level.CONFIG, getMessage( "defaultLogLevelInfo", this.logLevel.getLocalizedName() ), null );
373 }
374 }
375
376 return this.logLevel;
377 }
378
379
380
381
382
383
384
385
386
387 public final void setLogLevel( final Level value )
388 {
389 this.logLevel = value;
390 }
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406 public boolean isLoggable( final Level level )
407 {
408 if ( level == null )
409 {
410 throw new NullPointerException( "level" );
411 }
412
413 return level.intValue() >= this.getLogLevel().intValue();
414 }
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433 @Deprecated
434 public String getJavaPackageName( final Specification specification ) throws ModelObjectException
435 {
436 if ( specification == null )
437 {
438 throw new NullPointerException( "specification" );
439 }
440
441 final JavaTypeName javaTypeName = specification.getJavaTypeName();
442 return javaTypeName != null ? javaTypeName.getPackageName() : null;
443 }
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464 @Deprecated
465 public String getJavaTypeName( final Specification specification, final boolean qualified )
466 throws ModelObjectException
467 {
468 if ( specification == null )
469 {
470 throw new NullPointerException( "specification" );
471 }
472
473 final JavaTypeName javaTypeName = specification.getJavaTypeName();
474 return javaTypeName != null ? javaTypeName.getName( qualified ) : null;
475 }
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494 @Deprecated
495 public String getJavaClasspathLocation( final Specification specification ) throws ModelObjectException
496 {
497 if ( specification == null )
498 {
499 throw new NullPointerException( "specification" );
500 }
501
502 final JavaTypeName javaTypeName = specification.getJavaTypeName();
503 return javaTypeName != null ? javaTypeName.getQualifiedName().replace( '.', '/' ) : null;
504 }
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524 @Deprecated
525 public String getJavaPackageName( final SpecificationReference reference ) throws ModelObjectException
526 {
527 if ( reference == null )
528 {
529 throw new NullPointerException( "reference" );
530 }
531
532 Specification s = null;
533 String javaPackageName = null;
534
535 if ( this.getModules() != null
536 && ( s = this.getModules().getSpecification( reference.getIdentifier() ) ) != null )
537 {
538 final JavaTypeName javaTypeName = s.getJavaTypeName();
539 javaPackageName = javaTypeName != null ? javaTypeName.getPackageName() : null;
540 }
541 else if ( this.isLoggable( Level.WARNING ) )
542 {
543 this.log( Level.WARNING, getMessage( "specificationNotFound", reference.getIdentifier() ), null );
544 }
545
546 return javaPackageName;
547 }
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569 @Deprecated
570 public String getJavaTypeName( final SpecificationReference reference, final boolean qualified )
571 throws ModelObjectException
572 {
573 if ( reference == null )
574 {
575 throw new NullPointerException( "reference" );
576 }
577
578 Specification s = null;
579 String typeName = null;
580
581 if ( this.getModules() != null
582 && ( s = this.getModules().getSpecification( reference.getIdentifier() ) ) != null )
583 {
584 final JavaTypeName javaTypeName = s.getJavaTypeName();
585 typeName = javaTypeName != null ? javaTypeName.getName( qualified ) : null;
586 }
587 else if ( this.isLoggable( Level.WARNING ) )
588 {
589 this.log( Level.WARNING, getMessage( "specificationNotFound", reference.getIdentifier() ), null );
590 }
591
592 return typeName;
593 }
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612 @Deprecated
613 public String getJavaPackageName( final Implementation implementation ) throws ModelObjectException
614 {
615 if ( implementation == null )
616 {
617 throw new NullPointerException( "implementation" );
618 }
619
620 final JavaTypeName javaTypeName = implementation.getJavaTypeName();
621 return javaTypeName != null ? javaTypeName.getPackageName() : null;
622 }
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643 @Deprecated
644 public String getJavaTypeName( final Implementation implementation, final boolean qualified )
645 throws ModelObjectException
646 {
647 if ( implementation == null )
648 {
649 throw new NullPointerException( "implementation" );
650 }
651
652 final JavaTypeName javaTypeName = implementation.getJavaTypeName();
653 return javaTypeName != null ? javaTypeName.getName( qualified ) : null;
654 }
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673 @Deprecated
674 public String getJavaClasspathLocation( final Implementation implementation ) throws ModelObjectException
675 {
676 if ( implementation == null )
677 {
678 throw new NullPointerException( "implementation" );
679 }
680
681 final JavaTypeName javaTypeName = implementation.getJavaTypeName();
682 return javaTypeName != null ? javaTypeName.getQualifiedName().replace( '.', '/' ) : null;
683 }
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700 @Deprecated
701 public List<String> getJavaInterfaceNames( final Implementation implementation, final boolean qualified )
702 throws ModelObjectException
703 {
704 if ( implementation == null )
705 {
706 throw new NullPointerException( "implementation" );
707 }
708
709 return this.getImplementedJavaTypeNames( implementation, qualified );
710 }
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731 @Deprecated
732 public List<String> getImplementedJavaTypeNames( final Implementation implementation, final boolean qualified )
733 throws ModelObjectException
734 {
735 if ( implementation == null )
736 {
737 throw new NullPointerException( "implementation" );
738 }
739
740 List<String> col = null;
741
742 if ( this.getModules() != null )
743 {
744 final List<JavaTypeName> javaTypeNames =
745 this.getModules().getImplementedJavaTypeNames( implementation.getIdentifier() );
746
747 if ( javaTypeNames != null )
748 {
749 col = new ArrayList<String>( javaTypeNames.size() );
750
751 for ( int i = 0, s0 = javaTypeNames.size(); i < s0; i++ )
752 {
753 if ( !col.contains( javaTypeNames.get( i ).getName( qualified ) ) )
754 {
755 col.add( javaTypeNames.get( i ).getName( qualified ) );
756 }
757 }
758 }
759 }
760 else if ( this.isLoggable( Level.WARNING ) )
761 {
762 this.log( Level.WARNING, getMessage( "modulesNotFound", this.getModel().getIdentifier() ), null );
763 }
764
765 return Collections.unmodifiableList( col != null ? col : Collections.<String>emptyList() );
766 }
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785 @Deprecated
786 public String getJavaTypeName( final Argument argument ) throws ModelObjectException
787 {
788 if ( argument == null )
789 {
790 throw new NullPointerException( "argument" );
791 }
792
793 final JavaTypeName javaTypeName = argument.getJavaTypeName();
794 return javaTypeName != null ? javaTypeName.getName( true ) : null;
795 }
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814 @Deprecated
815 public String getJavaMethodParameterName( final Argument argument ) throws ModelObjectException
816 {
817 if ( argument == null )
818 {
819 throw new NullPointerException( "argument" );
820 }
821
822 return this.getJavaMethodParameterName( argument.getName() );
823 }
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845 @Deprecated
846 public String getJavaTypeName( final Property property, final boolean boxify ) throws ModelObjectException
847 {
848 if ( property == null )
849 {
850 throw new NullPointerException( "property" );
851 }
852
853 JavaTypeName javaTypeName = property.getJavaTypeName();
854
855 if ( javaTypeName != null )
856 {
857 if ( boxify && javaTypeName.isPrimitive() )
858 {
859 javaTypeName = javaTypeName.getBoxedName();
860 }
861
862 return javaTypeName.getName( true );
863 }
864
865 return null;
866 }
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885 @Deprecated
886 public boolean isJavaPrimitiveType( final Property property ) throws ModelObjectException
887 {
888 if ( property == null )
889 {
890 throw new NullPointerException( "property" );
891 }
892
893 final JavaTypeName javaTypeName = property.getJavaTypeName();
894 return javaTypeName != null && javaTypeName.isPrimitive();
895 }
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912 @Deprecated
913 public String getJavaGetterMethodName( final Property property ) throws ModelObjectException
914 {
915 if ( property == null )
916 {
917 throw new NullPointerException( "property" );
918 }
919
920 String prefix = "get";
921
922 final String javaTypeName = this.getJavaTypeName( property, true );
923 if ( Boolean.class.getName().equals( javaTypeName ) )
924 {
925 prefix = "is";
926 }
927
928 return prefix + this.getJavaIdentifier( property.getName(), true );
929 }
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948 @Deprecated
949 public String getJavaSetterMethodName( final Property property ) throws ModelObjectException
950 {
951 if ( property == null )
952 {
953 throw new NullPointerException( "property" );
954 }
955
956 return "set" + this.getJavaIdentifier( property.getName(), true );
957 }
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976 @Deprecated
977 public String getJavaMethodParameterName( final Property property ) throws ModelObjectException
978 {
979 if ( property == null )
980 {
981 throw new NullPointerException( "property" );
982 }
983
984 return this.getJavaMethodParameterName( property.getName() );
985 }
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004 @Deprecated
1005 public String getJavaFieldName( final Property property ) throws ModelObjectException
1006 {
1007 if ( property == null )
1008 {
1009 throw new NullPointerException( "property" );
1010 }
1011
1012 return this.getJavaFieldName( property.getName() );
1013 }
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032 @Deprecated
1033 public String getJavaTypeName( final Dependency dependency ) throws ModelObjectException
1034 {
1035 if ( dependency == null )
1036 {
1037 throw new NullPointerException( "dependency" );
1038 }
1039
1040 Specification s = null;
1041 StringBuilder typeName = null;
1042 String javaTypeName = null;
1043
1044 try
1045 {
1046 if ( this.getModules() != null
1047 && ( s = this.getModules().getSpecification( dependency.getIdentifier() ) ) != null )
1048 {
1049 if ( s.getClazz() != null )
1050 {
1051 typeName = new StringBuilder( s.getClazz().length() );
1052 typeName.append( this.getJavaTypeName( s, true ) );
1053
1054 if ( s.getMultiplicity() == Multiplicity.MANY && dependency.getImplementationName() == null )
1055 {
1056 typeName.append( "[]" );
1057 }
1058
1059 javaTypeName = JavaTypeName.parse( typeName.toString() ).getName( true );
1060 }
1061 }
1062 else if ( this.isLoggable( Level.WARNING ) )
1063 {
1064 this.log( Level.WARNING, getMessage( "specificationNotFound", dependency.getIdentifier() ), null );
1065 }
1066
1067 return javaTypeName;
1068 }
1069 catch ( final ParseException e )
1070 {
1071 throw new ModelObjectException( getMessage( "dependencyJavaTypeNameParseException", typeName,
1072 getMessage( e ) ), e );
1073
1074 }
1075 }
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092 @Deprecated
1093 public String getJavaGetterMethodName( final Dependency dependency ) throws ModelObjectException
1094 {
1095 if ( dependency == null )
1096 {
1097 throw new NullPointerException( "dependency" );
1098 }
1099
1100 return "get" + this.getJavaIdentifier( dependency.getName(), true );
1101 }
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120 @Deprecated
1121 public String getJavaSetterMethodName( final Dependency dependency ) throws ModelObjectException
1122 {
1123 if ( dependency == null )
1124 {
1125 throw new NullPointerException( "dependency" );
1126 }
1127
1128 return "set" + this.getJavaIdentifier( dependency.getName(), true );
1129 }
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148 @Deprecated
1149 public String getJavaMethodParameterName( final Dependency dependency ) throws ModelObjectException
1150 {
1151 if ( dependency == null )
1152 {
1153 throw new NullPointerException( "dependency" );
1154 }
1155
1156 return this.getJavaMethodParameterName( dependency.getName() );
1157 }
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176 @Deprecated
1177 public String getJavaFieldName( final Dependency dependency ) throws ModelObjectException
1178 {
1179 if ( dependency == null )
1180 {
1181 throw new NullPointerException( "dependency" );
1182 }
1183
1184 return this.getJavaFieldName( dependency.getName() );
1185 }
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202 @Deprecated
1203 public String getJavaGetterMethodName( final Message message ) throws ModelObjectException
1204 {
1205 if ( message == null )
1206 {
1207 throw new NullPointerException( "message" );
1208 }
1209
1210 return "get" + this.getJavaIdentifier( message.getName(), true );
1211 }
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230 @Deprecated
1231 public String getJavaSetterMethodName( final Message message ) throws ModelObjectException
1232 {
1233 if ( message == null )
1234 {
1235 throw new NullPointerException( "message" );
1236 }
1237
1238 return "set" + this.getJavaIdentifier( message.getName(), true );
1239 }
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258 @Deprecated
1259 public String getJavaMethodParameterName( final Message message ) throws ModelObjectException
1260 {
1261 if ( message == null )
1262 {
1263 throw new NullPointerException( "message" );
1264 }
1265
1266 return this.getJavaMethodParameterName( message.getName() );
1267 }
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286 @Deprecated
1287 public String getJavaFieldName( final Message message ) throws ModelObjectException
1288 {
1289 if ( message == null )
1290 {
1291 throw new NullPointerException( "message" );
1292 }
1293
1294 return this.getJavaFieldName( message.getName() );
1295 }
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310 @Deprecated
1311 public String getJavaModifierName( final Implementation implementation, final Dependency dependency )
1312 {
1313 if ( implementation == null )
1314 {
1315 throw new NullPointerException( "implementation" );
1316 }
1317 if ( dependency == null )
1318 {
1319 throw new NullPointerException( "dependency" );
1320 }
1321
1322 String modifierName = "private";
1323
1324 if ( this.getModules() != null )
1325 {
1326 modifierName =
1327 this.getModules().getDependencyJavaModifierName( implementation.getIdentifier(), dependency.getName() );
1328
1329 if ( modifierName == null )
1330 {
1331 modifierName = "private";
1332 }
1333 }
1334
1335 return modifierName;
1336 }
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351 @Deprecated
1352 public String getJavaModifierName( final Implementation implementation, final Message message )
1353 {
1354 if ( implementation == null )
1355 {
1356 throw new NullPointerException( "implementation" );
1357 }
1358 if ( message == null )
1359 {
1360 throw new NullPointerException( "message" );
1361 }
1362
1363 String modifierName = "private";
1364
1365 if ( this.getModules() != null )
1366 {
1367 modifierName =
1368 this.getModules().getMessageJavaModifierName( implementation.getIdentifier(), message.getName() );
1369
1370 if ( modifierName == null )
1371 {
1372 modifierName = "private";
1373 }
1374 }
1375
1376 return modifierName;
1377 }
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392 @Deprecated
1393 public String getJavaModifierName( final Implementation implementation, final Property property )
1394 {
1395 if ( implementation == null )
1396 {
1397 throw new NullPointerException( "implementation" );
1398 }
1399 if ( property == null )
1400 {
1401 throw new NullPointerException( "property" );
1402 }
1403
1404 String modifierName = "private";
1405
1406 if ( this.getModules() != null )
1407 {
1408 modifierName =
1409 this.getModules().getPropertyJavaModifierName( implementation.getIdentifier(), property.getName() );
1410
1411 if ( modifierName == null )
1412 {
1413 modifierName = "private";
1414 }
1415 }
1416
1417 return modifierName;
1418 }
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436 @Deprecated
1437 public String getJavadocComment( final Text text, final int indentationLevel, final String linePrefix )
1438 throws ModelObjectException
1439 {
1440 if ( text == null )
1441 {
1442 throw new NullPointerException( "text" );
1443 }
1444 if ( linePrefix == null )
1445 {
1446 throw new NullPointerException( "linePrefix" );
1447 }
1448 if ( indentationLevel < 0 )
1449 {
1450 throw new IllegalArgumentException( Integer.toString( indentationLevel ) );
1451 }
1452
1453 BufferedReader reader = null;
1454 boolean suppressExceptionOnClose = true;
1455
1456 try
1457 {
1458 String javadoc = "";
1459
1460 if ( text.getValue() != null )
1461 {
1462 final String indent = this.getIndentation( indentationLevel );
1463 reader = new BufferedReader( new StringReader( text.getValue() ) );
1464 final StringBuilder builder = new StringBuilder( text.getValue().length() );
1465
1466 String line;
1467 while ( ( line = reader.readLine() ) != null )
1468 {
1469 builder.append( this.getLineSeparator() ).append( indent ).append( linePrefix ).
1470 append( line.replaceAll( "\\/\\*\\*", "/*" ).replaceAll( "\\*/", "/" ) );
1471
1472 }
1473
1474 if ( builder.length() > 0 )
1475 {
1476 javadoc =
1477 builder.substring( this.getLineSeparator().length() + indent.length() + linePrefix.length() );
1478
1479 if ( !text.getMimeType().match( "text/html" ) )
1480 {
1481 javadoc = StringEscapeUtils.escapeHtml( javadoc );
1482 }
1483 }
1484 }
1485
1486 suppressExceptionOnClose = false;
1487 return javadoc;
1488 }
1489 catch ( final MimeTypeParseException e )
1490 {
1491 throw new AssertionError( e );
1492 }
1493 catch ( final IOException e )
1494 {
1495 throw new AssertionError( e );
1496 }
1497 finally
1498 {
1499 try
1500 {
1501 if ( reader != null )
1502 {
1503 reader.close();
1504 }
1505 }
1506 catch ( final IOException e )
1507 {
1508 if ( suppressExceptionOnClose )
1509 {
1510 this.log( Level.SEVERE, getMessage( e ), e );
1511 }
1512 else
1513 {
1514 throw new AssertionError( e );
1515 }
1516 }
1517 }
1518 }
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541 @Deprecated
1542 public String getJavadocComment( final Texts texts, final int indentationLevel, final String linePrefix )
1543 throws ModelObjectException
1544 {
1545 if ( texts == null )
1546 {
1547 throw new NullPointerException( "texts" );
1548 }
1549 if ( linePrefix == null )
1550 {
1551 throw new NullPointerException( "linePrefix" );
1552 }
1553 if ( indentationLevel < 0 )
1554 {
1555 throw new IllegalArgumentException( Integer.toString( indentationLevel ) );
1556 }
1557
1558 return this.getJavadocComment( texts.getText( this.getLocale().getLanguage() ), indentationLevel, linePrefix );
1559 }
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570 public String getJavaString( final String str )
1571 {
1572 return StringEscapeUtils.escapeJava( str );
1573 }
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589 @Deprecated
1590 public String getJavaClasspathLocation( final String str, final boolean absolute )
1591 {
1592 String classpathLocation = null;
1593
1594 if ( str != null )
1595 {
1596 classpathLocation = str.replace( '.', '/' );
1597
1598 if ( absolute )
1599 {
1600 classpathLocation = "/" + classpathLocation;
1601 }
1602 }
1603
1604 return classpathLocation;
1605 }
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621 @Deprecated
1622 public String getJavaIdentifier( final String str, final boolean capitalize )
1623 {
1624 String identifier = null;
1625
1626 if ( str != null )
1627 {
1628 final int len = str.length();
1629 final StringBuilder builder = new StringBuilder( len );
1630 boolean uc = capitalize;
1631
1632 for ( int i = 0; i < len; i++ )
1633 {
1634 final char c = str.charAt( i );
1635 final String charString = Character.toString( c );
1636
1637 if ( builder.length() > 0 )
1638 {
1639 if ( Character.isJavaIdentifierPart( c ) )
1640 {
1641 builder.append( uc ? charString.toUpperCase( this.getLocale() ) : charString );
1642 uc = false;
1643 }
1644 else
1645 {
1646 uc = true;
1647 }
1648 }
1649 else
1650 {
1651 if ( Character.isJavaIdentifierStart( c ) )
1652 {
1653 builder.append( uc ? charString.toUpperCase( this.getLocale() )
1654 : charString.toLowerCase( this.getLocale() ) );
1655
1656 uc = false;
1657 }
1658 else
1659 {
1660 uc = capitalize;
1661 }
1662 }
1663 }
1664
1665 identifier = builder.toString();
1666
1667 if ( identifier.length() <= 0 && this.isLoggable( Level.WARNING ) )
1668 {
1669 this.log( Level.WARNING, getMessage( "invalidJavaIdentifier", str ), null );
1670 }
1671 }
1672
1673 return identifier;
1674 }
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688 @Deprecated
1689 public String getJavaMethodParameterName( final String str )
1690 {
1691 String methodParameterName = null;
1692
1693 if ( str != null )
1694 {
1695 final int len = str.length();
1696 final StringBuilder builder = new StringBuilder( len );
1697 boolean uc = false;
1698
1699 for ( int i = 0; i < len; i++ )
1700 {
1701 final char c = str.charAt( i );
1702 final String charString = Character.toString( c );
1703
1704 if ( builder.length() > 0 )
1705 {
1706 if ( Character.isJavaIdentifierPart( c ) )
1707 {
1708 builder.append( uc ? charString.toUpperCase( this.getLocale() ) : charString );
1709 uc = false;
1710 }
1711 else
1712 {
1713 uc = true;
1714 }
1715 }
1716 else if ( Character.isJavaIdentifierStart( c ) )
1717 {
1718 builder.append( charString.toLowerCase( this.getLocale() ) );
1719 }
1720 }
1721
1722 methodParameterName = builder.toString();
1723
1724 if ( methodParameterName.length() <= 0 && this.isLoggable( Level.WARNING ) )
1725 {
1726 this.log( Level.WARNING, getMessage( "invalidJavaMethodParameterName", str ), null );
1727 }
1728
1729 if ( this.getJavaKeywords().contains( methodParameterName ) )
1730 {
1731 methodParameterName = "_" + methodParameterName;
1732 }
1733 }
1734
1735 return methodParameterName;
1736 }
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750 @Deprecated
1751 public String getJavaFieldName( final String str )
1752 {
1753 String fieldName = null;
1754
1755 if ( str != null )
1756 {
1757 final int len = str.length();
1758 final StringBuilder builder = new StringBuilder( len );
1759 boolean uc = false;
1760
1761 for ( int i = 0; i < len; i++ )
1762 {
1763 final char c = str.charAt( i );
1764 final String charString = Character.toString( c );
1765
1766 if ( builder.length() > 0 )
1767 {
1768 if ( Character.isJavaIdentifierPart( c ) )
1769 {
1770 builder.append( uc ? charString.toUpperCase( this.getLocale() ) : charString );
1771 uc = false;
1772 }
1773 else
1774 {
1775 uc = true;
1776 }
1777 }
1778 else if ( Character.isJavaIdentifierStart( c ) )
1779 {
1780 builder.append( charString.toLowerCase( this.getLocale() ) );
1781 }
1782 }
1783
1784 fieldName = builder.toString();
1785
1786 if ( fieldName.length() <= 0 && this.isLoggable( Level.WARNING ) )
1787 {
1788 this.log( Level.WARNING, getMessage( "invalidJavaFieldName", str ), null );
1789 }
1790
1791 if ( this.getJavaKeywords().contains( fieldName ) )
1792 {
1793 fieldName = "_" + fieldName;
1794 }
1795 }
1796
1797 return fieldName;
1798 }
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812 @Deprecated
1813 public String getJavaConstantName( final String str )
1814 {
1815 String name = null;
1816
1817 if ( str != null )
1818 {
1819 final int len = str.length();
1820 final StringBuilder builder = new StringBuilder( len );
1821 boolean separator = false;
1822
1823 for ( int i = 0; i < len; i++ )
1824 {
1825 final char c = str.charAt( i );
1826
1827 if ( builder.length() > 0 ? Character.isJavaIdentifierPart( c ) : Character.isJavaIdentifierStart( c ) )
1828 {
1829 if ( builder.length() > 0 )
1830 {
1831 if ( !separator )
1832 {
1833 final char previous = builder.charAt( builder.length() - 1 );
1834 separator = Character.isLowerCase( previous ) && Character.isUpperCase( c );
1835 }
1836
1837 if ( separator )
1838 {
1839 builder.append( '_' );
1840 }
1841 }
1842
1843 builder.append( c );
1844 separator = false;
1845 }
1846 else
1847 {
1848 separator = true;
1849 }
1850 }
1851
1852 name = builder.toString().toUpperCase( this.getLocale() );
1853
1854 if ( name.length() <= 0 && this.isLoggable( Level.WARNING ) )
1855 {
1856 this.log( Level.WARNING, getMessage( "invalidJavaConstantName", str ), null );
1857 }
1858 }
1859
1860 return name;
1861 }
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877 public JavaIdentifier toJavaConstantName( final String str ) throws ParseException
1878 {
1879 JavaIdentifier constantName = null;
1880
1881 if ( str != null )
1882 {
1883 constantName = JavaIdentifier.normalize( str, JavaIdentifier.NormalizationMode.CONSTANT_NAME_CONVENTION );
1884 }
1885
1886 return constantName;
1887 }
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903 public JavaIdentifier toJavaMethodName( final String str ) throws ParseException
1904 {
1905 JavaIdentifier variableName = null;
1906
1907 if ( str != null )
1908 {
1909 variableName =
1910 JavaIdentifier.normalize( str, JavaIdentifier.NormalizationMode.METHOD_NAME_CONVENTION );
1911
1912 }
1913
1914 return variableName;
1915 }
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931 public JavaIdentifier toJavaVariableName( final String str ) throws ParseException
1932 {
1933 JavaIdentifier variableName = null;
1934
1935 if ( str != null )
1936 {
1937 variableName =
1938 JavaIdentifier.normalize( str, JavaIdentifier.NormalizationMode.VARIABLE_NAME_CONVENTION );
1939
1940 }
1941
1942 return variableName;
1943 }
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963 @Deprecated
1964 public boolean isJavaDefaultPackage( final Specification specification ) throws ModelObjectException
1965 {
1966 if ( specification == null )
1967 {
1968 throw new NullPointerException( "specification" );
1969 }
1970
1971 final JavaTypeName javaTypeName = specification.getJavaTypeName();
1972 return javaTypeName != null && javaTypeName.isUnnamedPackage();
1973 }
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993 @Deprecated
1994 public boolean isJavaDefaultPackage( final Implementation implementation ) throws ModelObjectException
1995 {
1996 if ( implementation == null )
1997 {
1998 throw new NullPointerException( "implementation" );
1999 }
2000
2001 final JavaTypeName javaTypeName = implementation.getJavaTypeName();
2002 return javaTypeName != null && javaTypeName.isUnnamedPackage();
2003 }
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014 public String getHtmlString( final String str )
2015 {
2016 return str != null ? str.replace( "&", "&" ).replace( "<", "<" ).replace( ">", ">" ).
2017 replace( "\"", """ ).replace( "*", "∗" ) : null;
2018
2019 }
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032 public String getXmlString( final String str )
2033 {
2034 return StringEscapeUtils.escapeXml( str );
2035 }
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048 public String getJavaScriptString( final String str )
2049 {
2050 return StringEscapeUtils.escapeJavaScript( str );
2051 }
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064 public String getSqlString( final String str )
2065 {
2066 return StringEscapeUtils.escapeSql( str );
2067 }
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080 public String getCsvString( final String str )
2081 {
2082 return StringEscapeUtils.escapeCsv( str );
2083 }
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096 public String getBooleanString( final Boolean b )
2097 {
2098 final MessageFormat messageFormat = new MessageFormat( ResourceBundle.getBundle(
2099 JomcTool.class.getName().replace( '.', '/' ), this.getLocale() ).
2100 getString( b ? "booleanStringTrue" : "booleanStringFalse" ), this.getLocale() );
2101
2102 return messageFormat.format( null );
2103 }
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114 public String getDisplayLanguage( final String language )
2115 {
2116 if ( language == null )
2117 {
2118 throw new NullPointerException( "language" );
2119 }
2120
2121 final Locale l = new Locale( language );
2122 return l.getDisplayLanguage( l );
2123 }
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136 public String getShortDate( final Calendar calendar )
2137 {
2138 if ( calendar == null )
2139 {
2140 throw new NullPointerException( "calendar" );
2141 }
2142
2143 return DateFormat.getDateInstance( DateFormat.SHORT, this.getLocale() ).format( calendar.getTime() );
2144 }
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159 public String getMediumDate( final Calendar calendar )
2160 {
2161 if ( calendar == null )
2162 {
2163 throw new NullPointerException( "calendar" );
2164 }
2165
2166 return DateFormat.getDateInstance( DateFormat.MEDIUM, this.getLocale() ).format( calendar.getTime() );
2167 }
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180 public String getLongDate( final Calendar calendar )
2181 {
2182 if ( calendar == null )
2183 {
2184 throw new NullPointerException( "calendar" );
2185 }
2186
2187 return DateFormat.getDateInstance( DateFormat.LONG, this.getLocale() ).format( calendar.getTime() );
2188 }
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203 public String getIsoDate( final Calendar calendar )
2204 {
2205 if ( calendar == null )
2206 {
2207 throw new NullPointerException( "calendar" );
2208 }
2209
2210 return new SimpleDateFormat( "yyyy-DDD", this.getLocale() ).format( calendar.getTime() );
2211 }
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224 public String getShortTime( final Calendar calendar )
2225 {
2226 if ( calendar == null )
2227 {
2228 throw new NullPointerException( "calendar" );
2229 }
2230
2231 return DateFormat.getTimeInstance( DateFormat.SHORT, this.getLocale() ).format( calendar.getTime() );
2232 }
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247 public String getMediumTime( final Calendar calendar )
2248 {
2249 if ( calendar == null )
2250 {
2251 throw new NullPointerException( "calendar" );
2252 }
2253
2254 return DateFormat.getTimeInstance( DateFormat.MEDIUM, this.getLocale() ).format( calendar.getTime() );
2255 }
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268 public String getLongTime( final Calendar calendar )
2269 {
2270 if ( calendar == null )
2271 {
2272 throw new NullPointerException( "calendar" );
2273 }
2274
2275 return DateFormat.getTimeInstance( DateFormat.LONG, this.getLocale() ).format( calendar.getTime() );
2276 }
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291 public String getIsoTime( final Calendar calendar )
2292 {
2293 if ( calendar == null )
2294 {
2295 throw new NullPointerException( "calendar" );
2296 }
2297
2298 return new SimpleDateFormat( "HH:mm", this.getLocale() ).format( calendar.getTime() );
2299 }
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312 public String getShortDateTime( final Calendar calendar )
2313 {
2314 if ( calendar == null )
2315 {
2316 throw new NullPointerException( "calendar" );
2317 }
2318
2319 return DateFormat.getDateTimeInstance( DateFormat.SHORT, DateFormat.SHORT, this.getLocale() ).
2320 format( calendar.getTime() );
2321
2322 }
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337 public String getMediumDateTime( final Calendar calendar )
2338 {
2339 if ( calendar == null )
2340 {
2341 throw new NullPointerException( "calendar" );
2342 }
2343
2344 return DateFormat.getDateTimeInstance( DateFormat.MEDIUM, DateFormat.MEDIUM, this.getLocale() ).
2345 format( calendar.getTime() );
2346
2347 }
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360 public String getLongDateTime( final Calendar calendar )
2361 {
2362 if ( calendar == null )
2363 {
2364 throw new NullPointerException( "calendar" );
2365 }
2366
2367 return DateFormat.getDateTimeInstance( DateFormat.LONG, DateFormat.LONG, this.getLocale() ).
2368 format( calendar.getTime() );
2369
2370 }
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385 public String getIsoDateTime( final Calendar calendar )
2386 {
2387 if ( calendar == null )
2388 {
2389 throw new NullPointerException( "calendar" );
2390 }
2391
2392
2393 return new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ssZ", this.getLocale() ).format( calendar.getTime() );
2394 }
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406 public String getYears( final Calendar start, final Calendar end )
2407 {
2408 if ( start == null )
2409 {
2410 throw new NullPointerException( "start" );
2411 }
2412 if ( end == null )
2413 {
2414 throw new NullPointerException( "end" );
2415 }
2416
2417 final Format yearFormat = new SimpleDateFormat( "yyyy", this.getLocale() );
2418 final int s = start.get( Calendar.YEAR );
2419 final int e = end.get( Calendar.YEAR );
2420 final StringBuilder years = new StringBuilder();
2421
2422 if ( s != e )
2423 {
2424 if ( s < e )
2425 {
2426 years.append( yearFormat.format( start.getTime() ) ).append( " - " ).
2427 append( yearFormat.format( end.getTime() ) );
2428
2429 }
2430 else
2431 {
2432 years.append( yearFormat.format( end.getTime() ) ).append( " - " ).
2433 append( yearFormat.format( start.getTime() ) );
2434
2435 }
2436 }
2437 else
2438 {
2439 years.append( yearFormat.format( start.getTime() ) );
2440 }
2441
2442 return years.toString();
2443 }
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453 public final Model getModel()
2454 {
2455 if ( this.model == null )
2456 {
2457 this.model = new Model();
2458 this.model.setIdentifier( ModelObject.MODEL_PUBLIC_ID );
2459 }
2460
2461 return this.model;
2462 }
2463
2464
2465
2466
2467
2468
2469
2470
2471 public final void setModel( final Model value )
2472 {
2473 this.model = value;
2474 }
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484 public final Modules getModules()
2485 {
2486 return ModelHelper.getModules( this.getModel() );
2487 }
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498 public final VelocityEngine getVelocityEngine() throws IOException
2499 {
2500 if ( this.velocityEngine == null )
2501 {
2502
2503 class JomcLogChute implements LogChute
2504 {
2505
2506 JomcLogChute()
2507 {
2508 super();
2509 }
2510
2511 public void init( final RuntimeServices runtimeServices ) throws Exception
2512 {
2513 }
2514
2515 public void log( final int level, final String message )
2516 {
2517 this.log( level, message, null );
2518 }
2519
2520 public void log( final int level, final String message, final Throwable throwable )
2521 {
2522 JomcTool.this.log( Level.FINEST, message, throwable );
2523 }
2524
2525 public boolean isLevelEnabled( final int level )
2526 {
2527 return isLoggable( Level.FINEST );
2528 }
2529
2530 }
2531
2532 final VelocityEngine engine = new VelocityEngine();
2533 engine.setProperty( RuntimeConstants.RUNTIME_REFERENCES_STRICT, Boolean.TRUE.toString() );
2534 engine.setProperty( RuntimeConstants.VM_ARGUMENTS_STRICT, Boolean.TRUE.toString() );
2535 engine.setProperty( RuntimeConstants.STRICT_MATH, Boolean.TRUE.toString() );
2536 engine.setProperty( RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, new JomcLogChute() );
2537
2538 engine.setProperty( RuntimeConstants.RESOURCE_LOADER, "class" );
2539 engine.setProperty( "class.resource.loader.class", ClasspathResourceLoader.class.getName() );
2540 engine.setProperty( "class.resource.loader.cache", Boolean.TRUE.toString() );
2541
2542 if ( this.getTemplateLocation() != null )
2543 {
2544 engine.setProperty( RuntimeConstants.RESOURCE_LOADER, "class,url" );
2545 engine.setProperty( "url.resource.loader.class", URLResourceLoader.class.getName() );
2546 engine.setProperty( "url.resource.loader.cache", Boolean.TRUE.toString() );
2547 engine.setProperty( "url.resource.loader.root", this.getTemplateLocation().toExternalForm() );
2548 engine.setProperty( "url.resource.loader.timeout", Integer.toString( 60000 ) );
2549 }
2550
2551 this.velocityEngine = engine;
2552 this.defaultVelocityEngine = true;
2553 }
2554
2555 return this.velocityEngine;
2556 }
2557
2558
2559
2560
2561
2562
2563
2564
2565 public final void setVelocityEngine( final VelocityEngine value )
2566 {
2567 this.velocityEngine = value;
2568 this.defaultVelocityEngine = false;
2569 }
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580 public VelocityContext getVelocityContext() throws IOException
2581 {
2582 final Calendar now = Calendar.getInstance();
2583 final VelocityContext ctx =
2584 new VelocityContext( new HashMap<String, Object>( this.getTemplateParameters() ) );
2585
2586 this.mergeTemplateProfileContextProperties( this.getTemplateProfile(), this.getLocale().getLanguage(), ctx );
2587 this.mergeTemplateProfileContextProperties( this.getTemplateProfile(), null, ctx );
2588
2589 final Model clonedModel = this.getModel().clone();
2590 final Modules clonedModules = ModelHelper.getModules( clonedModel );
2591 assert clonedModules != null : "Unexpected missing modules for model '" + clonedModel.getIdentifier() + "'.";
2592
2593 ctx.put( "model", clonedModel );
2594 ctx.put( "modules", clonedModules );
2595 ctx.put( "imodel", new InheritanceModel( clonedModules ) );
2596 ctx.put( "tool", this );
2597 ctx.put( "toolName", this.getClass().getName() );
2598 ctx.put( "toolVersion", getMessage( "projectVersion" ) );
2599 ctx.put( "toolUrl", getMessage( "projectUrl" ) );
2600 ctx.put( "calendar", now.getTime() );
2601
2602
2603 ctx.put( "now",
2604 new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss.SSSZ", this.getLocale() ).format( now.getTime() ) );
2605
2606 ctx.put( "year", new SimpleDateFormat( "yyyy", this.getLocale() ).format( now.getTime() ) );
2607 ctx.put( "month", new SimpleDateFormat( "MM", this.getLocale() ).format( now.getTime() ) );
2608 ctx.put( "day", new SimpleDateFormat( "dd", this.getLocale() ).format( now.getTime() ) );
2609 ctx.put( "hour", new SimpleDateFormat( "HH", this.getLocale() ).format( now.getTime() ) );
2610 ctx.put( "minute", new SimpleDateFormat( "mm", this.getLocale() ).format( now.getTime() ) );
2611 ctx.put( "second", new SimpleDateFormat( "ss", this.getLocale() ).format( now.getTime() ) );
2612 ctx.put( "timezone", new SimpleDateFormat( "Z", this.getLocale() ).format( now.getTime() ) );
2613 ctx.put( "shortDate", this.getShortDate( now ) );
2614 ctx.put( "mediumDate", this.getMediumDate( now ) );
2615 ctx.put( "longDate", this.getLongDate( now ) );
2616 ctx.put( "isoDate", this.getIsoDate( now ) );
2617 ctx.put( "shortTime", this.getShortTime( now ) );
2618 ctx.put( "mediumTime", this.getMediumTime( now ) );
2619 ctx.put( "longTime", this.getLongTime( now ) );
2620 ctx.put( "isoTime", this.getIsoTime( now ) );
2621 ctx.put( "shortDateTime", this.getShortDateTime( now ) );
2622 ctx.put( "mediumDateTime", this.getMediumDateTime( now ) );
2623 ctx.put( "longDateTime", this.getLongDateTime( now ) );
2624 ctx.put( "isoDateTime", this.getIsoDateTime( now ) );
2625
2626 return ctx;
2627 }
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641 public final Map<String, Object> getTemplateParameters()
2642 {
2643 if ( this.templateParameters == null )
2644 {
2645 this.templateParameters = Collections.synchronizedMap( new HashMap<String, Object>() );
2646 }
2647
2648 return this.templateParameters;
2649 }
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660 public final URL getTemplateLocation()
2661 {
2662 return this.templateLocation;
2663 }
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674 public final void setTemplateLocation( final URL value )
2675 {
2676 this.templateLocation = value;
2677 this.templateProfileContextPropertiesCache = null;
2678 this.templateProfilePropertiesCache = null;
2679
2680 if ( this.defaultVelocityEngine )
2681 {
2682 this.setVelocityEngine( null );
2683 }
2684 }
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696 @Deprecated
2697 public final String getTemplateEncoding()
2698 {
2699 return this.getDefaultTemplateEncoding();
2700 }
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712 @Deprecated
2713 public final void setTemplateEncoding( final String value )
2714 {
2715 this.setDefaultTemplateEncoding( value );
2716 }
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727 public final String getDefaultTemplateEncoding()
2728 {
2729 if ( this.defaultTemplateEncoding == null )
2730 {
2731 this.defaultTemplateEncoding = getMessage( "buildSourceEncoding" );
2732
2733 if ( this.isLoggable( Level.CONFIG ) )
2734 {
2735 this.log( Level.CONFIG, getMessage( "defaultTemplateEncoding", this.defaultTemplateEncoding ), null );
2736 }
2737 }
2738
2739 return this.defaultTemplateEncoding;
2740 }
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751 public final void setDefaultTemplateEncoding( final String value )
2752 {
2753 this.defaultTemplateEncoding = value;
2754 this.templateCache = null;
2755 }
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771 public final String getTemplateEncoding( final String tp )
2772 {
2773 if ( tp == null )
2774 {
2775 throw new NullPointerException( "tp" );
2776 }
2777
2778 String te = null;
2779
2780 try
2781 {
2782 te = this.getTemplateProfileProperties( tp ).getProperty( TEMPLATE_ENCODING_PROFILE_PROPERTY_NAME );
2783 }
2784 catch ( final IOException e )
2785 {
2786 if ( this.isLoggable( Level.SEVERE ) )
2787 {
2788 this.log( Level.SEVERE, getMessage( e ), e );
2789 }
2790 }
2791
2792 return te != null ? te : this.getDefaultTemplateEncoding();
2793 }
2794
2795
2796
2797
2798
2799
2800
2801
2802 public final String getInputEncoding()
2803 {
2804 if ( this.inputEncoding == null )
2805 {
2806 this.inputEncoding = new InputStreamReader( new ByteArrayInputStream( NO_BYTES ) ).getEncoding();
2807
2808 if ( this.isLoggable( Level.CONFIG ) )
2809 {
2810 this.log( Level.CONFIG, getMessage( "defaultInputEncoding", this.inputEncoding ), null );
2811 }
2812 }
2813
2814 return this.inputEncoding;
2815 }
2816
2817
2818
2819
2820
2821
2822
2823
2824 public final void setInputEncoding( final String value )
2825 {
2826 this.inputEncoding = value;
2827 }
2828
2829
2830
2831
2832
2833
2834
2835
2836 public final String getOutputEncoding()
2837 {
2838 if ( this.outputEncoding == null )
2839 {
2840 this.outputEncoding = new OutputStreamWriter( new ByteArrayOutputStream() ).getEncoding();
2841
2842 if ( this.isLoggable( Level.CONFIG ) )
2843 {
2844 this.log( Level.CONFIG, getMessage( "defaultOutputEncoding", this.outputEncoding ), null );
2845 }
2846 }
2847
2848 return this.outputEncoding;
2849 }
2850
2851
2852
2853
2854
2855
2856
2857
2858 public final void setOutputEncoding( final String value )
2859 {
2860 this.outputEncoding = value;
2861 }
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875 @Deprecated
2876 public static String getDefaultTemplateProfile()
2877 {
2878 if ( defaultTemplateProfile == null )
2879 {
2880 defaultTemplateProfile = System.getProperty( "org.jomc.tools.JomcTool.defaultTemplateProfile",
2881 DEFAULT_TEMPLATE_PROFILE );
2882
2883 }
2884
2885 return defaultTemplateProfile;
2886 }
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897 @Deprecated
2898 public static void setDefaultTemplateProfile( final String value )
2899 {
2900 defaultTemplateProfile = value;
2901 }
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911 public final String getTemplateProfile()
2912 {
2913 if ( this.templateProfile == null )
2914 {
2915 this.templateProfile = getDefaultTemplateProfile();
2916
2917 if ( this.isLoggable( Level.CONFIG ) )
2918 {
2919 this.log( Level.CONFIG, getMessage( "defaultTemplateProfile", this.templateProfile ), null );
2920 }
2921 }
2922
2923 return this.templateProfile;
2924 }
2925
2926
2927
2928
2929
2930
2931
2932
2933 public final void setTemplateProfile( final String value )
2934 {
2935 this.templateProfile = value;
2936 }
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953 public final String getParentTemplateProfile( final String tp )
2954 {
2955 if ( tp == null )
2956 {
2957 throw new NullPointerException( "tp" );
2958 }
2959
2960 String parentTemplateProfile = null;
2961
2962 try
2963 {
2964 parentTemplateProfile =
2965 this.getTemplateProfileProperties( tp ).getProperty( PARENT_TEMPLATE_PROFILE_PROPERTY_NAME );
2966
2967 }
2968 catch ( final IOException e )
2969 {
2970 if ( this.isLoggable( Level.SEVERE ) )
2971 {
2972 this.log( Level.SEVERE, getMessage( e ), e );
2973 }
2974 }
2975
2976 return parentTemplateProfile != null ? parentTemplateProfile
2977 : tp.equals( this.getDefaultTemplateProfile() ) ? null : this.getDefaultTemplateProfile();
2978
2979 }
2980
2981
2982
2983
2984
2985
2986
2987
2988 public final String getIndentation()
2989 {
2990 if ( this.indentation == null )
2991 {
2992 this.indentation = " ";
2993
2994 if ( this.isLoggable( Level.CONFIG ) )
2995 {
2996 this.log( Level.CONFIG, getMessage( "defaultIndentation",
2997 StringEscapeUtils.escapeJava( this.indentation ) ), null );
2998
2999 }
3000 }
3001
3002 return this.indentation;
3003 }
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016 public final String getIndentation( final int level )
3017 {
3018 if ( level < 0 )
3019 {
3020 throw new IllegalArgumentException( Integer.toString( level ) );
3021 }
3022
3023 Map<String, String> map = this.indentationCache == null ? null : this.indentationCache.get();
3024
3025 if ( map == null )
3026 {
3027 map = new ConcurrentHashMap<String, String>( 8 );
3028 this.indentationCache = new SoftReference<Map<String, String>>( map );
3029 }
3030
3031 final String key = this.getIndentation() + "|" + level;
3032 String idt = map.get( key );
3033
3034 if ( idt == null )
3035 {
3036 final StringBuilder b = new StringBuilder( this.getIndentation().length() * level );
3037
3038 for ( int i = level; i > 0; i-- )
3039 {
3040 b.append( this.getIndentation() );
3041 }
3042
3043 idt = b.toString();
3044 map.put( key, idt );
3045 }
3046
3047 return idt;
3048 }
3049
3050
3051
3052
3053
3054
3055
3056
3057 public final void setIndentation( final String value )
3058 {
3059 this.indentation = value;
3060 }
3061
3062
3063
3064
3065
3066
3067
3068
3069 public final String getLineSeparator()
3070 {
3071 if ( this.lineSeparator == null )
3072 {
3073 this.lineSeparator = System.getProperty( "line.separator", "\n" );
3074
3075 if ( this.isLoggable( Level.CONFIG ) )
3076 {
3077 this.log( Level.CONFIG, getMessage( "defaultLineSeparator",
3078 StringEscapeUtils.escapeJava( this.lineSeparator ) ), null );
3079
3080 }
3081 }
3082
3083 return this.lineSeparator;
3084 }
3085
3086
3087
3088
3089
3090
3091
3092
3093 public final void setLineSeparator( final String value )
3094 {
3095 this.lineSeparator = value;
3096 }
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107 public final Locale getLocale()
3108 {
3109 if ( this.locale == null )
3110 {
3111 this.locale = Locale.ENGLISH;
3112
3113 if ( this.isLoggable( Level.CONFIG ) )
3114 {
3115 this.log( Level.CONFIG, getMessage( "defaultLocale", this.locale ), null );
3116 }
3117 }
3118
3119 return this.locale;
3120 }
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131 public final void setLocale( final Locale value )
3132 {
3133 this.locale = value;
3134 }
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161 public Template getVelocityTemplate( final String templateName ) throws FileNotFoundException, IOException
3162 {
3163 if ( templateName == null )
3164 {
3165 throw new NullPointerException( "templateName" );
3166 }
3167
3168 return this.getVelocityTemplate( this.getTemplateProfile(), templateName );
3169 }
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183 public void log( final Level level, final String message, final Throwable throwable )
3184 {
3185 if ( level == null )
3186 {
3187 throw new NullPointerException( "level" );
3188 }
3189
3190 if ( this.isLoggable( level ) )
3191 {
3192 for ( int i = this.getListeners().size() - 1; i >= 0; i-- )
3193 {
3194 this.getListeners().get( i ).onLog( level, message, throwable );
3195 }
3196 }
3197 }
3198
3199 private Template findVelocityTemplate( final String location, final String encoding ) throws IOException
3200 {
3201 try
3202 {
3203 return this.getVelocityEngine().getTemplate( location, encoding );
3204 }
3205 catch ( final ResourceNotFoundException e )
3206 {
3207 if ( this.isLoggable( Level.FINER ) )
3208 {
3209 this.log( Level.FINER, getMessage( "templateNotFound", location ), null );
3210 }
3211
3212 return null;
3213 }
3214 catch ( final ParseErrorException e )
3215 {
3216 String m = getMessage( e );
3217 m = m == null ? "" : " " + m;
3218
3219
3220 throw (IOException) new IOException( getMessage( "invalidTemplate", location, m ) ).initCause( e );
3221 }
3222 catch ( final VelocityException e )
3223 {
3224 String m = getMessage( e );
3225 m = m == null ? "" : " " + m;
3226
3227
3228 throw (IOException) new IOException( getMessage( "velocityException", location, m ) ).initCause( e );
3229 }
3230 }
3231
3232 private java.util.Properties getTemplateProfileContextProperties( final String profileName, final String language )
3233 throws IOException
3234 {
3235 Map<String, java.util.Properties> map = this.templateProfileContextPropertiesCache == null
3236 ? null : this.templateProfileContextPropertiesCache.get();
3237
3238 if ( map == null )
3239 {
3240 map = new ConcurrentHashMap<String, java.util.Properties>();
3241 this.templateProfileContextPropertiesCache = new SoftReference<Map<String, java.util.Properties>>( map );
3242 }
3243
3244 final String key = profileName + "|" + language;
3245 java.util.Properties profileProperties = map.get( key );
3246 boolean suppressExceptionOnClose = true;
3247
3248 if ( profileProperties == null )
3249 {
3250 InputStream in = null;
3251 URL url = null;
3252 profileProperties = new java.util.Properties();
3253
3254 final String resourceName = TEMPLATE_PREFIX + profileName + ( language == null ? "" : "/" + language )
3255 + "/context.properties";
3256
3257 try
3258 {
3259 url = this.getClass().getResource( "/" + resourceName );
3260
3261 if ( url != null )
3262 {
3263 in = url.openStream();
3264
3265 if ( this.isLoggable( Level.CONFIG ) )
3266 {
3267 this.log( Level.CONFIG, getMessage( "contextPropertiesFound", url.toExternalForm() ), null );
3268 }
3269
3270 profileProperties.load( in );
3271 }
3272 else if ( this.getTemplateLocation() != null )
3273 {
3274 if ( this.isLoggable( Level.CONFIG ) )
3275 {
3276 this.log( Level.CONFIG, getMessage( "contextPropertiesNotFound", resourceName ), null );
3277 }
3278
3279 url = new URL( this.getTemplateLocation(), resourceName );
3280 in = url.openStream();
3281
3282 if ( this.isLoggable( Level.CONFIG ) )
3283 {
3284 this.log( Level.CONFIG, getMessage( "contextPropertiesFound", url.toExternalForm() ), null );
3285 }
3286
3287 profileProperties.load( in );
3288 }
3289 else if ( this.isLoggable( Level.CONFIG ) )
3290 {
3291 this.log( Level.CONFIG, getMessage( "contextPropertiesNotFound", resourceName ), null );
3292 }
3293
3294 suppressExceptionOnClose = false;
3295 }
3296 catch ( final FileNotFoundException e )
3297 {
3298 if ( this.isLoggable( Level.CONFIG ) )
3299 {
3300 this.log( Level.CONFIG, getMessage( "contextPropertiesNotFound", url.toExternalForm() ), null );
3301 }
3302 }
3303 finally
3304 {
3305 map.put( key, profileProperties );
3306
3307 try
3308 {
3309 if ( in != null )
3310 {
3311 in.close();
3312 }
3313 }
3314 catch ( final IOException e )
3315 {
3316 if ( suppressExceptionOnClose )
3317 {
3318 this.log( Level.SEVERE, getMessage( e ), e );
3319 }
3320 else
3321 {
3322 throw e;
3323 }
3324 }
3325 }
3326 }
3327
3328 return profileProperties;
3329 }
3330
3331 private void mergeTemplateProfileContextProperties( final String profileName, final String language,
3332 final VelocityContext velocityContext ) throws IOException
3333 {
3334 if ( profileName != null )
3335 {
3336 final java.util.Properties templateProfileProperties =
3337 this.getTemplateProfileContextProperties( profileName, language );
3338
3339 for ( final Enumeration<?> e = templateProfileProperties.propertyNames(); e.hasMoreElements(); )
3340 {
3341 final String name = e.nextElement().toString();
3342 final String value = templateProfileProperties.getProperty( name );
3343 final String[] values = value.split( "\\|" );
3344
3345 if ( !velocityContext.containsKey( name ) )
3346 {
3347 final String className = values[0];
3348
3349 try
3350 {
3351 if ( values.length > 1 )
3352 {
3353 final Class<?> valueClass = Class.forName( className );
3354 velocityContext.put( name,
3355 valueClass.getConstructor( String.class ).newInstance( values[1] ) );
3356 }
3357 else if ( value.contains( "|" ) )
3358 {
3359 velocityContext.put( name, Class.forName( values[0] ).newInstance() );
3360 }
3361 else
3362 {
3363 velocityContext.put( name, value );
3364 }
3365 }
3366 catch ( final InstantiationException ex )
3367 {
3368
3369 throw (IOException) new IOException( getMessage(
3370 "contextPropertiesException", profileName + ( language != null ? ", " + language : "" ) ) ).
3371 initCause( ex );
3372
3373 }
3374 catch ( final IllegalAccessException ex )
3375 {
3376
3377 throw (IOException) new IOException( getMessage(
3378 "contextPropertiesException", profileName + ( language != null ? ", " + language : "" ) ) ).
3379 initCause( ex );
3380
3381 }
3382 catch ( final InvocationTargetException ex )
3383 {
3384
3385 throw (IOException) new IOException( getMessage(
3386 "contextPropertiesException", profileName + ( language != null ? ", " + language : "" ) ) ).
3387 initCause( ex );
3388
3389 }
3390 catch ( final NoSuchMethodException ex )
3391 {
3392
3393 throw (IOException) new IOException( getMessage(
3394 "contextPropertiesException", profileName + ( language != null ? ", " + language : "" ) ) ).
3395 initCause( ex );
3396
3397 }
3398 catch ( final ClassNotFoundException ex )
3399 {
3400
3401 throw (IOException) new IOException( getMessage(
3402 "contextPropertiesException", profileName + ( language != null ? ", " + language : "" ) ) ).
3403 initCause( ex );
3404
3405 }
3406 }
3407 }
3408
3409 this.mergeTemplateProfileContextProperties( this.getParentTemplateProfile( profileName ), language,
3410 velocityContext );
3411
3412 }
3413 }
3414
3415 private java.util.Properties getTemplateProfileProperties( final String profileName ) throws IOException
3416 {
3417 Map<String, java.util.Properties> map = this.templateProfilePropertiesCache == null
3418 ? null : this.templateProfilePropertiesCache.get();
3419
3420 if ( map == null )
3421 {
3422 map = new ConcurrentHashMap<String, java.util.Properties>();
3423 this.templateProfilePropertiesCache = new SoftReference<Map<String, java.util.Properties>>( map );
3424 }
3425
3426 java.util.Properties profileProperties = map.get( profileName );
3427 boolean suppressExceptionOnClose = true;
3428
3429 if ( profileProperties == null )
3430 {
3431 InputStream in = null;
3432 profileProperties = new java.util.Properties();
3433
3434 final String resourceName = TEMPLATE_PREFIX + profileName + "/profile.properties";
3435 URL url = null;
3436
3437 try
3438 {
3439 url = this.getClass().getResource( "/" + resourceName );
3440
3441 if ( url != null )
3442 {
3443 in = url.openStream();
3444
3445 if ( this.isLoggable( Level.CONFIG ) )
3446 {
3447 this.log( Level.CONFIG, getMessage( "templateProfilePropertiesFound", url.toExternalForm() ),
3448 null );
3449
3450 }
3451
3452 profileProperties.load( in );
3453 }
3454 else if ( this.getTemplateLocation() != null )
3455 {
3456 if ( this.isLoggable( Level.CONFIG ) )
3457 {
3458 this.log( Level.CONFIG, getMessage( "templateProfilePropertiesNotFound", resourceName ), null );
3459 }
3460
3461 url = new URL( this.getTemplateLocation(), resourceName );
3462 in = url.openStream();
3463
3464 if ( this.isLoggable( Level.CONFIG ) )
3465 {
3466 this.log( Level.CONFIG, getMessage( "templateProfilePropertiesFound", url.toExternalForm() ),
3467 null );
3468
3469 }
3470
3471 profileProperties.load( in );
3472 }
3473 else if ( this.isLoggable( Level.CONFIG ) )
3474 {
3475 this.log( Level.CONFIG, getMessage( "templateProfilePropertiesNotFound", resourceName ), null );
3476 }
3477
3478 suppressExceptionOnClose = false;
3479 }
3480 catch ( final FileNotFoundException e )
3481 {
3482 if ( this.isLoggable( Level.CONFIG ) )
3483 {
3484 this.log( Level.CONFIG, getMessage( "templateProfilePropertiesNotFound", url.toExternalForm() ),
3485 null );
3486
3487 }
3488 }
3489 finally
3490 {
3491 map.put( profileName, profileProperties );
3492
3493 try
3494 {
3495 if ( in != null )
3496 {
3497 in.close();
3498 }
3499 }
3500 catch ( final IOException e )
3501 {
3502 if ( suppressExceptionOnClose )
3503 {
3504 this.log( Level.SEVERE, getMessage( e ), e );
3505 }
3506 else
3507 {
3508 throw e;
3509 }
3510 }
3511 }
3512 }
3513
3514 return profileProperties;
3515 }
3516
3517 private Set<String> getJavaKeywords()
3518 {
3519 Reader in = null;
3520 Set<String> set = this.javaKeywordsCache == null ? null : this.javaKeywordsCache.get();
3521
3522 try
3523 {
3524 if ( set == null )
3525 {
3526 in = new InputStreamReader( this.getClass().getResourceAsStream(
3527 "/" + this.getClass().getPackage().getName().replace( ".", "/" ) + "/JavaKeywords.txt" ), "UTF-8" );
3528
3529 set = new CopyOnWriteArraySet<String>( IOUtils.readLines( in ) );
3530
3531 this.javaKeywordsCache = new SoftReference<Set<String>>( set );
3532 }
3533 }
3534 catch ( final IOException e )
3535 {
3536 throw new IllegalStateException( getMessage( e ), e );
3537 }
3538 finally
3539 {
3540 try
3541 {
3542 if ( in != null )
3543 {
3544 in.close();
3545 }
3546 }
3547 catch ( final IOException e )
3548 {
3549 throw new IllegalStateException( getMessage( e ), e );
3550 }
3551 }
3552
3553 return set;
3554 }
3555
3556 private Template getVelocityTemplate( final String tp, final String tn ) throws IOException
3557 {
3558 Template template = null;
3559
3560 if ( tp != null )
3561 {
3562 final String key = this.getLocale() + "|" + this.getTemplateProfile() + "|"
3563 + this.getDefaultTemplateProfile() + "|" + tn;
3564
3565 Map<String, TemplateData> map = this.templateCache == null
3566 ? null : this.templateCache.get();
3567
3568 if ( map == null )
3569 {
3570 map = new ConcurrentHashMap<String, TemplateData>( 32 );
3571 this.templateCache = new SoftReference<Map<String, TemplateData>>( map );
3572 }
3573
3574 TemplateData templateData = map.get( key );
3575
3576 if ( templateData == null )
3577 {
3578 templateData = new TemplateData();
3579
3580 if ( !StringUtils.EMPTY.equals( this.getLocale().getLanguage() ) )
3581 {
3582 templateData.location = TEMPLATE_PREFIX + tp + "/" + this.getLocale().getLanguage() + "/" + tn;
3583 templateData.template =
3584 this.findVelocityTemplate( templateData.location, this.getTemplateEncoding( tp ) );
3585
3586 }
3587
3588 if ( templateData.template == null )
3589 {
3590 templateData.location = TEMPLATE_PREFIX + tp + "/" + tn;
3591 templateData.template =
3592 this.findVelocityTemplate( templateData.location, this.getTemplateEncoding( tp ) );
3593
3594 }
3595
3596 if ( templateData.template == null )
3597 {
3598 template = this.getVelocityTemplate( this.getParentTemplateProfile( tp ), tn );
3599
3600 if ( template == null )
3601 {
3602 map.put( key, new TemplateData() );
3603 throw new FileNotFoundException( getMessage( "noSuchTemplate", tn ) );
3604 }
3605 }
3606 else
3607 {
3608 if ( this.isLoggable( Level.FINER ) )
3609 {
3610 this.log( Level.FINER, getMessage( "templateInfo", tn, templateData.location ), null );
3611 }
3612
3613 template = templateData.template;
3614 map.put( key, templateData );
3615 }
3616 }
3617 else if ( templateData.template == null )
3618 {
3619 throw new FileNotFoundException( getMessage( "noSuchTemplate", tn ) );
3620 }
3621 else
3622 {
3623 if ( this.isLoggable( Level.FINER ) )
3624 {
3625 this.log( Level.FINER, getMessage( "templateInfo", tn, templateData.location ), null );
3626 }
3627
3628 template = templateData.template;
3629 }
3630 }
3631
3632 return template;
3633 }
3634
3635 private static String getMessage( final String key, final Object... arguments )
3636 {
3637 return MessageFormat.format( ResourceBundle.getBundle(
3638 JomcTool.class.getName().replace( '.', '/' ) ).getString( key ), arguments );
3639
3640 }
3641
3642 private static String getMessage( final Throwable t )
3643 {
3644 return t != null ? t.getMessage() != null ? t.getMessage() : getMessage( t.getCause() ) : null;
3645 }
3646
3647
3648 private static class TemplateData
3649 {
3650
3651 private String location;
3652
3653 private Template template;
3654
3655 }
3656
3657 }