org.directwebremoting.impl
Class LegacyCompressor

java.lang.Object
  extended by org.directwebremoting.impl.LegacyCompressor
All Implemented Interfaces:
Compressor

public class LegacyCompressor
extends java.lang.Object
implements Compressor

An implementation of Compressor that does nothing.

Author:
Joe Walker [joe at getahead dot ltd dot uk]

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
LegacyCompressor()
           
 
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.
 java.lang.String compressJavaScript(java.lang.String script)
          Compress a JavaScript file to a smaller version of the original
 void setCompressionLevel(int compressionLevel)
           
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.
 
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 JavaScript's ability to infer where the ; should go.

See Also:
Constant Field Values
Constructor Detail

LegacyCompressor

public LegacyCompressor()
Method Detail

compressJavaScript

public java.lang.String compressJavaScript(java.lang.String script)
                                    throws java.io.IOException
Description copied from interface: Compressor
Compress a JavaScript file to a smaller version of the original

Specified by:
compressJavaScript in interface Compressor
Parameters:
script - The script to compress
Returns:
The compressed script
Throws:
java.io.IOException

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.

setCompressionLevel

public void setCompressionLevel(int compressionLevel)
Parameters:
compressionLevel - The compressionLevel to set.

Copyright 2008