1 /**
2 * Copyright 2005-2013 The Kuali Foundation
3 *
4 * Licensed under the Educational Community License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.opensource.org/licenses/ecl2.php
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
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.Properties;
29
30 /**
31 * FreemarkerUtil loads properties from user defined InputStream. systemPropertiesOverride should be used to override properties
32 * using a given key to identify them from JVM args. (i.e. -Dkey.name to override the name property in the key file.)
33 * TODO setup so the loading and overriding of properties is done for the user rather then them having to call it.
34 * @author Kuali Rice Team (rice.collab@kuali.org)
35 */
36 public class FreemarkerUtil {
37
38 protected Configuration cfg;
39
40 /**
41 * Calls ftlWrite that also accepts a key, using the output getName as the key.
42 * @param output
43 * @param template
44 * @return
45 * @throws java.io.IOException
46 * @throws freemarker.template.TemplateException
47 */
48 public File ftlWrite(File output, Template template, InputStream inputStream) throws IOException, TemplateException {
49
50 return ftlWrite(output.getName(), output, template, inputStream);
51 }
52
53 /**
54 * Loads properties from user defined properties file, if not available uses resource file
55 *
56 * writes processed template to file
57 * @param key
58 * @param output
59 * @param template
60 * @throws IOException
61 * @throws TemplateException
62 */
63 public static File ftlWrite(String key, File output, Template template, InputStream inputStream) throws IOException, TemplateException {
64 Properties props = loadProperties(inputStream);
65 systemPropertiesOverride(props, key);
66 File outputFile = writeTemplateToFile(output, template, props);
67
68 return outputFile;
69 }
70
71 protected static Properties loadProperties(InputStream inputStream) throws IOException {
72 Properties props = new Properties();
73
74 if(inputStream != null) {
75 props.load(inputStream);
76 }
77
78 return props;
79 }
80
81 /**
82 *
83 * @param file
84 * @param template
85 * @param props
86 * @return
87 * @throws IOException
88 * @throws freemarker.template.TemplateException
89 */
90 protected static File writeTemplateToFile(File file, Template template, Properties props) throws IOException, TemplateException {
91 String output = FreeMarkerTemplateUtils.processTemplateIntoString(template, props);
92 FileUtils.writeStringToFile(file, output);
93
94 return file;
95 }
96
97 /**
98 * -Dkey.propertyname= to override the property value for propertyname.
99 * @param props
100 */
101 public static void systemPropertiesOverride(Properties props, String key) {
102 Enumeration<?> names = props.propertyNames();
103 Object nameObject;
104 String name;
105 while (names.hasMoreElements()) {
106 nameObject = names.nextElement();
107 if (nameObject instanceof String) {
108 name = (String)nameObject;
109 props.setProperty(name, System.getProperty(key + "." + name, props.getProperty(name)));
110 }
111 }
112 }
113
114 }