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.template.Configuration;
19  import freemarker.template.Template;
20  import freemarker.template.TemplateException;
21  import org.apache.commons.io.FileUtils;
22  import org.apache.log4j.Logger;
23  import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
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.Enumeration;
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      /**
43       * Calls ftlWrite that also accepts a key, using the output getName as the key.
44       * {@link FreemarkerUtil#ftlWrite(java.io.File, freemarker.template.Template, java.io.InputStream)}
45       * @param output
46       * @param template
47       * @return
48       * @throws IOException
49       * @throws TemplateException
50       */
51      public File ftlWrite(File output, Template template, InputStream inputStream) throws IOException, TemplateException {
52  
53          return FreemarkerUtil.ftlWrite(output.getName(), output, template, inputStream);
54      }
55  
56      /**
57       * TODO can we cut this down to one param?
58       * {@link FreemarkerUtil#loadProperties(java.io.InputStream)}
59       * @param fileLocation null means use resourceLocation
60       * @param resourceLocation
61       * @return
62       * @throws IOException
63       */
64      protected Properties loadProperties(String fileLocation, String resourceLocation) throws IOException {
65          Properties props = new Properties();
66          InputStream in = null;
67          if(fileLocation != null) {
68              in = new FileInputStream(fileLocation);
69          } else {
70              in = getClass().getClassLoader().getResourceAsStream(resourceLocation);
71          }
72          if(in != null) {
73              FreemarkerUtil.loadProperties(in);
74              in.close();
75          }
76  
77          return props;
78      }
79  
80      /**
81       * {@link FreemarkerUtil#systemPropertiesOverride(java.util.Properties, String)}
82       * @param props
83       * @param key
84       */
85      protected void systemPropertiesOverride(Properties props, String key) {
86          FreemarkerUtil.systemPropertiesOverride(props, key);
87      }
88  
89      /**
90      * {@link FreemarkerUtil#writeTemplateToFile(java.io.File, freemarker.template.Template, java.util.Properties)}
91      * @param file
92      * @param template
93      * @param props
94      * @return
95      * @throws IOException
96      * @throws TemplateException
97      */
98      protected static File writeTemplateToFile(File file, Template template, Properties props) throws IOException, TemplateException {
99          return FreemarkerUtil.writeTemplateToFile(file, template, props);
100     }
101 }