Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## org.directwebremoting.util
Class JavascriptUtil

    java.lang.Object
      org.directwebremoting.util.JavascriptUtil

    public class JavascriptUtil

extends java.lang.Object

Various Javascript code utilities. The escape classes were taken from jakarta-commons-lang which in turn borrowed from Turbine and other projects. The list of authors below is almost certainly far too long, but I'm not sure who really wrote these methods.

Author:
  Joe Walker [joe at getahead dot ltd dot uk], Apache Jakarta Turbine, GenerationJavaCore library, Purple Technology, Henri Yandell, Alexander Day Chaffee, Antony Riley, Helge Tesgaard, Sean Brown, Gary Gregory, Phil Steitz, Pete Gieser

Field Summary
`static int` | `COMPRESS_REMOVE_NEWLINES` Flag for use in javascript compression: Remove all lines endings.
`static int` | `COMPRESS_SHRINK_VARS` Flag for use in javascript compression: Shrink variable names.
`static int` | `COMPRESS_STRIP_BLANKLINES` Flag for use in javascript compression: Remove blank lines.
`static int` | `COMPRESS_STRIP_ML_COMMENTS` Flag for use in javascript compression: Remove multi line comments.
`static int` | `COMPRESS_STRIP_SL_COMMENTS` Flag for use in javascript compression: Remove single line comments.
`static int` | `COMPRESS_TRIM_LINES` Flag for use in javascript compression: Remove whitespace at the start and end of a line.
`static int` | `LEVEL_DEBUGGABLE` Basic compression that leaves the source fully debuggable.
`static int` | `LEVEL_NONE` Compression level that leaves the source un-touched.
`static int` | `LEVEL_NORMAL` Normal compression makes all changes that will work for generic javascript.
`static int` | `LEVEL_ULTRA` LEVEL_ULTRA performs additional compression that makes some assumptions about the style of javascript.

Constructor Summary
`JavascriptUtil()`

Method Summary
`static java.lang.String` | `compress(java.lang.String text, int level)` Compress the source code by removing java style comments and removing leading and trailing spaces.
`static java.lang.String` | `escapeJavaScript(java.lang.String str)` Escapes the characters in a `String` using JavaScript String rules.
`static boolean` | `isReservedWord(java.lang.String name)` Check to see if the given word is reserved or a bad idea in any known version of JavaScript.
`static java.lang.String` | `shrinkVariableNames(java.lang.String text)` Shrink variable names to a minimum.
`static java.lang.String` | `stripBlankLines(java.lang.String text)` Remove all blank lines from a string.
`static java.lang.String` | `stripMultiLineComments(java.lang.String text)` Remove all the multi-line comments from a block of text
`static java.lang.String` | `stripNewlines(java.lang.String text)` Remove all newline characters from a string.
`static java.lang.String` | `stripSingleLineComments(java.lang.String text)` Remove all the single-line comments from a block of text
`static java.lang.String` | `trimLines(java.lang.String text)` Remove any leading or trailing spaces from a line of code.
`static java.lang.String` | `unescapeJavaScript(java.lang.String str)` Unescapes any JavaScript literals found in the `String`.

Methods inherited from class java.lang.Object
`equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Field Detail

### COMPRESS_STRIP_SL_COMMENTS

    public static final int COMPRESS_STRIP_SL_COMMENTS

  Flag for use in javascript compression: Remove single line comments. For ease of use you may wish to use one of the LEVEL_* compression levels.

  See Also:
    Constant Field Values

### COMPRESS_STRIP_ML_COMMENTS

    public static final int COMPRESS_STRIP_ML_COMMENTS

  Flag for use in javascript compression: Remove multi line comments. For ease of use you may wish to use one of the LEVEL_* compression levels.

  See Also:
    Constant Field Values

### COMPRESS_TRIM_LINES

    public static final int COMPRESS_TRIM_LINES

  Flag for use in javascript compression: Remove whitespace at the start and end of a line. For ease of use you may wish to use one of the LEVEL_* compression levels.

  See Also:
    Constant Field Values

### COMPRESS_STRIP_BLANKLINES

    public static final int COMPRESS_STRIP_BLANKLINES

  Flag for use in javascript compression: Remove blank lines. This option will make the javascript harder to debug because line number references are likely be altered. For ease of use you may wish to use one of the LEVEL_* compression levels.

  See Also:
    Constant Field Values

### COMPRESS_SHRINK_VARS

    public static final int COMPRESS_SHRINK_VARS

  Flag for use in javascript compression: Shrink variable names. This option is currently un-implemented. For ease of use you may wish to use one of the LEVEL_* compression levels.

  See Also:
    Constant Field Values

### COMPRESS_REMOVE_NEWLINES

    public static final int COMPRESS_REMOVE_NEWLINES

  Flag for use in javascript compression: Remove all lines endings. Warning: Javascript can add semi-colons in for you. If you make use of this feature then removing newlines may well break. For ease of use you may wish to use one of the LEVEL_* compression levels.

  See Also:
    Constant Field Values

### LEVEL_NONE

    public static final int LEVEL_NONE

  Compression level that leaves the source un-touched.

  See Also:
    Constant Field Values

### LEVEL_DEBUGGABLE

    public static final int LEVEL_DEBUGGABLE

  Basic compression that leaves the source fully debuggable. This includes removing all comments and extraneous whitespace.

  See Also:
    Constant Field Values

### LEVEL_NORMAL

    public static final int LEVEL_NORMAL

  Normal compression makes all changes that will work for generic javascript. This adds variable name compression and blank line removal in addition to the compressions done by LEVEL_DEBUGGABLE.

  See Also:
    Constant Field Values

### LEVEL_ULTRA

    public static final int LEVEL_ULTRA

  LEVEL_ULTRA performs additional compression that makes some assumptions about the style of javascript. Specifically it assumes that you are not using javascripts ability to infer where the ; should go.

  See Also:
    Constant Field Values

Constructor Detail

### JavascriptUtil

    public JavascriptUtil()

Method Detail

### compress

    public static java.lang.String compress(java.lang.String text,
                                            int level)

  Compress the source code by removing java style comments and removing leading and trailing spaces.

  Parameters:
    `text` - The javascript (or java) program to compress
    `level` - The compression level - see LEVEL_* and COMPRESS_* constants.
  Returns:
    The compressed version

### trimLines

    public static java.lang.String trimLines(java.lang.String text)

  Remove any leading or trailing spaces from a line of code. This function could be improved by making it strip unnecessary double spaces, but since we would need to leave double spaces inside strings this is not simple and since the benefit is small, we'll leave it for now

  Parameters:
    `text` - The javascript program to strip spaces from.
  Returns:
    The stripped program

### stripSingleLineComments

    public static java.lang.String stripSingleLineComments(java.lang.String text)

  Remove all the single-line comments from a block of text

  Parameters:
    `text` - The text to remove single-line comments from
  Returns:
    The single-line comment free text

### stripMultiLineComments

    public static java.lang.String stripMultiLineComments(java.lang.String text)

  Remove all the multi-line comments from a block of text

  Parameters:
    `text` - The text to remove multi-line comments from
  Returns:
    The multi-line comment free text

### stripBlankLines

    public static java.lang.String stripBlankLines(java.lang.String text)

  Remove all blank lines from a string. A blank line is defined to be a line where the only characters are whitespace. We always ensure that the line contains a newline at the end.

  Parameters:
    `text` - The string to strip blank lines from
  Returns:
    The blank line stripped reply

### stripNewlines

    public static java.lang.String stripNewlines(java.lang.String text)

  Remove all newline characters from a string.

  Parameters:
    `text` - The string to strip newline characters from
  Returns:
    The stripped reply

### shrinkVariableNames

    public static java.lang.String shrinkVariableNames(java.lang.String text)

  Shrink variable names to a minimum.

  Parameters:
    `text` - The javascript program to shrink the variable names in.
  Returns:
    The shrunk version of the javascript program.

### escapeJavaScript

    public static java.lang.String escapeJavaScript(java.lang.String str)

  Escapes the characters in a `String` using JavaScript String rules.

  Escapes any values it finds into their JavaScript String form. Deals correctly with quotes and control-chars (tab, backslash, cr, ff, etc.)

  So a tab becomes the characters `'\\'` and `'t'`.

  The only difference between Java strings and JavaScript strings is that in JavaScript, a single quote must be escaped.

  Example:

       input string: He didn't say, "Stop!"
       output string: He didn\'t say, \"Stop!\"

  Parameters:
    `str` - String to escape values in, may be null
  Returns:
    String with escaped values, `null` if null string input

### unescapeJavaScript

    public static java.lang.String unescapeJavaScript(java.lang.String str)

  Unescapes any JavaScript literals found in the `String`.

  For example, it will turn a sequence of `'\'` and `'n'` into a newline character, unless the `'\'` is preceded by another `'\'`.

  Parameters:
    `str` - the `String` to unescape, may be null
  Returns:
    A new unescaped `String`, `null` if null string input

### isReservedWord

    public static boolean isReservedWord(java.lang.String name)

  Check to see if the given word is reserved or a bad idea in any known version of JavaScript.

  Parameters:
    `name` - The word to check
  Returns:
    false if the word is not reserved

Overview | Package | Class | Use | Tree | Deprecated | Index | Help
| Copyright ¬ 2005
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD