KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freemarker > ext > jsp > FreemarkerTag


1 /*
2  * Copyright (c) 2003 The Visigoth Software Society. All rights
3  * reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  *
17  * 3. The end-user documentation included with the redistribution, if
18  * any, must include the following acknowledgement:
19  * "This product includes software developed by the
20  * Visigoth Software Society (http://www.visigoths.org/)."
21  * Alternately, this acknowledgement may appear in the software itself,
22  * if and wherever such third-party acknowledgements normally appear.
23  *
24  * 4. Neither the name "FreeMarker", "Visigoth", nor any of the names of the
25  * project contributors may be used to endorse or promote products derived
26  * from this software without prior written permission. For written
27  * permission, please contact visigoths@visigoths.org.
28  *
29  * 5. Products derived from this software may not be called "FreeMarker" or "Visigoth"
30  * nor may "FreeMarker" or "Visigoth" appear in their names
31  * without prior written permission of the Visigoth Software Society.
32  *
33  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
34  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36  * DISCLAIMED. IN NO EVENT SHALL THE VISIGOTH SOFTWARE SOCIETY OR
37  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
40  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
41  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
42  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
43  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44  * SUCH DAMAGE.
45  * ====================================================================
46  *
47  * This software consists of voluntary contributions made by many
48  * individuals on behalf of the Visigoth Software Society. For more
49  * information on the Visigoth Software Society, please see
50  * http://www.visigoths.org/
51  */

52
53 package freemarker.ext.jsp;
54
55 import java.io.IOException JavaDoc;
56
57 import javax.servlet.ServletException JavaDoc;
58 import javax.servlet.jsp.JspException JavaDoc;
59 import javax.servlet.jsp.PageContext JavaDoc;
60 import javax.servlet.jsp.tagext.BodyContent JavaDoc;
61 import javax.servlet.jsp.tagext.BodyTag JavaDoc;
62 import javax.servlet.jsp.tagext.Tag JavaDoc;
63
64 import freemarker.template.SimpleHash;
65 import freemarker.template.Template;
66
67 /**
68  * Simple implementation of JSP tag to allow use of FreeMarker templates in
69  * JSP. Inspired by similar class in Velocity template engine developed by
70  * <a HREF="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
71  * @author Attila Szegedi
72  */

73 public class FreemarkerTag implements BodyTag JavaDoc
74 {
75     private Tag JavaDoc parent;
76     private BodyContent JavaDoc bodyContent;
77     private PageContext JavaDoc pageContext;
78     private SimpleHash root;
79     private Template template;
80     private boolean caching = true;
81     private String JavaDoc name = "";
82     
83     public boolean getCaching()
84     {
85         return caching;
86     }
87
88     public void setCaching(boolean caching)
89     {
90         this.caching = caching;
91     }
92
93     public void setName(String JavaDoc name)
94     {
95         this.name = name == null ? "" : name;
96     }
97     
98     public Tag JavaDoc getParent()
99     {
100         return parent;
101     }
102
103     public void setParent(Tag JavaDoc parent)
104     {
105         this.parent = parent;
106     }
107
108     public int doStartTag()
109     {
110         return EVAL_BODY_BUFFERED;
111     }
112
113     public void setBodyContent(BodyContent JavaDoc bodyContent)
114     {
115         this.bodyContent = bodyContent;
116     }
117
118     public void setPageContext(PageContext JavaDoc pageContext)
119     {
120         this.pageContext = pageContext;
121         root = null;
122     }
123
124     public void doInitBody()
125     {
126     }
127
128     public int doAfterBody()
129     {
130         return SKIP_BODY;
131     }
132
133     public void release()
134     {
135         root = null;
136         template = null;
137         name = "";
138     }
139
140     public int doEndTag()
141         throws JspException JavaDoc
142     {
143         if (bodyContent == null)
144             return EVAL_PAGE;
145
146         try
147         {
148             if(template == null)
149             {
150                 template = new Template(name, bodyContent.getReader());
151             }
152
153             if(root == null)
154             {
155                 root = new SimpleHash();
156                 root.put("page", new JspContextModel(pageContext, JspContextModel.PAGE_SCOPE));
157                 root.put("request", new JspContextModel(pageContext, JspContextModel.REQUEST_SCOPE));
158                 root.put("session", new JspContextModel(pageContext, JspContextModel.SESSION_SCOPE));
159                 root.put("application", new JspContextModel(pageContext, JspContextModel.APPLICATION_SCOPE));
160                 root.put("any", new JspContextModel(pageContext, JspContextModel.ANY_SCOPE));
161             }
162             template.process(root, pageContext.getOut());
163         }
164         catch(Exception JavaDoc e)
165         {
166             try
167             {
168                 pageContext.handlePageException(e);
169             }
170             catch(ServletException JavaDoc e2)
171             {
172                 throw new JspException JavaDoc(e2.getMessage());
173             }
174             catch(IOException JavaDoc e2)
175             {
176                 throw new JspException JavaDoc(e2.getMessage());
177             }
178         }
179         finally
180         {
181             if(!caching)
182             {
183                 template = null;
184             }
185         }
186
187         return EVAL_PAGE;
188     }
189 }
190
Popular Tags