Author: jboynes Date: Sat Nov 16 22:47:58 2013 New Revision: 1542606 URL: http://svn.apache.org/r1542606 Log: Use Digester to load implicit.tld taglib descriptors
Added: tomcat/trunk/java/org/apache/tomcat/util/descriptor/tld/ImplicitTldRuleSet.java - copied, changed from r1542562, tomcat/trunk/java/org/apache/tomcat/util/descriptor/tld/TldRuleSet.java Modified: tomcat/trunk/java/org/apache/jasper/compiler/ImplicitTagLibraryInfo.java tomcat/trunk/java/org/apache/tomcat/util/descriptor/tld/TldParser.java Modified: tomcat/trunk/java/org/apache/jasper/compiler/ImplicitTagLibraryInfo.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/jasper/compiler/ImplicitTagLibraryInfo.java?rev=1542606&r1=1542605&r2=1542606&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/jasper/compiler/ImplicitTagLibraryInfo.java (original) +++ tomcat/trunk/java/org/apache/jasper/compiler/ImplicitTagLibraryInfo.java Sat Nov 16 22:47:58 2013 @@ -17,10 +17,10 @@ package org.apache.jasper.compiler; -import java.io.InputStream; +import java.io.IOException; +import java.net.URL; import java.util.Collection; import java.util.Hashtable; -import java.util.Iterator; import java.util.Set; import java.util.Vector; @@ -31,9 +31,11 @@ import javax.servlet.jsp.tagext.TagLibra import org.apache.jasper.JasperException; import org.apache.jasper.JspCompilationContext; -import org.apache.jasper.util.ExceptionUtils; -import org.apache.jasper.xmlparser.ParserUtils; -import org.apache.jasper.xmlparser.TreeNode; +import org.apache.tomcat.util.descriptor.tld.ImplicitTldRuleSet; +import org.apache.tomcat.util.descriptor.tld.TaglibXml; +import org.apache.tomcat.util.descriptor.tld.TldParser; +import org.apache.tomcat.util.descriptor.tld.TldResourcePath; +import org.xml.sax.SAXException; /** * Class responsible for generating an implicit tag library containing tag @@ -97,9 +99,7 @@ class ImplicitTagLibraryInfo extends Tag // Populate mapping of tag names to tag file paths Set<String> dirList = ctxt.getResourcePaths(tagdir); if (dirList != null) { - Iterator<String> it = dirList.iterator(); - while (it.hasNext()) { - String path = it.next(); + for (String path : dirList) { if (path.endsWith(TAG_FILE_SUFFIX) || path.endsWith(TAGX_FILE_SUFFIX)) { /* @@ -114,60 +114,32 @@ class ImplicitTagLibraryInfo extends Tag tagName.lastIndexOf(suffix)); tagFileMap.put(tagName, path); } else if (path.endsWith(IMPLICIT_TLD)) { - InputStream in = null; + TaglibXml taglibXml; try { - in = ctxt.getResourceAsStream(path); - if (in != null) { - - // Add implicit TLD to dependency list - if (pi != null) { - pi.addDependant(path, ctxt.getLastModified(path)); - } - - ParserUtils pu = new ParserUtils(); - TreeNode tld = pu.parseXMLDocument(uri, in); - - if (tld.findAttribute("version") != null) { - this.jspversion = tld.findAttribute("version"); - } - - // Process each child element of our <taglib> element - Iterator<TreeNode> list = tld.findChildren(); - - while (list.hasNext()) { - TreeNode element = list.next(); - String tname = element.getName(); - - if ("tlibversion".equals(tname) // JSP 1.1 - || "tlib-version".equals(tname)) { // JSP 1.2 - this.tlibversion = element.getBody(); - } else if ("jspversion".equals(tname) - || "jsp-version".equals(tname)) { - this.jspversion = element.getBody(); - } else if ("shortname".equals(tname) || "short-name".equals(tname)) { - // Ignore - } else { - // All other elements are invalid - err.jspError("jsp.error.invalid.implicit", path); - } - } - try { - double version = Double.parseDouble(this.jspversion); - if (version < 2.0) { - err.jspError("jsp.error.invalid.implicit.version", path); - } - } catch (NumberFormatException e) { - err.jspError("jsp.error.invalid.implicit.version", path); - } - } - } finally { - if (in != null) { - try { - in.close(); - } catch (Throwable t) { - ExceptionUtils.handleThrowable(t); - } + URL url = ctxt.getResource(path); + TldResourcePath resourcePath = new TldResourcePath(url, path); + // TODO enable validation + TldParser parser = new TldParser(true, false, new ImplicitTldRuleSet()); + taglibXml = parser.parse(resourcePath); + } catch (IOException | SAXException e) { + err.jspError(e); + // unreached + throw new JasperException(e); + } + this.tlibversion = taglibXml.getTlibVersion(); + this.jspversion = taglibXml.getJspVersion(); + try { + double version = Double.parseDouble(this.jspversion); + if (version < 2.0) { + err.jspError("jsp.error.invalid.implicit.version", path); } + } catch (NumberFormatException e) { + err.jspError("jsp.error.invalid.implicit.version", path); + } + + // Add implicit TLD to dependency list + if (pi != null) { + pi.addDependant(path, ctxt.getLastModified(path)); } } } Copied: tomcat/trunk/java/org/apache/tomcat/util/descriptor/tld/ImplicitTldRuleSet.java (from r1542562, tomcat/trunk/java/org/apache/tomcat/util/descriptor/tld/TldRuleSet.java) URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/descriptor/tld/ImplicitTldRuleSet.java?p2=tomcat/trunk/java/org/apache/tomcat/util/descriptor/tld/ImplicitTldRuleSet.java&p1=tomcat/trunk/java/org/apache/tomcat/util/descriptor/tld/TldRuleSet.java&r1=1542562&r2=1542606&rev=1542606&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/tomcat/util/descriptor/tld/TldRuleSet.java (original) +++ tomcat/trunk/java/org/apache/tomcat/util/descriptor/tld/ImplicitTldRuleSet.java Sat Nov 16 22:47:58 2013 @@ -16,26 +16,18 @@ */ package org.apache.tomcat.util.descriptor.tld; -import java.lang.reflect.Method; - -import javax.servlet.jsp.tagext.TagAttributeInfo; -import javax.servlet.jsp.tagext.TagVariableInfo; -import javax.servlet.jsp.tagext.VariableInfo; - import org.apache.tomcat.util.digester.Digester; import org.apache.tomcat.util.digester.Rule; import org.apache.tomcat.util.digester.RuleSetBase; import org.xml.sax.Attributes; /** - * RulesSet for digesting TLD files. + * RulesSet for digesting implicit.tld files. + * + * Only version information used and short names are allowed. */ -public class TldRuleSet extends RuleSetBase { +public class ImplicitTldRuleSet extends RuleSetBase { private static final String PREFIX = "taglib"; - private static final String VALIDATOR_PREFIX = PREFIX + "/validator"; - private static final String TAG_PREFIX = PREFIX + "/tag"; - private static final String TAGFILE_PREFIX = PREFIX + "/tag-file"; - private static final String FUNCTION_PREFIX = PREFIX + "/function"; @Override public void addRuleInstances(Digester digester) { @@ -55,305 +47,5 @@ public class TldRuleSet extends RuleSetB digester.addCallMethod(PREFIX + "/shortname", "setShortName", 0); digester.addCallMethod(PREFIX + "/short-name", "setShortName", 0); - // common rules - digester.addCallMethod(PREFIX + "/uri", "setUri", 0); - digester.addCallMethod(PREFIX + "/info", "setInfo", 0); - digester.addCallMethod(PREFIX + "/description", "setInfo", 0); - digester.addCallMethod(PREFIX + "/listener/listener-class", "addListener", 0); - - // validator - digester.addObjectCreate(VALIDATOR_PREFIX, ValidatorXml.class.getName()); - digester.addCallMethod(VALIDATOR_PREFIX + "/validator-class", "setValidatorClass", 0); - digester.addCallMethod(VALIDATOR_PREFIX + "/init-param", "addInitParam", 2); - digester.addCallParam(VALIDATOR_PREFIX + "/init-param/param-name", 0); - digester.addCallParam(VALIDATOR_PREFIX + "/init-param/param-value", 1); - digester.addSetNext(VALIDATOR_PREFIX, "setValidator", ValidatorXml.class.getName()); - - - // tag - digester.addObjectCreate(TAG_PREFIX, TagXml.class.getName()); - addDescriptionGroup(digester, TAG_PREFIX); - digester.addCallMethod(TAG_PREFIX + "/name", "setName", 0); - digester.addCallMethod(TAG_PREFIX + "/tagclass", "setTagClass", 0); - digester.addCallMethod(TAG_PREFIX + "/tag-class", "setTagClass", 0); - digester.addCallMethod(TAG_PREFIX + "/teiclass", "setTeiClass", 0); - digester.addCallMethod(TAG_PREFIX + "/tei-class", "setTeiClass", 0); - digester.addCallMethod(TAG_PREFIX + "/bodycontent", "setBodyContent", 0); - digester.addCallMethod(TAG_PREFIX + "/body-content", "setBodyContent", 0); - - digester.addRule(TAG_PREFIX + "/variable", new ScriptVariableRule()); - digester.addCallMethod(TAG_PREFIX + "/variable/name-given", "setNameGiven", 0); - digester.addCallMethod(TAG_PREFIX + "/variable/name-from-attribute", - "setNameFromAttribute", 0); - digester.addCallMethod(TAG_PREFIX + "/variable/variable-class", "setClassName", 0); - digester.addRule(TAG_PREFIX + "/variable/declare", - new GenericBooleanRule(Variable.class, "setDeclare")); - digester.addCallMethod(TAG_PREFIX + "/variable/scope", "setScope", 0); - - digester.addRule(TAG_PREFIX + "/attribute", new TagAttributeRule()); - digester.addCallMethod(TAG_PREFIX + "/attribute/description", "setDescription", 0); - digester.addCallMethod(TAG_PREFIX + "/attribute/name", "setName", 0); - digester.addRule(TAG_PREFIX + "/attribute/required", - new GenericBooleanRule(Attribute.class, "setRequired")); - digester.addRule(TAG_PREFIX + "/attribute/rtexprvalue", - new GenericBooleanRule(Attribute.class, "setRequestTime")); - digester.addCallMethod(TAG_PREFIX + "/attribute/type", "setType", 0); - digester.addCallMethod(TAG_PREFIX + "/attribute/deferred-value", "setDeferredValue"); - digester.addCallMethod(TAG_PREFIX + "/attribute/deferred-value/type", - "setExpectedTypeName", 0); - digester.addCallMethod(TAG_PREFIX + "/attribute/deferred-method", "setDeferredMethod"); - digester.addCallMethod(TAG_PREFIX + "/attribute/deferred-method/method-signature", - "setMethodSignature", 0); - digester.addRule(TAG_PREFIX + "/attribute/fragment", - new GenericBooleanRule(Attribute.class, "setFragment")); - - digester.addRule(TAG_PREFIX + "/dynamic-attributes", - new GenericBooleanRule(TagXml.class, "setDynamicAttributes")); - digester.addSetNext(TAG_PREFIX, "addTag", TagXml.class.getName()); - - // tag-file - digester.addObjectCreate(TAGFILE_PREFIX, TagFileXml.class.getName()); - addDescriptionGroup(digester, TAGFILE_PREFIX); - digester.addCallMethod(TAGFILE_PREFIX + "/name", "setName", 0); - digester.addCallMethod(TAGFILE_PREFIX + "/path", "setPath", 0); - digester.addSetNext(TAGFILE_PREFIX, "addTagFile", TagFileXml.class.getName()); - - // function - digester.addCallMethod(FUNCTION_PREFIX, "addFunction", 3); - digester.addCallParam(FUNCTION_PREFIX + "/name", 0); - digester.addCallParam(FUNCTION_PREFIX + "/function-class", 1); - digester.addCallParam(FUNCTION_PREFIX + "/function-signature", 2); - } - - private void addDescriptionGroup(Digester digester, String prefix) { - digester.addCallMethod(prefix + "/info", "setInfo", 0); - digester.addCallMethod(prefix + "small-icon", "setSmallIcon", 0); - digester.addCallMethod(prefix + "large-icon", "setLargeIcon", 0); - - digester.addCallMethod(prefix + "/description", "setInfo", 0); - digester.addCallMethod(prefix + "/display-name", "setDisplayName", 0); - digester.addCallMethod(prefix + "/icon/small-icon", "setSmallIcon", 0); - digester.addCallMethod(prefix + "/icon/large-icon", "setLargeIcon", 0); - } - - private static class TagAttributeRule extends Rule { - @Override - public void begin(String namespace, String name, Attributes attributes) throws Exception { - TaglibXml taglibXml = (TaglibXml) digester.peek(digester.getCount() - 1); - digester.push(new Attribute("1.2".equals(taglibXml.getJspVersion()))); - } - - @Override - public void end(String namespace, String name) throws Exception { - Attribute attribute = (Attribute) digester.pop(); - TagXml tag = (TagXml) digester.peek(); - tag.getAttributes().add(attribute.toTagAttributeInfo()); - } - } - - public static class Attribute { - private final boolean allowShortNames; - private String name; - private boolean required; - private String type; - private boolean requestTime; - private boolean fragment; - private String description; - private boolean deferredValue; - private boolean deferredMethod; - private String expectedTypeName; - private String methodSignature; - - private Attribute(boolean allowShortNames) { - this.allowShortNames = allowShortNames; - } - - public void setName(String name) { - this.name = name; - } - - public void setRequired(boolean required) { - this.required = required; - } - - public void setType(String type) { - if (allowShortNames) { - switch (type) { - case "Boolean": - this.type = "java.lang.Boolean"; - break; - case "Character": - this.type = "java.lang.Character"; - break; - case "Byte": - this.type = "java.lang.Byte"; - break; - case "Short": - this.type = "java.lang.Short"; - break; - case "Integer": - this.type = "java.lang.Integer"; - break; - case "Long": - this.type = "java.lang.Long"; - break; - case "Float": - this.type = "java.lang.Float"; - break; - case "Double": - this.type = "java.lang.Double"; - break; - case "String": - this.type = "java.lang.String"; - break; - case "Object": - this.type = "java.lang.Object"; - break; - default: - this.type = type; - break; - } - } else { - this.type = type; - } - } - - public void setRequestTime(boolean requestTime) { - this.requestTime = requestTime; - } - - public void setFragment(boolean fragment) { - this.fragment = fragment; - } - - public void setDescription(String description) { - this.description = description; - } - - public void setDeferredValue() { - this.deferredValue = true; - } - - public void setDeferredMethod() { - this.deferredMethod = true; - } - - public void setExpectedTypeName(String expectedTypeName) { - this.expectedTypeName = expectedTypeName; - } - - public void setMethodSignature(String methodSignature) { - this.methodSignature = methodSignature; - } - - public TagAttributeInfo toTagAttributeInfo() { - if (fragment) { - // JSP8.5.2: for a fragment type is fixed and rexprvalue is true - type = "javax.servlet.jsp.tagext.JspFragment"; - requestTime = true; - } else if (deferredValue) { - type = "javax.el.ValueExpression"; - if (expectedTypeName == null) { - expectedTypeName = "java.lang.Object"; - } - } else if (deferredMethod) { - type = "javax.el.MethodExpression"; - if (methodSignature == null) { - methodSignature = "java.lang.Object method()"; - } - } - - // According to JSP spec, for static values (those determined at - // translation time) the type is fixed at java.lang.String. - if (!requestTime && type == null) { - type = "java.lang.String"; - } - - return new TagAttributeInfo( - name, - required, - type, - requestTime, - fragment, - description, - deferredValue, - deferredMethod, - expectedTypeName, - methodSignature); - } - } - - private static class ScriptVariableRule extends Rule { - @Override - public void begin(String namespace, String name, Attributes attributes) throws Exception { - digester.push(new Variable()); - } - - @Override - public void end(String namespace, String name) throws Exception { - Variable variable = (Variable) digester.pop(); - TagXml tag = (TagXml) digester.peek(); - tag.getVariables().add(variable.toTagVariableInfo()); - } - } - - public static class Variable { - private String nameGiven; - private String nameFromAttribute; - private String className = "java.lang.String"; - private boolean declare; - private int scope = VariableInfo.NESTED; - - public void setNameGiven(String nameGiven) { - this.nameGiven = nameGiven; - } - - public void setNameFromAttribute(String nameFromAttribute) { - this.nameFromAttribute = nameFromAttribute; - } - - public void setClassName(String className) { - this.className = className; - } - - public void setDeclare(boolean declare) { - this.declare = declare; - } - - public void setScope(String scopeName) { - switch (scopeName) { - case "NESTED": - scope = VariableInfo.NESTED; - break; - case "AT_BEGIN": - scope = VariableInfo.AT_BEGIN; - break; - case "AT_END": - scope = VariableInfo.AT_END; - break; - } - } - - public TagVariableInfo toTagVariableInfo() { - return new TagVariableInfo(nameGiven, nameFromAttribute, className, declare, scope); - } - } - - private static class GenericBooleanRule extends Rule { - private final Method setter; - - private GenericBooleanRule(Class<?> type, String setterName) { - try { - this.setter = type.getMethod(setterName, Boolean.TYPE); - } catch (NoSuchMethodException e) { - throw new IllegalArgumentException(e); - } - } - - @Override - public void body(String namespace, String name, String text) throws Exception { - boolean value = "true".equalsIgnoreCase(text) || "yes".equalsIgnoreCase(text); - setter.invoke(digester.peek(), Boolean.valueOf(value)); - } } } Modified: tomcat/trunk/java/org/apache/tomcat/util/descriptor/tld/TldParser.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/descriptor/tld/TldParser.java?rev=1542606&r1=1542605&r2=1542606&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/tomcat/util/descriptor/tld/TldParser.java (original) +++ tomcat/trunk/java/org/apache/tomcat/util/descriptor/tld/TldParser.java Sat Nov 16 22:47:58 2013 @@ -24,6 +24,7 @@ import org.apache.juli.logging.LogFactor 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.RuleSet; import org.xml.sax.InputSource; import org.xml.sax.SAXException; @@ -35,7 +36,10 @@ public class TldParser { private final Digester digester; public TldParser(boolean namespaceAware, boolean validation) { - TldRuleSet ruleSet = new TldRuleSet(); + this(namespaceAware, validation, new TldRuleSet()); + } + + public TldParser(boolean namespaceAware, boolean validation, RuleSet ruleSet) { digester = DigesterFactory.newDigester(validation, namespaceAware, ruleSet); } --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org