Java Code Examples for javax.servlet.jsp.tagext.TagInfo#getAttributes()

The following examples show how to use javax.servlet.jsp.tagext.TagInfo#getAttributes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PageInfo.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public String tagInfoToString(TagInfo tag, String indent) {
    StringBuilder sb = new StringBuilder();
    if (tag != null) {
        sb.append(indent).append("tag name     : ").append(tag.getTagName()).append('\n');  // NOI18N
        sb.append(indent).append("    class    : ").append(tag.getTagClassName()).append('\n');  // NOI18N
        sb.append(indent).append("    attribs  : [");  // NOI18N
        TagAttributeInfo attrs[] = tag.getAttributes();
        for (int i = 0; i < attrs.length; i++) {
            sb.append(attrs[i].getName());
            if (i < attrs.length - 1) {
                sb.append(", ");  // NOI18N
            }
        }
        sb.append("]\n");  // NOI18N
    }
    else {
        sb.append(indent).append("taginfo is null\n");  // NOI18N
    }
    return sb.toString();
}
 
Example 2
Source File: Generator.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
/**
 * Generates declarations for tag handler attributes, and defines the getter
 * and setter methods for each.
 */
private void generateTagHandlerAttributes(TagInfo tagInfo) {

    if (tagInfo.hasDynamicAttributes()) {
        out.printil("private java.util.HashMap _jspx_dynamic_attrs = new java.util.HashMap();");
    }

    // Declare attributes
    TagAttributeInfo[] attrInfos = tagInfo.getAttributes();
    for (int i = 0; i < attrInfos.length; i++) {
        out.printin("private ");
        if (attrInfos[i].isFragment()) {
            out.print("javax.servlet.jsp.tagext.JspFragment ");
        } else {
            out.print(JspUtil.toJavaSourceType(attrInfos[i].getTypeName()));
            out.print(" ");
        }
        out.print(JspUtil.makeJavaIdentifierForAttribute(
                attrInfos[i].getName()));
        out.println(";");
    }
    out.println();

    // Define attribute getter and setter methods
    for (int i = 0; i < attrInfos.length; i++) {
        String javaName =
            JspUtil.makeJavaIdentifierForAttribute(attrInfos[i].getName());

        // getter method
        out.printin("public ");
        if (attrInfos[i].isFragment()) {
            out.print("javax.servlet.jsp.tagext.JspFragment ");
        } else {
            out.print(JspUtil.toJavaSourceType(attrInfos[i].getTypeName()));
            out.print(" ");
        }
        out.print(toGetterMethod(attrInfos[i].getName()));
        out.println(" {");
        out.pushIndent();
        out.printin("return this.");
        out.print(javaName);
        out.println(";");
        out.popIndent();
        out.printil("}");
        out.println();

        // setter method
        out.printin("public void ");
        out.print(toSetterMethodName(attrInfos[i].getName()));
        if (attrInfos[i].isFragment()) {
            out.print("(javax.servlet.jsp.tagext.JspFragment ");
        } else {
            out.print("(");
            out.print(JspUtil.toJavaSourceType(attrInfos[i].getTypeName()));
            out.print(" ");
        }
        out.print(javaName);
        out.println(") {");
        out.pushIndent();
        out.printin("this.");
        out.print(javaName);
        out.print(" = ");
        out.print(javaName);
        out.println(";");
        if (ctxt.isTagFile()) {
            // Tag files should also set jspContext attributes
            out.printin("jspContext.setAttribute(\"");
            out.print(attrInfos[i].getName());
            out.print("\", ");
            out.print(javaName);
            out.println(");");
        }
        out.popIndent();
        out.printil("}");
        out.println();
    }
}
 
Example 3
Source File: Generator.java    From packagedrone with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Generates declarations for tag handler attributes, and defines the
 * getter and setter methods for each.
 */
private void generateTagHandlerAttributes(TagInfo tagInfo)
    throws JasperException {

    if (tagInfo.hasDynamicAttributes()) {
        out.printil(
            "private java.util.HashMap _jspx_dynamic_attrs = new java.util.HashMap();");
    }

    // Declare attributes
    TagAttributeInfo[] attrInfos = tagInfo.getAttributes();
    for (int i = 0; i < attrInfos.length; i++) {
        out.printin("private ");
        if (attrInfos[i].isFragment()) {
            out.print("javax.servlet.jsp.tagext.JspFragment ");
        } else {
            out.print(JspUtil.toJavaSourceType(attrInfos[i].getTypeName()));
            out.print(" ");
        }
        out.print(attrInfos[i].getName());
        out.println(";");
    }
    out.println();

    // Define attribute getter and setter methods
    for (int i = 0; i < attrInfos.length; i++) {
        // getter method
        out.printin("public ");
        if (attrInfos[i].isFragment()) {
            out.print("javax.servlet.jsp.tagext.JspFragment ");
        } else {
            out.print(JspUtil.toJavaSourceType(attrInfos[i].getTypeName()));
            out.print(" ");
        }
        out.print(toGetterMethod(attrInfos[i].getName()));
        out.println(" {");
        out.pushIndent();
        out.printin("return this.");
        out.print(attrInfos[i].getName());
        out.println(";");
        out.popIndent();
        out.printil("}");
        out.println();

        // setter method
        out.printin("public void ");
        out.print(toSetterMethodName(attrInfos[i].getName()));
        if (attrInfos[i].isFragment()) {
            out.print("(javax.servlet.jsp.tagext.JspFragment ");
        } else {
            out.print("(");
            out.print(JspUtil.toJavaSourceType(attrInfos[i].getTypeName()));
            out.print(" ");
        }
        out.print(attrInfos[i].getName());
        out.println(") {");
        out.pushIndent();
        out.printin("this.");
        out.print(attrInfos[i].getName());
        out.print(" = ");
        out.print(attrInfos[i].getName());
        out.println(";");
        out.popIndent();
        out.printil("}");
        out.println();
    }
}
 
Example 4
Source File: Parser.java    From packagedrone with Eclipse Public License 1.0 4 votes vote down vote up
/**
    * Determine the body type of <jsp:attribute> from the enclosing node
    */
   private String getAttributeBodyType(Node n, String name) {

if (n instanceof Node.CustomTag) {
    TagInfo tagInfo = ((Node.CustomTag)n).getTagInfo();
    TagAttributeInfo[] tldAttrs = tagInfo.getAttributes();
    for (int i=0; i<tldAttrs.length; i++) {
	if (name.equals(tldAttrs[i].getName())) {
	    if (tldAttrs[i].isFragment()) {
	        return TagInfo.BODY_CONTENT_SCRIPTLESS;
	    }
	    if (tldAttrs[i].canBeRequestTime()) {
	        return TagInfo.BODY_CONTENT_JSP;
	    }
	}
    }
    if (tagInfo.hasDynamicAttributes()) {
	return TagInfo.BODY_CONTENT_JSP;
    }
} else if (n instanceof Node.IncludeAction) {
    if ("page".equals(name)) {
	return TagInfo.BODY_CONTENT_JSP;
    }
} else if (n instanceof Node.ForwardAction) {
    if ("page".equals(name)) {
	return TagInfo.BODY_CONTENT_JSP;
    }
} else if (n instanceof Node.SetProperty) {
    if ("value".equals(name)) {
	return TagInfo.BODY_CONTENT_JSP;
    }
} else if (n instanceof Node.UseBean) {
    if ("beanName".equals(name)) {
	return TagInfo.BODY_CONTENT_JSP;
    }
} else if (n instanceof Node.PlugIn) {
    if ("width".equals(name) || "height".equals(name)) {
	return TagInfo.BODY_CONTENT_JSP;
    }
} else if (n instanceof Node.ParamAction) {
    if ("value".equals(name)) {
	return TagInfo.BODY_CONTENT_JSP;
    }
} else if (n instanceof Node.JspElement) {
    return TagInfo.BODY_CONTENT_JSP;
}

return JAVAX_BODY_CONTENT_TEMPLATE_TEXT;
   }
 
Example 5
Source File: Validator.java    From packagedrone with Eclipse Public License 1.0 4 votes vote down vote up
private void checkNamedAttributes(Node.CustomTag n,
				  Node.JspAttribute[] jspAttrs,
				  int start,
				  Hashtable<String,Object> tagDataAttrs)
        throws JasperException {

    TagInfo tagInfo = n.getTagInfo();
    if (tagInfo == null) {
	err.jspError(n, "jsp.error.missing.tagInfo", n.getQName());
    }
    TagAttributeInfo[] tldAttrs = tagInfo.getAttributes();
           Node.Nodes naNodes = n.getNamedAttributeNodes();

    for (int i=0; i<naNodes.size(); i++) {
               Node.NamedAttribute na = (Node.NamedAttribute)
	    naNodes.getNode(i);
	boolean found = false;
	for (int j=0; j<tldAttrs.length; j++) {
	    /*
	     * See above comment about namespace matches. For named
	     * attributes, we use the prefix instead of URI as the
	     * match criterion, because in the case of a JSP document,
	     * we'd have to keep track of which namespaces are in scope
	     * when parsing a named attribute, in order to determine
	     * the URI that the prefix of the named attribute's name
	     * matches to.
	     */
	    String attrPrefix = na.getPrefix();
	    if (na.getLocalName().equals(tldAttrs[j].getName())
		    && (attrPrefix == null || attrPrefix.length() == 0
			|| attrPrefix.equals(n.getPrefix()))) {
		jspAttrs[start + i] = new Node.JspAttribute(na, false);
		NamedAttributeVisitor nav = null;
		if (na.getBody() != null) {
		    nav = new NamedAttributeVisitor();
		    na.getBody().visit(nav);
		}
		if (nav != null && nav.hasDynamicContent()) {
		    tagDataAttrs.put(na.getName(),
				     TagData.REQUEST_TIME_VALUE);
		} else {
		    tagDataAttrs.put(na.getName(), na.getText());    
		}
		found = true;
		break;
	    }
	}
	if (!found) {
	    if (tagInfo.hasDynamicAttributes()) {
		jspAttrs[start + i] = new Node.JspAttribute(na, true);
	    } else {
		err.jspError(n, "jsp.error.bad_attribute",
			     na.getName(), n.getLocalName());
	    }
	}
    }
}
 
Example 6
Source File: Validator.java    From packagedrone with Eclipse Public License 1.0 4 votes vote down vote up
public void visit(Node.CustomTag n) throws JasperException {

	    TagInfo tagInfo = n.getTagInfo();
	    if (tagInfo == null) {
		err.jspError(n, "jsp.error.missing.tagInfo", n.getQName());
	    }

	    /*
	     * The bodycontent of a SimpleTag cannot be JSP.
	     */
	    if (n.implementsSimpleTag() &&
                tagInfo.getBodyContent().equals(TagInfo.BODY_CONTENT_JSP)) {
                err.jspError(n, "jsp.error.simpletag.badbodycontent",
                             tagInfo.getTagClassName());
	    }

	    /*
	     * If the tag handler declares in the TLD that it supports dynamic
	     * attributes, it also must implement the DynamicAttributes
	     * interface.
	     */
	    if (tagInfo.hasDynamicAttributes()
		    && !n.implementsDynamicAttributes()) {
		err.jspError(n, "jsp.error.dynamic.attributes.not.implemented",
			     n.getQName());
	    }

	    /*
	     * Make sure all required attributes are present, either as
             * attributes or named attributes (<jsp:attribute>).
 	     * Also make sure that the same attribute is not specified in
	     * both attributes or named attributes.
	     */
	    TagAttributeInfo[] tldAttrs = tagInfo.getAttributes();
	    String customActionUri = n.getURI();
	    Attributes attrs = n.getAttributes();
	    int attrsSize = (attrs == null) ? 0 : attrs.getLength();
	    for (int i=0; i<tldAttrs.length; i++) {
		String attr = null;
		if (attrs != null) {
		    attr = attrs.getValue(tldAttrs[i].getName());
		    if (attr == null) {
			attr = attrs.getValue(customActionUri,
					      tldAttrs[i].getName());
		    }
		}
		Node.NamedAttribute na =
			n.getNamedAttributeNode(tldAttrs[i].getName());
		
		if (tldAttrs[i].isRequired() && attr == null && na == null) {
		    err.jspError(n, "jsp.error.missing_attribute",
				 tldAttrs[i].getName(), n.getLocalName());
		}
		if (attr != null && na != null) {
		    err.jspError(n, "jsp.error.duplicate.name.jspattribute",
			tldAttrs[i].getName());
		}
	    }

            Node.Nodes naNodes = n.getNamedAttributeNodes();
	    int jspAttrsSize = naNodes.size() + attrsSize;
	    Node.JspAttribute[] jspAttrs = null;
	    if (jspAttrsSize > 0) {
		jspAttrs = new Node.JspAttribute[jspAttrsSize];
	    }
	    Hashtable<String, Object> tagDataAttrs =
                    new Hashtable<String, Object>(attrsSize);

	    checkXmlAttributes(n, jspAttrs, tagDataAttrs);
            checkNamedAttributes(n, jspAttrs, attrsSize, tagDataAttrs);

	    TagData tagData = new TagData(tagDataAttrs);

	    // JSP.C1: It is a (translation time) error for an action that
	    // has one or more variable subelements to have a TagExtraInfo
	    // class that returns a non-null object.
	    TagExtraInfo tei = tagInfo.getTagExtraInfo();
	    if (tei != null
		    && tei.getVariableInfo(tagData) != null
		    && tei.getVariableInfo(tagData).length > 0
		    && tagInfo.getTagVariableInfos().length > 0) {
		err.jspError("jsp.error.non_null_tei_and_var_subelems",
			     n.getQName());
	    }

	    n.setTagData(tagData);
	    n.setJspAttributes(jspAttrs);

	    visitBody(n);
	}
 
Example 7
Source File: Generator.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
/**
 * Generates declarations for tag handler attributes, and defines the getter
 * and setter methods for each.
 */
private void generateTagHandlerAttributes(TagInfo tagInfo) {

    if (tagInfo.hasDynamicAttributes()) {
        out.printil("private java.util.HashMap _jspx_dynamic_attrs = new java.util.HashMap();");
    }

    // Declare attributes
    TagAttributeInfo[] attrInfos = tagInfo.getAttributes();
    for (int i = 0; i < attrInfos.length; i++) {
        out.printin("private ");
        if (attrInfos[i].isFragment()) {
            out.print("javax.servlet.jsp.tagext.JspFragment ");
        } else {
            out.print(JspUtil.toJavaSourceType(attrInfos[i].getTypeName()));
            out.print(" ");
        }
        out.print(JspUtil.makeJavaIdentifierForAttribute(
                attrInfos[i].getName()));
        out.println(";");
    }
    out.println();

    // Define attribute getter and setter methods
    for (int i = 0; i < attrInfos.length; i++) {
        String javaName =
            JspUtil.makeJavaIdentifierForAttribute(attrInfos[i].getName());

        // getter method
        out.printin("public ");
        if (attrInfos[i].isFragment()) {
            out.print("javax.servlet.jsp.tagext.JspFragment ");
        } else {
            out.print(JspUtil.toJavaSourceType(attrInfos[i].getTypeName()));
            out.print(" ");
        }
        out.print(toGetterMethod(attrInfos[i].getName()));
        out.println(" {");
        out.pushIndent();
        out.printin("return this.");
        out.print(javaName);
        out.println(";");
        out.popIndent();
        out.printil("}");
        out.println();

        // setter method
        out.printin("public void ");
        out.print(toSetterMethodName(attrInfos[i].getName()));
        if (attrInfos[i].isFragment()) {
            out.print("(javax.servlet.jsp.tagext.JspFragment ");
        } else {
            out.print("(");
            out.print(JspUtil.toJavaSourceType(attrInfos[i].getTypeName()));
            out.print(" ");
        }
        out.print(javaName);
        out.println(") {");
        out.pushIndent();
        out.printin("this.");
        out.print(javaName);
        out.print(" = ");
        out.print(javaName);
        out.println(";");
        if (ctxt.isTagFile()) {
            // Tag files should also set jspContext attributes
            out.printin("jspContext.setAttribute(\"");
            out.print(attrInfos[i].getName());
            out.print("\", ");
            out.print(javaName);
            out.println(");");
        }
        out.popIndent();
        out.printil("}");
        out.println();
    }
}
 
Example 8
Source File: Generator.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
private void generateTagHandlerPostamble(TagInfo tagInfo) {
    out.popIndent();

    // Have to catch Throwable because a classic tag handler
    // helper method is declared to throw Throwable.
    out.printil("} catch( java.lang.Throwable t ) {");
    out.pushIndent();
    out.printil("if( t instanceof javax.servlet.jsp.SkipPageException )");
    out.printil("    throw (javax.servlet.jsp.SkipPageException) t;");
    out.printil("if( t instanceof java.io.IOException )");
    out.printil("    throw (java.io.IOException) t;");
    out.printil("if( t instanceof java.lang.IllegalStateException )");
    out.printil("    throw (java.lang.IllegalStateException) t;");
    out.printil("if( t instanceof javax.servlet.jsp.JspException )");
    out.printil("    throw (javax.servlet.jsp.JspException) t;");
    out.printil("throw new javax.servlet.jsp.JspException(t);");
    out.popIndent();
    out.printil("} finally {");
    out.pushIndent();

    // handle restoring VariableMapper
    TagAttributeInfo[] attrInfos = tagInfo.getAttributes();
    for (int i = 0; i < attrInfos.length; i++) {
        if (attrInfos[i].isDeferredMethod() || attrInfos[i].isDeferredValue()) {
            out.printin("_el_variablemapper.setVariable(");
            out.print(quote(attrInfos[i].getName()));
            out.print(",_el_ve");
            out.print(i);
            out.println(");");
        }
    }

    // restore nested JspContext on ELContext
    out.printil("jspContext.getELContext().putContext(javax.servlet.jsp.JspContext.class,super.getJspContext());");

    out.printil("((org.apache.jasper.runtime.JspContextWrapper) jspContext).syncEndTagFile();");
    if (isPoolingEnabled && !tagHandlerPoolNames.isEmpty()) {
        out.printil("_jspDestroy();");
    }
    out.popIndent();
    out.printil("}");

    // Close the doTag method
    out.popIndent();
    out.printil("}");

    // Generated methods, helper classes, etc.
    genCommonPostamble();
}
 
Example 9
Source File: Parser.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
/**
 * Determine the body type of <jsp:attribute> from the enclosing node
 */
private String getAttributeBodyType(Node n, String name) {

    if (n instanceof Node.CustomTag) {
        TagInfo tagInfo = ((Node.CustomTag) n).getTagInfo();
        TagAttributeInfo[] tldAttrs = tagInfo.getAttributes();
        for (int i = 0; i < tldAttrs.length; i++) {
            if (name.equals(tldAttrs[i].getName())) {
                if (tldAttrs[i].isFragment()) {
                    return TagInfo.BODY_CONTENT_SCRIPTLESS;
                }
                if (tldAttrs[i].canBeRequestTime()) {
                    return TagInfo.BODY_CONTENT_JSP;
                }
            }
        }
        if (tagInfo.hasDynamicAttributes()) {
            return TagInfo.BODY_CONTENT_JSP;
        }
    } else if (n instanceof Node.IncludeAction) {
        if ("page".equals(name)) {
            return TagInfo.BODY_CONTENT_JSP;
        }
    } else if (n instanceof Node.ForwardAction) {
        if ("page".equals(name)) {
            return TagInfo.BODY_CONTENT_JSP;
        }
    } else if (n instanceof Node.SetProperty) {
        if ("value".equals(name)) {
            return TagInfo.BODY_CONTENT_JSP;
        }
    } else if (n instanceof Node.UseBean) {
        if ("beanName".equals(name)) {
            return TagInfo.BODY_CONTENT_JSP;
        }
    } else if (n instanceof Node.PlugIn) {
        if ("width".equals(name) || "height".equals(name)) {
            return TagInfo.BODY_CONTENT_JSP;
        }
    } else if (n instanceof Node.ParamAction) {
        if ("value".equals(name)) {
            return TagInfo.BODY_CONTENT_JSP;
        }
    } else if (n instanceof Node.JspElement) {
        return TagInfo.BODY_CONTENT_JSP;
    }

    return JAVAX_BODY_CONTENT_TEMPLATE_TEXT;
}
 
Example 10
Source File: Validator.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
private void checkNamedAttributes(Node.CustomTag n,
        Node.JspAttribute[] jspAttrs, int start,
        Hashtable<String, Object> tagDataAttrs)
        throws JasperException {

    TagInfo tagInfo = n.getTagInfo();
    if (tagInfo == null) {
        err.jspError(n, "jsp.error.missing.tagInfo", n.getQName());
    }
    TagAttributeInfo[] tldAttrs = tagInfo.getAttributes();
    Node.Nodes naNodes = n.getNamedAttributeNodes();

    for (int i = 0; i < naNodes.size(); i++) {
        Node.NamedAttribute na = (Node.NamedAttribute) naNodes
                .getNode(i);
        boolean found = false;
        for (int j = 0; j < tldAttrs.length; j++) {
            /*
             * See above comment about namespace matches. For named
             * attributes, we use the prefix instead of URI as the match
             * criterion, because in the case of a JSP document, we'd
             * have to keep track of which namespaces are in scope when
             * parsing a named attribute, in order to determine the URI
             * that the prefix of the named attribute's name matches to.
             */
            String attrPrefix = na.getPrefix();
            if (na.getLocalName().equals(tldAttrs[j].getName())
                    && (attrPrefix == null || attrPrefix.length() == 0 || attrPrefix
                            .equals(n.getPrefix()))) {
                jspAttrs[start + i] = new Node.JspAttribute(na,
                        tldAttrs[j], false);
                NamedAttributeVisitor nav = null;
                if (na.getBody() != null) {
                    nav = new NamedAttributeVisitor();
                    na.getBody().visit(nav);
                }
                if (nav != null && nav.hasDynamicContent()) {
                    tagDataAttrs.put(na.getName(),
                            TagData.REQUEST_TIME_VALUE);
                } else {
                    tagDataAttrs.put(na.getName(), na.getText());
                }
                found = true;
                break;
            }
        }
        if (!found) {
            if (tagInfo.hasDynamicAttributes()) {
                jspAttrs[start + i] = new Node.JspAttribute(na, null,
                        true);
            } else {
                err.jspError(n, "jsp.error.bad_attribute",
                        na.getName(), n.getLocalName());
            }
        }
    }
}
 
Example 11
Source File: Validator.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
@Override
public void visit(Node.CustomTag n) throws JasperException {

    TagInfo tagInfo = n.getTagInfo();
    if (tagInfo == null) {
        err.jspError(n, "jsp.error.missing.tagInfo", n.getQName());
    }

    /*
     * The bodycontent of a SimpleTag cannot be JSP.
     */
    if (n.implementsSimpleTag()
            && tagInfo.getBodyContent().equalsIgnoreCase(
                    TagInfo.BODY_CONTENT_JSP)) {
        err.jspError(n, "jsp.error.simpletag.badbodycontent", tagInfo
                .getTagClassName());
    }

    /*
     * If the tag handler declares in the TLD that it supports dynamic
     * attributes, it also must implement the DynamicAttributes
     * interface.
     */
    if (tagInfo.hasDynamicAttributes()
            && !n.implementsDynamicAttributes()) {
        err.jspError(n, "jsp.error.dynamic.attributes.not.implemented",
                n.getQName());
    }

    /*
     * Make sure all required attributes are present, either as
     * attributes or named attributes (<jsp:attribute>). Also make sure
     * that the same attribute is not specified in both attributes or
     * named attributes.
     */
    TagAttributeInfo[] tldAttrs = tagInfo.getAttributes();
    String customActionUri = n.getURI();
    Attributes attrs = n.getAttributes();
    int attrsSize = (attrs == null) ? 0 : attrs.getLength();
    for (int i = 0; i < tldAttrs.length; i++) {
        String attr = null;
        if (attrs != null) {
            attr = attrs.getValue(tldAttrs[i].getName());
            if (attr == null) {
                attr = attrs.getValue(customActionUri, tldAttrs[i]
                        .getName());
            }
        }
        Node.NamedAttribute na = n.getNamedAttributeNode(tldAttrs[i]
                .getName());

        if (tldAttrs[i].isRequired() && attr == null && na == null) {
            err.jspError(n, "jsp.error.missing_attribute", tldAttrs[i]
                    .getName(), n.getLocalName());
        }
        if (attr != null && na != null) {
            err.jspError(n, "jsp.error.duplicate.name.jspattribute",
                    tldAttrs[i].getName());
        }
    }

    Node.Nodes naNodes = n.getNamedAttributeNodes();
    int jspAttrsSize = naNodes.size() + attrsSize;
    Node.JspAttribute[] jspAttrs = null;
    if (jspAttrsSize > 0) {
        jspAttrs = new Node.JspAttribute[jspAttrsSize];
    }
    Hashtable<String, Object> tagDataAttrs = new Hashtable<String, Object>(attrsSize);

    checkXmlAttributes(n, jspAttrs, tagDataAttrs);
    checkNamedAttributes(n, jspAttrs, attrsSize, tagDataAttrs);

    TagData tagData = new TagData(tagDataAttrs);

    // JSP.C1: It is a (translation time) error for an action that
    // has one or more variable subelements to have a TagExtraInfo
    // class that returns a non-null object.
    TagExtraInfo tei = tagInfo.getTagExtraInfo();
    if (tei != null && tei.getVariableInfo(tagData) != null
            && tei.getVariableInfo(tagData).length > 0
            && tagInfo.getTagVariableInfos().length > 0) {
        err.jspError("jsp.error.non_null_tei_and_var_subelems", n
                .getQName());
    }

    n.setTagData(tagData);
    n.setJspAttributes(jspAttrs);

    visitBody(n);
}
 
Example 12
Source File: Validator.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@SuppressWarnings("null") // tagInfo can't be null after initial test
@Override
public void visit(Node.CustomTag n) throws JasperException {

    TagInfo tagInfo = n.getTagInfo();
    if (tagInfo == null) {
        err.jspError(n, "jsp.error.missing.tagInfo", n.getQName());
    }

    /*
     * The bodycontent of a SimpleTag cannot be JSP.
     */
    if (n.implementsSimpleTag()
            && tagInfo.getBodyContent().equalsIgnoreCase(
                    TagInfo.BODY_CONTENT_JSP)) {
        err.jspError(n, "jsp.error.simpletag.badbodycontent", tagInfo
                .getTagClassName());
    }

    /*
     * If the tag handler declares in the TLD that it supports dynamic
     * attributes, it also must implement the DynamicAttributes
     * interface.
     */
    if (tagInfo.hasDynamicAttributes()
            && !n.implementsDynamicAttributes()) {
        err.jspError(n, "jsp.error.dynamic.attributes.not.implemented",
                n.getQName());
    }

    /*
     * Make sure all required attributes are present, either as
     * attributes or named attributes (<jsp:attribute>). Also make sure
     * that the same attribute is not specified in both attributes or
     * named attributes.
     */
    TagAttributeInfo[] tldAttrs = tagInfo.getAttributes();
    String customActionUri = n.getURI();
    Attributes attrs = n.getAttributes();
    int attrsSize = (attrs == null) ? 0 : attrs.getLength();
    for (int i = 0; i < tldAttrs.length; i++) {
        String attr = null;
        if (attrs != null) {
            attr = attrs.getValue(tldAttrs[i].getName());
            if (attr == null) {
                attr = attrs.getValue(customActionUri, tldAttrs[i]
                        .getName());
            }
        }
        Node.NamedAttribute na = n.getNamedAttributeNode(tldAttrs[i]
                .getName());

        if (tldAttrs[i].isRequired() && attr == null && na == null) {
            err.jspError(n, "jsp.error.missing_attribute", tldAttrs[i]
                    .getName(), n.getLocalName());
        }
        if (attr != null && na != null) {
            err.jspError(n, "jsp.error.duplicate.name.jspattribute",
                    tldAttrs[i].getName());
        }
    }

    Node.Nodes naNodes = n.getNamedAttributeNodes();
    int jspAttrsSize = naNodes.size() + attrsSize;
    Node.JspAttribute[] jspAttrs = null;
    if (jspAttrsSize > 0) {
        jspAttrs = new Node.JspAttribute[jspAttrsSize];
    }
    Hashtable<String, Object> tagDataAttrs = new Hashtable<>(attrsSize);

    checkXmlAttributes(n, jspAttrs, tagDataAttrs);
    checkNamedAttributes(n, jspAttrs, attrsSize, tagDataAttrs);

    TagData tagData = new TagData(tagDataAttrs);

    // JSP.C1: It is a (translation time) error for an action that
    // has one or more variable subelements to have a TagExtraInfo
    // class that returns a non-null object.
    TagExtraInfo tei = tagInfo.getTagExtraInfo();
    if (tei != null && tei.getVariableInfo(tagData) != null
            && tei.getVariableInfo(tagData).length > 0
            && tagInfo.getTagVariableInfos().length > 0) {
        err.jspError("jsp.error.non_null_tei_and_var_subelems", n
                .getQName());
    }

    n.setTagData(tagData);
    n.setJspAttributes(jspAttrs);

    visitBody(n);
}
 
Example 13
Source File: Generator.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
private void generateTagHandlerPostamble(TagInfo tagInfo) {
    out.popIndent();

    // Have to catch Throwable because a classic tag handler
    // helper method is declared to throw Throwable.
    out.printil("} catch( java.lang.Throwable t ) {");
    out.pushIndent();
    out.printil("if( t instanceof javax.servlet.jsp.SkipPageException )");
    out.printil("    throw (javax.servlet.jsp.SkipPageException) t;");
    out.printil("if( t instanceof java.io.IOException )");
    out.printil("    throw (java.io.IOException) t;");
    out.printil("if( t instanceof java.lang.IllegalStateException )");
    out.printil("    throw (java.lang.IllegalStateException) t;");
    out.printil("if( t instanceof javax.servlet.jsp.JspException )");
    out.printil("    throw (javax.servlet.jsp.JspException) t;");
    out.printil("throw new javax.servlet.jsp.JspException(t);");
    out.popIndent();
    out.printil("} finally {");
    out.pushIndent();

    // handle restoring VariableMapper
    TagAttributeInfo[] attrInfos = tagInfo.getAttributes();
    for (int i = 0; i < attrInfos.length; i++) {
        if (attrInfos[i].isDeferredMethod() || attrInfos[i].isDeferredValue()) {
            out.printin("_el_variablemapper.setVariable(");
            out.print(quote(attrInfos[i].getName()));
            out.print(",_el_ve");
            out.print(i);
            out.println(");");
        }
    }

    // restore nested JspContext on ELContext
    out.printil("jspContext.getELContext().putContext(javax.servlet.jsp.JspContext.class,super.getJspContext());");

    out.printil("((org.apache.jasper.runtime.JspContextWrapper) jspContext).syncEndTagFile();");
    if (isPoolingEnabled && !tagHandlerPoolNames.isEmpty()) {
        out.printil("_jspDestroy();");
    }
    out.popIndent();
    out.printil("}");

    // Close the doTag method
    out.popIndent();
    out.printil("}");

    // Generated methods, helper classes, etc.
    genCommonPostamble();
}
 
Example 14
Source File: Parser.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
/**
 * Determine the body type of <jsp:attribute> from the enclosing node
 */
private String getAttributeBodyType(Node n, String name) {

    if (n instanceof Node.CustomTag) {
        TagInfo tagInfo = ((Node.CustomTag) n).getTagInfo();
        TagAttributeInfo[] tldAttrs = tagInfo.getAttributes();
        for (int i = 0; i < tldAttrs.length; i++) {
            if (name.equals(tldAttrs[i].getName())) {
                if (tldAttrs[i].isFragment()) {
                    return TagInfo.BODY_CONTENT_SCRIPTLESS;
                }
                if (tldAttrs[i].canBeRequestTime()) {
                    return TagInfo.BODY_CONTENT_JSP;
                }
            }
        }
        if (tagInfo.hasDynamicAttributes()) {
            return TagInfo.BODY_CONTENT_JSP;
        }
    } else if (n instanceof Node.IncludeAction) {
        if ("page".equals(name)) {
            return TagInfo.BODY_CONTENT_JSP;
        }
    } else if (n instanceof Node.ForwardAction) {
        if ("page".equals(name)) {
            return TagInfo.BODY_CONTENT_JSP;
        }
    } else if (n instanceof Node.SetProperty) {
        if ("value".equals(name)) {
            return TagInfo.BODY_CONTENT_JSP;
        }
    } else if (n instanceof Node.UseBean) {
        if ("beanName".equals(name)) {
            return TagInfo.BODY_CONTENT_JSP;
        }
    } else if (n instanceof Node.PlugIn) {
        if ("width".equals(name) || "height".equals(name)) {
            return TagInfo.BODY_CONTENT_JSP;
        }
    } else if (n instanceof Node.ParamAction) {
        if ("value".equals(name)) {
            return TagInfo.BODY_CONTENT_JSP;
        }
    } else if (n instanceof Node.JspElement) {
        return TagInfo.BODY_CONTENT_JSP;
    }

    return JAVAX_BODY_CONTENT_TEMPLATE_TEXT;
}
 
Example 15
Source File: Validator.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
private void checkNamedAttributes(Node.CustomTag n,
        Node.JspAttribute[] jspAttrs, int start,
        Hashtable<String, Object> tagDataAttrs)
        throws JasperException {

    TagInfo tagInfo = n.getTagInfo();
    if (tagInfo == null) {
        err.jspError(n, "jsp.error.missing.tagInfo", n.getQName());
    }
    TagAttributeInfo[] tldAttrs = tagInfo.getAttributes();
    Node.Nodes naNodes = n.getNamedAttributeNodes();

    for (int i = 0; i < naNodes.size(); i++) {
        Node.NamedAttribute na = (Node.NamedAttribute) naNodes
                .getNode(i);
        boolean found = false;
        for (int j = 0; j < tldAttrs.length; j++) {
            /*
             * See above comment about namespace matches. For named
             * attributes, we use the prefix instead of URI as the match
             * criterion, because in the case of a JSP document, we'd
             * have to keep track of which namespaces are in scope when
             * parsing a named attribute, in order to determine the URI
             * that the prefix of the named attribute's name matches to.
             */
            String attrPrefix = na.getPrefix();
            if (na.getLocalName().equals(tldAttrs[j].getName())
                    && (attrPrefix == null || attrPrefix.length() == 0 || attrPrefix
                            .equals(n.getPrefix()))) {
                jspAttrs[start + i] = new Node.JspAttribute(na,
                        tldAttrs[j], false);
                NamedAttributeVisitor nav = null;
                if (na.getBody() != null) {
                    nav = new NamedAttributeVisitor();
                    na.getBody().visit(nav);
                }
                if (nav != null && nav.hasDynamicContent()) {
                    tagDataAttrs.put(na.getName(),
                            TagData.REQUEST_TIME_VALUE);
                } else {
                    tagDataAttrs.put(na.getName(), na.getText());
                }
                found = true;
                break;
            }
        }
        if (!found) {
            if (tagInfo.hasDynamicAttributes()) {
                jspAttrs[start + i] = new Node.JspAttribute(na, null,
                        true);
            } else {
                err.jspError(n, "jsp.error.bad_attribute",
                        na.getName(), n.getLocalName());
            }
        }
    }
}
 
Example 16
Source File: Validator.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
@Override
public void visit(Node.CustomTag n) throws JasperException {

    TagInfo tagInfo = n.getTagInfo();
    if (tagInfo == null) {
        err.jspError(n, "jsp.error.missing.tagInfo", n.getQName());
    }

    /*
     * The bodycontent of a SimpleTag cannot be JSP.
     */
    if (n.implementsSimpleTag()
            && tagInfo.getBodyContent().equalsIgnoreCase(
                    TagInfo.BODY_CONTENT_JSP)) {
        err.jspError(n, "jsp.error.simpletag.badbodycontent", tagInfo
                .getTagClassName());
    }

    /*
     * If the tag handler declares in the TLD that it supports dynamic
     * attributes, it also must implement the DynamicAttributes
     * interface.
     */
    if (tagInfo.hasDynamicAttributes()
            && !n.implementsDynamicAttributes()) {
        err.jspError(n, "jsp.error.dynamic.attributes.not.implemented",
                n.getQName());
    }

    /*
     * Make sure all required attributes are present, either as
     * attributes or named attributes (<jsp:attribute>). Also make sure
     * that the same attribute is not specified in both attributes or
     * named attributes.
     */
    TagAttributeInfo[] tldAttrs = tagInfo.getAttributes();
    String customActionUri = n.getURI();
    Attributes attrs = n.getAttributes();
    int attrsSize = (attrs == null) ? 0 : attrs.getLength();
    for (int i = 0; i < tldAttrs.length; i++) {
        String attr = null;
        if (attrs != null) {
            attr = attrs.getValue(tldAttrs[i].getName());
            if (attr == null) {
                attr = attrs.getValue(customActionUri, tldAttrs[i]
                        .getName());
            }
        }
        Node.NamedAttribute na = n.getNamedAttributeNode(tldAttrs[i]
                .getName());

        if (tldAttrs[i].isRequired() && attr == null && na == null) {
            err.jspError(n, "jsp.error.missing_attribute", tldAttrs[i]
                    .getName(), n.getLocalName());
        }
        if (attr != null && na != null) {
            err.jspError(n, "jsp.error.duplicate.name.jspattribute",
                    tldAttrs[i].getName());
        }
    }

    Node.Nodes naNodes = n.getNamedAttributeNodes();
    int jspAttrsSize = naNodes.size() + attrsSize;
    Node.JspAttribute[] jspAttrs = null;
    if (jspAttrsSize > 0) {
        jspAttrs = new Node.JspAttribute[jspAttrsSize];
    }
    Hashtable<String, Object> tagDataAttrs = new Hashtable<String, Object>(attrsSize);

    checkXmlAttributes(n, jspAttrs, tagDataAttrs);
    checkNamedAttributes(n, jspAttrs, attrsSize, tagDataAttrs);

    TagData tagData = new TagData(tagDataAttrs);

    // JSP.C1: It is a (translation time) error for an action that
    // has one or more variable subelements to have a TagExtraInfo
    // class that returns a non-null object.
    TagExtraInfo tei = tagInfo.getTagExtraInfo();
    if (tei != null && tei.getVariableInfo(tagData) != null
            && tei.getVariableInfo(tagData).length > 0
            && tagInfo.getTagVariableInfos().length > 0) {
        err.jspError("jsp.error.non_null_tei_and_var_subelems", n
                .getQName());
    }

    n.setTagData(tagData);
    n.setJspAttributes(jspAttrs);

    visitBody(n);
}
 
Example 17
Source File: Generator.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
/**
 * Generates declarations for tag handler attributes, and defines the getter
 * and setter methods for each.
 */
private void generateTagHandlerAttributes(TagInfo tagInfo) {

    if (tagInfo.hasDynamicAttributes()) {
        out.printil("private java.util.HashMap _jspx_dynamic_attrs = new java.util.HashMap();");
    }

    // Declare attributes
    TagAttributeInfo[] attrInfos = tagInfo.getAttributes();
    for (int i = 0; i < attrInfos.length; i++) {
        out.printin("private ");
        if (attrInfos[i].isFragment()) {
            out.print("javax.servlet.jsp.tagext.JspFragment ");
        } else {
            out.print(JspUtil.toJavaSourceType(attrInfos[i].getTypeName()));
            out.print(" ");
        }
        out.print(JspUtil.makeJavaIdentifierForAttribute(
                attrInfos[i].getName()));
        out.println(";");
    }
    out.println();

    // Define attribute getter and setter methods
    for (int i = 0; i < attrInfos.length; i++) {
        String javaName =
            JspUtil.makeJavaIdentifierForAttribute(attrInfos[i].getName());

        // getter method
        out.printin("public ");
        if (attrInfos[i].isFragment()) {
            out.print("javax.servlet.jsp.tagext.JspFragment ");
        } else {
            out.print(JspUtil.toJavaSourceType(attrInfos[i].getTypeName()));
            out.print(" ");
        }
        out.print(toGetterMethod(attrInfos[i].getName()));
        out.println(" {");
        out.pushIndent();
        out.printin("return this.");
        out.print(javaName);
        out.println(";");
        out.popIndent();
        out.printil("}");
        out.println();

        // setter method
        out.printin("public void ");
        out.print(toSetterMethodName(attrInfos[i].getName()));
        if (attrInfos[i].isFragment()) {
            out.print("(javax.servlet.jsp.tagext.JspFragment ");
        } else {
            out.print("(");
            out.print(JspUtil.toJavaSourceType(attrInfos[i].getTypeName()));
            out.print(" ");
        }
        out.print(javaName);
        out.println(") {");
        out.pushIndent();
        out.printin("this.");
        out.print(javaName);
        out.print(" = ");
        out.print(javaName);
        out.println(";");
        if (ctxt.isTagFile()) {
            // Tag files should also set jspContext attributes
            out.printin("jspContext.setAttribute(\"");
            out.print(attrInfos[i].getName());
            out.print("\", ");
            out.print(javaName);
            out.println(");");
        }
        out.popIndent();
        out.printil("}");
        out.println();
    }
}
 
Example 18
Source File: Generator.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
private void generateTagHandlerPostamble(TagInfo tagInfo) {
    out.popIndent();

    // Have to catch Throwable because a classic tag handler
    // helper method is declared to throw Throwable.
    out.printil("} catch( java.lang.Throwable t ) {");
    out.pushIndent();
    out.printil("if( t instanceof javax.servlet.jsp.SkipPageException )");
    out.printil("    throw (javax.servlet.jsp.SkipPageException) t;");
    out.printil("if( t instanceof java.io.IOException )");
    out.printil("    throw (java.io.IOException) t;");
    out.printil("if( t instanceof java.lang.IllegalStateException )");
    out.printil("    throw (java.lang.IllegalStateException) t;");
    out.printil("if( t instanceof javax.servlet.jsp.JspException )");
    out.printil("    throw (javax.servlet.jsp.JspException) t;");
    out.printil("throw new javax.servlet.jsp.JspException(t);");
    out.popIndent();
    out.printil("} finally {");
    out.pushIndent();

    // handle restoring VariableMapper
    TagAttributeInfo[] attrInfos = tagInfo.getAttributes();
    for (int i = 0; i < attrInfos.length; i++) {
        if (attrInfos[i].isDeferredMethod() || attrInfos[i].isDeferredValue()) {
            out.printin("_el_variablemapper.setVariable(");
            out.print(quote(attrInfos[i].getName()));
            out.print(",_el_ve");
            out.print(i);
            out.println(");");
        }
    }

    // restore nested JspContext on ELContext
    out.printil("jspContext.getELContext().putContext(javax.servlet.jsp.JspContext.class,super.getJspContext());");

    out.printil("((org.apache.jasper.runtime.JspContextWrapper) jspContext).syncEndTagFile();");
    if (isPoolingEnabled && !tagHandlerPoolNames.isEmpty()) {
        out.printil("_jspDestroy();");
    }
    out.popIndent();
    out.printil("}");

    // Close the doTag method
    out.popIndent();
    out.printil("}");

    // Generated methods, helper classes, etc.
    genCommonPostamble();
}
 
Example 19
Source File: Parser.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
/**
 * Determine the body type of <jsp:attribute> from the enclosing node
 */
private String getAttributeBodyType(Node n, String name) {

    if (n instanceof Node.CustomTag) {
        TagInfo tagInfo = ((Node.CustomTag) n).getTagInfo();
        TagAttributeInfo[] tldAttrs = tagInfo.getAttributes();
        for (int i = 0; i < tldAttrs.length; i++) {
            if (name.equals(tldAttrs[i].getName())) {
                if (tldAttrs[i].isFragment()) {
                    return TagInfo.BODY_CONTENT_SCRIPTLESS;
                }
                if (tldAttrs[i].canBeRequestTime()) {
                    return TagInfo.BODY_CONTENT_JSP;
                }
            }
        }
        if (tagInfo.hasDynamicAttributes()) {
            return TagInfo.BODY_CONTENT_JSP;
        }
    } else if (n instanceof Node.IncludeAction) {
        if ("page".equals(name)) {
            return TagInfo.BODY_CONTENT_JSP;
        }
    } else if (n instanceof Node.ForwardAction) {
        if ("page".equals(name)) {
            return TagInfo.BODY_CONTENT_JSP;
        }
    } else if (n instanceof Node.SetProperty) {
        if ("value".equals(name)) {
            return TagInfo.BODY_CONTENT_JSP;
        }
    } else if (n instanceof Node.UseBean) {
        if ("beanName".equals(name)) {
            return TagInfo.BODY_CONTENT_JSP;
        }
    } else if (n instanceof Node.PlugIn) {
        if ("width".equals(name) || "height".equals(name)) {
            return TagInfo.BODY_CONTENT_JSP;
        }
    } else if (n instanceof Node.ParamAction) {
        if ("value".equals(name)) {
            return TagInfo.BODY_CONTENT_JSP;
        }
    } else if (n instanceof Node.JspElement) {
        return TagInfo.BODY_CONTENT_JSP;
    }

    return JAVAX_BODY_CONTENT_TEMPLATE_TEXT;
}
 
Example 20
Source File: Validator.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
private void checkNamedAttributes(Node.CustomTag n,
        Node.JspAttribute[] jspAttrs, int start,
        Hashtable<String, Object> tagDataAttrs)
        throws JasperException {

    TagInfo tagInfo = n.getTagInfo();
    TagAttributeInfo[] tldAttrs = tagInfo.getAttributes();
    Node.Nodes naNodes = n.getNamedAttributeNodes();

    for (int i = 0; i < naNodes.size(); i++) {
        Node.NamedAttribute na = (Node.NamedAttribute) naNodes
                .getNode(i);
        boolean found = false;
        for (int j = 0; j < tldAttrs.length; j++) {
            /*
             * See above comment about namespace matches. For named
             * attributes, we use the prefix instead of URI as the match
             * criterion, because in the case of a JSP document, we'd
             * have to keep track of which namespaces are in scope when
             * parsing a named attribute, in order to determine the URI
             * that the prefix of the named attribute's name matches to.
             */
            String attrPrefix = na.getPrefix();
            if (na.getLocalName().equals(tldAttrs[j].getName())
                    && (attrPrefix == null || attrPrefix.length() == 0 || attrPrefix
                            .equals(n.getPrefix()))) {
                jspAttrs[start + i] = new Node.JspAttribute(na,
                        tldAttrs[j], false);
                NamedAttributeVisitor nav = null;
                if (na.getBody() != null) {
                    nav = new NamedAttributeVisitor();
                    na.getBody().visit(nav);
                }
                if (nav != null && nav.hasDynamicContent()) {
                    tagDataAttrs.put(na.getName(),
                            TagData.REQUEST_TIME_VALUE);
                } else {
                    tagDataAttrs.put(na.getName(), na.getText());
                }
                found = true;
                break;
            }
        }
        if (!found) {
            if (tagInfo.hasDynamicAttributes()) {
                jspAttrs[start + i] = new Node.JspAttribute(na, null,
                        true);
            } else {
                err.jspError(n, "jsp.error.bad_attribute",
                        na.getName(), n.getLocalName());
            }
        }
    }
}