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.cache.ClassTemplateLoader;
19 import freemarker.template.Configuration;
20 import freemarker.template.Template;
21 import freemarker.template.TemplateException;
22 import org.apache.log4j.Logger;
23 import org.junit.Before;
24
25 import java.io.File;
26 import java.io.FileInputStream;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.util.Properties;
30
31 /**
32 * @see FreemarkerUtil
33 * @author Kuali Rice Team (rice.collab@kuali.org)
34 */
35 public abstract class FreemarkerSTBase extends WebDriverLegacyITBase {
36
37 protected final Logger LOG = Logger.getLogger(getClass());
38
39 protected Configuration cfg;
40
41 protected abstract String getTemplateDir();
42
43 @Override
44 @Before
45 public void testSetUp() {
46 super.testSetUp();
47 // generated load users and group resources
48 cfg = new Configuration();
49 cfg.setTemplateLoader(new ClassTemplateLoader(getClass().getClassLoader().getClass(), getTemplateDir()));
50 }
51
52 /**
53 * Calls ftlWrite that also accepts a key, using the output getName as the key.
54 * {@link FreemarkerUtil#ftlWrite(java.io.File, freemarker.template.Template, java.io.InputStream)}
55 * @param output
56 * @param template
57 * @return
58 * @throws IOException
59 * @throws TemplateException
60 */
61 public File ftlWrite(File output, Template template, InputStream inputStream) throws IOException, TemplateException {
62
63 return FreemarkerUtil.ftlWrite(output.getName(), output, template, inputStream);
64 }
65
66 /**
67 * TODO can we cut this down to one param?
68 * {@link FreemarkerUtil#loadProperties(java.io.InputStream)}
69 * @param fileLocation null means use resourceLocation
70 * @param resourceLocation
71 * @return
72 * @throws IOException
73 */
74 protected Properties loadProperties(String fileLocation, String resourceLocation) throws IOException {
75 Properties props = null;
76 InputStream in = null;
77 if(fileLocation != null) {
78 in = new FileInputStream(fileLocation);
79 } else {
80 in = getClass().getClassLoader().getResourceAsStream(resourceLocation);
81 }
82 if(in != null) {
83 props = PropertiesUtils.loadProperties(in);
84 in.close();
85 }
86
87 return props;
88 }
89
90 /**
91 * {@link FreemarkerUtil#writeTemplateToFile(java.io.File, freemarker.template.Template, java.util.Properties)}
92 * @param file
93 * @param template
94 * @param props
95 * @return
96 * @throws IOException
97 * @throws TemplateException
98 */
99 protected static File writeTemplateToFile(File file, Template template, Properties props) throws IOException, TemplateException {
100 return FreemarkerUtil.writeTemplateToFile(file, template, props);
101 }
102 }