View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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       *  strictaccess : determines if the JSPContext is used
51       *  to autofetch information from the 'scopes' 
52       *  or if the scopetool() is used
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          *  if there is no body, we are done
142          */
143 
144         if ( bodyContent == null)
145             return EVAL_PAGE;
146 
147         try
148         {
149             JspWriter writer = pageContext.getOut();
150 
151             /*
152              *  get our body
153              */
154 
155             Reader bodyreader = bodyContent.getReader();
156 
157             /*
158              * now make a JSPContext
159              */
160 
161             Context vc = null;
162 
163             /*
164              *  if strictAccess == true, then we want to use a regular
165              *  VelocityContext, as we assume that the template will
166              *  bring into the context any beans using the scope tool
167              *  or the like
168              */
169             if (strictAccess)
170             {
171                 vc = new VelocityContext();
172             }
173             else
174             {
175                vc = new JSPContext( pageContext );
176             }
177 
178             /*
179              *  add the scope tool
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 }