0

I want to format some json files from Java code, and I want to use the famous http://jsbeautifier.org library.

I followed this question: Call external javascript functions from java code , but can't find a correct function to call.

I tried inv.invokeFunction("js_beautify", json), but it reports:

java.lang.NoSuchMethodException: no such method: js_beautify

My code (actually it's Scala, but just using 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:

(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?


Update:

I followed the "JSBeautify NetBeans plugin" from @tiblu's answer:

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.

I'm using "org.mozilla" % "rhino" % "1.7R5"

Is there anything wrong?

Community
  • 1
  • 1
Freewind
  • 193,756
  • 157
  • 432
  • 708
  • Are you properly loading the `js_beautify` library? Show some code. – Halcyon Apr 10 '15 at 14:00
  • Possible duplicate of [Command line JavaScript code beautifier that works on Windows and Linux](http://stackoverflow.com/questions/18985/command-line-javascript-code-beautifier-that-works-on-windows-and-linux) – Stephan Nov 15 '16 at 10:54
  • @Stephan Thanks, but that's not a duplicated question of this one. The point of this question is how to invoke that library in Java/Scala code, not how to call it from command line. – Freewind Nov 15 '16 at 13:43

1 Answers1

0

Check out how JSBeautify NetBeans plugin does it which is written in Java - https://github.com/drewhamlett/netbeans-jsbeautify/tree/master/src/com/drewhamlett/jsbeautify

tiblu
  • 2,959
  • 1
  • 22
  • 22