Wednesday, March 23, 2011

How can I convert JavaScript code into one big Java string

So I have 1000 lines of javascript. I need to turn it into a Java String so that I can output (via System.out.println or whatever).

I'm looking for an online tool to escape all the quotes... something geared toward my specific need would be nice as I don't want other special characters changed. Lines like:

var rgx = /(\d+)(\d{3})/;

need to stay intact.

The situation mandates the JavaScript be put into a String so please no workarounds.

From stackoverflow
  • You can compress the file using one of the available tools to achieve this effect:

    YUI Compressor Online

    Dean Edward's Packer

    Douglas Crockford's JSMIN

    ninesided : I don't see how this is relevant, the question is about escaping quotes in a String not making 1000 lines of JavaScript smaller.
    Diodeus : They make 1000 lines of JavaScript one line of JavaScript.
    Christoph : @Diodeus: But that's not what Paul wants to do - he's looking for a function which produces appropriate escape sequences for unsafe characters used in scripts...
  • You can use the jsmin tool to compress the Javascript to a single line (hopefully), but it doesn't escape the quotes. This can be done with search/replace in an editor or the server side scripting language used.

    ninesided : the number of lines is relevant, you can have new line characters in a String.
  • Here's a link which features Crockford's implementation of the quote() function. Use it to build your own JavaScript converter.

    Edit: I also slightly modified the function to output an ascii-safe string by default.

    Edit2: Just a suggestion: It might be smarter to keep the JavaScript in an external file and read it at runtime instead of hardcoding it...

    Edit3: And here's a fully-featured solution - just copy to a .html file and replace the dummy script:

    <script src="quote.js"></script>
    <script>
    // this is the JavaScript to be converted:
    var foo = 'bar';
    var spam = 'eggs';
    
    function fancyFunction() {
        return 'cool';
    }
    </script>
    <pre><script>
    document.writeln(quote(
        document.getElementsByTagName('script')[1].firstChild.nodeValue, true));
    </script></pre>
    
  • So everything I tried ended up breaking the javascript. I finally got it to work by doing the following:

    Using Notepad++:

    1. Hit Shift + Tab a bunch of times to unindent every line

    2. Do View -> Show End Of Line

    3. Highlight the LF char and do a Replace All to replace with empty string
    4. Repeat for the CR char

    5. Highlight a " (quote character) and do a Replace All with \" (escaped quote)... just typing the quote character into the Replace prompt only grabbed some of the quotes for some reason.

    6. Now You have 1 enormously long line... I ended up having to break the 1 string apart into about 2000 character long lines.... The crazy long line was killing IE and/or breaking the Java String limit.

0 comments:

Post a Comment