KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jasper > compiler > TagPluginManager


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.jasper.compiler;
19
20 import java.util.*;
21 import java.io.*;
22 import javax.servlet.ServletContext JavaDoc;
23
24 import org.apache.jasper.JasperException;
25 import org.apache.jasper.xmlparser.ParserUtils;
26 import org.apache.jasper.xmlparser.TreeNode;
27 import org.apache.jasper.compiler.tagplugin.TagPlugin;
28 import org.apache.jasper.compiler.tagplugin.TagPluginContext;
29
30 /**
31  * Manages tag plugin optimizations.
32  * @author Kin-man Chung
33  */

34
35 public class TagPluginManager {
36
37     private static final String JavaDoc TAG_PLUGINS_XML = "/WEB-INF/tagPlugins.xml";
38     private static final String JavaDoc TAG_PLUGINS_ROOT_ELEM = "tag-plugins";
39
40     private boolean initialized = false;
41     private HashMap tagPlugins = null;
42     private ServletContext JavaDoc ctxt;
43     private PageInfo pageInfo;
44
45     public TagPluginManager(ServletContext JavaDoc ctxt) {
46     this.ctxt = ctxt;
47     }
48
49     public void apply(Node.Nodes page, ErrorDispatcher err, PageInfo pageInfo)
50         throws JasperException {
51
52     init(err);
53     if (tagPlugins == null || tagPlugins.size() == 0) {
54         return;
55     }
56
57     this.pageInfo = pageInfo;
58
59         page.visit(new Node.Visitor() {
60             public void visit(Node.CustomTag n)
61                     throws JasperException {
62                 invokePlugin(n);
63                 visitBody(n);
64             }
65         });
66
67     }
68  
69     private void init(ErrorDispatcher err) throws JasperException {
70     if (initialized)
71         return;
72
73     InputStream is = ctxt.getResourceAsStream(TAG_PLUGINS_XML);
74     if (is == null)
75         return;
76
77     TreeNode root = (new ParserUtils()).parseXMLDocument(TAG_PLUGINS_XML,
78                                  is);
79     if (root == null) {
80         return;
81     }
82
83     if (!TAG_PLUGINS_ROOT_ELEM.equals(root.getName())) {
84         err.jspError("jsp.error.plugin.wrongRootElement", TAG_PLUGINS_XML,
85              TAG_PLUGINS_ROOT_ELEM);
86     }
87
88     tagPlugins = new HashMap();
89     Iterator pluginList = root.findChildren("tag-plugin");
90     while (pluginList.hasNext()) {
91         TreeNode pluginNode = (TreeNode) pluginList.next();
92             TreeNode tagClassNode = pluginNode.findChild("tag-class");
93         if (tagClassNode == null) {
94         // Error
95
return;
96         }
97         String JavaDoc tagClass = tagClassNode.getBody().trim();
98         TreeNode pluginClassNode = pluginNode.findChild("plugin-class");
99         if (pluginClassNode == null) {
100         // Error
101
return;
102         }
103
104         String JavaDoc pluginClassStr = pluginClassNode.getBody();
105         TagPlugin tagPlugin = null;
106         try {
107         Class JavaDoc pluginClass = Class.forName(pluginClassStr);
108         tagPlugin = (TagPlugin) pluginClass.newInstance();
109         } catch (Exception JavaDoc e) {
110         throw new JasperException(e);
111         }
112         if (tagPlugin == null) {
113         return;
114         }
115         tagPlugins.put(tagClass, tagPlugin);
116     }
117     initialized = true;
118     }
119
120     /**
121      * Invoke tag plugin for the given custom tag, if a plugin exists for
122      * the custom tag's tag handler.
123      *
124      * The given custom tag node will be manipulated by the plugin.
125      */

126     private void invokePlugin(Node.CustomTag n) {
127     TagPlugin tagPlugin = (TagPlugin)
128         tagPlugins.get(n.getTagHandlerClass().getName());
129     if (tagPlugin == null) {
130         return;
131     }
132
133     TagPluginContext tagPluginContext = new TagPluginContextImpl(n, pageInfo);
134     n.setTagPluginContext(tagPluginContext);
135     tagPlugin.doTag(tagPluginContext);
136     }
137
138     static class TagPluginContextImpl implements TagPluginContext {
139     private Node.CustomTag node;
140     private Node.Nodes curNodes;
141     private PageInfo pageInfo;
142     private HashMap pluginAttributes;
143
144     TagPluginContextImpl(Node.CustomTag n, PageInfo pageInfo) {
145         this.node = n;
146         this.pageInfo = pageInfo;
147         curNodes = new Node.Nodes();
148         n.setAtETag(curNodes);
149         curNodes = new Node.Nodes();
150         n.setAtSTag(curNodes);
151         n.setUseTagPlugin(true);
152         pluginAttributes = new HashMap();
153     }
154
155     public TagPluginContext getParentContext() {
156         Node parent = node.getParent();
157         if (! (parent instanceof Node.CustomTag)) {
158         return null;
159         }
160         return ((Node.CustomTag) parent).getTagPluginContext();
161     }
162
163     public void setPluginAttribute(String JavaDoc key, Object JavaDoc value) {
164         pluginAttributes.put(key, value);
165     }
166
167     public Object JavaDoc getPluginAttribute(String JavaDoc key) {
168         return pluginAttributes.get(key);
169     }
170
171     public boolean isScriptless() {
172         return node.getChildInfo().isScriptless();
173     }
174
175     public boolean isConstantAttribute(String JavaDoc attribute) {
176         Node.JspAttribute attr = getNodeAttribute(attribute);
177         if (attr == null)
178         return false;
179         return attr.isLiteral();
180     }
181
182     public String JavaDoc getConstantAttribute(String JavaDoc attribute) {
183         Node.JspAttribute attr = getNodeAttribute(attribute);
184             if (attr == null)
185         return null;
186         return attr.getValue();
187     }
188
189     public boolean isAttributeSpecified(String JavaDoc attribute) {
190         return getNodeAttribute(attribute) != null;
191     }
192
193     public String JavaDoc getTemporaryVariableName() {
194         return JspUtil.nextTemporaryVariableName();
195     }
196
197     public void generateImport(String JavaDoc imp) {
198         pageInfo.addImport(imp);
199     }
200
201     public void generateDeclaration(String JavaDoc id, String JavaDoc text) {
202         if (pageInfo.isPluginDeclared(id)) {
203         return;
204         }
205         curNodes.add(new Node.Declaration(text, node.getStart(), null));
206     }
207
208     public void generateJavaSource(String JavaDoc sourceCode) {
209         curNodes.add(new Node.Scriptlet(sourceCode, node.getStart(),
210                         null));
211     }
212
213     public void generateAttribute(String JavaDoc attributeName) {
214         curNodes.add(new Node.AttributeGenerator(node.getStart(),
215                              attributeName,
216                              node));
217     }
218
219     public void dontUseTagPlugin() {
220         node.setUseTagPlugin(false);
221     }
222
223     public void generateBody() {
224         // Since we'll generate the body anyway, this is really a nop,
225
// except for the fact that it lets us put the Java sources the
226
// plugins produce in the correct order (w.r.t the body).
227
curNodes = node.getAtETag();
228     }
229
230     private Node.JspAttribute getNodeAttribute(String JavaDoc attribute) {
231         Node.JspAttribute[] attrs = node.getJspAttributes();
232         for (int i=0; attrs != null && i < attrs.length; i++) {
233         if (attrs[i].getName().equals(attribute)) {
234             return attrs[i];
235         }
236         }
237         return null;
238     }
239     }
240 }
241
Popular Tags