public void invokeCommandLineJelly(String[] args) throws JellyException {
CommandLine cmdLine = null;
try {
cmdLine = parseCommandLineOptions(args);
} catch (ParseException e) {
throw new JellyException(e);
}
// get the -script option. If there isn't one then use args[0]
String scriptFile = null;
if (cmdLine.hasOption("script")) {
scriptFile = cmdLine.getOptionValue("script");
} else {
scriptFile = args[0];
}
//
// Use classloader to find file
//
URL url = ClassLoaderUtils.getClassLoader(getClass()).getResource(scriptFile);
// check if the script file exists
if (url == null && !(new File(scriptFile)).exists()) {
throw new JellyException("Script file " + scriptFile + " not found");
}
try {
// extract the -o option for the output file to use
final XMLOutput output = cmdLine.hasOption("o") ?
XMLOutput.createXMLOutput(new FileWriter(cmdLine.getOptionValue("o"))) :
XMLOutput.createXMLOutput(System.out);
Jelly jelly = new Jelly();
jelly.setScript(scriptFile);
Script script = jelly.compileScript();
// add the system properties and the command line arguments
JellyContext context = jelly.getJellyContext();
context.setVariable("args", args);
context.setVariable("commandLine", cmdLine);
script.run(context, output);
// now lets wait for all threads to close
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
try {
output.close();
}
catch (Exception e) {
// ignore errors
}
}
}
);
} catch (Exception e) {
throw new JellyException(e);
}
}