Search in sources :

Example 1 with TreeNode

use of org.apache.sling.scripting.jsp.jasper.xmlparser.TreeNode in project sling by apache.

the class JspConfig method processWebDotXml.

private void processWebDotXml(ServletContext ctxt) throws JasperException {
    InputStream is = null;
    try {
        URL uri = ctxt.getResource(WEB_XML);
        if (uri == null) {
            // no web.xml
            return;
        }
        is = uri.openStream();
        InputSource ip = new InputSource(is);
        ip.setSystemId(uri.toExternalForm());
        ParserUtils pu = new ParserUtils();
        TreeNode webApp = pu.parseXMLDocument(WEB_XML, ip);
        if (webApp == null || getVersion(webApp) < 2.4) {
            defaultIsELIgnored = "true";
            return;
        }
        TreeNode jspConfig = webApp.findChild("jsp-config");
        if (jspConfig == null) {
            return;
        }
        jspProperties = new Vector();
        Iterator jspPropertyList = jspConfig.findChildren("jsp-property-group");
        while (jspPropertyList.hasNext()) {
            TreeNode element = (TreeNode) jspPropertyList.next();
            Iterator list = element.findChildren();
            Vector urlPatterns = new Vector();
            String pageEncoding = null;
            String scriptingInvalid = null;
            String elIgnored = null;
            String isXml = null;
            Vector includePrelude = new Vector();
            Vector includeCoda = new Vector();
            String deferredSyntaxAllowedAsLiteral = null;
            String trimDirectiveWhitespaces = null;
            while (list.hasNext()) {
                element = (TreeNode) 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();
            }
            if (urlPatterns.size() == 0) {
                continue;
            }
            // the matching logic easier.
            for (int p = 0; p < urlPatterns.size(); p++) {
                String urlPattern = (String) 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 follwoing:
                    // 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);
                JspPropertyGroup propertyGroup = new JspPropertyGroup(path, extension, property);
                jspProperties.addElement(propertyGroup);
            }
        }
    } catch (Exception ex) {
        throw new JasperException(ex);
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (Throwable t) {
            }
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) InputStream(java.io.InputStream) URL(java.net.URL) JasperException(org.apache.sling.scripting.jsp.jasper.JasperException) JasperException(org.apache.sling.scripting.jsp.jasper.JasperException) TreeNode(org.apache.sling.scripting.jsp.jasper.xmlparser.TreeNode) Iterator(java.util.Iterator) ParserUtils(org.apache.sling.scripting.jsp.jasper.xmlparser.ParserUtils) Vector(java.util.Vector)

Example 2 with TreeNode

use of org.apache.sling.scripting.jsp.jasper.xmlparser.TreeNode in project sling by apache.

the class TagLibraryInfoImpl method createTagInfo.

private TagInfo createTagInfo(TreeNode elem, String jspVersion) throws JasperException {
    String tagName = null;
    String tagClassName = null;
    String teiClassName = null;
    /*
         * Default body content for JSP 1.2 tag handlers (<body-content> has
         * become mandatory in JSP 2.0, because the default would be invalid for
         * simple tag handlers)
         */
    String bodycontent = "JSP";
    String info = null;
    String displayName = null;
    String smallIcon = null;
    String largeIcon = null;
    boolean dynamicAttributes = false;
    Vector attributeVector = new Vector();
    Vector variableVector = new Vector();
    Iterator list = elem.findChildren();
    while (list.hasNext()) {
        TreeNode element = (TreeNode) list.next();
        String tname = element.getName();
        if ("name".equals(tname)) {
            tagName = element.getBody();
        } else if ("tagclass".equals(tname) || "tag-class".equals(tname)) {
            tagClassName = element.getBody();
        } else if ("teiclass".equals(tname) || "tei-class".equals(tname)) {
            teiClassName = element.getBody();
        } else if ("bodycontent".equals(tname) || "body-content".equals(tname)) {
            bodycontent = element.getBody();
        } else if ("display-name".equals(tname)) {
            displayName = element.getBody();
        } else if ("small-icon".equals(tname)) {
            smallIcon = element.getBody();
        } else if ("large-icon".equals(tname)) {
            largeIcon = element.getBody();
        } else if ("icon".equals(tname)) {
            TreeNode icon = element.findChild("small-icon");
            if (icon != null) {
                smallIcon = icon.getBody();
            }
            icon = element.findChild("large-icon");
            if (icon != null) {
                largeIcon = icon.getBody();
            }
        } else if ("info".equals(tname) || "description".equals(tname)) {
            info = element.getBody();
        } else if ("variable".equals(tname)) {
            variableVector.addElement(createVariable(element));
        } else if ("attribute".equals(tname)) {
            attributeVector.addElement(createAttribute(element, jspVersion));
        } else if ("dynamic-attributes".equals(tname)) {
            dynamicAttributes = JspUtil.booleanValue(element.getBody());
        } else if ("example".equals(tname)) {
        // Ignored elements
        } else if ("tag-extension".equals(tname)) {
        // Ignored
        } else {
            if (log.isWarnEnabled()) {
                log.warn(Localizer.getMessage("jsp.warning.unknown.element.in.tag", tname));
            }
        }
    }
    TagExtraInfo tei = null;
    if (teiClassName != null && !teiClassName.equals("")) {
        try {
            Class teiClass = ctxt.getClassLoader().loadClass(teiClassName);
            tei = (TagExtraInfo) teiClass.newInstance();
        } catch (Exception e) {
            err.jspError("jsp.error.teiclass.instantiation", teiClassName, e);
        }
    }
    TagAttributeInfo[] tagAttributeInfo = new TagAttributeInfo[attributeVector.size()];
    attributeVector.copyInto(tagAttributeInfo);
    TagVariableInfo[] tagVariableInfos = new TagVariableInfo[variableVector.size()];
    variableVector.copyInto(tagVariableInfos);
    TagInfo taginfo = new TagInfo(tagName, tagClassName, bodycontent, info, this, tei, tagAttributeInfo, displayName, smallIcon, largeIcon, tagVariableInfos, dynamicAttributes);
    return taginfo;
}
Also used : TagAttributeInfo(javax.servlet.jsp.tagext.TagAttributeInfo) TagVariableInfo(javax.servlet.jsp.tagext.TagVariableInfo) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) JasperException(org.apache.sling.scripting.jsp.jasper.JasperException) TreeNode(org.apache.sling.scripting.jsp.jasper.xmlparser.TreeNode) TagExtraInfo(javax.servlet.jsp.tagext.TagExtraInfo) TagInfo(javax.servlet.jsp.tagext.TagInfo) Iterator(java.util.Iterator) Vector(java.util.Vector)

Example 3 with TreeNode

use of org.apache.sling.scripting.jsp.jasper.xmlparser.TreeNode in project sling 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(JspCompilationContext ctxt, String uri, InputStream in, URL jarFileUrl) throws JasperException {
    Vector tagVector = new Vector();
    Vector tagFileVector = new Vector();
    Hashtable functionTable = new Hashtable();
    // Create an iterator over the child elements of our <taglib> element
    ParserUtils pu = new ParserUtils();
    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 list = tld.findChildren();
    while (list.hasNext()) {
        TreeNode element = (TreeNode) 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, uri, jarFileUrl);
            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 (// Ignored elements
        "display-name".equals(tname) || "small-icon".equals(tname) || "large-icon".equals(tname) || "listener".equals(tname)) {
            ;
        } 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");
    }
    if (jspversion == null) {
        err.jspError("jsp.error.tld.mandatory.element.missing", "jsp-version");
    }
    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 enumeration = functionTable.elements();
    while (enumeration.hasMoreElements()) {
        this.functions[i++] = (FunctionInfo) enumeration.nextElement();
    }
}
Also used : TagFileInfo(javax.servlet.jsp.tagext.TagFileInfo) Enumeration(java.util.Enumeration) Hashtable(java.util.Hashtable) TreeNode(org.apache.sling.scripting.jsp.jasper.xmlparser.TreeNode) Iterator(java.util.Iterator) FunctionInfo(javax.servlet.jsp.tagext.FunctionInfo) ParserUtils(org.apache.sling.scripting.jsp.jasper.xmlparser.ParserUtils) Vector(java.util.Vector)

Example 4 with TreeNode

use of org.apache.sling.scripting.jsp.jasper.xmlparser.TreeNode in project sling by apache.

the class TagPluginManager method init.

private void init(ErrorDispatcher err) throws JasperException {
    if (initialized)
        return;
    InputStream is = ctxt.getResourceAsStream(TAG_PLUGINS_XML);
    if (is == null)
        return;
    TreeNode root = (new ParserUtils()).parseXMLDocument(TAG_PLUGINS_XML, is);
    if (root == null) {
        return;
    }
    if (!TAG_PLUGINS_ROOT_ELEM.equals(root.getName())) {
        err.jspError("jsp.error.plugin.wrongRootElement", TAG_PLUGINS_XML, TAG_PLUGINS_ROOT_ELEM);
    }
    tagPlugins = new HashMap();
    Iterator pluginList = root.findChildren("tag-plugin");
    while (pluginList.hasNext()) {
        TreeNode pluginNode = (TreeNode) 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 = this.getClass().getClassLoader().loadClass(pluginClassStr);
            tagPlugin = (TagPlugin) pluginClass.newInstance();
        } catch (Exception e) {
            throw new JasperException(e);
        }
        if (tagPlugin == null) {
            return;
        }
        tagPlugins.put(tagClass, tagPlugin);
    }
    initialized = true;
}
Also used : HashMap(java.util.HashMap) JasperException(org.apache.sling.scripting.jsp.jasper.JasperException) InputStream(java.io.InputStream) TreeNode(org.apache.sling.scripting.jsp.jasper.xmlparser.TreeNode) Iterator(java.util.Iterator) TagPlugin(org.apache.sling.scripting.jsp.jasper.compiler.tagplugin.TagPlugin) ParserUtils(org.apache.sling.scripting.jsp.jasper.xmlparser.ParserUtils) JasperException(org.apache.sling.scripting.jsp.jasper.JasperException)

Example 5 with TreeNode

use of org.apache.sling.scripting.jsp.jasper.xmlparser.TreeNode in project sling by apache.

the class TagLibraryInfoImpl method createFunctionInfo.

FunctionInfo createFunctionInfo(TreeNode elem) {
    String name = null;
    String klass = null;
    String signature = null;
    Iterator list = elem.findChildren();
    while (list.hasNext()) {
        TreeNode element = (TreeNode) list.next();
        String tname = element.getName();
        if ("name".equals(tname)) {
            name = element.getBody();
        } else if ("function-class".equals(tname)) {
            klass = element.getBody();
        } else if ("function-signature".equals(tname)) {
            signature = element.getBody();
        } else if (// Ignored elements
        "display-name".equals(tname) || "small-icon".equals(tname) || "large-icon".equals(tname) || "description".equals(tname) || "example".equals(tname)) {
        } else {
            if (log.isWarnEnabled()) {
                log.warn(Localizer.getMessage("jsp.warning.unknown.element.in.function", tname));
            }
        }
    }
    return new FunctionInfo(name, klass, signature);
}
Also used : TreeNode(org.apache.sling.scripting.jsp.jasper.xmlparser.TreeNode) Iterator(java.util.Iterator) FunctionInfo(javax.servlet.jsp.tagext.FunctionInfo)

Aggregations

TreeNode (org.apache.sling.scripting.jsp.jasper.xmlparser.TreeNode)15 Iterator (java.util.Iterator)12 ParserUtils (org.apache.sling.scripting.jsp.jasper.xmlparser.ParserUtils)8 InputStream (java.io.InputStream)5 JasperException (org.apache.sling.scripting.jsp.jasper.JasperException)5 URL (java.net.URL)3 Vector (java.util.Vector)3 InputSource (org.xml.sax.InputSource)3 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 MalformedURLException (java.net.MalformedURLException)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 TagInfo (javax.servlet.jsp.tagext.TagInfo)2 TagVariableInfo (javax.servlet.jsp.tagext.TagVariableInfo)2 Enumeration (java.util.Enumeration)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1