1 17 18 package org.apache.jasper.compiler; 19 20 import java.util.*; 21 import java.io.*; 22 import javax.servlet.ServletContext ; 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 34 35 public class TagPluginManager { 36 37 private static final String TAG_PLUGINS_XML = "/WEB-INF/tagPlugins.xml"; 38 private static final String TAG_PLUGINS_ROOT_ELEM = "tag-plugins"; 39 40 private boolean initialized = false; 41 private HashMap tagPlugins = null; 42 private ServletContext ctxt; 43 private PageInfo pageInfo; 44 45 public TagPluginManager(ServletContext 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 return; 96 } 97 String tagClass = tagClassNode.getBody().trim(); 98 TreeNode pluginClassNode = pluginNode.findChild("plugin-class"); 99 if (pluginClassNode == null) { 100 return; 102 } 103 104 String pluginClassStr = pluginClassNode.getBody(); 105 TagPlugin tagPlugin = null; 106 try { 107 Class pluginClass = Class.forName(pluginClassStr); 108 tagPlugin = (TagPlugin) pluginClass.newInstance(); 109 } catch (Exception 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 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 key, Object value) { 164 pluginAttributes.put(key, value); 165 } 166 167 public Object getPluginAttribute(String key) { 168 return pluginAttributes.get(key); 169 } 170 171 public boolean isScriptless() { 172 return node.getChildInfo().isScriptless(); 173 } 174 175 public boolean isConstantAttribute(String attribute) { 176 Node.JspAttribute attr = getNodeAttribute(attribute); 177 if (attr == null) 178 return false; 179 return attr.isLiteral(); 180 } 181 182 public String getConstantAttribute(String 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 attribute) { 190 return getNodeAttribute(attribute) != null; 191 } 192 193 public String getTemporaryVariableName() { 194 return JspUtil.nextTemporaryVariableName(); 195 } 196 197 public void generateImport(String imp) { 198 pageInfo.addImport(imp); 199 } 200 201 public void generateDeclaration(String id, String 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 sourceCode) { 209 curNodes.add(new Node.Scriptlet(sourceCode, node.getStart(), 210 null)); 211 } 212 213 public void generateAttribute(String 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 curNodes = node.getAtETag(); 228 } 229 230 private Node.JspAttribute getNodeAttribute(String 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 |