You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by jb...@apache.org on 2013/11/17 02:31:56 UTC
svn commit: r1542630 - in /tomcat/trunk/java/org/apache:
jasper/compiler/TagPluginManager.java tomcat/util/descriptor/tagplugin/
tomcat/util/descriptor/tagplugin/TagPluginParser.java
Author: jboynes
Date: Sun Nov 17 01:31:56 2013
New Revision: 1542630
URL: http://svn.apache.org/r1542630
Log:
Refactor TagPluginManager to use Digester
The split in packages is due to import restrictions between o.a.tomcat and o.a.jasper
Added:
tomcat/trunk/java/org/apache/tomcat/util/descriptor/tagplugin/
tomcat/trunk/java/org/apache/tomcat/util/descriptor/tagplugin/TagPluginParser.java (with props)
Modified:
tomcat/trunk/java/org/apache/jasper/compiler/TagPluginManager.java
Modified: tomcat/trunk/java/org/apache/jasper/compiler/TagPluginManager.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/jasper/compiler/TagPluginManager.java?rev=1542630&r1=1542629&r2=1542630&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/jasper/compiler/TagPluginManager.java (original)
+++ tomcat/trunk/java/org/apache/jasper/compiler/TagPluginManager.java Sun Nov 17 01:31:56 2013
@@ -17,22 +17,22 @@
package org.apache.jasper.compiler;
import java.io.IOException;
-import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.HashMap;
-import java.util.Iterator;
+import java.util.Map;
import javax.servlet.ServletContext;
import org.apache.jasper.JasperException;
import org.apache.jasper.compiler.tagplugin.TagPlugin;
import org.apache.jasper.compiler.tagplugin.TagPluginContext;
-import org.apache.jasper.xmlparser.ParserUtils;
-import org.apache.jasper.xmlparser.TreeNode;
+import org.apache.tomcat.util.descriptor.tagplugin.TagPluginParser;
+import org.xml.sax.SAXException;
/**
* Manages tag plugin optimizations.
+ *
* @author Kin-man Chung
*/
public class TagPluginManager {
@@ -40,11 +40,9 @@ public class TagPluginManager {
private static final String META_INF_JASPER_TAG_PLUGINS_XML =
"META-INF/org.apache.jasper/tagPlugins.xml";
private static final String TAG_PLUGINS_XML = "/WEB-INF/tagPlugins.xml";
- private static final String TAG_PLUGINS_ROOT_ELEM = "tag-plugins";
-
- private boolean initialized = false;
- private HashMap<String, TagPlugin> tagPlugins = null;
private final ServletContext ctxt;
+ private HashMap<String, TagPlugin> tagPlugins;
+ private boolean initialized = false;
public TagPluginManager(ServletContext ctxt) {
this.ctxt = ctxt;
@@ -54,93 +52,47 @@ public class TagPluginManager {
throws JasperException {
init(err);
- if (tagPlugins == null || tagPlugins.size() == 0) {
- return;
+ if (!tagPlugins.isEmpty()) {
+ page.visit(new NodeVisitor(this, pageInfo));
}
-
- page.visit(new NodeVisitor(this, pageInfo));
}
private void init(ErrorDispatcher err) throws JasperException {
if (initialized)
return;
- tagPlugins = new HashMap<>();
+ TagPluginParser parser = new TagPluginParser(ctxt);
- Enumeration<URL> urls = null;
try {
- urls = ctxt.getClassLoader().getResources(
- META_INF_JASPER_TAG_PLUGINS_XML);
- } catch (IOException ioe) {
- throw new JasperException(ioe);
- }
-
- if (urls != null) {
- while(urls.hasMoreElements()) {
- URL url = urls.nextElement();
- try (InputStream is = url.openStream()){
- loadTagPlugins(err, is);
- } catch(IOException ioe) {
- throw new JasperException(ioe);
+ Enumeration<URL> urls =
+ ctxt.getClassLoader().getResources(META_INF_JASPER_TAG_PLUGINS_XML);
+ if (urls != null) {
+ while (urls.hasMoreElements()) {
+ URL url = urls.nextElement();
+ parser.parse(url);
}
}
- }
- try (InputStream is = ctxt.getResourceAsStream(TAG_PLUGINS_XML)) {
- if (is != null) {
- loadTagPlugins(err, is);
+ URL url = ctxt.getResource(TAG_PLUGINS_XML);
+ if (url != null) {
+ parser.parse(url);
}
- } catch (IOException ioe) {
- throw new JasperException(ioe);
- }
-
- initialized = true;
- }
-
-
- private void loadTagPlugins(ErrorDispatcher err, InputStream is)
- throws JasperException {
-
- TreeNode root =
- (new ParserUtils()).parseXMLDocument(TAG_PLUGINS_XML, is);
- if (root == null) {
- return;
- }
-
- if (!TAG_PLUGINS_ROOT_ELEM.equals(root.getName())) {
- err.jspError("jsp.error.plugin.wrongRootElement", TAG_PLUGINS_XML,
- TAG_PLUGINS_ROOT_ELEM);
+ } catch (IOException | SAXException e) {
+ throw new JasperException(e);
}
- tagPlugins = new HashMap<>();
- Iterator<TreeNode> pluginList = root.findChildren("tag-plugin");
- while (pluginList.hasNext()) {
- TreeNode pluginNode = pluginList.next();
- TreeNode tagClassNode = pluginNode.findChild("tag-class");
- if (tagClassNode == null) {
- // Error
- return;
- }
- String tagClass = tagClassNode.getBody().trim();
- TreeNode pluginClassNode = pluginNode.findChild("plugin-class");
- if (pluginClassNode == null) {
- // Error
- return;
- }
-
- String pluginClassStr = pluginClassNode.getBody();
- TagPlugin tagPlugin = null;
+ Map<String, String> plugins = parser.getPlugins();
+ tagPlugins = new HashMap<>(plugins.size());
+ for (Map.Entry<String, String> entry : plugins.entrySet()) {
try {
- Class<?> pluginClass =
- ctxt.getClassLoader().loadClass(pluginClassStr);
- tagPlugin = (TagPlugin) pluginClass.newInstance();
+ String tagClass = entry.getKey();
+ String pluginName = entry.getValue();
+ Class<?> pluginClass = ctxt.getClassLoader().loadClass(pluginName);
+ TagPlugin plugin = (TagPlugin) pluginClass.newInstance();
+ tagPlugins.put(tagClass, plugin);
} catch (Exception e) {
- throw new JasperException(e);
- }
- if (tagPlugin == null) {
- return;
+ err.jspError(e);
}
- tagPlugins.put(tagClass, tagPlugin);
}
initialized = true;
}
@@ -148,7 +100,7 @@ public class TagPluginManager {
/**
* Invoke tag plugin for the given custom tag, if a plugin exists for
* the custom tag's tag handler.
- *
+ * <p/>
* The given custom tag node will be manipulated by the plugin.
*/
private void invokePlugin(Node.CustomTag n, PageInfo pageInfo) {
@@ -180,9 +132,9 @@ public class TagPluginManager {
private static class TagPluginContextImpl implements TagPluginContext {
private final Node.CustomTag node;
- private Node.Nodes curNodes;
private final PageInfo pageInfo;
private final HashMap<String, Object> pluginAttributes;
+ private Node.Nodes curNodes;
TagPluginContextImpl(Node.CustomTag n, PageInfo pageInfo) {
this.node = n;
@@ -198,7 +150,7 @@ public class TagPluginManager {
@Override
public TagPluginContext getParentContext() {
Node parent = node.getParent();
- if (! (parent instanceof Node.CustomTag)) {
+ if (!(parent instanceof Node.CustomTag)) {
return null;
}
return ((Node.CustomTag) parent).getTagPluginContext();
@@ -261,14 +213,14 @@ public class TagPluginManager {
@Override
public void generateJavaSource(String sourceCode) {
curNodes.add(new Node.Scriptlet(sourceCode, node.getStart(),
- null));
+ null));
}
@Override
public void generateAttribute(String attributeName) {
curNodes.add(new Node.AttributeGenerator(node.getStart(),
- attributeName,
- node));
+ attributeName,
+ node));
}
@Override
@@ -291,7 +243,7 @@ public class TagPluginManager {
private Node.JspAttribute getNodeAttribute(String attribute) {
Node.JspAttribute[] attrs = node.getJspAttributes();
- for (int i=0; attrs != null && i < attrs.length; i++) {
+ for (int i = 0; attrs != null && i < attrs.length; i++) {
if (attrs[i].getName().equals(attribute)) {
return attrs[i];
}
@@ -299,4 +251,6 @@ public class TagPluginManager {
return null;
}
}
+
}
+
Added: tomcat/trunk/java/org/apache/tomcat/util/descriptor/tagplugin/TagPluginParser.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/descriptor/tagplugin/TagPluginParser.java?rev=1542630&view=auto
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/util/descriptor/tagplugin/TagPluginParser.java (added)
+++ tomcat/trunk/java/org/apache/tomcat/util/descriptor/tagplugin/TagPluginParser.java Sun Nov 17 01:31:56 2013
@@ -0,0 +1,88 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.tomcat.util.descriptor.tagplugin;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.servlet.ServletContext;
+
+import org.apache.juli.logging.Log;
+import org.apache.juli.logging.LogFactory;
+import org.apache.tomcat.util.descriptor.DigesterFactory;
+import org.apache.tomcat.util.descriptor.XmlErrorHandler;
+import org.apache.tomcat.util.digester.Digester;
+import org.apache.tomcat.util.digester.RuleSetBase;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+
+/**
+ * Parser for Tag Plugin descriptors.
+ */
+public class TagPluginParser {
+ private static final Log LOG = LogFactory.getLog(TagPluginParser.class);
+ private static final String PREFIX = "tag-plugins/tag-plugin";
+ private final Digester digester;
+ private final Map<String, String> plugins = new HashMap<>();
+
+ public TagPluginParser(ServletContext context) {
+ digester = DigesterFactory.newDigester(false, false, new TagPluginRuleSet());
+ digester.setClassLoader(context.getClassLoader());
+ }
+
+ public void parse(URL url) throws IOException, SAXException {
+ try (InputStream is = url.openStream()) {
+ XmlErrorHandler handler = new XmlErrorHandler();
+ digester.setErrorHandler(handler);
+
+ digester.push(this);
+
+ InputSource source = new InputSource(url.toExternalForm());
+ source.setByteStream(is);
+ digester.parse(source);
+ if (!handler.getWarnings().isEmpty() || !handler.getErrors().isEmpty()) {
+ handler.logFindings(LOG, source.getSystemId());
+ if (!handler.getErrors().isEmpty()) {
+ // throw the first to indicate there was a error during processing
+ throw handler.getErrors().iterator().next();
+ }
+ }
+ } finally {
+ digester.reset();
+ }
+ }
+
+ public void addPlugin(String tagClass, String pluginClass) {
+ plugins.put(tagClass, pluginClass);
+ }
+
+ public Map<String, String> getPlugins() {
+ return plugins;
+ }
+
+ private static class TagPluginRuleSet extends RuleSetBase {
+ @Override
+ public void addRuleInstances(Digester digester) {
+ digester.addCallMethod(PREFIX, "addPlugin", 2);
+ digester.addCallParam(PREFIX + "/tag-class", 0);
+ digester.addCallParam(PREFIX + "/plugin-class", 1);
+ }
+ }
+}
Propchange: tomcat/trunk/java/org/apache/tomcat/util/descriptor/tagplugin/TagPluginParser.java
------------------------------------------------------------------------------
svn:eol-style = native
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org