Search in sources :

Example 1 with TreeNode

use of org.apache.jasper.xmlparser.TreeNode in project tomcat70 by apache.

the class TagLibraryInfoImpl method createValidator.

private TagLibraryValidator createValidator(TreeNode elem) throws JasperException {
    String validatorClass = null;
    Map<String, Object> initParams = new Hashtable<String, Object>();
    Iterator<TreeNode> list = elem.findChildren();
    while (list.hasNext()) {
        TreeNode element = list.next();
        String tname = element.getName();
        if ("validator-class".equals(tname))
            validatorClass = element.getBody();
        else if ("init-param".equals(tname)) {
            String[] initParam = createInitParam(element);
            initParams.put(initParam[0], initParam[1]);
        } else if (// Ignored elements
        "description".equals(tname) || false) {
        } else {
            if (log.isWarnEnabled()) {
                log.warn(Localizer.getMessage("jsp.warning.unknown.element.in.validator", tname));
            }
        }
    }
    TagLibraryValidator tlv = null;
    if (validatorClass != null && !validatorClass.equals("")) {
        try {
            Class<?> tlvClass = ctxt.getClassLoader().loadClass(validatorClass);
            tlv = (TagLibraryValidator) tlvClass.newInstance();
        } catch (Exception e) {
            err.jspError(e, "jsp.error.tlvclass.instantiation", validatorClass);
        }
    }
    if (tlv != null) {
        tlv.setInitParameters(initParams);
    }
    return tlv;
}
Also used : Hashtable(java.util.Hashtable) TreeNode(org.apache.jasper.xmlparser.TreeNode) TagLibraryValidator(javax.servlet.jsp.tagext.TagLibraryValidator) JasperException(org.apache.jasper.JasperException) FileNotFoundException(java.io.FileNotFoundException)

Example 2 with TreeNode

use of org.apache.jasper.xmlparser.TreeNode in project tomcat70 by apache.

the class TagLibraryInfoImpl method parseTLD.

/*
     * @param ctxt The JSP compilation context @param uri The TLD's uri @param
     * in The TLD's input stream @param jarFileUrl The JAR file containing the
     * TLD, or null if the tag library is not packaged in a JAR
     */
private void parseTLD(String uri, InputStream in, JarResource jarResource) throws JasperException {
    Vector<TagInfo> tagVector = new Vector<TagInfo>();
    Vector<TagFileInfo> tagFileVector = new Vector<TagFileInfo>();
    Hashtable<String, FunctionInfo> functionTable = new Hashtable<String, FunctionInfo>();
    ServletContext servletContext = ctxt.getServletContext();
    boolean validate = Boolean.parseBoolean(servletContext.getInitParameter(Constants.XML_VALIDATION_TLD_INIT_PARAM));
    String blockExternalString = servletContext.getInitParameter(Constants.XML_BLOCK_EXTERNAL_INIT_PARAM);
    boolean blockExternal;
    if (blockExternalString == null) {
        blockExternal = true;
    } else {
        blockExternal = Boolean.parseBoolean(blockExternalString);
    }
    // Create an iterator over the child elements of our <taglib> element
    ParserUtils pu = new ParserUtils(validate, blockExternal);
    TreeNode tld = pu.parseXMLDocument(uri, in);
    // Check to see if the <taglib> root element contains a 'version'
    // attribute, which was added in JSP 2.0 to replace the <jsp-version>
    // subelement
    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 (// JSP 1.1
        "tlibversion".equals(tname) || "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))
            this.shortname = element.getBody();
        else if ("uri".equals(tname))
            this.urn = element.getBody();
        else if ("info".equals(tname) || "description".equals(tname))
            this.info = element.getBody();
        else if ("validator".equals(tname))
            this.tagLibraryValidator = createValidator(element);
        else if ("tag".equals(tname))
            tagVector.addElement(createTagInfo(element, jspversion));
        else if ("tag-file".equals(tname)) {
            TagFileInfo tagFileInfo = createTagFileInfo(element, jarResource);
            tagFileVector.addElement(tagFileInfo);
        } else if ("function".equals(tname)) {
            // JSP2.0
            FunctionInfo funcInfo = createFunctionInfo(element);
            String funcName = funcInfo.getName();
            if (functionTable.containsKey(funcName)) {
                err.jspError("jsp.error.tld.fn.duplicate.name", funcName, uri);
            }
            functionTable.put(funcName, funcInfo);
        } else if ("display-name".equals(tname) || "small-icon".equals(tname) || "large-icon".equals(tname) || "listener".equals(tname)) {
        // Ignored elements
        } else if ("taglib-extension".equals(tname)) {
        // Recognized but ignored
        } else {
            if (log.isWarnEnabled()) {
                log.warn(Localizer.getMessage("jsp.warning.unknown.element.in.taglib", tname));
            }
        }
    }
    if (tlibversion == null) {
        err.jspError("jsp.error.tld.mandatory.element.missing", "tlib-version", uri);
    }
    if (jspversion == null) {
        err.jspError("jsp.error.tld.mandatory.element.missing", "jsp-version", uri);
    }
    this.tags = new TagInfo[tagVector.size()];
    tagVector.copyInto(this.tags);
    this.tagFiles = new TagFileInfo[tagFileVector.size()];
    tagFileVector.copyInto(this.tagFiles);
    this.functions = new FunctionInfo[functionTable.size()];
    int i = 0;
    Enumeration<FunctionInfo> enumeration = functionTable.elements();
    while (enumeration.hasMoreElements()) {
        this.functions[i++] = enumeration.nextElement();
    }
}
Also used : TagFileInfo(javax.servlet.jsp.tagext.TagFileInfo) Hashtable(java.util.Hashtable) FunctionInfo(javax.servlet.jsp.tagext.FunctionInfo) TreeNode(org.apache.jasper.xmlparser.TreeNode) TagInfo(javax.servlet.jsp.tagext.TagInfo) ServletContext(javax.servlet.ServletContext) ParserUtils(org.apache.jasper.xmlparser.ParserUtils) Vector(java.util.Vector)

Example 3 with TreeNode

use of org.apache.jasper.xmlparser.TreeNode in project tomcat70 by apache.

the class TagLibraryInfoImpl method createTagFileInfo.

/*
     * Parses the tag file directives of the given TagFile and turns them into a
     * TagInfo.
     * 
     * @param elem The <tag-file> element in the TLD @param uri The location of
     * the TLD, in case the tag file is specified relative to it @param jarFile
     * The JAR file, in case the tag file is packaged in a JAR
     * 
     * @return TagInfo corresponding to tag file directives
     */
private TagFileInfo createTagFileInfo(TreeNode elem, JarResource jarResource) throws JasperException {
    String name = null;
    String path = null;
    Iterator<TreeNode> list = elem.findChildren();
    while (list.hasNext()) {
        TreeNode child = list.next();
        String tname = child.getName();
        if ("name".equals(tname)) {
            name = child.getBody();
        } else if ("path".equals(tname)) {
            path = child.getBody();
        } else if ("example".equals(tname)) {
        // Ignore <example> element: Bugzilla 33538
        } else if ("tag-extension".equals(tname)) {
        // Ignore <tag-extension> element: Bugzilla 33538
        } else if ("icon".equals(tname) || "display-name".equals(tname) || "description".equals(tname)) {
        // Ignore these elements: Bugzilla 38015
        } else {
            if (log.isWarnEnabled()) {
                log.warn(Localizer.getMessage("jsp.warning.unknown.element.in.tagfile", tname));
            }
        }
    }
    if (path == null) {
        // path is required
        err.jspError("jsp.error.tagfile.missingPath");
    } else if (path.startsWith("/META-INF/tags")) {
        // Tag file packaged in JAR
        // See https://bz.apache.org/bugzilla/show_bug.cgi?id=46471
        // This needs to be removed once all the broken code that depends on
        // it has been removed
        ctxt.setTagFileJarResource(path, jarResource);
    } else if (!path.startsWith("/WEB-INF/tags")) {
        err.jspError("jsp.error.tagfile.illegalPath", path);
    }
    TagInfo tagInfo = TagFileProcessor.parseTagFileDirectives(parserController, name, path, jarResource, this);
    return new TagFileInfo(name, path, tagInfo);
}
Also used : TagFileInfo(javax.servlet.jsp.tagext.TagFileInfo) TreeNode(org.apache.jasper.xmlparser.TreeNode) TagInfo(javax.servlet.jsp.tagext.TagInfo)

Example 4 with TreeNode

use of org.apache.jasper.xmlparser.TreeNode in project tomcat70 by apache.

the class TldLocationsCache method tldScanWebXml.

/*
     * Populates taglib map described in web.xml.
     * 
     * This is not kept in sync with o.a.c.startup.TldConfig as the Jasper only
     * needs the URI to TLD mappings from scan web.xml whereas TldConfig needs
     * to scan the actual TLD files.
     */
private void tldScanWebXml() throws Exception {
    WebXml webXml = null;
    try {
        webXml = new WebXml(ctxt);
        if (webXml.getInputSource() == null) {
            return;
        }
        boolean validate = Boolean.parseBoolean(ctxt.getInitParameter(Constants.XML_VALIDATION_INIT_PARAM));
        String blockExternalString = ctxt.getInitParameter(Constants.XML_BLOCK_EXTERNAL_INIT_PARAM);
        boolean blockExternal;
        if (blockExternalString == null) {
            blockExternal = true;
        } else {
            blockExternal = Boolean.parseBoolean(blockExternalString);
        }
        // Parse the web application deployment descriptor
        ParserUtils pu = new ParserUtils(validate, blockExternal);
        TreeNode webtld = null;
        webtld = pu.parseXMLDocument(webXml.getSystemId(), webXml.getInputSource());
        // Allow taglib to be an element of the root or jsp-config (JSP2.0)
        TreeNode jspConfig = webtld.findChild("jsp-config");
        if (jspConfig != null) {
            webtld = jspConfig;
        }
        Iterator<TreeNode> taglibs = webtld.findChildren("taglib");
        while (taglibs.hasNext()) {
            // Parse the next <taglib> element
            TreeNode taglib = taglibs.next();
            String tagUri = null;
            String tagLoc = null;
            TreeNode child = taglib.findChild("taglib-uri");
            if (child != null)
                tagUri = child.getBody();
            child = taglib.findChild("taglib-location");
            if (child != null)
                tagLoc = child.getBody();
            // Save this location if appropriate
            if (tagLoc == null)
                continue;
            if (uriType(tagLoc) == NOROOT_REL_URI)
                tagLoc = "/WEB-INF/" + tagLoc;
            TldLocation location;
            if (tagLoc.endsWith(JAR_EXT)) {
                location = new TldLocation("META-INF/taglib.tld", ctxt.getResource(tagLoc).toString());
            } else {
                location = new TldLocation(tagLoc);
            }
            mappings.put(tagUri, location);
        }
    } finally {
        if (webXml != null) {
            webXml.close();
        }
    }
}
Also used : TreeNode(org.apache.jasper.xmlparser.TreeNode) ParserUtils(org.apache.jasper.xmlparser.ParserUtils)

Example 5 with TreeNode

use of org.apache.jasper.xmlparser.TreeNode in project tomcat70 by apache.

the class TldLocationsCache method tldScanStream.

/*
     * Scan the TLD contents in the specified input stream and add any new URIs
     * to the map.
     * 
     * @param resourcePath  Path of the resource
     * @param entryName     If the resource is a JAR file, the name of the entry
     *                      in the JAR file
     * @param stream        The input stream for the resource
     * @throws IOException
     */
private void tldScanStream(String resourcePath, String entryName, InputStream stream) throws IOException {
    try {
        // Parse the tag library descriptor at the specified resource path
        String uri = null;
        boolean validate = Boolean.parseBoolean(ctxt.getInitParameter(Constants.XML_VALIDATION_TLD_INIT_PARAM));
        String blockExternalString = ctxt.getInitParameter(Constants.XML_BLOCK_EXTERNAL_INIT_PARAM);
        boolean blockExternal;
        if (blockExternalString == null) {
            blockExternal = true;
        } else {
            blockExternal = Boolean.parseBoolean(blockExternalString);
        }
        ParserUtils pu = new ParserUtils(validate, blockExternal);
        TreeNode tld = pu.parseXMLDocument(resourcePath, stream);
        TreeNode uriNode = tld.findChild("uri");
        if (uriNode != null) {
            String body = uriNode.getBody();
            if (body != null)
                uri = body;
        }
        // present in the map
        if (uri != null && mappings.get(uri) == null) {
            TldLocation location;
            if (entryName == null) {
                location = new TldLocation(resourcePath);
            } else {
                location = new TldLocation(entryName, resourcePath);
            }
            mappings.put(uri, location);
        }
    } catch (JasperException e) {
        // Hack - makes exception handling simpler
        throw new IOException(e);
    }
}
Also used : JasperException(org.apache.jasper.JasperException) TreeNode(org.apache.jasper.xmlparser.TreeNode) IOException(java.io.IOException) ParserUtils(org.apache.jasper.xmlparser.ParserUtils)

Aggregations

TreeNode (org.apache.jasper.xmlparser.TreeNode)12 JasperException (org.apache.jasper.JasperException)5 ParserUtils (org.apache.jasper.xmlparser.ParserUtils)5 Vector (java.util.Vector)3 TagInfo (javax.servlet.jsp.tagext.TagInfo)3 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 Hashtable (java.util.Hashtable)2 FunctionInfo (javax.servlet.jsp.tagext.FunctionInfo)2 TagAttributeInfo (javax.servlet.jsp.tagext.TagAttributeInfo)2 TagFileInfo (javax.servlet.jsp.tagext.TagFileInfo)2 TagVariableInfo (javax.servlet.jsp.tagext.TagVariableInfo)2 ServletContext (javax.servlet.ServletContext)1 TagExtraInfo (javax.servlet.jsp.tagext.TagExtraInfo)1 TagLibraryValidator (javax.servlet.jsp.tagext.TagLibraryValidator)1 TagPlugin (org.apache.jasper.compiler.tagplugin.TagPlugin)1