KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > deliver > util > FreemarkerTemplateProcessor


1 /* ===============================================================================
2 *
3 * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4 *
5 * ===============================================================================
6 *
7 * Copyright (C)
8 *
9 * This program is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License version 2, as published by the
11 * Free Software Foundation. See the file LICENSE.html for more information.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19 * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20 *
21 * ===============================================================================
22 */

23 package org.infoglue.deliver.util;
24
25 import java.io.PrintWriter JavaDoc;
26 import java.io.Reader JavaDoc;
27 import java.io.StringReader JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import freemarker.template.Configuration;
31 import freemarker.template.Template;
32 import freemarker.template.TemplateException;
33
34 /**
35  * @author mattias
36  *
37  * This class makes it possible to choose freemarker as the template mechanism.
38  */

39
40 public class FreemarkerTemplateProcessor
41 {
42     private static FreemarkerTemplateProcessor processor = null;
43     
44     private Configuration cfg;
45     
46     private FreemarkerTemplateProcessor()
47     {
48         cfg = new Configuration();
49         // - Templates are stoted in the WEB-INF/templates directory of the Web app.
50
//cfg.setServletContextForTemplateLoading(getServletContext(), "WEB-INF/templates");
51
}
52     
53     public static FreemarkerTemplateProcessor getProcessor()
54     {
55         if(processor == null)
56             processor = new FreemarkerTemplateProcessor();
57         
58         return processor;
59     }
60         
61     
62     
63     public void renderTemplate(Map JavaDoc params, PrintWriter JavaDoc pw, String JavaDoc templateAsString) throws Exception JavaDoc
64     {
65         /*
66         int index = templateAsString.indexOf("$templateLogic");
67         while(index > -1)
68         {
69             int indexEnd = templateAsString.indexOf(")", index);
70             if(index > -1 && indexEnd > index)
71             {
72                 templateAsString = templateAsString.substring(0, index + 1) + "{" + templateAsString.substring(index + 1, indexEnd) + "}" + templateAsString.substring(indexEnd + 1);
73             }
74             index = templateAsString.indexOf("$templateLogic", index + 1);
75         }
76         */

77         
78         int hashCode = templateAsString.hashCode();
79
80         String JavaDoc fileName = "Template_" + hashCode + ".ftl";
81
82         // Get the templat object
83
Reader JavaDoc reader = new StringReader JavaDoc(templateAsString);
84         Template t = new Template(fileName, reader, cfg);
85         
86         try
87         {
88             t.process(params, pw);
89         }
90         catch (TemplateException e)
91         {
92             throw new Exception JavaDoc("Error while processing FreeMarker template", e);
93         }
94     }
95 }
96
Popular Tags