KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > base > util > template > FtlTransform


1 /*
2  * $Id: FtlTransform.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.base.util.template;
25
26 import java.util.Map JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Set JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.net.URL JavaDoc;
31 import java.net.URLConnection JavaDoc;
32 import java.io.InputStream JavaDoc;
33 import java.io.InputStreamReader JavaDoc;
34
35 import org.ofbiz.base.util.URLConnector;
36 import org.ofbiz.base.util.cache.UtilCache;
37 import org.ofbiz.base.location.FlexibleLocation;
38 import java.io.IOException JavaDoc;
39 import java.io.Writer JavaDoc;
40
41 import freemarker.ext.beans.BeansWrapper;
42 import freemarker.template.SimpleHash;
43 import freemarker.template.Template;
44 import freemarker.template.TemplateException;
45 import freemarker.template.WrappingTemplateModel;
46 import freemarker.template.Configuration;
47
48
49 /**
50  * FtlTransform
51  *
52  * This utility takes the URL to a Freemarker template and some parameters
53  * and runs the template and writes to the writer that was passed.
54  * It keeps its own cache for storing the fetched template.
55  *
56  * @author <a HREF="mailto:byersa@automationgroups.com">Al Byers</a>
57  * @version $Rev: 5462 $
58  * @since 3.2
59  */

60 public final class FtlTransform {
61
62     public static final String JavaDoc module = FtlTransform.class.getName();
63     public static UtilCache ftlTemplatesCache = new UtilCache("FtlTemplates", 0, 0);
64     private static Configuration cfg = new Configuration();
65     private static Map JavaDoc templateRoot = new HashMap JavaDoc();
66
67     static {
68         
69         WrappingTemplateModel.setDefaultObjectWrapper(BeansWrapper.getDefaultInstance());
70         try {
71             cfg.setObjectWrapper(BeansWrapper.getDefaultInstance());
72             cfg.setSetting("datetime_format", "yyyy-MM-dd HH:mm:ss.SSS");
73         } catch (TemplateException e) {
74             throw new RuntimeException JavaDoc("Freemarker TemplateException", e.getCause());
75         }
76         prepOfbizRoot(templateRoot);
77     }
78     
79     public FtlTransform() {
80     }
81  
82     public static void transform(Writer JavaDoc writer, String JavaDoc path, Map JavaDoc params)
83         throws TemplateException, IOException JavaDoc {
84         
85         Template template = (Template)ftlTemplatesCache.get(path);
86         if (template == null) {
87             template = getTemplate(path);
88             if (template != null) {
89                 ftlTemplatesCache.put(path, template);
90             }
91         }
92         
93         SimpleHash templateContext = new SimpleHash();
94         templateContext.putAll(templateRoot);
95         if (params != null) {
96            Set JavaDoc entrySet = params.entrySet();
97            Iterator JavaDoc iter = entrySet.iterator();
98            while (iter.hasNext()) {
99                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc)iter.next();
100                 String JavaDoc key = (String JavaDoc)entry.getKey();
101                 Object JavaDoc val = entry.getValue();
102                 if (val != null) {
103                     templateContext.put(key, val);
104                 }
105            }
106         }
107         template.process(templateContext, writer);
108         return;
109     }
110     
111     private static Template getTemplate( String JavaDoc inputUrl) throws IOException JavaDoc {
112         
113         URL JavaDoc url = FlexibleLocation.resolveLocation(inputUrl);
114         URLConnection JavaDoc conn = URLConnector.openConnection(url);
115         InputStream JavaDoc in = conn.getInputStream();
116         InputStreamReader JavaDoc rdr = new InputStreamReader JavaDoc(in);
117         Template template = new Template(inputUrl, rdr, cfg);
118         template.setObjectWrapper(BeansWrapper.getDefaultInstance());
119         return template;
120     }
121     
122     public static void prepOfbizRoot(Map JavaDoc root) {
123         FreeMarkerWorker.addAllOfbizTransforms(root);
124     }
125 }
126
Popular Tags