KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2003-2004 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;
19
20
21 import java.io.InputStream JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import org.apache.commons.digester.Digester;
29 import org.apache.commons.digester.RuleSet;
30 import org.apache.velocity.app.Velocity;
31 import org.apache.velocity.tools.view.ToolboxRuleSet;
32 import org.apache.velocity.tools.view.context.ToolboxContext;
33
34
35 /**
36  * A ToolboxManager for loading a toolbox from xml.
37  *
38  * <p>A toolbox manager is responsible for automatically filling the Velocity
39  * context with a set of view tools. This class provides the following
40  * features:</p>
41  * <ul>
42  * <li>configurable through an XML-based configuration file</li>
43  * <li>assembles a set of view tools (the toolbox) on request</li>
44  * <li>supports any class with a public constructor without parameters
45  * to be used as a view tool</li>
46  * <li>supports adding primitive data values to the context(String,Number,Boolean)</li>
47  * </ul>
48  *
49  *
50  * <p><strong>Configuration</strong></p>
51  * <p>The toolbox manager is configured through an XML-based configuration
52  * file. The configuration file is passed to the {@link #load(java.io.InputStream input)}
53  * method. The format is shown in the following example:</p>
54  * <pre>
55  * &lt;?xml version="1.0"?&gt;
56  * &lt;toolbox&gt;
57  * &lt;tool&gt;
58  * &lt;key&gt;date&lt;/key&gt;
59  * &lt;class&gt;org.apache.velocity.tools.generic.DateTool&lt;/class&gt;
60  * &lt;/tool&gt;
61  * &lt;data type="Number"&gt;
62  * &lt;key&gt;luckynumber&lt;/key&gt;
63  * &lt;value&gt;1.37&lt;/value&gt;
64  * &lt;/data&gt;
65  * &lt;data type="String"&gt;
66  * &lt;key&gt;greeting&lt;/key&gt;
67  * &lt;value&gt;Hello World!&lt;/value&gt;
68  * &lt;/data&gt;
69  * &lt;/toolbox&gt;
70  * </pre>
71  *
72  *
73  * @author <a HREF="mailto:nathan@esha.com">Nathan Bubna</a>
74  * @author <a HREF="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
75  *
76  * @version $Id: XMLToolboxManager.java,v 1.9 2004/02/18 20:08:29 nbubna Exp $
77  */

78 public class XMLToolboxManager implements ToolboxManager
79 {
80
81     private List JavaDoc toolinfo;
82     private Map JavaDoc toolbox;
83
84     private static RuleSet ruleSet = new ToolboxRuleSet();
85
86
87     /**
88      * Default constructor
89      */

90     public XMLToolboxManager()
91     {
92         toolinfo = new ArrayList JavaDoc();
93         toolbox = new HashMap JavaDoc();
94     }
95
96
97     // ------------------------------- ToolboxManager interface ------------
98

99     public void addTool(ToolInfo info)
100     {
101         toolinfo.add(info);
102         Velocity.info("Added "+info.getKey()+" ("+info.getClassname()+") to the toolbox.");
103     }
104
105
106     public ToolboxContext getToolboxContext(Object JavaDoc initData)
107     {
108         Iterator JavaDoc i = toolinfo.iterator();
109         while(i.hasNext())
110         {
111             ToolInfo info = (ToolInfo)i.next();
112             toolbox.put(info.getKey(), info.getInstance(initData));
113         }
114
115         return new ToolboxContext(toolbox);
116     }
117
118
119     // ------------------------------- toolbox loading methods ------------
120

121     /**
122      * <p>Reads an XML document from an {@link InputStream}
123      * and sets up the toolbox from that.</p>
124      *
125      * The DTD for toolbox schema is:
126      * <pre>
127      * &lt;?xml version="1.0"?&gt;
128      * &lt;!ELEMENT toolbox (tool*,data*)&gt;
129      * &lt;!ELEMENT tool (key,class,#PCDATA)&gt;
130      * &lt;!ELEMENT data (key,value)&gt;
131      * &lt;!ATTLIST data type (string|number|boolean) "string"&gt;
132      * &lt;!ELEMENT key (#CDATA)&gt;
133      * &lt;!ELEMENT class (#CDATA)&gt;
134      * &lt;!ELEMENT value (#CDATA)&gt;
135      * </pre>
136      *
137      * @param input the InputStream to read from
138      */

139     public void load(InputStream JavaDoc input) throws Exception JavaDoc
140     {
141         Velocity.debug("XMLToolboxManager: Loading toolbox...");
142
143         Digester digester = new Digester();
144         digester.setValidating(false);
145         digester.setUseContextClassLoader(true);
146         digester.push(this);
147         digester.addRuleSet(getRuleSet());
148         digester.parse(input);
149
150         Velocity.debug("XMLToolboxManager: Toolbox loaded.");
151     }
152
153
154     /**
155      * For subclassing convienence.
156      *
157      * @since VelocityTools 1.1
158      */

159     protected RuleSet getRuleSet()
160     {
161         return ruleSet;
162     }
163
164 }
165
Popular Tags