KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > modules > apache > bsf > BsfSubTemplateEngine


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

5 package xdoclet.modules.apache.bsf;
6
7 import java.io.ByteArrayOutputStream JavaDoc;
8 import java.io.PrintStream JavaDoc;
9
10 import java.util.Properties JavaDoc;
11 import org.apache.bsf.BSFEngine;
12 import org.apache.bsf.BSFException;
13 import org.apache.bsf.BSFManager;
14 import org.apache.bsf.util.ObjectRegistry;
15 import org.apache.commons.logging.Log;
16 import xdoclet.XDocletException;
17
18 import xdoclet.modules.apache.SubTemplateEngine;
19 import xdoclet.util.LogUtil;
20
21 /**
22  * Template engine uses a BSF script to generate output
23  *
24  * @author zluspai
25  * @created July 16, 2003
26  */

27 public class BsfSubTemplateEngine implements SubTemplateEngine
28 {
29     /*
30      * The scripts share the same namespaces, and should use the
31      * bsf.lookupBean() to access the objects. The generated script should be written to the 'out' PrintStream. An example
32      * to use it: <pre>
33      * <XDtTemplateEngines:BSFGenerator scriptengine="javascript" >
34      * out = bsf.lookupBean ("out");
35      * out.println ("Hello World from BSF script!");
36      * print out the methods of the current class
37      * currentClass = bsf.lookupBean ("currentClass");
38      * out.println ("Current class:"+currentClass.getName());
39      * out.println ("Methods are:");
40      * imethods = currentClass.getMethods().iterator();
41      * while (imethods.hasNext()) {
42      * method = imethods.next();
43      * out.println (" "+method.getName());
44      * }
45      * </XDtTemplateEngines:BSFGenerator> </pre>
46      */

47     private BSFManager bsfManager;
48
49
50     public BsfSubTemplateEngine()
51     {
52         Log log = LogUtil.getLog(BsfEngineTagHandler.class, "ctor");
53
54         try {
55             log.debug("Creating BSFManager()");
56             bsfManager = new BSFManager();
57         }
58         catch (Exception JavaDoc ex) {
59             log.error("Exception when creating BSFManager", ex);
60         }
61     }
62
63     /*
64      * (non-Javadoc)
65      * @see xdoclet.templateutil.SubTemplateEngine#getVariable(java.lang.String)
66      */

67     public Object JavaDoc getVariable(String JavaDoc name)
68     {
69         ObjectRegistry objreg = bsfManager.getObjectRegistry();
70
71         return objreg.lookup(name);
72     }
73
74     /*
75      * (non-Javadoc)
76      * @see xdoclet.templateutil.SubTemplateEngine#setVariable(java.lang.String, java.lang.Object)
77      */

78     public void setVariable(String JavaDoc name, Object JavaDoc value)
79     {
80         // can't store null value in BSF registry
81
if (value != null) {
82             ObjectRegistry objreg = bsfManager.getObjectRegistry();
83
84             objreg.register(name, value);
85         }
86     }
87
88     /*
89      * (non-Javadoc)
90      * @see xdoclet.templateutil.SubTemplateEngine#clearVariables()
91      */

92     public void clearVariables()
93     {
94         bsfManager.setObjectRegistry(new ObjectRegistry());
95     }
96
97     /*
98      * (non-Javadoc)
99      * @see xdoclet.templateutil.SubTemplateEngine#generate(java.lang.String, java.util.Properties)
100      */

101     public String JavaDoc generate(String JavaDoc template, Properties JavaDoc attributes) throws XDocletException
102     {
103         Log log = LogUtil.getLog(BsfEngineTagHandler.class, "generate");
104
105         log.debug("generate() called with attributes:" + attributes);
106
107         String JavaDoc scriptingEngine = attributes.getProperty("scriptengine");
108
109         if (scriptingEngine == null) {
110             throw new XDocletException("Missing attribute 'scriptengine' specifying BSF script language");
111         }
112
113         // create 'out' object
114
ByteArrayOutputStream JavaDoc bbuf = new ByteArrayOutputStream JavaDoc();
115         PrintStream JavaDoc out = new PrintStream JavaDoc(bbuf);
116
117         try {
118             BSFEngine bsfEngine = bsfManager.loadScriptingEngine(scriptingEngine);
119
120             bsfManager.getObjectRegistry().register("out", out);
121             bsfEngine.exec("", 0, 0, template);
122
123         }
124         catch (BSFException e) {
125             throw new XDocletException(e, "Exception when running scriptengine='" + scriptingEngine + "'");
126         }
127         out.flush();
128         return bbuf.toString();
129     }
130
131 }
132
Popular Tags