View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.freemarker;
35  
36  import info.magnolia.cms.util.AlertUtil;
37  import org.apache.commons.lang.StringUtils;
38  import org.slf4j.Logger;
39  import org.slf4j.LoggerFactory;
40  
41  import java.io.PrintWriter;
42  import java.io.StringWriter;
43  import java.io.Writer;
44  import java.util.HashMap;
45  import java.util.Map;
46  
47  /**
48   * A bunch of utility methods to render freemarker templates into Strings.
49   *
50   * @author Philipp Bracher
51   * @author gjoseph
52   * @version $Revision: $ ($Author: $)
53   * @see info.magnolia.freemarker.FreemarkerHelper
54   */
55  public class FreemarkerUtil {
56      private static final Logger log = LoggerFactory.getLogger(FreemarkerUtil.class);
57  
58      /**
59       * Uses the class of the object to create the templates name, passes the object under the name 'this'
60       * and returns the result in a String.
61       * Used only in DialogMultiSelect - VersionCommentPopup - Inbox - SubPagesControl
62       */
63      public static String process(Object thisObj) {
64          final Writer writer = new StringWriter();
65          process(thisObj, writer);
66          return writer.toString();
67      }
68  
69      /**
70       * @see #process(Object)
71       */
72      public static void process(Object thisObj, Writer out) {
73          process(thisObj.getClass(), thisObj, out);
74      }
75  
76      /**
77       * Same as {@link #process(Object)} but adds the classifier and the extension to the template name.
78       */
79      public static String process(Object thisObj, String classifier, String ext) {
80          final Writer writer = new StringWriter();
81          process(thisObj, classifier, ext, writer);
82          return writer.toString();
83      }
84  
85      /**
86       * @see #process(Object, String, String)
87       */
88      public static void process(Object thisObj, String classifier, String ext, Writer out) {
89          final Map data = new HashMap();
90          data.put("this", thisObj);
91          String template = createTemplateName(thisObj.getClass(), classifier, ext);
92          process(template, data, out);
93      }
94  
95      /**
96       * Uses the class to create the templates name and passes the object under the name 'this'.
97       * Uses "html" as template filename extension.
98       * Only used in AbstractSimpleSearchList and VersionsList.
99       */
100     public static String process(Class klass, Object thisObj) {
101         final Writer writer = new StringWriter();
102         process(klass, thisObj, writer);
103         return writer.toString();
104     }
105 
106     /**
107      * @see #process(Class, Object)
108      */
109     public static void process(Class klass, Object thisObj, Writer out) {
110         final Map data = new HashMap();
111         data.put("this", thisObj);
112         String template = createTemplateName(klass, "html");
113         process(template, data, out);
114     }
115 
116     /**
117      * Process this template with the passed data and returns the result in a String.
118      */
119     public static String process(String name, Map data) {
120         final Writer writer = new StringWriter();
121         process(name, data, writer);
122         return writer.toString();
123     }
124 
125     /**
126      * Process the template with the data and writes the result to the writer.
127      * TODO : move this to FreemarkerHelper
128      */
129     public static void process(String name, Map data, Writer writer) {
130         try {
131             // add some useful default data
132             if (AlertUtil.isMessageSet()) {
133                 data.put("message", AlertUtil.getMessage());
134             }
135             FreemarkerHelper.getInstance().render(name, data, writer);
136         }
137         catch (Exception e) {
138             e.printStackTrace(new PrintWriter(writer));
139             log.error("Exception while processing template " + name, e);
140         }
141     }
142 
143     /**
144      * Creates a template name based on the class name and adds the extension.
145      * If the class is org.mydomain.TheClass the returned name is /org/mydomain/TheClass.html.
146      */
147     public static String createTemplateName(Class klass, String ext) {
148         return createTemplateName(klass, null, ext);
149     }
150 
151     /**
152      * Same as {@link #createTemplateName(Class, String)} but adds the classifier between
153      * the template name and the extension.
154      */
155     public static String createTemplateName(Class klass, String classifier, String ext) {
156         classifier = (classifier != null) ? StringUtils.capitalize(classifier) : "";
157         return "/" + StringUtils.replace(klass.getName(), ".", "/") + classifier + "." + ext;
158     }
159 
160 }