1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package edu.samplu.common;
17
18 import freemarker.template.Configuration;
19 import freemarker.template.Template;
20 import freemarker.template.TemplateException;
21 import org.apache.commons.io.FileUtils;
22 import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
23
24 import java.io.File;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.util.Enumeration;
28 import java.util.HashMap;
29 import java.util.Iterator;
30 import java.util.Map;
31 import java.util.Properties;
32
33
34
35
36
37
38
39 public class FreemarkerUtil {
40
41 protected Configuration cfg;
42
43
44
45
46
47
48
49
50
51 public static File ftlWrite(File output, Template template, InputStream inputStream) throws IOException, TemplateException {
52
53 return ftlWrite(output.getName(), output, template, inputStream);
54 }
55
56
57
58
59
60
61
62
63
64
65
66 public static File ftlWrite(String key, File output, Template template, InputStream inputStream) throws IOException, TemplateException {
67 Properties props = loadProperties(inputStream);
68 props.put("baseName", output.getName().substring(0, output.getName().indexOf("ST")));
69 props.put("className", output.getName().substring(0, output.getName().indexOf("ST")));
70 if (output.getName().contains("TmplMthd")) {
71 props.put("className", output.getName().substring(0, output.getName().indexOf("TmplMthd")));
72 }
73
74 systemPropertiesOverride(props, key);
75 transformNumberedTestPropertiesToList(props);
76 File outputFile = writeTemplateToFile(output, template, props);
77
78 return outputFile;
79 }
80
81 protected static Properties loadProperties(InputStream inputStream) throws IOException {
82 Properties props = new Properties();
83
84 if(inputStream != null) {
85 props.load(inputStream);
86 }
87
88 return props;
89 }
90
91 protected static void transformNumberedTestPropertiesToList(Properties props) {
92 Iterator keys = props.keySet().iterator();
93 Map<String, String> keyLists = new HashMap<String, String>();
94 while (keys.hasNext()) {
95 String key = (String)keys.next();
96 if (Character.isDigit(key.charAt(key.length()-1))) {
97 keyLists.put(key, props.getProperty(key));
98 }
99 }
100
101 Iterator listKeys = keyLists.keySet().iterator();
102 while (listKeys.hasNext()) {
103 String key = (String)listKeys.next();
104 props.remove(key);
105 }
106
107 if (keyLists.values().size() == 0) {
108 keyLists.put("test1", (String)props.get("className"));
109 }
110
111 props.put("tests", keyLists.values());
112 }
113
114
115
116
117
118
119
120
121
122
123 protected static File writeTemplateToFile(File file, Template template, Properties props) throws IOException, TemplateException {
124 String output = FreeMarkerTemplateUtils.processTemplateIntoString(template, props);
125 FileUtils.writeStringToFile(file, output);
126
127 return file;
128 }
129
130
131
132
133
134 public static void systemPropertiesOverride(Properties props, String key) {
135 Enumeration<?> names = props.propertyNames();
136 Object nameObject;
137 String name;
138 while (names.hasMoreElements()) {
139 nameObject = names.nextElement();
140 if (nameObject instanceof String) {
141 name = (String)nameObject;
142 props.setProperty(name, System.getProperty(key + "." + name, props.getProperty(name)));
143 }
144 }
145 }
146
147 }