Search in sources :

Example 11 with TagInfo

use of javax.servlet.jsp.tagext.TagInfo in project sling by apache.

the class TagFileProcessor method loadTagFile.

/**
     * Compiles and loads a tagfile.
     */
private void loadTagFile(Compiler compiler, String tagFilePath, Node.CustomTag n, PageInfo parentPageInfo) throws JasperException {
    JspCompilationContext ctxt = compiler.getCompilationContext();
    JspRuntimeContext rctxt = ctxt.getRuntimeContext();
    rctxt.lockTagFileLoading(tagFilePath);
    try {
        JspServletWrapper wrapper = rctxt.getWrapper(tagFilePath);
        if (wrapper == null) {
            wrapper = new JspServletWrapper(ctxt.getServletContext(), ctxt.getOptions(), tagFilePath, n.getTagInfo(), ctxt.getRuntimeContext(), compiler.getDefaultIsSession(), ctxt.getTagFileJarUrl(tagFilePath));
            wrapper = rctxt.addWrapper(tagFilePath, wrapper);
        // Use same classloader and classpath for compiling tag files
        //wrapper.getJspEngineContext().setClassLoader(ctxt.getClassLoader());
        //wrapper.getJspEngineContext().setClassPath(ctxt.getClassPath());
        } else {
            // Make sure that JspCompilationContext gets the latest TagInfo
            // for the tag file. TagInfo instance was created the last
            // time the tag file was scanned for directives, and the tag
            // file may have been modified since then.
            wrapper.getJspEngineContext().setTagInfo(n.getTagInfo());
        }
        Class tagClazz;
        int tripCount = wrapper.incTripCount();
        try {
            if (tripCount > 0) {
                final String postfix = "_" + String.valueOf(tripCount);
                // When tripCount is greater than zero, a circular
                // dependency exists. The circularily dependant tag
                // file is compiled in prototype mode, to avoid infinite
                // recursion.
                final String tempTagFilePath = tagFilePath + postfix;
                final TagInfo tempTagInfo = new JasperTagInfo(n.getTagInfo().getTagName(), n.getTagInfo().getTagClassName() + postfix, n.getTagInfo().getBodyContent(), n.getTagInfo().getInfoString(), n.getTagInfo().getTagLibrary(), n.getTagInfo().getTagExtraInfo(), n.getTagInfo().getAttributes(), n.getTagInfo().getDisplayName(), n.getTagInfo().getSmallIcon(), n.getTagInfo().getLargeIcon(), n.getTagInfo().getTagVariableInfos(), ((JasperTagInfo) n.getTagInfo()).getDynamicAttributesMapName());
                JspServletWrapper tempWrapper = new JspServletWrapper(ctxt.getServletContext(), ctxt.getOptions(), tagFilePath, tempTagInfo, ctxt.getRuntimeContext(), compiler.getDefaultIsSession(), ctxt.getTagFileJarUrl(tempTagFilePath));
                tagClazz = tempWrapper.loadTagFilePrototype();
                tempVector.add(tempWrapper.getJspEngineContext().getCompiler());
                String name = JspUtil.getCanonicalName(tagClazz);
                final int underscorePos = name.lastIndexOf(postfix);
                if (underscorePos > -1) {
                    n.setTagHandlerClassName(name.substring(0, underscorePos));
                }
            } else {
                tagClazz = wrapper.loadTagFile();
            }
        } finally {
            wrapper.decTripCount();
        }
        // can only be obtained from the tag instance.
        try {
            Object tagIns = tagClazz.newInstance();
            if (tagIns instanceof JspSourceDependent) {
                Iterator iter = ((List) ((JspSourceDependent) tagIns).getDependants()).iterator();
                while (iter.hasNext()) {
                    parentPageInfo.addDependant((String) iter.next());
                }
            }
        } catch (Exception e) {
        // ignore errors
        }
        n.setTagHandlerClass(tagClazz);
    } finally {
        rctxt.unlockTagFileLoading(tagFilePath);
    }
}
Also used : JspServletWrapper(org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper) JspCompilationContext(org.apache.sling.scripting.jsp.jasper.JspCompilationContext) JspSourceDependent(org.apache.sling.scripting.jsp.jasper.runtime.JspSourceDependent) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) JasperException(org.apache.sling.scripting.jsp.jasper.JasperException) TagInfo(javax.servlet.jsp.tagext.TagInfo) Iterator(java.util.Iterator) List(java.util.List)

Example 12 with TagInfo

use of javax.servlet.jsp.tagext.TagInfo in project sling 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 correspoding to tag file directives
     */
private TagFileInfo createTagFileInfo(TreeNode elem, String uri, URL jarFileUrl) throws JasperException {
    String name = null;
    String path = null;
    Iterator list = elem.findChildren();
    while (list.hasNext()) {
        TreeNode child = (TreeNode) 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.startsWith("/META-INF/tags")) {
        // Tag file packaged in JAR
        ctxt.setTagFileJarUrl(path, jarFileUrl);
    } else if (!path.startsWith("/WEB-INF/tags")) {
        err.jspError("jsp.error.tagfile.illegalPath", path);
    }
    TagInfo tagInfo = TagFileProcessor.parseTagFileDirectives(parserController, name, path, this);
    return new TagFileInfo(name, path, tagInfo);
}
Also used : TagFileInfo(javax.servlet.jsp.tagext.TagFileInfo) TreeNode(org.apache.sling.scripting.jsp.jasper.xmlparser.TreeNode) TagInfo(javax.servlet.jsp.tagext.TagInfo) Iterator(java.util.Iterator)

Example 13 with TagInfo

use of javax.servlet.jsp.tagext.TagInfo in project sling by apache.

the class Parser method parseCustomTag.

/*
     * # '<' CustomAction CustomActionBody
     *
     * CustomAction ::= TagPrefix ':' CustomActionName
     *
     * TagPrefix ::= Name
     *
     * CustomActionName ::= Name
     *
     * CustomActionBody ::= ( Attributes CustomActionEnd ) | <TRANSLATION_ERROR>
     *
     * Attributes ::= ( S Attribute )* S?
     *
     * CustomActionEnd ::= CustomActionTagDependent | CustomActionJSPContent |
     * CustomActionScriptlessContent
     *
     * CustomActionTagDependent ::= TagDependentOptionalBody
     *
     * CustomActionJSPContent ::= OptionalBody
     *
     * CustomActionScriptlessContent ::= ScriptlessOptionalBody
     */
private boolean parseCustomTag(Node parent) throws JasperException {
    if (reader.peekChar() != '<') {
        return false;
    }
    // Parse 'CustomAction' production (tag prefix and custom action name)
    // skip '<'
    reader.nextChar();
    String tagName = reader.parseToken(false);
    int i = tagName.indexOf(':');
    if (i == -1) {
        reader.reset(start);
        return false;
    }
    String prefix = tagName.substring(0, i);
    String shortTagName = tagName.substring(i + 1);
    // Check if this is a user-defined tag.
    String uri = pageInfo.getURI(prefix);
    if (uri == null) {
        reader.reset(start);
        // Remember the prefix for later error checking
        pageInfo.putNonCustomTagPrefix(prefix, reader.mark());
        return false;
    }
    TagLibraryInfo tagLibInfo = pageInfo.getTaglib(uri);
    TagInfo tagInfo = tagLibInfo.getTag(shortTagName);
    TagFileInfo tagFileInfo = tagLibInfo.getTagFile(shortTagName);
    if (tagInfo == null && tagFileInfo == null) {
        err.jspError(start, "jsp.error.bad_tag", shortTagName, prefix);
    }
    Class tagHandlerClass = null;
    if (tagInfo != null) {
        // Must be a classic tag, load it here.
        // tag files will be loaded later, in TagFileProcessor
        String handlerClassName = tagInfo.getTagClassName();
        try {
            tagHandlerClass = ctxt.getClassLoader().loadClass(handlerClassName);
        } catch (Exception e) {
            err.jspError(start, "jsp.error.loadclass.taghandler", handlerClassName, tagName);
        }
    }
    // Parse 'CustomActionBody' production:
    // At this point we are committed - if anything fails, we produce
    // a translation error.
    // Parse 'Attributes' production:
    Attributes attrs = parseAttributes();
    reader.skipSpaces();
    // Parse 'CustomActionEnd' production:
    if (reader.matches("/>")) {
        if (tagInfo != null) {
            new Node.CustomTag(tagName, prefix, shortTagName, uri, attrs, start, parent, tagInfo, tagHandlerClass);
        } else {
            new Node.CustomTag(tagName, prefix, shortTagName, uri, attrs, start, parent, tagFileInfo);
        }
        return true;
    }
    // Now we parse one of 'CustomActionTagDependent',
    // 'CustomActionJSPContent', or 'CustomActionScriptlessContent'.
    // depending on body-content in TLD.
    // Looking for a body, it still can be empty; but if there is a
    // a tag body, its syntax would be dependent on the type of
    // body content declared in the TLD.
    String bc;
    if (tagInfo != null) {
        bc = tagInfo.getBodyContent();
    } else {
        bc = tagFileInfo.getTagInfo().getBodyContent();
    }
    Node tagNode = null;
    if (tagInfo != null) {
        tagNode = new Node.CustomTag(tagName, prefix, shortTagName, uri, attrs, start, parent, tagInfo, tagHandlerClass);
    } else {
        tagNode = new Node.CustomTag(tagName, prefix, shortTagName, uri, attrs, start, parent, tagFileInfo);
    }
    parseOptionalBody(tagNode, tagName, bc);
    return true;
}
Also used : TagFileInfo(javax.servlet.jsp.tagext.TagFileInfo) TagInfo(javax.servlet.jsp.tagext.TagInfo) Attributes(org.xml.sax.Attributes) TagLibraryInfo(javax.servlet.jsp.tagext.TagLibraryInfo) FileNotFoundException(java.io.FileNotFoundException) JasperException(org.apache.sling.scripting.jsp.jasper.JasperException)

Example 14 with TagInfo

use of javax.servlet.jsp.tagext.TagInfo in project sling by apache.

the class JspDocumentParser method parseCustomAction.

/*
     * Checks if the XML element with the given tag name is a custom action,
     * and returns the corresponding Node object.
     */
private Node parseCustomAction(String qName, String localName, String uri, Attributes nonTaglibAttrs, Attributes nonTaglibXmlnsAttrs, Attributes taglibAttrs, Mark start, Node parent) throws SAXException {
    // Check if this is a user-defined (custom) tag
    TagLibraryInfo tagLibInfo = pageInfo.getTaglib(uri);
    if (tagLibInfo == null) {
        return null;
    }
    TagInfo tagInfo = tagLibInfo.getTag(localName);
    TagFileInfo tagFileInfo = tagLibInfo.getTagFile(localName);
    if (tagInfo == null && tagFileInfo == null) {
        throw new SAXException(Localizer.getMessage("jsp.error.xml.bad_tag", localName, uri));
    }
    Class tagHandlerClass = null;
    if (tagInfo != null) {
        String handlerClassName = tagInfo.getTagClassName();
        try {
            tagHandlerClass = ctxt.getClassLoader().loadClass(handlerClassName);
        } catch (Exception e) {
            throw new SAXException(Localizer.getMessage("jsp.error.loadclass.taghandler", handlerClassName, qName), e);
        }
    }
    String prefix = "";
    int colon = qName.indexOf(':');
    if (colon != -1) {
        prefix = qName.substring(0, colon);
    }
    Node.CustomTag ret = null;
    if (tagInfo != null) {
        ret = new Node.CustomTag(qName, prefix, localName, uri, nonTaglibAttrs, nonTaglibXmlnsAttrs, taglibAttrs, start, parent, tagInfo, tagHandlerClass);
    } else {
        ret = new Node.CustomTag(qName, prefix, localName, uri, nonTaglibAttrs, nonTaglibXmlnsAttrs, taglibAttrs, start, parent, tagFileInfo);
    }
    return ret;
}
Also used : TagFileInfo(javax.servlet.jsp.tagext.TagFileInfo) TagInfo(javax.servlet.jsp.tagext.TagInfo) TagLibraryInfo(javax.servlet.jsp.tagext.TagLibraryInfo) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException) JasperException(org.apache.sling.scripting.jsp.jasper.JasperException) SAXException(org.xml.sax.SAXException)

Example 15 with TagInfo

use of javax.servlet.jsp.tagext.TagInfo in project tomcat70 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<TagAttributeInfo> attributeVector = new Vector<TagAttributeInfo>();
    Vector<TagVariableInfo> variableVector = new Vector<TagVariableInfo>();
    Iterator<TreeNode> list = elem.findChildren();
    while (list.hasNext()) {
        TreeNode element = 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(e, "jsp.error.teiclass.instantiation", teiClassName);
        }
    }
    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) JasperException(org.apache.jasper.JasperException) FileNotFoundException(java.io.FileNotFoundException) TreeNode(org.apache.jasper.xmlparser.TreeNode) TagExtraInfo(javax.servlet.jsp.tagext.TagExtraInfo) TagInfo(javax.servlet.jsp.tagext.TagInfo) Vector(java.util.Vector)

Aggregations

TagInfo (javax.servlet.jsp.tagext.TagInfo)16 TagFileInfo (javax.servlet.jsp.tagext.TagFileInfo)9 FileNotFoundException (java.io.FileNotFoundException)7 TagAttributeInfo (javax.servlet.jsp.tagext.TagAttributeInfo)5 TagLibraryInfo (javax.servlet.jsp.tagext.TagLibraryInfo)5 JasperException (org.apache.sling.scripting.jsp.jasper.JasperException)5 IOException (java.io.IOException)4 JasperException (org.apache.jasper.JasperException)4 Iterator (java.util.Iterator)3 Vector (java.util.Vector)3 TagExtraInfo (javax.servlet.jsp.tagext.TagExtraInfo)3 TreeNode (org.apache.jasper.xmlparser.TreeNode)3 Hashtable (java.util.Hashtable)2 TagVariableInfo (javax.servlet.jsp.tagext.TagVariableInfo)2 TreeNode (org.apache.sling.scripting.jsp.jasper.xmlparser.TreeNode)2 Attributes (org.xml.sax.Attributes)2 SAXException (org.xml.sax.SAXException)2 SAXParseException (org.xml.sax.SAXParseException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 List (java.util.List)1