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.apache.log4j.Logger;
023 import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
024
025 import java.io.File;
026 import java.io.FileInputStream;
027 import java.io.IOException;
028 import java.io.InputStream;
029 import java.util.Enumeration;
030 import java.util.Properties;
031
032 /**
033 * @see FreemarkerUtil
034 * @author Kuali Rice Team (rice.collab@kuali.org)
035 */
036 public abstract class FreemarkerSTBase extends WebDriverLegacyITBase {
037
038 protected final Logger LOG = Logger.getLogger(getClass());
039
040 protected Configuration cfg;
041
042 /**
043 * Calls ftlWrite that also accepts a key, using the output getName as the key.
044 * {@link FreemarkerUtil#ftlWrite(java.io.File, freemarker.template.Template, java.io.InputStream)}
045 * @param output
046 * @param template
047 * @return
048 * @throws IOException
049 * @throws TemplateException
050 */
051 public File ftlWrite(File output, Template template, InputStream inputStream) throws IOException, TemplateException {
052
053 return FreemarkerUtil.ftlWrite(output.getName(), output, template, inputStream);
054 }
055
056 /**
057 * TODO can we cut this down to one param?
058 * {@link FreemarkerUtil#loadProperties(java.io.InputStream)}
059 * @param fileLocation null means use resourceLocation
060 * @param resourceLocation
061 * @return
062 * @throws IOException
063 */
064 protected Properties loadProperties(String fileLocation, String resourceLocation) throws IOException {
065 Properties props = new Properties();
066 InputStream in = null;
067 if(fileLocation != null) {
068 in = new FileInputStream(fileLocation);
069 } else {
070 in = getClass().getClassLoader().getResourceAsStream(resourceLocation);
071 }
072 if(in != null) {
073 FreemarkerUtil.loadProperties(in);
074 in.close();
075 }
076
077 return props;
078 }
079
080 /**
081 * {@link FreemarkerUtil#systemPropertiesOverride(java.util.Properties, String)}
082 * @param props
083 * @param key
084 */
085 protected void systemPropertiesOverride(Properties props, String key) {
086 FreemarkerUtil.systemPropertiesOverride(props, key);
087 }
088
089 /**
090 * {@link FreemarkerUtil#writeTemplateToFile(java.io.File, freemarker.template.Template, java.util.Properties)}
091 * @param file
092 * @param template
093 * @param props
094 * @return
095 * @throws IOException
096 * @throws TemplateException
097 */
098 protected static File writeTemplateToFile(File file, Template template, Properties props) throws IOException, TemplateException {
099 return FreemarkerUtil.writeTemplateToFile(file, template, props);
100 }
101 }