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