You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Jaime Garza <Ja...@newScale.com> on 2004/04/09 03:08:38 UTC

[Jelly] A contribution: I added these methods to JellyContext

...as I am using jelly as part of an XML cache, in which I don't have a
File, URL, or URI.  It would be nice if these methods can be reviewed
and checked in.  Of course I am not a commiter, so I need to hand these
over by mail (is there any other way?)  I checked out the source this
morning.

ISSUE: I did ChildContext with a URL of '/' as no parent URL can be
given
ISSUE: Very brute force method (copy/paste design pattern), but I'll
leave the elegance to the owner
ISSUE: I am not showing the new imports (SAX Input source)

Here they go...

    /**
     * Attempts to parse the script from the given input stream using
the
     * {@link #getResource} method then returns the compiled script.
     */
    public Script compileScript(InputStream in) throws JellyException {
        XMLParser parser = getXMLParser();
        parser.setContext(this);
        Script script = null;
        try {
            script = parser.parse(in);
        } catch (IOException e) {
            throw new JellyException("Could not parse Jelly script",e);
        } catch (SAXException e) {
            throw new JellyException("Could not parse Jelly script",e);
        }

        return script.compile();
    }

    /**
     * Attempts to parse the script from the given input source using
the
     * {@link #getResource} method then returns the compiled script.
     */
    public Script compileScript(InputSource in) throws JellyException {
        XMLParser parser = getXMLParser();
        parser.setContext(this);
        Script script = null;
        try {
            script = parser.parse(in);
        } catch (IOException e) {
            throw new JellyException("Could not parse Jelly script",e);
        } catch (SAXException e) {
            throw new JellyException("Could not parse Jelly script",e);
        }

        return script.compile();
    }

    /**
     * Parses the script from the given input stream then compiles it
and runs it.
     *
     * @return the new child context that was used to run the script
     */
    public JellyContext runScript(InputStream in, XMLOutput output)
throws JellyException {
        return runScript(in, output, JellyContext.DEFAULT_EXPORT,
            JellyContext.DEFAULT_INHERIT);
    }

    /**
     * Parses the script from the given input source then compiles it
and runs it.
     *
     * @return the new child context that was used to run the script
     */
    public JellyContext runScript(InputSource in, XMLOutput output)
throws JellyException {
        return runScript(in, output, JellyContext.DEFAULT_EXPORT,
            JellyContext.DEFAULT_INHERIT);
    }

    /**
     * Parses the script from the given file then compiles it and runs
it.
     *
     * @return the new child context that was used to run the script
     */
    public JellyContext runScript(InputStream in, XMLOutput output,
                          boolean export, boolean inherit) throws
JellyException {
        Script script = compileScript(in);

        URL newJellyContextURL = null;
        try {
            newJellyContextURL = new URL("/");
        } catch (MalformedURLException e) {
            throw new JellyException(e.toString());
        }

        JellyContext newJellyContext = newJellyContext();
        newJellyContext.setRootURL( newJellyContextURL );
        newJellyContext.setCurrentURL( newJellyContextURL );
        newJellyContext.setExport( export );
        newJellyContext.setInherit( inherit );

        if ( inherit ) {
            // use the same variable scopes
            newJellyContext.variables = this.variables;
        }

        if (log.isDebugEnabled() ) {
            log.debug( "About to run script from input stream");
            log.debug( "root context URL: " + newJellyContext.rootURL );
            log.debug( "current context URL: " +
newJellyContext.currentURL );
        }

        script.run(newJellyContext, output);

        return newJellyContext;
    }

    /**
     * Parses the script from the given input source then compiles it
and runs it.
     *
     * @return the new child context that was used to run the script
     */
    public JellyContext runScript(InputSource in, XMLOutput output,
                          boolean export, boolean inherit) throws
JellyException {
        Script script = compileScript(in);

        URL newJellyContextURL = null;
        try {
            newJellyContextURL = new URL("/");
        } catch (MalformedURLException e) {
            throw new JellyException(e.toString());
        }

        JellyContext newJellyContext = newJellyContext();
        newJellyContext.setRootURL( newJellyContextURL );
        newJellyContext.setCurrentURL( newJellyContextURL );
        newJellyContext.setExport( export );
        newJellyContext.setInherit( inherit );

        if ( inherit ) {
            // use the same variable scopes
            newJellyContext.variables = this.variables;
        }

        if (log.isDebugEnabled() ) {
            log.debug( "About to run script from input source");
            log.debug( "root context URL: " + newJellyContext.rootURL );
            log.debug( "current context URL: " +
newJellyContext.currentURL );
        }

        script.run(newJellyContext, output);

        return newJellyContext;
    }

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org