English 中文(简体)
Loading a javascript library in javax.script?
原标题:

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?

最佳回答

Well why would you not load the whole script in and hand it to eval()? If you want that Javascript execution context to parse and interpret the library, then something has to do that. You could of course supply a mechanism to the script context to let the script itself do the equivalent of the <script> tag example you gave, but it would end up being exactly the same thing.

I don t understand your comment about Rhino. The Javascript interpreter in JDK 1.6 is Rhino.

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签