In my architecture Javascript code is generated in the server.
A huge chunk of Javascript code is generated, stored in a java.lang.String
and sent to the client side.
I want to more easily debug the generated code (both the generation and how it runs).
Someone knows an easy way to format Javascript code?
In my architecture Javascript code is generated in the server.
A huge chunk of Javascript code is generated, stored in a java.lang.String
and sent to the client side.
I want to more easily debug the generated code (both the generation and how it runs).
Someone knows an easy way to format Javascript code?
Share Improve this question asked Oct 24, 2011 at 14:56 flybywireflybywire 274k201 gold badges406 silver badges509 bronze badges 4- 1 see stackoverflow./questions/351298/… – user865192 Commented Oct 24, 2011 at 15:02
-
1
"Javascript code is generated in the server." (Shudder) Why is this JS even being generated using Java? I cannot think of a circumstance where the JS is not better left as external files which are read in by the HTML. Configure base conditions for the scripts using some trivial
var
attributes written to the page, and the job is done. – Andrew Thompson Commented Oct 24, 2011 at 17:59 - 1 @AndrewThompson By creating it on the server you open the door to interesting, concisely-defined behavior. As long as it's "invisible" (i.e., handled by app-/framework-specific code), I don't see an issue with it. It's a tool like any other. – Dave Newton Commented Oct 24, 2011 at 18:16
- @Andrew, it has some good reasons, the most important one being that it is a legacy system with its amount of happy paying customers – flybywire Commented Oct 25, 2011 at 8:28
5 Answers
Reset to default 5It may be too late, but still.
I haven't found popular Java libraries for doing what you want; however, there are many javascript libraries for that (for example, js-beautify). You can save such library source code in resources of your application (you can get the code from one of cdn links, so you don't have to group and minify it manually), and then load it and invoke it with the Nashorn javascript engine.
Your code may look like this (roughly):
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.io.InputStreamReader;
public class JavascriptBeautifierForJava {
// my javascript beautifier of choice
private static final String BEAUTIFY_JS_RESOURCE = "beautify.js";
// name of beautifier function
private static final String BEAUTIFY_METHOD_NAME = "js_beautify";
private final ScriptEngine engine;
JavascriptBeautifierForJava() throws ScriptException {
engine = new ScriptEngineManager().getEngineByName("nashorn");
// this is needed to make self invoking function modules work
// otherwise you won't be able to invoke your function
engine.eval("var global = this;");
engine.eval(new InputStreamReader(getClass().getClassLoader().getResourceAsStream(BEAUTIFY_JS_RESOURCE)));
}
public String beautify(String javascriptCode) throws ScriptException, NoSuchMethodException {
return (String) ((Invocable) engine).invokeFunction(BEAUTIFY_METHOD_NAME, javascriptCode);
}
public static void main(String[] args) throws ScriptException, NoSuchMethodException {
String unformattedJs = "var a = 1; b = 2; var user = { name : \n \"Andrew\"}";
JavascriptBeautifierForJava javascriptBeautifierForJava = new JavascriptBeautifierForJava();
String formattedJs = javascriptBeautifierForJava.beautify(unformattedJs);
System.out.println(formattedJs);
// will print out:
// var a = 1;
// b = 2;
// var user = {
// name: "Andrew"
// }
}
}
If you're going to use this approach, make sure to reuse JavascriptBeautifier object, because it's not too effective to recreate one whenever you need to beautify code.
Two closely-related questions (possible duplicates):
- Command line JavaScript code beautifier that works on Windows and Linux
- https://stackoverflow./questions/6260431/pretty-print-javascript-using-java
May be late - but still :)
There is a pretty printer sample in jdk9 repo. This uses Nashorn Parser API (https://docs.oracle./javase/9/docs/api/jdk/nashorn/api/tree/Parser.html)
http://hg.openjdk.java/jdk9/dev/nashorn/file/17cc754c8936/samples/prettyprinter.js
The idea that es to mind is manage your JavaScript output line by line (like a CodeLine
class that has text and an indentation level) and when you're actually displaying this to the user and printing each line on a it's own line you can tab appropriately to format the code. It would require a custom class and probably something like an ArrayList<CodeLine>
.
use firefox with addon firebug. You can add breakpoints in your generated javascript and do some step by step debugging.