Example usage for org.apache.commons.jelly XMLOutput createXMLOutput

List of usage examples for org.apache.commons.jelly XMLOutput createXMLOutput

Introduction

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

Prototype

protected static XMLOutput createXMLOutput(final XMLWriter xmlWriter) 

Source Link

Document

Factory method to create a new XMLOutput from an XMLWriter

Usage

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

/**
 * Main entry to run a script/* w  w w .ja v  a  2s  .  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.viltgroup.xcp.jenkins.maven.XcpMavenSettings.java

public static void generateFile(String workspacePath, String xcpDesignerPath, String localRepositoryPath,
        FilePath generatedFilePath) {/* w  ww  . ja  va  2s . co  m*/
    XMLOutput output = null;
    try {
        // create directories if needed
        generatedFilePath.getParent().mkdirs();

        // prepare output writer
        output = XMLOutput.createXMLOutput(generatedFilePath.write());

        // load jelly template
        String templateLocation = String.format("/%s/template.jelly",
                XcpMavenSettings.class.getName().replace('.', '/'));
        URL templateURL = XcpMavenSettings.class.getResource(templateLocation);

        // generate settings.xml from template
        Jelly jelly = new Jelly();
        jelly.setUrl(templateURL);
        Script script = jelly.compileScript();
        // add xCP Designer path to the jelly context
        JellyContext context = new JellyContext();
        context.setVariable("workspacePath", OSPathHacks.processFilePath(workspacePath));
        context.setVariable("xcpDesignerPath", xcpDesignerPath);
        context.setVariable("xcpDesignerMavenPath",
                String.format("%s/maven", OSPathHacks.processFilePath(xcpDesignerPath)));
        context.setVariable("localRepositoryPath", OSPathHacks.processFilePath(localRepositoryPath));
        script.run(context, output);
        output.flush();
    } catch (IOException e) {
        throw new RuntimeException("Error generating xCP settings.xml file.", e);
    } catch (JellyTagException e) {
        throw new RuntimeException("Error generating xCP settings.xml file.", e);
    } catch (JellyException e) {
        throw new RuntimeException("Error generating xCP settings.xml file.", e);
    } catch (InterruptedException e) {
        throw new RuntimeException("Error generating xCP settings.xml file.", e);
    } finally {
        if (output != null)
            try {
                output.close();
            } catch (IOException e) {
            }
    }
}

From source file:com.latticeware.xecute.Engine.java

public Object execute(String script, Map<String, Object> env)
        throws UnsupportedEncodingException, JellyException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream(5120);
    ByteArrayInputStream bais = new ByteArrayInputStream(script.getBytes());
    XMLOutput xo = XMLOutput.createXMLOutput(baos);
    InputSource is = new InputSource(bais);

    JellyContext context = new JellyContext();
    env.keySet().forEach((key) -> {//from  w  w w  . ja  v a2s . c  o m
        context.setVariable(key, env.get(key));
    });
    context.runScript(is, xo);
    return baos.toString();
}

From source file:com.marvelution.hudson.plugins.jirareporter.utils.IssueTextUtils.java

/**
 * Create the text content for a specific issue field
 * /*from ww w  . j  ava  2  s.  co m*/
 * @param type the {@link Type} of text to create
 * @param build the {@link AbstractBuild}
 * @param site the {@link JIRASite}
 * @return the created text content
 */
public static String createFieldText(Type type, AbstractBuild<?, ?> build, JIRASite site) {
    final StringWriter writer = new StringWriter();
    final XMLOutput output = XMLOutput.createXMLOutput(writer);
    final JellyContext context = new JellyContext();
    context.setVariable("rootURL", Hudson.getInstance().getRootUrl());
    context.setVariable("build", build);
    context.setVariable("site", site);
    try {
        Class.forName("jenkins.model.Jenkins");
        context.setVariable("system", "Jenkins");
    } catch (ClassNotFoundException e) {
        context.setVariable("system", "Hudson");
    }
    context.setVariable("version", Hudson.getVersion().toString());
    try {
        context.setVariable("environment", build.getEnvironment(new LogTaskListener(LOGGER, Level.INFO)));
    } catch (Exception e) {
        context.setVariable("environment", Collections.emptyMap());
    }
    // Utilize the Dozer Mapper and its convertors of the API V2 plugin to get the ChangeLog and TestResults
    try {
        context.setVariable("changelog", DozerUtils.getMapper().map(build.getChangeSet(), ChangeLog.class));
    } catch (Exception e) {
        context.setVariable("changelog", new ChangeLog());
    }
    try {
        context.setVariable("testresults", DozerUtils.getMapper().map(build, TestResult.class));
    } catch (Exception e) {
        context.setVariable("testresults", new TestResult());
    }
    try {
        context.runScript(
                HudsonPluginUtils.getPluginClassloader().getResource("fields/" + type.field(site) + ".jelly"),
                output);
    } catch (JellyException e) {
        LOGGER.log(Level.SEVERE, "Failed to create Text of type " + type.name(), e);
        throw new IllegalStateException("Cannot raise an issue if no text is available", e);
    }
    return writer.toString().trim();
}

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

/**
 * Test do tag//from   w  w  w  . ja  v a  2  s. co 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.jelly.JellyView.java

/**
 * Overwrite or implement method render()
 * @see com.cyclopsgroup.waterview.spi.View#render(com.cyclopsgroup.waterview.RuntimeData, com.cyclopsgroup.waterview.Context)
 *///from   w w w  .j a v a 2s. c  om
public void render(RuntimeData data, Context viewContext) throws Exception {
    runModule(data, viewContext);

    JellyEngine je = (JellyEngine) data.getServiceManager().lookup(JellyEngine.ROLE);
    JellyContext jellyContext = je.createJellyContext(viewContext);
    XMLOutput output = XMLOutput.createXMLOutput(data.getOutput());
    try {
        script.run(jellyContext, output);
    } catch (Exception e) {
        data.getOutput().print("<div>");
        e.printStackTrace(data.getOutput());
        data.getOutput().println("</div>");
    } finally {
        output.flush();
    }
}

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)
 *//*  w  ww.j  a v  a  2 s  . co  m*/
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.jelly.JellyPanel.java

/**
 * Override or implement method of parent class or interface
 *
 * @see com.cyclopsgroup.waterview.spi.Panel#render(com.cyclopsgroup.waterview.RuntimeData, com.cyclopsgroup.waterview.spi.View[])
 *//*from  www  .  ja  v  a2  s .co m*/
public void render(RuntimeData data, View[] views) throws Exception {
    JellyEngine je = (JellyEngine) data.getServiceManager().lookup(JellyEngine.ROLE);
    JellyContext jc = je.createJellyContext(data.getRequestContext());
    jc.setVariable(VIEWS_NAME, views);

    runModule(data, new JellyContextAdapter(jc));

    try {
        XMLOutput output = XMLOutput.createXMLOutput(data.getOutput());
        script.run(jc, output);
        output.flush();
    } catch (Exception e) {
        data.getOutput().println("<pre>");
        e.printStackTrace(data.getOutput());
        data.getOutput().println("</pre>");
    } finally {
        data.getOutput().flush();
    }
}

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

/**
 * Override or implement method of parent class or interface
 *
 * @see com.cyclopsgroup.waterview.spi.Layout#render(com.cyclopsgroup.waterview.RuntimeData, com.cyclopsgroup.waterview.spi.Page)
 *///from w w w .  j  a va2 s.  co  m
public synchronized void render(RuntimeData data, Page page) throws Exception {
    data.getRequestContext().put(Page.NAME, page);
    data.getRequestContext().put(NAME, this);

    runModule(data, data.getRequestContext());

    JellyEngine je = (JellyEngine) data.getServiceManager().lookup(JellyEngine.ROLE);
    JellyContext jellyContext = je.createJellyContext(data.getRequestContext());
    XMLOutput output = XMLOutput.createXMLOutput(data.getOutput());
    script.run(jellyContext, output);
    data.getRequestContext().put(Page.NAME, null);
    data.getRequestContext().put(NAME, null);
}

From source file:com.groupon.jenkins.buildsetup.GithubRepoAction.java

public String getHtml(ProjectConfigInfo projectConfigInfo)
        throws IOException, ClassNotFoundException, JellyException {
    String name = getClass().getName().replace('.', '/').replace('$', '/') + "/" + "index.jelly";
    URL actionTemplate = getClass().getClassLoader().getResource(name);
    JellyContext context = new JellyContext();
    context.setVariable("p", projectConfigInfo);
    context.setVariable("it", this);
    OutputStream outputStream = new ByteArrayOutputStream();
    XMLOutput output = XMLOutput.createXMLOutput(outputStream);
    context.runScript(actionTemplate, output);
    output.flush();/* www . j  ava2s . co  m*/
    return "<p>" + outputStream.toString() + " </p>";
}