简体   繁体   English

如何从Java / scala调用jsbeautifier?

[英]How to call jsbeautifier from Java/scala?

I want to format some json files from Java code, and I want to use the famous http://jsbeautifier.org library. 我想从Java代码格式化一些json文件,并且想使用著名的http://jsbeautifier.org库。

I followed this question: Call external javascript functions from java code , but can't find a correct function to call. 我遵循了这个问题: 从Java代码调用外部javascript函数 ,但是找不到正确的函数来调用。

I tried inv.invokeFunction("js_beautify", json) , but it reports: 我尝试了inv.invokeFunction("js_beautify", json) ,但报告:

java.lang.NoSuchMethodException: no such method: js_beautify

My code (actually it's Scala, but just using Java API): 我的代码(实际上是Scala,但仅使用Java API):

val manager = new ScriptEngineManager()
val engine = manager.getEngineByName("JavaScript")
// read script file
engine.eval(FileUtils.readFileToString(new File("beautify.js")))

val inv = engine.asInstanceOf[Invocable]
// call function from script file
inv.invokeFunction("js_beautify", json).asInstanceOf[String]

And the structure of file beautify.js is: 文件beautify.js的结构为:

(function() {

    // a lot of js code
    // ...

    if (typeof define === "function" && define.amd) {
        // Add support for AMD ( https://github.com/amdjs/amdjs-api/wiki/AMD#defineamd-property- )
        define([], function() {
            return { 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;
    }
}());

Maybe I need to provide some global or window context from Java? 也许我需要从Java提供一些globalwindow上下文?


Update: 更新:

I followed the "JSBeautify NetBeans plugin" from @tiblu's answer: 我遵循了@tiblu的回答中的“ JSBeautify NetBeans插件”:

class FormatJson {
  def apply(json: String): String = {
    val context = Context.enter()
    context.setLanguageVersion(Context.VERSION_1_6)
    val scope = context.initStandardObjects()
    val reader = new FileReader(new File("beautify.js"))
    context.evaluateReader(scope, reader, "Beautify", 1, null)
    val fct = scope.get("js_beautify", scope).asInstanceOf[org.mozilla.javascript.Function]
    fct.call(context, scope, scope, Array[AnyRef](json)).toString
  }
}

But it reports: 但它报告:

Exception in thread "main" java.lang.ClassCastException: org.mozilla.javascript.UniqueTag cannot be cast to org.mozilla.javascript.Function

And the value of scope.get("js_beautify", scope) actually is NOT_FOUND . 并且scope.get("js_beautify", scope)实际上是NOT_FOUND

I'm using "org.mozilla" % "rhino" % "1.7R5" 我正在使用"org.mozilla" % "rhino" % "1.7R5"

Is there anything wrong? 有什么问题吗?

看看JSBeautify NetBeans插件是如何用Java编写的-https: //github.com/drewhamlett/netbeans-jsbeautify/tree/master/src/com/drewhamlett/jsbeautify

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM