问题
I'm following up on this answer:
I managed to run simple JavaScript
code from Rhino Engine on Java.
But when it comes to D3.js, for example:
var svg = d3.select("body").append("svg")
as you can see d3
requires DOM to be available on first place.
So, for that reason JSDOM is supposed to be the solution.
But JSDOM
happens to be depended on requirejs
From RequireJs
site:
The r.js file allows you to run the optimizer as well as run modules in Node, Rhino or xpcshell.
Then my Java Rhino code look like this:
FileReader fr1 = new FileReader("lib/r.js");
FileReader fr2 = new FileReader("lib/jsdom.js");
engine.eval(fr1);
engine.eval(fr2);
But for now I end up having an error like this:
ReferenceError: "arguments" is not defined.
Then I found this nice answer:
Then my code look like this:
cx.evaluateReader(sharedScope, new FileReader("lib/r.js"), "require", 1, null);
cx.evaluateReader(sharedScope, new FileReader("lib/loader.js"), "loader", 1, null);
cx.evaluateReader(sharedScope, new FileReader("lib/jsdom.js"), "loader", 1, null);
Error: Module name "fs" has not been loaded yet
That basically means that jsdom.js
itself is referencing to:
var fs = require('fs');
var path = require('path');
var URL = require('url');
So, it seems I just need to download all of them.
But fs - is about File System. That means that it depends on NodeJs native implementation. Which is not good for my attempt to be on plain java & plain js side.
Update:
For now I'm doing my research in this direction:
https://github.com/nodyn/jvm-npm
http://nodyn.io/
The question is: How can I load JSDOM to Rhino to let D3.js to generate my SVG?
Once more: Java Rhino -> D3.JS -> JSDOME -> RequireJS -> FS => SVG ?
"Or" : How Mozilla Rhino can use nodejs "fs" module?
I know I can use PhantomJS, but I'm looking or something that is Java-sticky. More lightweight, with no external processe launching involved.
回答1:
JsDom might be a bad option since it relies on so many internal features of Node and Google's V8 JavaScript engine. Env.JS should be a better option but if it doesn't work then you're probably out of luck. It might not work because it's essentially a frozen project with last release in 2010. You'll have to either run your JavaScript in Node or Phantom, or generate SVG with Java-based solutions like Apache Batik.
By the way require
in JsDom is not the same as require.js
but a build-in Node.js function.
回答2:
Another approach would be using Domino as a DOM environment. Look here (http://jazdw.net/content/server-side-svg-generation-using-d3js) for details. Reading comments section would be also helpful
来源:https://stackoverflow.com/questions/24557308/launch-jsdom-from-rhino-on-java