Revision [1081]
This is an old revision of EmbeddingXmlsh made by DavidLee on 2009-12-03 10:17:45.
Embedding xmlsh
xmlsh is a pure java program and can be embedded into java applications easily.
The easiest way to embed xmlsh is to create an instance of xmlsh command and execute it.
The following is an example of how to invoke xmlsh from within java.
import java.util.ArrayList; import java.util.List; import org.xmlsh.core.XValue; import org.xmlsh.sh.shell.Shell; class myclass { // The method executing the task public void run_xmlsh() throws Exception { List<XValue> vargs = new ArrayList<XValue>(); vargs.add( new XValue("-c")); vargs.add( new XValue("xslt -f file.xsl < file.xml" )); org.xmlsh.commands.builtin.xmlsh cmd = new org.xmlsh.commands.builtin.xmlsh(); int ret = cmd.run(null , "xmlsh" , vargs); } }
xmlsh will inherit System.in, System.out and System.err from the current JVM which becomes stdin, stdout and stderr.
If you want to avoid having xmlsh inherit the system streams then you need to create an instance of the Shell specifying false for the constructor. You can specify your own ports for in/out/error as well.
Shell shell = new Shell(false); shell.getEnv().setStdout( new StreamOutputPort( output_stream ,false) ); shell.getEnv().setStderr( new StreamOutputPort( error_stream,false) ); shell.getEnv().setStdin( input_stream ); int ret = cmd.run(shell , "xmlsh" , vargs);