1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.taglibs.velocity;
19
20 import java.io.Reader;
21
22 import javax.servlet.jsp.tagext.BodyTag;
23 import javax.servlet.jsp.tagext.Tag;
24 import javax.servlet.jsp.tagext.BodyContent;
25
26 import javax.servlet.jsp.JspWriter;
27 import javax.servlet.jsp.JspException;
28 import javax.servlet.jsp.PageContext;
29
30 import org.apache.velocity.app.Velocity;
31 import org.apache.velocity.VelocityContext;
32 import org.apache.velocity.context.Context;
33
34 /***
35 * <p>
36 * Simple implementation of JSP tag to allow
37 * use of VTL in JSP's.
38 * </p>
39 *
40 * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
41 * @version $Id: VelocityTag.java 517121 2007-03-12 07:45:49Z ate $
42 */
43 public class VelocityTag implements BodyTag
44 {
45 protected Tag parent = null;
46 protected BodyContent bodyContent = null;
47 protected PageContext pageContext = null;
48
49
50
51
52
53
54 protected boolean strictAccess = false;
55
56 /***
57 * CTOR : current implementation uses the Singleton
58 * model for velocity.
59 */
60 public VelocityTag()
61 {
62 try
63 {
64 Velocity.init();
65 }
66 catch(Exception e)
67 {
68 System.out.println("VelocityTag() : " + e );
69 }
70 }
71
72 /***
73 * switch for strictaccess
74 *
75 * @param sa if true, then normal VelocityContext is used
76 * and template must directly get beans from scopes
77 * Otherwise, the JSPContext is used which searches for
78 * objects/beans in the scopes automatically.
79 */
80 public void setStrictaccess( boolean sa )
81 {
82 this.strictAccess = sa;
83 }
84
85 public Tag getParent()
86 {
87 return parent;
88 }
89
90 public void setParent( Tag parent)
91 {
92 this.parent = parent;
93 return;
94 }
95
96 public int doStartTag()
97 throws JspException
98 {
99 return EVAL_BODY_BUFFERED;
100 }
101
102 public void setBodyContent( BodyContent bc )
103 {
104 this.bodyContent = bc;
105 return;
106 }
107
108 public void setPageContext( PageContext pc )
109 {
110 this.pageContext = pc;
111 return;
112 }
113
114 public void doInitBody()
115 throws JspException
116 {
117 return;
118 }
119
120 public int doAfterBody()
121 throws JspException
122 {
123 return 0;
124 }
125
126 public void release()
127 {
128 return;
129 }
130
131 /***
132 * This is the real worker for this taglib.
133 * There are efficiencies to be added - the plan
134 * is to cache the AST to avoid reparsing every
135 * time.
136 */
137 public int doEndTag()
138 throws JspException
139 {
140
141
142
143
144 if ( bodyContent == null)
145 return EVAL_PAGE;
146
147 try
148 {
149 JspWriter writer = pageContext.getOut();
150
151
152
153
154
155 Reader bodyreader = bodyContent.getReader();
156
157
158
159
160
161 Context vc = null;
162
163
164
165
166
167
168
169 if (strictAccess)
170 {
171 vc = new VelocityContext();
172 }
173 else
174 {
175 vc = new JSPContext( pageContext );
176 }
177
178
179
180
181
182 vc.put( "scopetool", new ScopeTool( pageContext ) );
183
184 Velocity.evaluate( vc , writer, "JSP for me!", bodyreader );
185 }
186 catch( Exception e )
187 {
188 System.out.println( e.toString() );
189 }
190
191 return EVAL_PAGE;
192 }
193 }