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.sampleu.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.kuali.rice.testtools.common.PropertiesUtils;
023 import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
024
025 import java.io.File;
026 import java.io.IOException;
027 import java.io.InputStream;
028 import java.util.Properties;
029
030 /**
031 * FreemarkerUtil loads properties from user defined InputStream. systemPropertiesOverride should be used to override properties
032 * using a given key to identify them from JVM args. (i.e. -Dkey.name to override the name property in the key file.)
033 * TODO setup so the loading and overriding of properties is done for the user rather then them having to call it.
034 * @author Kuali Rice Team (rice.collab@kuali.org)
035 */
036 public class FreemarkerUtil {
037
038 protected Configuration cfg;
039
040 /**
041 * Calls ftlWrite that also accepts a key, using the output getName as the key.
042 * @param output
043 * @param template
044 * @return
045 * @throws java.io.IOException
046 * @throws freemarker.template.TemplateException
047 */
048 public static File ftlWrite(File output, Template template, InputStream inputStream) throws IOException, TemplateException {
049
050 return ftlWrite(output.getName(), output, template, inputStream);
051 }
052
053 /**
054 * Loads properties from user defined properties file, if not available uses resource file
055 *
056 * writes processed template to file
057 * @param key
058 * @param output
059 * @param template
060 * @throws IOException
061 * @throws TemplateException
062 */
063 public static File ftlWrite(String key, File output, Template template, InputStream inputStream) throws IOException, TemplateException {
064 PropertiesUtils propUtils = new PropertiesUtils();
065 Properties props = propUtils.loadProperties(inputStream);
066 props.put("baseName", output.getName().substring(0, output.getName().indexOf("ST")));
067 props.put("className", output.getName().substring(0, output.getName().indexOf("ST"))); // backwards compatibility
068 if (output.getName().contains("TmplMthd")) { // Template method pattern
069 props.put("className", output.getName().substring(0, output.getName().indexOf("TmplMthd")));
070 }
071
072 if (props.get("test1") == null ) { // backwards compatibility for Smoke Test Freemarker Generation
073 props.put("test1", "test" + props.get("className") + "Bookmark");
074 props.put("test2", "test" + props.get("className") + "Nav");
075 }
076
077 props = propUtils.systemPropertiesOverride(props, key);
078 props = propUtils.transformNumberedPropertiesToList(props);
079 File outputFile = writeTemplateToFile(output, template, props);
080
081 return outputFile;
082 }
083
084 protected static Properties loadProperties(InputStream inputStream) throws IOException {
085 Properties props = new Properties();
086
087 if(inputStream != null) {
088 props.load(inputStream);
089 }
090
091 return props;
092 }
093
094 /**
095 *
096 * @param file
097 * @param template
098 * @param props
099 * @return
100 * @throws IOException
101 * @throws freemarker.template.TemplateException
102 */
103 protected static File writeTemplateToFile(File file, Template template, Properties props) throws IOException, TemplateException {
104 String output = FreeMarkerTemplateUtils.processTemplateIntoString(template, props);
105 FileUtils.writeStringToFile(file, output);
106
107 return file;
108 }
109 }