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

public static XMLOutput createXMLOutput(OutputStream out, boolean escapeText)
        throws UnsupportedEncodingException 

Source Link

Document

Creates a text based XMLOutput which converts all XML events into text and writes to the underlying OutputStream.

Usage

From source file:org.wyona.yanel.impl.resources.usecase.UsecaseResource.java

/**
 * Generate jelly view.//from   w  ww  .j a va  2s  .  c  o  m
 */
private InputStream getJellyInputStream(String viewID, String viewTemplate, boolean XMLoutput)
        throws UsecaseException {
    try {

        if (log.isDebugEnabled())
            log.debug("viewTemplate: " + viewTemplate);
        Repository repo = this.getRealm().getRepository();

        JellyContext jellyContext = new JellyContext();
        jellyContext.setVariable("resource", this);
        jellyContext.setVariable("yanel.back2context", PathUtil.backToContext(realm, getPath()));
        jellyContext.setVariable("yanel.back2realm", PathUtil.backToRealm(getPath()));
        jellyContext.setVariable("yanel.globalHtdocsPath", PathUtil.getGlobalHtdocsPath(this));
        jellyContext.setVariable("yanel.resourcesHtdocsPath", PathUtil.getResourcesHtdocsPathURLencoded(this));
        jellyContext.setVariable("yanel.reservedPrefix", this.getYanel().getReservedPrefix());
        //jellyContext.setVariable("request", request);

        ByteArrayOutputStream jellyResultStream = new ByteArrayOutputStream();
        XMLOutput jellyOutput = XMLOutput.createXMLOutput(jellyResultStream, XMLoutput);
        InputStream templateInputStream;
        String templatePublicId;
        String templateSystemId;
        if (viewTemplate.startsWith("/")) {
            if (log.isDebugEnabled())
                log.debug(
                        "Accessing view template directly from the repo (no protocol specified). View Template: "
                                + viewTemplate);
            // for backwards compatibility. when not using a protocol
            templateInputStream = repo.getNode(viewTemplate).getInputStream();
            templatePublicId = "yanelrepo:" + viewTemplate;
            /*XXX HACK the following does not work: Jelly wants URLs => protocol must be registered by the JVM!
            templateSystemId = "yanelrepo:"+viewTemplate;
            */
            templateSystemId = "file:///yanelrepo" + viewTemplate;

        } else {
            if (log.isDebugEnabled())
                log.debug(
                        "Accessing view template through the source-resolver (protocol specified). View Template: "
                                + viewTemplate);
            SourceResolver resolver = new SourceResolver(this);
            Source templateSource = resolver.resolve(viewTemplate, null);
            templateInputStream = ((StreamSource) templateSource).getInputStream();
            templatePublicId = templateSource.getSystemId();
            /*XXX HACK the following does not work: Jelly wants URLs => protocol must be registered by the JVM!
            templateSystemId = templateSource.getSystemId();
            */
            templateSystemId = "file:///" + viewTemplate.replaceFirst(":", "/");
        }
        InputSource inputSource = new InputSource(templateInputStream);
        inputSource.setPublicId(templatePublicId);
        inputSource.setSystemId(templateSystemId);
        jellyContext.runScript(inputSource, jellyOutput);
        //XXX should we close templateInputStream here?!?

        jellyOutput.flush();
        byte[] result = jellyResultStream.toByteArray();
        //System.out.println(new String(result, "utf-8"));
        return new ByteArrayInputStream(result);
    } catch (Exception e) {
        String errorMsg = "Error creating 'jelly' view '" + viewID + "' of usecase resource: " + getName()
                + ": " + e;
        log.error(errorMsg, e);
        throw new UsecaseException(errorMsg, e);
    }
}