com.latticeware.xecute.Engine.java Source code

Java tutorial

Introduction

Here is the source code for com.latticeware.xecute.Engine.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.latticeware.xecute;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Map;
import org.apache.commons.jelly.JellyContext;
import org.apache.commons.jelly.JellyException;
import org.apache.commons.jelly.XMLOutput;
import org.xml.sax.InputSource;

/**
 *
 * @author sfisque
 */
public class Engine {
    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) -> {
            context.setVariable(key, env.get(key));
        });
        context.runScript(is, xo);
        return baos.toString();
    }
}