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