从Rhino启动jsdom [英] Launch jsdom from Rhino on Java

查看:245
本文介绍了从Rhino启动jsdom的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要追踪这个答案



我设法运行来自 JavaScript .mozilla.org / en-US / docs / Mozilla / Projects / Rhinorel =nofollow> Java上的Rhino Engine



来到 D3.js ,例如:

  var svg = d3.select(body)append(svg)


$ b b

,因为您可以看到 d3 需要DOM在第一时间可用。



原因 JSDOM 应该是解决方案。



但是 JSDOM 恰好取决于 requirejs



RequireJs 网站:


r.js 文件允许您运行优化器,以及在Node, Rhino 或xpcshell中运行模块


然后我的Java Rhino代码如下所示:

  FileReader fr1 = new FileReader(lib / r.js); 
FileReader fr2 = new FileReader(lib / jsdom.js);
engine.eval(fr1);
engine.eval(fr2);

但是现在我最终出现了这样的错误:


$


然后我发现这个不错的答案



然后我的代码看起来像这样:

  cx.evaluateReader(sharedScope,new FileReader(lib / r。 js),require,1,null); 
c.x.evaluateReader(sharedScope,new FileReader(lib / loader.js),loader,1,null);
cx.evaluateReader(sharedScope,new FileReader(lib / jsdom.js),loader,1,null);




错误:模块名称fs p>

这基本上意味着 jsdom.js 本身引用:

  var fs = require('fs'); 
var path = require('path');
var URL = require('url');

所以,看来我只需要下载所有这些。



fs - 关于文件系统 。这意味着它依赖于NodeJs 本机实现。这不利于我的尝试在简单的Java&



在这方面做我的研究:

  https://github.com/nodyn/jvm-npm 
http://nodyn.io/

问题是:如何 :Java Rhino - > D3.JS - > JSDOME - > RequireJS - > FS => SVG?



Mozilla Rhino可以使用nodejsfs模块?



我知道我可以使用 PhantomJS ,但我正在寻找或是Java-sticky的东西。更轻量级,没有涉及到外部进程启动。

解决方案

JsDom 可能是一个不好的选择,因为它依赖于Node和Google的V8 JavaScript引擎的许多内部功能。 Env.JS 应该是一个更好的选择,但如果它不工作,那么你可能是运气不好。它可能无法正常工作,因为它本质上是一个冻结的项目,最后一次发布是在2010年。您必须在节点 Phantom ,或使用基于Java的解决方案生成SVG,例如 Apache Batik



顺便说一下 require 在JsDom中与 require.js 不同,但一个内置的Node.js函数


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.

解决方案

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.

这篇关于从Rhino启动jsdom的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆