001: /*
002: * Copyright (c) 2003 The Visigoth Software Society. All rights
003: * reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * 1. Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * 2. Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in
014: * the documentation and/or other materials provided with the
015: * distribution.
016: *
017: * 3. The end-user documentation included with the redistribution, if
018: * any, must include the following acknowledgement:
019: * "This product includes software developed by the
020: * Visigoth Software Society (http://www.visigoths.org/)."
021: * Alternately, this acknowledgement may appear in the software itself,
022: * if and wherever such third-party acknowledgements normally appear.
023: *
024: * 4. Neither the name "FreeMarker", "Visigoth", nor any of the names of the
025: * project contributors may be used to endorse or promote products derived
026: * from this software without prior written permission. For written
027: * permission, please contact visigoths@visigoths.org.
028: *
029: * 5. Products derived from this software may not be called "FreeMarker" or "Visigoth"
030: * nor may "FreeMarker" or "Visigoth" appear in their names
031: * without prior written permission of the Visigoth Software Society.
032: *
033: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
034: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
035: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
036: * DISCLAIMED. IN NO EVENT SHALL THE VISIGOTH SOFTWARE SOCIETY OR
037: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
038: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
039: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
040: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
041: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
042: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
043: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
044: * SUCH DAMAGE.
045: * ====================================================================
046: *
047: * This software consists of voluntary contributions made by many
048: * individuals on behalf of the Visigoth Software Society. For more
049: * information on the Visigoth Software Society, please see
050: * http://www.visigoths.org/
051: */
052:
053: package freemarker.ext.jsp;
054:
055: import java.io.IOException;
056:
057: import javax.servlet.ServletException;
058: import javax.servlet.jsp.JspException;
059: import javax.servlet.jsp.PageContext;
060: import javax.servlet.jsp.tagext.BodyContent;
061: import javax.servlet.jsp.tagext.BodyTag;
062: import javax.servlet.jsp.tagext.Tag;
063:
064: import freemarker.template.SimpleHash;
065: import freemarker.template.Template;
066:
067: /**
068: * Simple implementation of JSP tag to allow use of FreeMarker templates in
069: * JSP. Inspired by similar class in Velocity template engine developed by
070: * <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
071: * @author Attila Szegedi
072: */
073: public class FreemarkerTag implements BodyTag {
074: private Tag parent;
075: private BodyContent bodyContent;
076: private PageContext pageContext;
077: private SimpleHash root;
078: private Template template;
079: private boolean caching = true;
080: private String name = "";
081:
082: public boolean getCaching() {
083: return caching;
084: }
085:
086: public void setCaching(boolean caching) {
087: this .caching = caching;
088: }
089:
090: public void setName(String name) {
091: this .name = name == null ? "" : name;
092: }
093:
094: public Tag getParent() {
095: return parent;
096: }
097:
098: public void setParent(Tag parent) {
099: this .parent = parent;
100: }
101:
102: public int doStartTag() {
103: return EVAL_BODY_BUFFERED;
104: }
105:
106: public void setBodyContent(BodyContent bodyContent) {
107: this .bodyContent = bodyContent;
108: }
109:
110: public void setPageContext(PageContext pageContext) {
111: this .pageContext = pageContext;
112: root = null;
113: }
114:
115: public void doInitBody() {
116: }
117:
118: public int doAfterBody() {
119: return SKIP_BODY;
120: }
121:
122: public void release() {
123: root = null;
124: template = null;
125: name = "";
126: }
127:
128: public int doEndTag() throws JspException {
129: if (bodyContent == null)
130: return EVAL_PAGE;
131:
132: try {
133: if (template == null) {
134: template = new Template(name, bodyContent.getReader());
135: }
136:
137: if (root == null) {
138: root = new SimpleHash();
139: root.put("page", new JspContextModel(pageContext,
140: JspContextModel.PAGE_SCOPE));
141: root.put("request", new JspContextModel(pageContext,
142: JspContextModel.REQUEST_SCOPE));
143: root.put("session", new JspContextModel(pageContext,
144: JspContextModel.SESSION_SCOPE));
145: root
146: .put("application", new JspContextModel(
147: pageContext,
148: JspContextModel.APPLICATION_SCOPE));
149: root.put("any", new JspContextModel(pageContext,
150: JspContextModel.ANY_SCOPE));
151: }
152: template.process(root, pageContext.getOut());
153: } catch (Exception e) {
154: try {
155: pageContext.handlePageException(e);
156: } catch (ServletException e2) {
157: throw new JspException(e2.getMessage());
158: } catch (IOException e2) {
159: throw new JspException(e2.getMessage());
160: }
161: } finally {
162: if (!caching) {
163: template = null;
164: }
165: }
166:
167: return EVAL_PAGE;
168: }
169: }
|