KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > modules > apache > velocity > VelocityEngineTagHandler


1 /*
2  * Copyright (c) 2001, 2002 The XDoclet team
3  * All rights reserved.
4  */

5 package xdoclet.modules.apache.velocity;
6
7 import java.util.Properties JavaDoc;
8
9 import xdoclet.XDocletException;
10 import xdoclet.modules.apache.ScriptEngineTagHandler;
11 import xdoclet.template.TemplateException;
12
13
14 /**
15  * This is a tag handler able to execute Velocity template engine on a block!
16  *
17  * @author zluspai
18  * @created July 16, 2003
19  * @xdoclet.taghandler namespace="Velocity"
20  */

21 public class VelocityEngineTagHandler extends ScriptEngineTagHandler
22 {
23     /*
24      * @todo - put this section in the above docs, but needs care so the reformatter doesn't mangle it
25      * <XDtTemplateEngines:generator>
26      * currentClass is: ${currentClass.Name}
27      * Methods are: #set ( $numMethods = 0 )
28      * #foreach ($method in ${currentClass.methods})
29      * $method.Name #set ( $numMethods = $numMethods+1 )
30      * #end </XDtTemplateEngines:generator>
31      * And you can get a variable from the last run Velocity context by using this XDoclet tag:
32      * <XDtTemplateEngines:getVelocityVariable name="numMethods" />
33      * Also if the result of the template contains a special <XDt></XDt> section that will be merged by the XDoclet
34      * engine. For example:
35      * <XDtTemplateEngines:generator>
36      * Print out the methods using the XDoclet Templates:
37      * <XDt> <XDtMethod:forAllMethods> <XDtMethod:methodName/> </XDtMethod:forAllMethods> </XDt>
38      * </XDtTemplateEngines:generator>
39      */

40     private VelocitySubTemplateEngine velocityEngine;
41
42
43     /**
44      * Get a value of a velocity variable from the context <pre>
45      * <XDtTemplateEngines:getVelocityVariable name="numMethods" default="0" />
46      * </pre>
47      *
48      * @param attributes
49      * @return
50      * @exception XDocletException
51      * @doc.tag type="content"
52      */

53     public String JavaDoc getVariable(Properties JavaDoc attributes) throws XDocletException
54     {
55         return getSubTemplateVariable(getVelocityEngine(), attributes);
56     }
57
58     /**
59      * Evaluates the body block with the Velocity template engine If the silent="yes" attribute is set then the
60      * Generator will not produce any output, but the template will run. If the disable="yes" attribute is set then the
61      * Velocity template will not run at all.
62      *
63      * @param template The body of the block tag
64      * @param attributes The attributes of the template tag
65      * @exception TemplateException
66      * @doc.tag type="block"
67      */

68     public void generator(String JavaDoc template, Properties JavaDoc attributes) throws TemplateException
69     {
70         generate(getVelocityEngine(), template, attributes);
71     }
72
73     /**
74      * Clear all velocity variables
75      *
76      * @doc.tag type="content"
77      * @throws XDocletException
78      */

79     public void clearVariables() throws XDocletException
80     {
81         getVelocityEngine().clearVariables();
82     }
83
84     private VelocitySubTemplateEngine getVelocityEngine()
85     {
86         if (velocityEngine == null) {
87             velocityEngine = new VelocitySubTemplateEngine();
88         }
89         return velocityEngine;
90     }
91 }
92
93
Popular Tags