View Javadoc

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