使用Domino等维护的项目:
Domino基于Mozilla的Andreas Gal的工作,目前似乎得到了维护。虽然它不能直接使用,但很容易修补。我所做的修改如下:
更改文件以使用AMD样式加载而不是CommonJS(这样我可以使用RequireJS在Rhino和浏览器中加载它们)
在序列化时向SVG元素添加xmlns属性
修改CSS解析器以解决Rhino中大小写不敏感正则表达式的问题
添加具有类似HTML元素的样式属性的SVG元素类型
将SVG特定的CSS属性添加到CSS解析器中
几个错误修复
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.tools.shell.Global;
import com.google.common.base.Charsets;
public abstract class RunJS {
static final Log LOG = LogFactory.getLog(RunJS.class);
static final int JS_VERSION = Context.VERSION_1_8;
static final int OPTIMIZATION_LEVEL = 9;
private static ScriptableObject ENVIRONMENT = null;
static {
ENVIRONMENT = createNewEnvironment();
}
public static ScriptableObject createNewEnvironment() {
Global global = null;
Context cx = Context.enter();
try {
cx.setOptimizationLevel(OPTIMIZATION_LEVEL);
cx.setLanguageVersion(JS_VERSION);
global = new Global();
global.setSealedStdLib(true);
global.init(cx);
Scriptable argsObj = cx.newArray(global, new Object[] {});
global.defineProperty("arguments", argsObj, ScriptableObject.DONTENUM);
loadJS(cx, global, "/path/to/r.js");
loadJS(cx, global, "/path/to/bootstrap.js");
global.sealObject();
} catch (Exception e) {
LOG.error("Error setting up Javascript environment", e);
}
finally {
Context.exit();
}
return global;
}
public static void loadJS(Context cx, Scriptable scr, String fileName) throws Exception {
Reader reader;
try {
reader = new InputStreamReader(new FileInputStream(new File(fileName)), Charsets.UTF_8);
} catch (FileNotFoundException e) {
throw new Exception("Could not find file", e);
};
try {
cx.evaluateReader(scr, reader, fileName, 0, null);
} catch (IOException e) {
throw new Exception("IO error reading file", e);
}
finally {
try { reader.close(); } catch (IOException e) {
throw new Exception("IO error closing file", e);
}
}
}
protected static Scriptable getScope(Context cx) {
Scriptable scope = cx.newObject(RunJS.ENVIRONMENT);
scope.setPrototype(RunJS.ENVIRONMENT);
scope.setParentScope(null);
return scope;
}
}
boostrap.js
require.config({
baseUrl: '/base/path/to/packages'
});
var window, document, d3;
require(['domino/index'], function(domino) {
window = domino.createWindow('');
document = window.document;
require(['d3/d3'], function(_d3) {
d3 = _d3;
});
});
require(["mypackage/mymodule1", "mypackage/mymodule2"]);
Subclass RunJS and load your JavaScript file like this:
Context cx = Context.enter();
try {
cx.setOptimizationLevel(OPTIMIZATION_LEVEL);
cx.setLanguageVersion(JS_VERSION);
Scriptable scope = getScope(cx);
scope.put("myvar", scope, myvar);
loadJS(cx, scope, "/path/to/yourcode.js");
}
catch (Exception e) {
LOG.error(e);
}
finally {
Context.exit();
}
参考资料