views:

204

answers:

4

I'm generating huge amount of JavaScript(on server side, ExtJS code) and for debug purposes(mainly,since for production I will -probably - minify the code) it would be nice to have well formatted JavaScript code.

Anybody know a library which offer such support?

Thanks.

Edit 1: What I'm looking for is a Java library which will :

  • layout/format a generated script or
  • help me to create the script(which I actually started to do, because it is easier to layout/format during generation than post generation - analyzing a complex JavaScript file looks more complicated than writing the script with correct indentation/layout) by providing a higher level Writer, which takes care of all the layout/indentation.
A: 

I find PolyStyle very handy. It's a Windows executable, works with many languages, integrates with the Shell and many IDEs, allows definition of custom styles by example, and comes with a trial version. It costs $15, money well spent in my opinion.

I'm not sure about server side JS, though, and whether there are any intricacies it can't handle (I've never worked with any). You would have to check for that.

Pekka
Looks nice, but I'm looking for something made in Java, it is easier to integrate.
adrian.tarau
You may want to be clearer on that in your question, it's hard to tell right now.
Pekka
expanded the question...
adrian.tarau
+1  A: 

I'm not aware of a pure Java solution but I was able to run that javascript beautifuler written in javascipt under Rhino (javascript on the JVM).

Unfortunately there's a bug affecting this script in the Java 6 version of Rhino. You could edit the script to avoid that bug or use the Mozilla version of Rhino.

Here's some code to run the beautifuler script under Mozilla Rhino:

import java.io.FileReader;
import java.io.IOException;
import org.mozilla.javascript.*;

public class JsBeautify {
    private static final String BEAUTIFY_SCRIPT_PATH = "js-beautify/beautify.js";

    private static String jsBeautify(String jsCode) {
     Context cx = Context.enter();
     Scriptable scope = cx.initStandardObjects();

     try {
      FileReader reader = new FileReader(BEAUTIFY_SCRIPT_PATH);
      cx.evaluateReader(scope, reader, "beautify.js", 1, null);
      reader.close();
     } catch (IOException e) {
      throw new Error("Error reading "+BEAUTIFY_SCRIPT_PATH);
     }
     scope.put("jsCode", scope, jsCode);
     return (String) cx.evaluateString(scope, "js_beautify(jsCode)",
       "inline", 1, null);
    }
}
Alexandre Jasmin
Looks promising, but I'm a little bit concerned about performance. I will give it a try, thanks.
adrian.tarau
@adrian.tarau - if you're feeling adventurous, why not try porting the beautifier code to Java? It's not much code, and it doesn't look *too* difficult to port.
Eli Acherkan
I am adventurous, but I don't have too much time ;)
adrian.tarau
You could gain some performance by keeping around a reference to cx and scope, only making the last evaluateString() call each time.
Alexandre Jasmin
True, but still layouting a JS around 40K with FireFox took around ~1sec, I presume with Rhino it would take at least double the time.Even if it would be used only for debug purposes - like enabled on devel servers - and not every time I will have a JS around this size, it still seems a bottleneck - opening a complex dialog with ExtJS will be displayed to the user after 4-6 seconds(6-8 if you use IE6).
adrian.tarau
A: 

I use JSEclipse from Adobe, its very useful for writing well formatted JS. It even has code suggestion. i.e. Auto Complete.

medopal
Thanks, but I'm Looking for a Java library...
adrian.tarau
+1  A: 

Well, in the end I made my own JavaScript "Writer" since I couldn't find something that fits my needs.

I think it is always preferred to write things in the correct form instead of dump them with a template engine(like Velocity or FreeMaker) and then apply a layout engine.

Thank you for your answers.

adrian.tarau