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

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

Introduction

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

Prototype

public JellyContext(JellyContext parent) 

Source Link

Document

Create a new context with the given parent context.

Usage

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

/**
 * Main entry to run a script/* w w  w  .  j a  va  2 s  .  co m*/
 * 
 * @param args Script paths
 * @throws Exception Throw it out
 */
public static final void main(String[] args) throws Exception {
    List scripts = new ArrayList();
    for (int i = 0; i < args.length; i++) {
        String path = args[i];
        File file = new File(path);
        if (file.isFile()) {
            scripts.add(file.toURL());
        } else {
            Enumeration enu = JellyRunner.class.getClassLoader().getResources(path);
            CollectionUtils.addAll(scripts, enu);
        }
    }
    if (scripts.isEmpty()) {
        System.out.println("No script to run, return!");
        return;
    }

    String basedir = new File("").getAbsolutePath();
    Properties initProperties = new Properties(System.getProperties());
    initProperties.setProperty("basedir", basedir);
    initProperties.setProperty("plexus.home", basedir);

    WaterviewPlexusContainer container = new WaterviewPlexusContainer();
    for (Iterator j = initProperties.keySet().iterator(); j.hasNext();) {
        String initPropertyName = (String) j.next();
        container.addContextValue(initPropertyName, initProperties.get(initPropertyName));
    }

    container.addContextValue(Waterview.INIT_PROPERTIES, initProperties);
    container.initialize();
    container.start();

    JellyEngine je = (JellyEngine) container.lookup(JellyEngine.ROLE);
    JellyContext jc = new JellyContext(je.getGlobalContext());

    for (Iterator i = scripts.iterator(); i.hasNext();) {
        URL script = (URL) i.next();
        System.out.print("Running script " + script);
        jc.runScript(script, XMLOutput.createDummyXMLOutput());
        System.out.println("... Done!");
    }
    container.dispose();
}

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

/**
 * Main entry to run a script/*from   ww  w.ja  va2 s.  c o  m*/
 *
 * @param args Script paths
 * @throws Exception Throw it out
 */
public static final void main(String[] args) throws Exception {
    List scripts = new ArrayList();
    for (int i = 0; i < args.length; i++) {
        String path = args[i];
        File file = new File(path);
        if (file.isFile()) {
            scripts.add(file.toURL());
        } else {
            Enumeration enu = JellyScriptsRunner.class.getClassLoader().getResources(path);
            CollectionUtils.addAll(scripts, enu);
        }
    }
    if (scripts.isEmpty()) {
        System.out.println("No script to run, return!");
        return;
    }

    String basedir = new File("").getAbsolutePath();
    Properties initProperties = new Properties(System.getProperties());
    initProperties.setProperty("basedir", basedir);
    initProperties.setProperty("plexus.home", basedir);

    WaterviewPlexusContainer container = new WaterviewPlexusContainer();
    for (Iterator j = initProperties.keySet().iterator(); j.hasNext();) {
        String initPropertyName = (String) j.next();
        container.addContextValue(initPropertyName, initProperties.get(initPropertyName));
    }

    container.addContextValue(Waterview.INIT_PROPERTIES, initProperties);
    container.initialize();
    container.start();

    JellyEngine je = (JellyEngine) container.lookup(JellyEngine.ROLE);
    JellyContext jc = new JellyContext(je.getGlobalContext());
    XMLOutput output = XMLOutput.createXMLOutput(System.out);
    for (Iterator i = scripts.iterator(); i.hasNext();) {
        URL script = (URL) i.next();
        System.out.print("Running script " + script);
        ExtendedProperties ep = new ExtendedProperties();
        ep.putAll(initProperties);
        ep.load(script.openStream());
        for (Iterator j = ep.getKeys("script"); j.hasNext();) {
            String name = (String) j.next();
            if (name.endsWith(".file")) {
                File file = new File(ep.getString(name));
                if (file.exists()) {
                    System.out.println("Runner jelly file " + file);
                    jc.runScript(file, output);
                }
            } else if (name.endsWith(".resource")) {
                Enumeration k = JellyScriptsRunner.class.getClassLoader().getResources(ep.getString(name));
                while (j != null && k.hasMoreElements()) {
                    URL s = (URL) k.nextElement();
                    System.out.println("Running jelly script " + s);
                    jc.runScript(s, output);
                }
            }
        }
        //jc.runScript( script, XMLOutput.createDummyXMLOutput() );
        System.out.println("... Done!");
    }
    container.dispose();
}

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

/**
 * Overwrite or implement method processTag()
 *
 * @see com.cyclopsgroup.waterview.utils.TagSupportBase#processTag(org.apache.commons.jelly.XMLOutput)
 *//* w  w  w.jav  a2  s .co m*/
protected void processTag(XMLOutput output) throws Exception {
    requireAttribute("action");
    FormTag formTag = (FormTag) findAncestorWithClass(FormTag.class);
    if (formTag != null) {
        formTag.addButtonTag(this);
    }
    if (!FormTag.isControlsHidden(getContext())) {
        JellyContext jc = new JellyContext(getContext());
        jc.setVariable("submit", this);
        getBody().run(jc, output);
    }
}

From source file:com.cyclopsgroup.waterview.richweb.fs.TreeTagTest.java

/**
 * Test do tag/*ww  w .jav a2s .c o m*/
 *
 * @throws Exception Throw it out
 */
public void testDoTag() throws Exception {
    JellyEngine je = (JellyEngine) lookup(JellyEngine.ROLE);
    Script script = je.getScript("TestTreeScript.jelly");
    JellyContext jc = new JellyContext(je.getGlobalContext());
    File root = new File("src/java/test/com/cyclopsgroup/waterview/richweb");
    jc.setVariable("tree", new DirectoryTree(root));
    jc.setVariable("treeInstance", new RuntimeTree());
    jc.setVariable(FakePageRuntime.NAME, new FakePageRuntime(new PrintWriter(System.out)));
    script.run(jc, XMLOutput.createXMLOutput(System.out));
}

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

/**
 * Overwrite or implement method processTag()
 *
 * @see com.cyclopsgroup.waterview.utils.TagSupportBase#processTag(org.apache.commons.jelly.XMLOutput)
 *///w w w  .j  a va2 s . co m
protected void processTag(XMLOutput output) throws Exception {
    TreeTag tree = (TreeTag) getContext().getVariable(TreeTag.TREE_TAG);
    RuntimeTreeNode runtimeNode = (RuntimeTreeNode) getContext().getVariable(TreeTag.CURRENT_RUNTIME_NODE);
    JellyContext jc = new JellyContext(getContext());
    Node[] nodes = runtimeNode.getChildrenNodes();
    for (int i = 0; i < nodes.length; i++) {
        RuntimeTreeNode node = (RuntimeTreeNode) nodes[i];
        jc.setVariable(tree.getVar(), tree.isRuntime() ? node : node.getContent());
        jc.setVariable(TreeTag.CURRENT_RUNTIME_NODE, node);
        tree.getBody().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)
 *///from   w  w w .j av a2  s. c  o 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.jelly.ScriptFrame.java

/**
 * Override or implement method of parent class or interface
 *
 * @see com.cyclopsgroup.waterview.Frame#display(com.cyclopsgroup.waterview.Page, com.cyclopsgroup.waterview.PageRuntime)
 *//*from   w  ww  . j  av a2s . c om*/
public void display(Page page, PageRuntime runtime) throws Exception {
    JellyEngine je = (JellyEngine) runtime.getServiceManager().lookup(JellyEngine.ROLE);
    JellyContext jc = new JellyContext(je.getGlobalContext());
    JellyEngine.passVariables(runtime.getPageContext(), jc);
    runtime.getPageContext().put(JellyEngine.JELLY_CONTEXT, jc);
    jc.setVariable(Page.NAME, page);
    jc.setVariable(PageRuntime.NAME, runtime);
    XMLOutput output = XMLOutput.createXMLOutput(runtime.getOutput());
    runtime.getPageContext().put(JellyEngine.JELLY_OUTPUT, output);
    script.run(jc, output);
    output.flush();
}

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)
 *//*  w w  w  .ja va  2  s .  c om*/
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.richweb.taglib.TreeTag.java

/**
 * Override or implement method of parent class or interface
 *
 * @see com.cyclopsgroup.waterview.jelly.AbstractTag#doTag(org.apache.avalon.framework.service.ServiceManager, org.apache.commons.jelly.XMLOutput)
 *//*from ww  w.j  a v  a 2  s.  c  o  m*/
protected void doTag(ServiceManager serviceManager, XMLOutput output) throws Exception {
    requireAttribute("tree");
    requireAttribute("instance");
    invokeBody(output);
    if (treeScript == null || nodeVar == null) {
        throw new JellyTagException("A TreeScript tag must be defined in Tree tag");
    }

    JellyContext jc = new JellyContext(getContext());
    jc.setVariable(RuntimeTree.NAME, getInstance());
    jc.setVariable(nodeVar, getInstance().createRuntimeNode(tree.getRootNode()));
    treeScript.run(jc, output);
}

From source file:com.cyclopsgroup.waterview.richweb.taglib.TreeChildrenTag.java

/**
 * Override or implement method of parent class or interface
 *
 * @see com.cyclopsgroup.waterview.jelly.AbstractTag#doTag(org.apache.avalon.framework.service.ServiceManager, org.apache.commons.jelly.XMLOutput)
 *//* w  w  w.  java2 s.  c o  m*/
protected void doTag(ServiceManager serviceManager, XMLOutput output) throws Exception {
    TreeScriptTag treeScriptTag = (TreeScriptTag) findAncestorWithClass(TreeScriptTag.class);
    String var = treeScriptTag.getVar();
    if (treeScriptTag == null) {
        throw new JellyTagException("TreeChildren must be in the TreeScript");
    }
    TreeNode node = (TreeNode) getContext().getVariable(var);
    TreeNode[] children = node.getChildren();

    RuntimeTree treeRuntime = (RuntimeTree) getContext().getVariable(RuntimeTree.NAME);
    for (int i = 0; i < children.length; i++) {
        TreeNode child = children[i];
        JellyContext jc = new JellyContext(getContext());
        jc.setVariable(var, treeRuntime.createRuntimeNode(child));
        treeScriptTag.getBody().run(jc, output);
    }
}