Loading a javascript library in javax.script?

Posted by Shane on Stack Overflow See other posts from Stack Overflow or by Shane
Published on 2010-04-18T23:11:00Z Indexed on 2010/04/18 23:13 UTC
Read the original article Hit count: 453

Filed under:
|
|
|

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.)

© Stack Overflow or respective owner

Related posts about java

Related posts about JavaScript