- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在使用 Rhino(17R4) 时遇到错误。
我的目标是使用 jsbeautifier.js 缩进 javascript 源代码.
这是我的代码:
import java.io.InputStream;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
public class JSBeautifier {
public String beautify(String jsSource) {
Context context = Context.enter();
Scriptable globalScope = context.initStandardObjects();
String beautify = getFileContents(JSBeautifier.class.getResourceAsStream("beautify.js"));
context.evaluateString(globalScope, beautify, "beautify", 1, null);
globalScope.put("source", globalScope, jsSource);
context.evaluateString(globalScope, "result = js_beautify(source);", "beautify", 1, null);
Object result = globalScope.get("result", globalScope);
return (String)result;
}
private String getFileContents(InputStream stream) {
StringBuffer contents = new StringBuffer("");
try {
byte[] readBytes = new byte[1024];
int i = 0;
while ((i = stream.read(readBytes)) > 0)
contents.append(new String(readBytes, 0, i));
} catch (Exception e) {
e.printStackTrace();
}
return contents.toString();
}
}
当我执行这段代码时,它发生了以下异常。
org.mozilla.javascript.EcmaError: ReferenceError: "js_beautify" is not defined. (beautify#1)
at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3687)
at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3665)
at org.mozilla.javascript.ScriptRuntime.notFoundError(ScriptRuntime.java:3750)
at org.mozilla.javascript.ScriptRuntime.getNameFunctionAndThis(ScriptRuntime.java:2176)
at org.mozilla.javascript.optimizer.OptRuntime.callName(OptRuntime.java:61)
at org.mozilla.javascript.gen.beautify_2._c_script_0(beautify:1)
at org.mozilla.javascript.gen.beautify_2.call(beautify)
at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:394)
at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3091)
at org.mozilla.javascript.gen.beautify_2.call(beautify)
at org.mozilla.javascript.gen.beautify_2.exec(beautify)
at org.mozilla.javascript.Context.evaluateString(Context.java:1079)
at test.util.jsconverter.JSBeautifier.beautify(JSBeautifier.java:20)
我该如何解决这个问题?
最佳答案
问题是 beautify.js正在将其功能分配给全局范围内的变量
if (typeof define === "function") {
// Add support for require.js
define(function(require, exports, module) {
exports.js_beautify = js_beautify;
});
} else if (typeof exports !== "undefined") {
// Add support for CommonJS. Just put this file somewhere on your require.paths
// and you will be able to `var js_beautify = require("beautify").js_beautify`.
exports.js_beautify = js_beautify;
} else if (typeof window !== "undefined") {
// If we're running a web page and don't have either of the above, add our one global
window.js_beautify = js_beautify;
} else if (typeof global !== "undefined") {
// If we don't even have window, try global.
global.js_beautify = js_beautify;
}
因此,您必须创建一个可以将其功能分配给的全局变量
context.evaluateString(globalScope, "var global = {};", "global", 1, null);
context.evaluateString(globalScope, beautify, "beautify", 1, null);
globalScope.put("source", globalScope, jsSource);
context.evaluateString(globalScope, "result = global.js_beautify(source);", "beautify", 1, null);
关于java - 使用 Rhino,我有一个 "ReferenceError"异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16337496/
尝试使用 RhinoMock 模拟函数。 var repository = new Rhino.Mocks.MockRepository(); var classMock = repository.D
我已经阅读了很多示例/教程(包括 Ayende 在 MSDN 上的 Alexandria)。 但事实证明,仅仅获得一些更新的程序集本身就是一个障碍。获得正确版本的 CaSTLe.Windsor 后 -
我正在使用 java 运行用 Rhino 编写的命令行实用程序,但如果出现错误,我需要将该错误返回给 shell,以便停止处理。如何将错误代码从 Rhino 传回给 shell? 最佳答案 你可以用
为了它的乐趣,我尝试使用 envjs 在 Rhino 中运行 JSLinux,它似乎没有抛出任何错误,也似乎不起作用。 如此处所述:How do I locally host the webbrows
有谁知道为什么 UsernameExists 不会返回 True。我的语法一定在某个地方搞砸了。 [TestMethod()] public void GenerateUsername
Rhino Mock 库是否有可比较且可靠的替代品。我现在将 Rhino 用于 MVC ASP.NET 项目,但正在考虑(就像计划 B)看看是否有任何其他等效的替代方案。 最佳答案 RhinoMock
我有一个 Java 7 项目,该项目大量使用 Javascript 来编写各种功能的脚本。到目前为止,我一直使用 Rhino 作为脚本引擎。我现在想迁移到 Java 8,这也意味着我将用 Nashor
我正在寻找一种让 stub 的返回值取决于其输入的干净方法。 目前我正在使用以下方法,但效果不佳。 metadataLogic.Expect(x => x.GetMake(args.Vehicle1.
我正在设置调用构建和执行查询的方法的期望。我想询问所用参数的属性。这可能吗 using (mocks.Record()) { Expect.Call(connection.Retrieve(S
有没有人有一个使用 Rhino Mocks 和 NInject 的自动模拟容器的实现? 最佳答案 好的,我使用 Moq 集成作为起点自己构建了一个。这很简单。你需要这 3 个类: public cla
谁能告诉我为什么下面的测试没有失败? [Test] public void uhh_what() { var a = MockRepository.GenerateMock(); a.
有人可以分享下载最新版本的 Rhino 模拟二进制文件的链接吗(仅限二进制文件,我不需要源文件)?链接 http://ayende.com/projects/rhino-mocks/downloads
我最近才开始使用 Rhino-Etl对于非常简单的 ETL 过程,并取得了巨大的成功。我现在有一个稍微复杂的场景要解决,但我没有发现 ConventionInputCommandOperation 以
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 3年前关闭。 Improve thi
我通常是单元测试和模拟的新手,并且正在尝试为我的一个类设置测试,我想确保从同一类中的另一个方法调用特定方法。因此,我想使用具体的实现,但要模拟它的一部分。这可能吗? 公共(public)课 MyCla
我一直在阅读有关 Rhino 的文章,认为这是一种在我的 Java 代码中实现 JavaScript 的有用方法。 在阅读了一段时间后,在 google 和这里进行了搜索,我找不到在 Java 中
我正在尝试使用 Rhino 服务总线在 1 个发布者和多个订阅者之间设置发布-订阅。然而,我似乎得到的只是竞争消费者(消息在一个消费者或另一个消费者之间分发,但不发送给两者)。 我当前的发布者配置如下
我有一个设置属性的方法 public void SetNetworkCredential(string userName, string password, string domain) {
有没有比以下 kludge 更简洁的方法来获取 Javascript 对象的 JSON 表示? System.out.println(((ScriptableObject) scope).callMe
好的,我知道 Rhino Mocks 中新的 AAA 语法存在很多混淆,但我必须说实话,从我目前所看到的来看,我喜欢。它读起来更好,并节省了一些击键。 基本上,我正在测试 ListController
我是一名优秀的程序员,十分优秀!