001 /**
002 * Copyright 2005-2013 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package edu.samplu.common;
017
018 import freemarker.template.Configuration;
019 import freemarker.template.Template;
020 import freemarker.template.TemplateException;
021 import org.apache.commons.io.FileUtils;
022 import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
023
024 import java.io.File;
025 import java.io.IOException;
026 import java.io.InputStream;
027 import java.util.Enumeration;
028 import java.util.HashMap;
029 import java.util.Iterator;
030 import java.util.Map;
031 import java.util.Properties;
032
033 /**
034 * FreemarkerUtil loads properties from user defined InputStream. systemPropertiesOverride should be used to override properties
035 * using a given key to identify them from JVM args. (i.e. -Dkey.name to override the name property in the key file.)
036 * TODO setup so the loading and overriding of properties is done for the user rather then them having to call it.
037 * @author Kuali Rice Team (rice.collab@kuali.org)
038 */
039 public class FreemarkerUtil {
040
041 protected Configuration cfg;
042
043 /**
044 * Calls ftlWrite that also accepts a key, using the output getName as the key.
045 * @param output
046 * @param template
047 * @return
048 * @throws java.io.IOException
049 * @throws freemarker.template.TemplateException
050 */
051 public static File ftlWrite(File output, Template template, InputStream inputStream) throws IOException, TemplateException {
052
053 return ftlWrite(output.getName(), output, template, inputStream);
054 }
055
056 /**
057 * Loads properties from user defined properties file, if not available uses resource file
058 *
059 * writes processed template to file
060 * @param key
061 * @param output
062 * @param template
063 * @throws IOException
064 * @throws TemplateException
065 */
066 public static File ftlWrite(String key, File output, Template template, InputStream inputStream) throws IOException, TemplateException {
067 Properties props = loadProperties(inputStream);
068 props.put("baseName", output.getName().substring(0, output.getName().indexOf("ST")));
069 if (output.getName().contains("TmplMthd")) { // Template method pattern
070 props.put("className", output.getName().substring(0, output.getName().indexOf("TmplMthd")));
071 }
072
073 systemPropertiesOverride(props, key);
074 transformNumberedTestPropertiesToList(props);
075 File outputFile = writeTemplateToFile(output, template, props);
076
077 return outputFile;
078 }
079
080 protected static Properties loadProperties(InputStream inputStream) throws IOException {
081 Properties props = new Properties();
082
083 if(inputStream != null) {
084 props.load(inputStream);
085 }
086
087 return props;
088 }
089
090 protected static void transformNumberedTestPropertiesToList(Properties props) {
091 Iterator keys = props.keySet().iterator();
092 Map<String, String> keyLists = new HashMap<String, String>();
093 while (keys.hasNext()) {
094 String key = (String)keys.next();
095 if (Character.isDigit(key.charAt(key.length()-1))) {
096 keyLists.put(key, props.getProperty(key));
097 }
098 }
099
100 Iterator listKeys = keyLists.keySet().iterator();
101 while (listKeys.hasNext()) {
102 String key = (String)listKeys.next();
103 props.remove(key);
104 }
105
106 props.put("tests", keyLists.values());
107 }
108
109 /**
110 *
111 * @param file
112 * @param template
113 * @param props
114 * @return
115 * @throws IOException
116 * @throws freemarker.template.TemplateException
117 */
118 protected static File writeTemplateToFile(File file, Template template, Properties props) throws IOException, TemplateException {
119 String output = FreeMarkerTemplateUtils.processTemplateIntoString(template, props);
120 FileUtils.writeStringToFile(file, output);
121
122 return file;
123 }
124
125 /**
126 * -Dkey.propertyname= to override the property value for propertyname.
127 * @param props
128 */
129 public static void systemPropertiesOverride(Properties props, String key) {
130 Enumeration<?> names = props.propertyNames();
131 Object nameObject;
132 String name;
133 while (names.hasMoreElements()) {
134 nameObject = names.nextElement();
135 if (nameObject instanceof String) {
136 name = (String)nameObject;
137 props.setProperty(name, System.getProperty(key + "." + name, props.getProperty(name)));
138 }
139 }
140 }
141
142 }