Loading a javascript library in javax.script?
- by Shane
I want to run Protovis javascript from Java and get the evaluated SVG code. I am using javax.script.* to run the Javascript:
public static void EvalScript() throws Exception {
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("JavaScript");
Object result = engine.eval("var vis = new pv.Panel().width(300).height(300)
.add (pv.Line).data ([1,0.5, 0.1, 0.01, 0.001, 0.3, 0.2,0.1,1])
.left (function () { return this.index * 30; })
.bottom (function (d) { return d * 250; });
vis.root.render();
vis.scene[0].canvas.innerHTML;");
System.out.println(result);
}
This would complain because I never loaded Protovis itself, as would ordinarily be done with
<script type="text/javascript" src="../protovis-r3.1.0.js"></script>
Is there a good way, short of sourcing in the full Javascript into the eval() command, of loading a library when running Javascript through javax.script?
(Incidentally, I know of examples that use Rhino to do this from the Google discussion group.)