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.kuali.rice.testtools.common.PropertiesUtils;
23 import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
24
25 import java.io.File;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.util.Properties;
29
30
31
32
33
34
35
36 public class FreemarkerUtil {
37
38 protected Configuration cfg;
39
40
41
42
43
44
45
46
47
48 public static File ftlWrite(File output, Template template, InputStream inputStream) throws IOException, TemplateException {
49
50 return ftlWrite(output.getName(), output, template, inputStream);
51 }
52
53
54
55
56
57
58
59
60
61
62
63 public static File ftlWrite(String key, File output, Template template, InputStream inputStream) throws IOException, TemplateException {
64 Properties props = PropertiesUtils.loadProperties(inputStream);
65 props.put("baseName", output.getName().substring(0, output.getName().indexOf("ST")));
66 props.put("className", output.getName().substring(0, output.getName().indexOf("ST")));
67 if (output.getName().contains("TmplMthd")) {
68 props.put("className", output.getName().substring(0, output.getName().indexOf("TmplMthd")));
69 }
70
71 if (props.get("test1") == null ) {
72 props.put("test1", "test" + props.get("className") + "Bookmark");
73 props.put("test2", "test" + props.get("className") + "Nav");
74 }
75
76 PropertiesUtils.systemPropertiesOverride(props, key);
77 PropertiesUtils.transformNumberedPropertiesToList(props);
78 File outputFile = writeTemplateToFile(output, template, props);
79
80 return outputFile;
81 }
82
83 protected static Properties loadProperties(InputStream inputStream) throws IOException {
84 Properties props = new Properties();
85
86 if(inputStream != null) {
87 props.load(inputStream);
88 }
89
90 return props;
91 }
92
93
94
95
96
97
98
99
100
101
102 protected static File writeTemplateToFile(File file, Template template, Properties props) throws IOException, TemplateException {
103 String output = FreeMarkerTemplateUtils.processTemplateIntoString(template, props);
104 FileUtils.writeStringToFile(file, output);
105
106 return file;
107 }
108 }