KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > util > FreeMarkerUtil


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2005 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.util;
14
15 import info.magnolia.context.MgnlContext;
16
17 import java.io.PrintWriter JavaDoc;
18 import java.io.StringWriter JavaDoc;
19 import java.io.Writer JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.apache.commons.lang.StringUtils;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import freemarker.template.Configuration;
28 import freemarker.template.ObjectWrapper;
29 import freemarker.template.Template;
30
31
32 /**
33  * This is a FreeMaker Util loading the templates from the classpath
34  * @author Philipp Bracher
35  * @version $Revision: 6901 $ ($Author: scharles $)
36  */

37 public class FreeMarkerUtil {
38
39     /**
40      * The internal configuration used
41      */

42     private static Configuration cfg;
43
44     private static Logger log = LoggerFactory.getLogger(FreeMarkerUtil.class);
45
46     static {
47         cfg = new Configuration();
48         cfg.setObjectWrapper(ObjectWrapper.DEFAULT_WRAPPER);
49         cfg.setClassForTemplateLoading(FreeMarkerUtil.class, "/");
50         cfg.setTagSyntax(Configuration.AUTO_DETECT_TAG_SYNTAX);
51         cfg.setDefaultEncoding("UTF8");
52     }
53
54     /**
55      * Process this template with the passed data
56      * @param name
57      * @param data
58      * @return the resuling string
59      */

60     public static String JavaDoc process(String JavaDoc name, Map JavaDoc data) {
61         Writer JavaDoc writer = new StringWriter JavaDoc();
62         process(name, data, writer);
63         return writer.toString();
64     }
65
66     /**
67      * Uses the class of the object to create the templates name and passes the object under the name 'this'
68      * @param thisObj
69      * @return the resuling string
70      */

71     public static String JavaDoc process(Object JavaDoc thisObj) {
72         return process(thisObj.getClass(), thisObj);
73     }
74
75     /**
76      * Default extension is html
77      * @param klass
78      * @param thisObj
79      * @return
80      */

81     public static String JavaDoc process(Class JavaDoc klass, Object JavaDoc thisObj) {
82         return process(klass, thisObj, "html");
83     }
84
85     /**
86      * Uses the class to create the templates name and passes the object under the name 'this'
87      * @param klass
88      * @param thisObj
89      * @param ext
90      * @return
91      */

92     public static String JavaDoc process(Class JavaDoc klass, Object JavaDoc thisObj, String JavaDoc ext) {
93         Map JavaDoc data = new HashMap JavaDoc();
94         data.put("this", thisObj);
95         return process(klass, data, ext);
96     }
97
98     /**
99      * Uses the class to create the templates name.
100      * @param klass
101      * @param data
102      * @param ext
103      * @return
104      */

105     public static String JavaDoc process(Class JavaDoc klass, Map JavaDoc data, String JavaDoc ext) {
106         return process(createTemplateName(klass, ext), data);
107     }
108
109     /**
110      * Process the template with the data and writes the result to the writer.
111      * @param name
112      * @param data
113      * @param writer
114      */

115     public static void process(String JavaDoc name, Map JavaDoc data, Writer JavaDoc writer) {
116         try {
117             Template tmpl = cfg.getTemplate(name);
118             // add some usfull default data
119
data.put("contextPath", MgnlContext.getContextPath());
120             if (AlertUtil.isMessageSet()) {
121                 data.put("message", AlertUtil.getMessage());
122             }
123             tmpl.process(data, writer);
124         }
125         catch (Exception JavaDoc e) {
126             e.printStackTrace(new PrintWriter JavaDoc(writer));
127             log.error("exception in template", e);
128         }
129     }
130
131     public static String JavaDoc createTemplateName(Class JavaDoc klass, String JavaDoc ext) {
132         return "/" + StringUtils.replace(klass.getName(), ".", "/") + "." + ext;
133     }
134
135     /**
136      * @return get default static freemarker configuration
137      * */

138     public static Configuration getDefaultConfiguration() {
139         return cfg;
140     }
141
142 }
143
Popular Tags