KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > modules > apache > ScriptEngineTagHandler


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

5 package xdoclet.modules.apache;
6
7 import java.util.Properties JavaDoc;
8 import org.apache.commons.logging.Log;
9 import xdoclet.XDocletException;
10
11 import xdoclet.tagshandler.AbstractProgramElementTagsHandler;
12 import xdoclet.template.TemplateEngine;
13 import xdoclet.template.TemplateException;
14 import xdoclet.util.LogUtil;
15
16 /**
17  * @created July 17, 2003
18  */

19 public abstract class ScriptEngineTagHandler extends AbstractProgramElementTagsHandler
20 {
21     // XDOCLET subtemplate section start tag inside the other template
22
protected final String JavaDoc XDTSectionStart = "<XDt>";
23     // subtemplate end tag
24
protected final String JavaDoc XDTSectionEnd = "</XDt>";
25
26     /**
27      * Get a subtemplate variable
28      *
29      * @param subengine The subtemplate engine
30      * @param attributes The attributes from XDOCLET tag
31      * @return The value
32      * @throws XDocletException
33      */

34     protected final String JavaDoc getSubTemplateVariable(SubTemplateEngine subengine, Properties JavaDoc attributes) throws XDocletException
35     {
36         String JavaDoc vname = attributes.getProperty("name");
37
38         if (vname == null) {
39             throw new XDocletException("Missing name property the name of the Velocity variable!");
40         }
41
42         Object JavaDoc value = subengine.getVariable(vname);
43
44         if (value == null) {
45             String JavaDoc defaultValue = attributes.getProperty("default", "");
46
47             return defaultValue;
48         }
49         return value.toString();
50     }
51
52     /**
53      * @param subengine The SubTemplateEngine used to generate
54      * @param template The body of the block tag
55      * @param attributes The attributes of the template tag
56      * @exception TemplateException
57      * @throws xdoclet.template.TemplateException
58      */

59     protected final void generate(SubTemplateEngine subengine, String JavaDoc template, Properties JavaDoc attributes) throws TemplateException
60     {
61         if ("yes".equalsIgnoreCase(attributes.getProperty("disable"))) {
62             return;
63         }
64
65         fillVariables(subengine);
66
67         String JavaDoc result = subengine.generate(template, attributes);
68         Log log = LogUtil.getLog(ScriptEngineTagHandler.class, "generate");
69
70         log.debug("Subengine generated results:" + result);
71
72         StringBuffer JavaDoc output = new StringBuffer JavaDoc(result);
73
74         // send back the XDOCLET engine the velocity output, but only when silent="true" is not set
75
if (!"yes".equalsIgnoreCase(attributes.getProperty("silent"))) {
76             TemplateEngine engine = getEngine();
77
78             escapeResults(engine, output);
79             engine.print(output.toString());
80         }
81     }
82
83
84     /**
85      * Fill the variables passed to the engines
86      *
87      * @param templateEngine
88      * @exception XDocletException
89      */

90     protected final void fillVariables(SubTemplateEngine templateEngine) throws XDocletException
91     {
92         Log log = LogUtil.getLog(ScriptEngineTagHandler.class, "fillVariables");
93
94         log.debug("fillVariables() called");
95         try {
96             templateEngine.setVariable("tagHandler", this);
97             templateEngine.setVariable("currentPackage", getCurrentPackage());
98             templateEngine.setVariable("currentClass", getCurrentClass());
99             templateEngine.setVariable("currentMethod", getCurrentMethod());
100             templateEngine.setVariable("currentConstructor", getCurrentConstructor());
101             templateEngine.setVariable("currentField", getCurrentField());
102             templateEngine.setVariable("currentClassTag", getCurrentClassTag());
103             templateEngine.setVariable("currentFieldTag", getCurrentFieldTag());
104             templateEngine.setVariable("currentMethodTag", getCurrentMethodTag());
105         }
106         catch (Exception JavaDoc ex) {
107             log.error("Exception when setting variables", ex);
108             throw new XDocletException(ex, "Exception when setting variables");
109         }
110     }
111
112     /**
113      * Escape and evaluate the <XDt></XDt> sections with XDOCLET template engine. This allows embedding XDOCLET sections
114      * into Velocity sections
115      *
116      * @param engine The XDOCLET template engine
117      * @param results The results
118      * @exception TemplateException
119      */

120     protected final void escapeResults(TemplateEngine engine, StringBuffer JavaDoc results) throws TemplateException
121     {
122         Log log = LogUtil.getLog(ScriptEngineTagHandler.class, "escapeResults");
123
124         // will return when no more subtemplate found
125
while (true) {
126             // find the <XDt> sections
127
int startidx = results.indexOf(XDTSectionStart);
128             int endidx = results.indexOf(XDTSectionEnd);
129
130             boolean templatefound = (startidx >= 0 && endidx >= 0);
131
132             if (!templatefound)
133                 return;
134
135             // extract the content of the <XDt> section
136
String JavaDoc subXDtTemplate = results.substring(startidx + XDTSectionStart.length(), endidx);
137
138             log.debug("subTemplate found:<" + subXDtTemplate + ">");
139
140             // remove the <XDt> section
141
results.delete(startidx, endidx + XDTSectionEnd.length());
142             log.debug("Results after XDt section removed:<" + results.toString() + ">");
143
144             // generate the subtemplate by calling the XDOCLET engine
145
String JavaDoc subTemplateOutput = engine.outputOf(subXDtTemplate);
146
147             log.debug("Generated subTempalte output:" + subTemplateOutput);
148
149             // write back the result
150
results.insert(startidx, subTemplateOutput);
151         }
152     }
153 }
154
Popular Tags