Search in sources :

Example 1 with ParserUtils

use of org.apache.jasper.xmlparser.ParserUtils 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 2 with ParserUtils

use of org.apache.jasper.xmlparser.ParserUtils 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 3 with ParserUtils

use of org.apache.jasper.xmlparser.ParserUtils 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)

Example 4 with ParserUtils

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

the class TagPluginManager method loadTagPlugins.

private void loadTagPlugins(ErrorDispatcher err, InputStream is) throws JasperException {
    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(false, blockExternal);
    TreeNode root = pu.parseXMLDocument(TAG_PLUGINS_XML, is);
    if (!TAG_PLUGINS_ROOT_ELEM.equals(root.getName())) {
        err.jspError("jsp.error.plugin.wrongRootElement", TAG_PLUGINS_XML, TAG_PLUGINS_ROOT_ELEM);
    }
    tagPlugins = new HashMap<String, TagPlugin>();
    Iterator<TreeNode> pluginList = root.findChildren("tag-plugin");
    while (pluginList.hasNext()) {
        TreeNode pluginNode = pluginList.next();
        TreeNode tagClassNode = pluginNode.findChild("tag-class");
        if (tagClassNode == null) {
            // Error
            return;
        }
        String tagClass = tagClassNode.getBody().trim();
        TreeNode pluginClassNode = pluginNode.findChild("plugin-class");
        if (pluginClassNode == null) {
            // Error
            return;
        }
        String pluginClassStr = pluginClassNode.getBody();
        TagPlugin tagPlugin = null;
        try {
            Class<?> pluginClass = ctxt.getClassLoader().loadClass(pluginClassStr);
            tagPlugin = (TagPlugin) pluginClass.newInstance();
            tagPlugins.put(tagClass, tagPlugin);
        } catch (Exception e) {
            throw new JasperException(e);
        }
    }
    initialized = true;
}
Also used : JasperException(org.apache.jasper.JasperException) TreeNode(org.apache.jasper.xmlparser.TreeNode) TagPlugin(org.apache.jasper.compiler.tagplugin.TagPlugin) ParserUtils(org.apache.jasper.xmlparser.ParserUtils) IOException(java.io.IOException) JasperException(org.apache.jasper.JasperException)

Example 5 with ParserUtils

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

the class JspConfig method processWebDotXml.

private void processWebDotXml() throws JasperException {
    WebXml webXml = null;
    try {
        webXml = new WebXml(ctxt);
        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);
        }
        TreeNode webApp = null;
        if (webXml.getInputSource() != null) {
            ParserUtils pu = new ParserUtils(validate, blockExternal);
            webApp = pu.parseXMLDocument(webXml.getSystemId(), webXml.getInputSource());
        }
        if (webApp == null || getVersion(webApp) < 2.4) {
            defaultIsELIgnored = "true";
            defaultDeferedSyntaxAllowedAsLiteral = "true";
            return;
        }
        if (getVersion(webApp) < 2.5) {
            defaultDeferedSyntaxAllowedAsLiteral = "true";
        }
        TreeNode jspConfig = webApp.findChild("jsp-config");
        if (jspConfig == null) {
            return;
        }
        jspProperties = new Vector<JspPropertyGroup>();
        Iterator<TreeNode> jspPropertyList = jspConfig.findChildren("jsp-property-group");
        while (jspPropertyList.hasNext()) {
            TreeNode element = jspPropertyList.next();
            Iterator<TreeNode> list = element.findChildren();
            Vector<String> urlPatterns = new Vector<String>();
            String pageEncoding = null;
            String scriptingInvalid = null;
            String elIgnored = null;
            String isXml = null;
            Vector<String> includePrelude = new Vector<String>();
            Vector<String> includeCoda = new Vector<String>();
            String deferredSyntaxAllowedAsLiteral = null;
            String trimDirectiveWhitespaces = null;
            String defaultContentType = null;
            String buffer = null;
            String errorOnUndeclaredNamespace = null;
            while (list.hasNext()) {
                element = list.next();
                String tname = element.getName();
                if ("url-pattern".equals(tname))
                    urlPatterns.addElement(element.getBody());
                else if ("page-encoding".equals(tname))
                    pageEncoding = element.getBody();
                else if ("is-xml".equals(tname))
                    isXml = element.getBody();
                else if ("el-ignored".equals(tname))
                    elIgnored = element.getBody();
                else if ("scripting-invalid".equals(tname))
                    scriptingInvalid = element.getBody();
                else if ("include-prelude".equals(tname))
                    includePrelude.addElement(element.getBody());
                else if ("include-coda".equals(tname))
                    includeCoda.addElement(element.getBody());
                else if ("deferred-syntax-allowed-as-literal".equals(tname))
                    deferredSyntaxAllowedAsLiteral = element.getBody();
                else if ("trim-directive-whitespaces".equals(tname))
                    trimDirectiveWhitespaces = element.getBody();
                else if ("default-content-type".equals(tname))
                    defaultContentType = element.getBody();
                else if ("buffer".equals(tname))
                    buffer = element.getBody();
                else if ("error-on-undeclared-namespace".equals(tname))
                    errorOnUndeclaredNamespace = element.getBody();
            }
            if (urlPatterns.size() == 0) {
                continue;
            }
            // the matching logic easier.
            for (int p = 0; p < urlPatterns.size(); p++) {
                String urlPattern = urlPatterns.elementAt(p);
                String path = null;
                String extension = null;
                if (urlPattern.indexOf('*') < 0) {
                    // Exact match
                    path = urlPattern;
                } else {
                    int i = urlPattern.lastIndexOf('/');
                    String file;
                    if (i >= 0) {
                        path = urlPattern.substring(0, i + 1);
                        file = urlPattern.substring(i + 1);
                    } else {
                        file = urlPattern;
                    }
                    // pattern must be "*", or of the form "*.jsp"
                    if (file.equals("*")) {
                        extension = "*";
                    } else if (file.startsWith("*.")) {
                        extension = file.substring(file.indexOf('.') + 1);
                    }
                    // The url patterns are reconstructed as the following:
                    // path != null, extension == null:  / or /foo/bar.ext
                    // path == null, extension != null:  *.ext
                    // path != null, extension == "*":   /foo/*
                    boolean isStar = "*".equals(extension);
                    if ((path == null && (extension == null || isStar)) || (path != null && !isStar)) {
                        if (log.isWarnEnabled()) {
                            log.warn(Localizer.getMessage("jsp.warning.bad.urlpattern.propertygroup", urlPattern));
                        }
                        continue;
                    }
                }
                JspProperty property = new JspProperty(isXml, elIgnored, scriptingInvalid, pageEncoding, includePrelude, includeCoda, deferredSyntaxAllowedAsLiteral, trimDirectiveWhitespaces, defaultContentType, buffer, errorOnUndeclaredNamespace);
                JspPropertyGroup propertyGroup = new JspPropertyGroup(path, extension, property);
                jspProperties.addElement(propertyGroup);
            }
        }
    } catch (Exception ex) {
        throw new JasperException(ex);
    } finally {
        if (webXml != null) {
            webXml.close();
        }
    }
}
Also used : JasperException(org.apache.jasper.JasperException) JasperException(org.apache.jasper.JasperException) TreeNode(org.apache.jasper.xmlparser.TreeNode) ParserUtils(org.apache.jasper.xmlparser.ParserUtils) Vector(java.util.Vector)

Aggregations

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