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.