Example usage for org.apache.commons.jelly JellyContext compileScript

List of usage examples for org.apache.commons.jelly JellyContext compileScript

Introduction

In this page you can find the example usage for org.apache.commons.jelly JellyContext compileScript.

Prototype

public Script compileScript(InputSource source) throws JellyException 

Source Link

Document

Attempts to parse the script from the given InputSource using the #getResource method then returns the compiled script.

Usage

From source file:com.cyclopsgroup.waterview.jelly.JellyEngine.java

/**
 * Get script with give path and package name
 *
 * @param scriptPath Script path/*from  w ww  .ja v a  2  s .c  o m*/
 * @param packageName Look for it in this package
 * @param defaultScript Return as default value
 * @return Script object
 * @throws JellyException Throw it out
 */
public Script getScript(String packageName, String scriptPath, Script defaultScript) throws JellyException {
    String fullPath = scriptPath;
    String pkg = moduleManager.getPackageName(packageName);
    if (StringUtils.isNotEmpty(pkg)) {
        fullPath = pkg.replace('.', '/') + scriptPath;
    }
    Script script = null;
    synchronized (this) {
        if (getCacheManager().contains(this, fullPath)) {
            script = (Script) getCacheManager().get(this, fullPath);
        } else {
            final URL resource = getClass().getClassLoader().getResource(fullPath);

            if (resource == null) {
                script = DUMMY_SCRIPT;
            } else {
                JellyContext jc = new JellyContext(getGlobalContext());
                final Script s = jc.compileScript(resource);
                script = new Script() {

                    public Script compile() throws JellyException {
                        return this;
                    }

                    public void run(JellyContext context, XMLOutput output) throws JellyTagException {
                        TagSupport.addScriptResource(resource, context);
                        try {
                            s.run(context, output);
                        } catch (JellyTagException e) {
                            throw e;
                        } finally {
                            TagSupport.removeScriptResource(resource, context);
                        }
                    }
                };
            }
            getCacheManager().put(this, fullPath, script);
        }
    }
    return script == DUMMY_SCRIPT ? defaultScript : script;
}