Search in sources :

Example 1 with JasperException

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

the class ImplicitTagLibraryInfo method getTagFile.

/**
     * Checks to see if the given tag name maps to a tag file path,
     * and if so, parses the corresponding tag file.
     *
     * @return The TagFileInfo corresponding to the given tag name, or null if
     * the given tag name is not implemented as a tag file
     */
public TagFileInfo getTagFile(String shortName) {
    TagFileInfo tagFile = super.getTagFile(shortName);
    if (tagFile == null) {
        String path = (String) tagFileMap.get(shortName);
        if (path == null) {
            return null;
        }
        TagInfo tagInfo = null;
        try {
            tagInfo = TagFileProcessor.parseTagFileDirectives(pc, shortName, path, this);
        } catch (JasperException je) {
            throw new RuntimeException(je.toString(), je);
        }
        tagFile = new TagFileInfo(shortName, path, tagInfo);
        vec.addElement(tagFile);
        this.tagFiles = new TagFileInfo[vec.size()];
        vec.copyInto(this.tagFiles);
    }
    return tagFile;
}
Also used : TagFileInfo(javax.servlet.jsp.tagext.TagFileInfo) JasperException(org.apache.sling.scripting.jsp.jasper.JasperException) TagInfo(javax.servlet.jsp.tagext.TagInfo)

Example 2 with JasperException

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

the class JDTCompiler method generateClass.

/**
     * Compile the servlet from .java file to .class file
     */
@Override
protected void generateClass(String[] smap) throws FileNotFoundException, JasperException, Exception {
    long t1 = 0;
    if (log.isDebugEnabled()) {
        t1 = System.currentTimeMillis();
    }
    final String sourceFile = ctxt.getServletJavaFileName();
    final String packageName = ctxt.getServletPackageName();
    final String targetClassName = ((packageName.length() != 0) ? (packageName + ".") : "") + ctxt.getServletClassName();
    final CompilationUnit unit = new CompilationUnitWithSource() {

        /**
             * @see org.apache.sling.commons.compiler.CompilationUnit#getLastModified()
             */
        public long getLastModified() {
            return -1;
        }

        /**
             * @see org.apache.sling.commons.compiler.CompilationUnit#getMainClassName()
             */
        public String getMainClassName() {
            return targetClassName;
        }

        /**
             * @see org.apache.sling.commons.compiler.CompilationUnit#getSource()
             */
        public Reader getSource() throws IOException {
            return new BufferedReader(new InputStreamReader(ctxt.getInputStream(sourceFile), ctxt.getOptions().getJavaEncoding()));
        }

        /**
             * @see org.apache.sling.commons.compiler.CompilationUnitWithSource#getFileName()
             */
        public String getFileName() {
            return sourceFile;
        }
    };
    final Options options = new Options();
    options.put(Options.KEY_CLASS_LOADER_WRITER, ctxt.getRuntimeContext().getIOProvider().getClassLoaderWriter());
    options.put(Options.KEY_GENERATE_DEBUG_INFO, ctxt.getOptions().getClassDebugInfo());
    // Source JVM
    if (ctxt.getOptions().getCompilerSourceVM() != null) {
        options.put(Options.KEY_SOURCE_VERSION, ctxt.getOptions().getCompilerSourceVM());
    } else {
        // Default to 1.6
        options.put(Options.KEY_SOURCE_VERSION, Options.VERSION_1_6);
    }
    // Target JVM
    if (ctxt.getOptions().getCompilerTargetVM() != null) {
        options.put(Options.KEY_TARGET_VERSION, ctxt.getOptions().getCompilerTargetVM());
    } else {
        // Default to 1.6
        options.put(Options.KEY_TARGET_VERSION, Options.VERSION_1_6);
    }
    final ArrayList<JavacErrorDetail> problemList = new ArrayList<JavacErrorDetail>();
    final CompilationResult result = this.ctxt.getRuntimeContext().getIOProvider().getJavaCompiler().compile(new CompilationUnit[] { unit }, options);
    if (result.getErrors() != null) {
        for (final CompilerMessage cm : result.getErrors()) {
            final String name = cm.getFile();
            try {
                problemList.add(ErrorDispatcher.createJavacError(name, pageNodes, new StringBuffer(cm.getMessage()), cm.getLine(), ctxt));
            } catch (JasperException e) {
                log.error("Error visiting node", e);
            }
        }
    }
    if (!ctxt.keepGenerated()) {
        ctxt.delete(ctxt.getServletJavaFileName());
    }
    if (!problemList.isEmpty()) {
        JavacErrorDetail[] jeds = problemList.toArray(new JavacErrorDetail[0]);
        errDispatcher.javacError(jeds);
    }
    if (log.isDebugEnabled()) {
        long t2 = System.currentTimeMillis();
        log.debug("Compiled " + ctxt.getServletJavaFileName() + " " + (t2 - t1) + "ms");
    }
    if (ctxt.isPrototypeMode()) {
        return;
    }
    // JSR45 Support
    if (!this.options.isSmapSuppressed()) {
        SmapUtil.installSmap(getCompilationContext(), smap);
    }
}
Also used : CompilationUnit(org.apache.sling.commons.compiler.CompilationUnit) Options(org.apache.sling.commons.compiler.Options) CompilationUnitWithSource(org.apache.sling.commons.compiler.CompilationUnitWithSource) CompilerMessage(org.apache.sling.commons.compiler.CompilerMessage) InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) JasperException(org.apache.sling.scripting.jsp.jasper.JasperException) BufferedReader(java.io.BufferedReader) CompilationResult(org.apache.sling.commons.compiler.CompilationResult)

Example 3 with JasperException

use of org.apache.sling.scripting.jsp.jasper.JasperException 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 4 with JasperException

use of org.apache.sling.scripting.jsp.jasper.JasperException 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 5 with JasperException

use of org.apache.sling.scripting.jsp.jasper.JasperException 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)

Aggregations

JasperException (org.apache.sling.scripting.jsp.jasper.JasperException)42 IOException (java.io.IOException)31 ServletException (javax.servlet.ServletException)19 PrivilegedActionException (java.security.PrivilegedActionException)17 Method (java.lang.reflect.Method)14 FileNotFoundException (java.io.FileNotFoundException)9 MalformedURLException (java.net.MalformedURLException)6 Iterator (java.util.Iterator)6 InputStream (java.io.InputStream)5 JarFile (java.util.jar.JarFile)5 TagInfo (javax.servlet.jsp.tagext.TagInfo)4 TreeNode (org.apache.sling.scripting.jsp.jasper.xmlparser.TreeNode)4 DependencyResolutionRequiredException (org.apache.maven.artifact.DependencyResolutionRequiredException)3 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)3 SAXException (org.xml.sax.SAXException)3 File (java.io.File)2 InputStreamReader (java.io.InputStreamReader)2 URL (java.net.URL)2 Enumeration (java.util.Enumeration)2 HashMap (java.util.HashMap)2