Java Code Examples for org.apache.jasper.xmlparser.ParserUtils#parseXMLDocument()

The following examples show how to use org.apache.jasper.xmlparser.ParserUtils#parseXMLDocument() . 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: TldLocationsCache.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
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();
            }
        }
    }
 
Example 2
Source File: TldLocationsCache.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
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;
        }

        // Add implicit map entry only if its uri is not already
        // 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);
    }
}
 
Example 3
Source File: TagLibraryInfoImpl.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
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 ("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))
            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();
    }
}
 
Example 4
Source File: TldLocationsCache.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
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();
            }
        }
    }
 
Example 5
Source File: TldLocationsCache.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
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;
        }

        // Add implicit map entry only if its uri is not already
        // 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);
    }
}
 
Example 6
Source File: TagLibraryInfoImpl.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
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 ("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))
            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();
    }
}