KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > velocity > tools > generic > RenderTool


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.generic;
19
20
21 import java.io.StringWriter JavaDoc;
22 import org.apache.velocity.app.Velocity;
23 import org.apache.velocity.context.Context;
24
25
26 /**
27  * This tool exposes methods to evaluate the given
28  * strings as VTL (Velocity Template Language)
29  * using the given context.
30  * <p>
31  * NOTE: These examples assume you have placed an
32  * instance of the current context within itself
33  * as 'ctx'. And, of course, the RenderTool is
34  * assumed to be available as 'render'.
35  * </p>
36  * <pre>
37  * Example of eval():
38  * Input
39  * -----
40  * #set( $list = [1,2,3] )
41  * #set( $object = '$list' )
42  * #set( $method = 'size()' )
43  * $render.eval($ctx, "${object}.$method")
44  *
45  * Output
46  * ------
47  * 3
48  *
49  * Example of recurse():
50  * Input
51  * -----
52  * #macro( say_hi )hello world!#end
53  * #set( $foo = '#say_hi()' )
54  * #set( $bar = '$foo' )
55  * $render.recurse($ctx, $bar)
56  *
57  * Output
58  * ------
59  * hello world!
60  *
61  * </pre>
62  *
63  * <p>Ok, so these examples are really lame. But, it seems like
64  * someone out there is always asking how to do stuff like this
65  * and we always tell them to write a tool. Now we can just tell
66  * them to use this tool.</p>
67  *
68  * <p>This tool is safe (and optimized) for use in the application
69  * scope of a servlet environment.</p>
70  *
71  * @author <a HREF="mailto:nathan@esha.com">Nathan Bubna</a>
72  * @version $Revision: 1.6 $ $Date: 2004/02/18 20:11:07 $
73  */

74
75 public class RenderTool
76 {
77
78     /**
79      * Constructs a new instance
80      */

81     public RenderTool()
82     {}
83
84     private static final String JavaDoc LOG_TAG = "RenderTool.eval()";
85
86     /**
87      * <p>Evaluates a String containing VTL using the current context,
88      * and returns the result as a String. If this fails, then
89      * <code>null</code> will be returned. This evaluation is not
90      * recursive.</p>
91      *
92      * @param ctx the current Context
93      * @param vtl the code to be evaluated
94      * @return the evaluated code as a String
95      */

96     public String JavaDoc eval(Context ctx, String JavaDoc vtl) throws Exception JavaDoc
97     {
98         StringWriter JavaDoc sw = new StringWriter JavaDoc();
99         boolean success = Velocity.evaluate(ctx, sw, LOG_TAG, vtl);
100         if (success)
101         {
102             return sw.toString();
103         }
104         /* or would it be preferable to return the original? */
105         return null;
106     }
107
108     /**
109      * <p>Recursively evaluates a String containing VTL using the
110      * current context, and returns the result as a String. It
111      * will continue to re-evaluate the output of the last
112      * evaluation until an evaluation returns the same code
113      * that was fed into it.</p>
114      *
115      * FIXME? add a parse-depth to prevent infinite recursion?
116      *
117      * @param ctx the current Context
118      * @param vtl the code to be evaluated
119      * @return the evaluated code as a String
120      */

121     public String JavaDoc recurse(Context ctx, String JavaDoc vtl) throws Exception JavaDoc
122     {
123         String JavaDoc result = eval(ctx, vtl);
124         if (result.equals(vtl))
125         {
126             return result;
127         }
128         else
129         {
130             return recurse(ctx, result);
131         }
132     }
133
134 }
135
Popular Tags