Example usage for org.apache.commons.jelly Script run

List of usage examples for org.apache.commons.jelly Script run

Introduction

In this page you can find the example usage for org.apache.commons.jelly Script run.

Prototype

public void run(JellyContext context, XMLOutput output) throws JellyTagException;

Source Link

Document

Evaluates the body of a tag

Usage

From source file:com.cyclopsgroup.waterview.web.taglib.SelectTag.java

/**
 * Overwrite or implement method processTag()
 *
 * @see com.cyclopsgroup.waterview.utils.TagSupportBase#processTag(org.apache.commons.jelly.XMLOutput)
 *//*from   w w  w  . ja  v a2s  .com*/
protected void processTag(XMLOutput output) throws Exception {
    FieldTag fieldTag = (FieldTag) requireParent(FieldTag.class);
    options = ListOrderedMap.decorate(new HashMap());
    invokeBody(output);
    if (getItems() != null) {
        Iterator i = Collections.EMPTY_LIST.iterator();
        if (TypeUtils.isIteratable(getItems())) {
            i = TypeUtils.iterate(getItems());
        } else if (getItems() instanceof Map) {
            i = ((Map) getItems()).entrySet().iterator();
        }
        while (i.hasNext()) {
            Object item = i.next();
            SelectOption option = null;
            if (item instanceof SelectOption) {
                option = (SelectOption) item;
            } else if (item instanceof Map.Entry) {
                Map.Entry e = (Map.Entry) item;
                String name = TypeUtils.toString(e.getKey());
                option = new DefaultSelectOption(name, TypeUtils.toString(e.getValue()));
            } else {
                String name = TypeUtils.toString(item);
                option = new DefaultSelectOption(name, name);
            }
            addOption(option);
        }
    }
    JellyEngine je = (JellyEngine) getServiceManager().lookup(JellyEngine.ROLE);
    final Script script = je.getScript("/waterview/FormSelectInput.jelly");
    Script s = new Script() {

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

        public void run(JellyContext context, XMLOutput output) throws JellyTagException {
            context.setVariable("selectTag", SelectTag.this);
            script.run(context, output);
        }
    };
    fieldTag.setBodyScript(s);
}

From source file:com.cyclopsgroup.waterview.web.taglib.JellyTableControlTag.java

/**
 * Overwrite or implement method processTag()
 *
 * @see com.cyclopsgroup.waterview.utils.TagSupportBase#processTag(org.apache.commons.jelly.XMLOutput)
 *///w  w  w.j a v a  2s  .  c  o  m
protected void processTag(XMLOutput output) throws Exception {
    requireAttribute("script");

    invokeBody(XMLOutput.createDummyXMLOutput());
    JellyEngine je = (JellyEngine) getServiceManager().lookup(JellyEngine.ROLE);
    Script s = je.getScript(getScript());

    if (tableTag == null) {
        throw new JellyException("One table must be defined");
    }

    if (data == null) {
        throw new JellyException("Tabular data must be included");
    }

    JellyContext jc = new JellyContext(getContext());
    jc.setVariable("tableTag", tableTag);
    jc.setVariable("table", tableTag.getTable());
    jc.setVariable("tabularData", getData());
    s.run(jc, output);
}

From source file:com.cyclopsgroup.waterview.web.taglib.BaseJellyFormControlTag.java

/**
 * Override method runScript in class BaseJellyFormControlTag
 *
 * @see com.cyclopsgroup.waterview.jelly.taglib.BaseJellyControlTag#runScript(org.apache.commons.jelly.Script, org.apache.commons.jelly.XMLOutput)
 *//*  w  w w.  j a v  a 2  s . co m*/
protected void runScript(Script script, XMLOutput output) throws Exception {
    FormTag.setHideControls(true, getContext());
    String formContent = getBodyText();
    FormTag.setHideControls(false, getContext());
    if (formTag == null) {
        throw new JellyTagException("Form tag must be defined");
    }
    JellyContext jc = new JellyContext(getContext());
    jc.setVariable("formControl", this);
    jc.setVariable("formTag", formTag);
    jc.setVariable("form", formTag.getForm());
    jc.setVariable("formContent", formContent);
    script.run(jc, output);
}

From source file:com.cyclopsgroup.waterview.web.taglib.BaseJellyTableControlTag.java

/**
 * Override method runScript in class BaseJellyTableControlTag
 *
 * @see com.cyclopsgroup.waterview.jelly.taglib.BaseJellyControlTag#runScript(org.apache.commons.jelly.Script, org.apache.commons.jelly.XMLOutput)
 *//*from w  w w.  j  a va2  s.  com*/
protected void runScript(Script script, XMLOutput output) throws Exception {
    invokeBody(XMLOutput.createDummyXMLOutput());
    if (tableTag == null) {
        throw new JellyException("One table must be defined");
    }

    if (data == null) {
        throw new JellyException("Tabular data must be included");
    }

    JellyContext jc = new JellyContext(getContext());
    jc.setVariable("tableTag", tableTag);
    jc.setVariable("table", tableTag.getTable());
    jc.setVariable("tabularData", data);
    jc.setVariable("tableControl", this);
    script.run(jc, output);
}

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

/**
 * Get script with give path and package name
 *
 * @param scriptPath Script path/*w  ww .jav a2  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;
}

From source file:com.cyclopsgroup.waterview.jelly.taglib.JellyScriptTag.java

/**
 * Overwrite or implement method processTag()
 *
 * @see com.cyclopsgroup.waterview.utils.TagSupportBase#processTag(org.apache.commons.jelly.XMLOutput)
 */// w w w.  jav a  2 s  .c o  m
protected void processTag(XMLOutput output) throws Exception {
    requireAttribute("type");
    requireAttribute("path");
    Script script = null;
    if (StringUtils.equals(getType(), "system")) {
        JellyEngine je = (JellyEngine) getServiceManager().lookup(JellyEngine.ROLE);
        script = je.getScript(getPath());
    } else if (StringUtils.equals(getType(), "classpath")) {
        URL resource = getClass().getClassLoader().getResource(getPath());
        if (resource != null) {
            script = context.compileScript(resource);
        }
    } else if (StringUtils.equals(getType(), "file")) {
        File file = new File(getPath());
        if (file.exists()) {
            script = context.compileScript(file.toURL());
        }
    } else {
        throw new JellyTagException("Type must be system|classpath|file, default value is system");
    }
    if (script == null) {
        throw new FileNotFoundException("Resource " + getPath() + " is not found in " + getType());
    }
    JellyContext jc = new JellyContext(getContext());
    if (script != null) {
        script.run(jc, output);
        output.flush();
    }
}

From source file:org.kohsuke.stapler.jelly.groovy.JellyBuilder.java

private void _include(Object it, Klass clazz, String view) throws IOException, JellyException {
    JellyClassTearOff t = request.getWebApp().getMetaClass(clazz).getTearOff(JellyClassTearOff.class);
    Script s = t.findScript(view);
    if (s == null)
        throw new IllegalArgumentException("No such view: " + view + " for " + clazz);

    JellyContext context = new JellyContext(getContext());
    if (it != null)
        context.setVariable("it", it);
    context.setVariable("from", it);

    ClassLoader old = Thread.currentThread().getContextClassLoader();
    if (clazz.clazz instanceof Class)
        Thread.currentThread().setContextClassLoader(((Class) clazz.clazz).getClassLoader());
    try {/*from  w ww . j av a2  s  .  c  o m*/
        s.run(context, output);
    } finally {
        Thread.currentThread().setContextClassLoader(old);
    }
}