KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > velocity > tools > view > tools > ViewRenderTool


1 /*
2  * Copyright 2003 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17
18 package org.apache.velocity.tools.view.tools;
19
20
21 import java.io.StringWriter JavaDoc;
22 import org.apache.velocity.app.Velocity;
23 import org.apache.velocity.context.Context;
24 import org.apache.velocity.tools.generic.RenderTool;
25 import org.apache.velocity.tools.view.tools.ViewTool;
26
27
28 /**
29  * This tool expose methods to evaluate the given
30  * strings as VTL (Velocity Template Language)
31  * and automatically using the current context.
32  * <br />
33  * <pre>
34  * Example of eval():
35  * Input
36  * -----
37  * #set( $list = [1,2,3] )
38  * #set( $object = '$list' )
39  * #set( $method = 'size()' )
40  * $render.eval("${object}.$method")
41  *
42  * Output
43  * ------
44  * 3
45  *
46  * Example of recurse():
47  * Input
48  * -----
49  * #macro( say_hi )hello world!#end
50  * #set( $foo = '#say_hi()' )
51  * #set( $bar = '$foo' )
52  * $render.recurse('$bar')
53  *
54  * Output
55  * ------
56  * hello world!
57  *
58  *
59  * Toolbox configuration:
60  * &lt;tool&gt;
61  * &lt;key&gt;render&lt;/key&gt;
62  * &lt;scope&gt;request&lt;/scope&gt;
63  * &lt;class&gt;org.apache.velocity.tools.view.tools.ViewRenderTool&lt;/class&gt;
64  * &lt;/tool&gt;
65  * </pre>
66  *
67  * <p>Ok, so these examples are really lame. But, it seems like
68  * someone out there is always asking how to do stuff like this
69  * and we always tell them to write a tool. Now we can just tell
70  * them to use this tool.</p>
71  *
72  * <p>This tool is NOT meant to be used in either application or
73  * session scopes of a servlet environment.</p>
74  *
75  * @author <a HREF="mailto:nathan@esha.com">Nathan Bubna</a>
76  * @version $Revision: 1.4.2.1 $ $Date: 2004/03/12 20:16:28 $
77  */

78
79 public class ViewRenderTool extends RenderTool implements ViewTool
80 {
81
82     private Context context;
83
84
85     /**
86      * Constructs a new instance
87      */

88     public ViewRenderTool()
89     {}
90     
91     
92     /**
93      * Initializes this instance.
94      *
95      * @param obj the current Context
96      */

97     public void init(Object JavaDoc obj)
98     {
99         context = (Context)obj;
100     }
101
102     /**
103      * <p>Evaluates a String containing VTL using the current context,
104      * and returns the result as a String. If this fails, then
105      * <code>null</code> will be returned. This evaluation is not
106      * recursive.</p>
107      *
108      * @param vtl the code to be evaluated
109      * @return the evaluated code as a String
110      */

111     public String JavaDoc eval(String JavaDoc vtl) throws Exception JavaDoc
112     {
113         return eval(context, vtl);
114     }
115
116
117     /**
118      * <p>Recursively evaluates a String containing VTL using the
119      * current context, and returns the result as a String. It
120      * will continue to re-evaluate the output of the last
121      * evaluation until an evaluation returns the same code
122      * that was fed into it.</p>
123      *
124      * TODO? add a parse-depth to prevent infinite recursion?
125      *
126      * @param vtl the code to be evaluated
127      * @return the evaluated code as a String
128      */

129     public String JavaDoc recurse(String JavaDoc vtl) throws Exception JavaDoc
130     {
131         return recurse(context, vtl);
132     }
133
134
135 }
136
Popular Tags