com.googlecode.htmlcompressor.compressor
Class HtmlCompressor

java.lang.Object
  extended by com.googlecode.htmlcompressor.compressor.HtmlCompressor
All Implemented Interfaces:
Compressor

public class HtmlCompressor
extends java.lang.Object
implements Compressor

Class that compresses given HTML source by removing comments, extra spaces and line breaks while preserving content within <pre>, <textarea>, <script> and <style> tags. Can optionally compress content inside <script> or <style> tags using Yahoo YUI Compressor library.

Author:
Sergiy Kovalchuk

Constructor Summary
HtmlCompressor()
           
 
Method Summary
 java.lang.String compress(java.lang.String html)
          The main method that compresses given HTML source and returns compressed result.
 java.util.List<java.util.regex.Pattern> getPreservePatterns()
          Returns a list of Patterns defining custom preserving block rules
 int getYuiCssLineBreak()
          Returns number of symbols per line Yahoo YUI Compressor will use during CSS compression.
 org.mozilla.javascript.ErrorReporter getYuiErrorReporter()
          Returns ErrorReporter used by YUI Compressor to log error messages during JavasSript compression
 int getYuiJsLineBreak()
          Returns number of symbols per line Yahoo YUI Compressor will use during JavaScript compression.
 boolean isCompressCss()
          Returns true if CSS compression is enabled.
 boolean isCompressJavaScript()
          Returns true if JavaScript compression is enabled.
 boolean isEnabled()
          Returns true if compression is enabled.
 boolean isRemoveComments()
          Returns true if all HTML comments will be removed.
 boolean isRemoveIntertagSpaces()
          Returns true if all inter-tag whitespace characters will be removed.
 boolean isRemoveMultiSpaces()
          Returns true if all multiple whitespace characters will be replaced with single spaces.
 boolean isRemoveQuotes()
          Returns true if all unnecessary quotes will be removed from tag attributes.
 boolean isYuiJsDisableOptimizations()
          Returns true if Yahoo YUI Compressor will disable all the built-in micro optimizations during JavaScript compression.
 boolean isYuiJsNoMunge()
          Returns true if Yahoo YUI Compressor will only minify javascript without obfuscating local symbols.
 boolean isYuiJsPreserveAllSemiColons()
          Returns true if Yahoo YUI Compressor will preserve unnecessary semicolons during JavaScript compression.
 void setCompressCss(boolean compressCss)
          Enables CSS compression within <style> tags using Yahoo YUI Compressor if set to true.
 void setCompressJavaScript(boolean compressJavaScript)
          Enables JavaScript compression within <script> tags using Yahoo YUI Compressor if set to true.
 void setEnabled(boolean enabled)
          If set to false all compression will be bypassed.
 void setPreservePatterns(java.util.List<java.util.regex.Pattern> preservePatterns)
          This method allows setting custom block preservation rules defined by regular expression patterns.
 void setRemoveComments(boolean removeComments)
          If set to true all HTML comments will be removed.
 void setRemoveIntertagSpaces(boolean removeIntertagSpaces)
          If set to true all inter-tag whitespace characters will be removed.
 void setRemoveMultiSpaces(boolean removeMultiSpaces)
          If set to true all multiple whitespace characters will be replaced with single spaces.
 void setRemoveQuotes(boolean removeQuotes)
          If set to true all unnecessary quotes will be removed from tag attributes.
 void setYuiCssLineBreak(int yuiCssLineBreak)
          Tells Yahoo YUI Compressor to break lines after the specified number of symbols during CSS compression.
 void setYuiErrorReporter(org.mozilla.javascript.ErrorReporter yuiErrorReporter)
          Sets ErrorReporter that YUI Compressor will use for reporting errors during JavaScript compression.
 void setYuiJsDisableOptimizations(boolean yuiJsDisableOptimizations)
          Tells Yahoo YUI Compressor to disable all the built-in micro optimizations during JavaScript compression.
 void setYuiJsLineBreak(int yuiJsLineBreak)
          Tells Yahoo YUI Compressor to break lines after the specified number of symbols during JavaScript compression.
 void setYuiJsNoMunge(boolean yuiJsNoMunge)
          Tells Yahoo YUI Compressor to only minify javascript without obfuscating local symbols.
 void setYuiJsPreserveAllSemiColons(boolean yuiJsPreserveAllSemiColons)
          Tells Yahoo YUI Compressor to preserve unnecessary semicolons during JavaScript compression.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HtmlCompressor

public HtmlCompressor()
Method Detail

compress

public java.lang.String compress(java.lang.String html)
                          throws java.lang.Exception
The main method that compresses given HTML source and returns compressed result.

Specified by:
compress in interface Compressor
Parameters:
html - HTML content to compress
Returns:
compressed content.
Throws:
java.lang.Exception

isCompressJavaScript

public boolean isCompressJavaScript()
Returns true if JavaScript compression is enabled.

Returns:
current state of JavaScript compression.

setCompressJavaScript

public void setCompressJavaScript(boolean compressJavaScript)
Enables JavaScript compression within <script> tags using Yahoo YUI Compressor if set to true. Default is false for performance reasons.

Note: Compressing JavaScript is not recommended if pages are compressed dynamically on-the-fly because of performance impact. You should consider putting JavaScript into a separate file and compressing it using standalone YUICompressor for example.

Parameters:
compressJavaScript - set true to enable JavaScript compression. Default is false
See Also:
Yahoo YUI Compressor

isCompressCss

public boolean isCompressCss()
Returns true if CSS compression is enabled.

Returns:
current state of CSS compression.

setCompressCss

public void setCompressCss(boolean compressCss)
Enables CSS compression within <style> tags using Yahoo YUI Compressor if set to true. Default is false for performance reasons.

Note: Compressing CSS is not recommended if pages are compressed dynamically on-the-fly because of performance impact. You should consider putting CSS into a separate file and compressing it using standalone YUICompressor for example.

Parameters:
compressCss - set true to enable CSS compression. Default is false
See Also:
Yahoo YUI Compressor

isYuiJsNoMunge

public boolean isYuiJsNoMunge()
Returns true if Yahoo YUI Compressor will only minify javascript without obfuscating local symbols. This corresponds to --nomunge command line option.

Returns:
nomunge parameter value used for JavaScript compression.
See Also:
Yahoo YUI Compressor

setYuiJsNoMunge

public void setYuiJsNoMunge(boolean yuiJsNoMunge)
Tells Yahoo YUI Compressor to only minify javascript without obfuscating local symbols. This corresponds to --nomunge command line option. This option has effect only if JavaScript compression is enabled. Default is false.

Parameters:
yuiJsNoMunge - set true to enable nomunge mode
See Also:
Yahoo YUI Compressor

isYuiJsPreserveAllSemiColons

public boolean isYuiJsPreserveAllSemiColons()
Returns true if Yahoo YUI Compressor will preserve unnecessary semicolons during JavaScript compression. This corresponds to --preserve-semi command line option.

Returns:
preserve-semi parameter value used for JavaScript compression.
See Also:
Yahoo YUI Compressor

setYuiJsPreserveAllSemiColons

public void setYuiJsPreserveAllSemiColons(boolean yuiJsPreserveAllSemiColons)
Tells Yahoo YUI Compressor to preserve unnecessary semicolons during JavaScript compression. This corresponds to --preserve-semi command line option. This option has effect only if JavaScript compression is enabled. Default is false.

Parameters:
yuiJsPreserveAllSemiColons - set true to enable preserve-semi mode
See Also:
Yahoo YUI Compressor

isYuiJsDisableOptimizations

public boolean isYuiJsDisableOptimizations()
Returns true if Yahoo YUI Compressor will disable all the built-in micro optimizations during JavaScript compression. This corresponds to --disable-optimizations command line option.

Returns:
disable-optimizations parameter value used for JavaScript compression.
See Also:
Yahoo YUI Compressor

setYuiJsDisableOptimizations

public void setYuiJsDisableOptimizations(boolean yuiJsDisableOptimizations)
Tells Yahoo YUI Compressor to disable all the built-in micro optimizations during JavaScript compression. This corresponds to --disable-optimizations command line option. This option has effect only if JavaScript compression is enabled. Default is false.

Parameters:
yuiJsDisableOptimizations - set true to enable disable-optimizations mode
See Also:
Yahoo YUI Compressor

getYuiJsLineBreak

public int getYuiJsLineBreak()
Returns number of symbols per line Yahoo YUI Compressor will use during JavaScript compression. This corresponds to --line-break command line option.

Returns:
line-break parameter value used for JavaScript compression.
See Also:
Yahoo YUI Compressor

setYuiJsLineBreak

public void setYuiJsLineBreak(int yuiJsLineBreak)
Tells Yahoo YUI Compressor to break lines after the specified number of symbols during JavaScript compression. This corresponds to --line-break command line option. This option has effect only if JavaScript compression is enabled. Default is -1 to disable line breaks.

Parameters:
yuiJsLineBreak - set number of symbols per line
See Also:
Yahoo YUI Compressor

getYuiCssLineBreak

public int getYuiCssLineBreak()
Returns number of symbols per line Yahoo YUI Compressor will use during CSS compression. This corresponds to --line-break command line option.

Returns:
line-break parameter value used for CSS compression.
See Also:
Yahoo YUI Compressor

setYuiCssLineBreak

public void setYuiCssLineBreak(int yuiCssLineBreak)
Tells Yahoo YUI Compressor to break lines after the specified number of symbols during CSS compression. This corresponds to --line-break command line option. This option has effect only if CSS compression is enabled. Default is -1 to disable line breaks.

Parameters:
yuiCssLineBreak - set number of symbols per line
See Also:
Yahoo YUI Compressor

isRemoveQuotes

public boolean isRemoveQuotes()
Returns true if all unnecessary quotes will be removed from tag attributes.


setRemoveQuotes

public void setRemoveQuotes(boolean removeQuotes)
If set to true all unnecessary quotes will be removed from tag attributes. Default is false.

Note: Even though quotes are removed only when it is safe to do so, it still might break strict HTML validation. Turn this option on only if a page validation is not very important or to squeeze the most out of the compression. This option has no performance impact.

Parameters:
removeQuotes - set true to remove unnecessary quotes from tag attributes

isEnabled

public boolean isEnabled()
Returns true if compression is enabled.

Returns:
true if compression is enabled.

setEnabled

public void setEnabled(boolean enabled)
If set to false all compression will be bypassed. Might be useful for testing purposes. Default is true.

Parameters:
enabled - set false to bypass all compression

isRemoveComments

public boolean isRemoveComments()
Returns true if all HTML comments will be removed.

Returns:
true if all HTML comments will be removed

setRemoveComments

public void setRemoveComments(boolean removeComments)
If set to true all HTML comments will be removed. Default is true.

Parameters:
removeComments - set true to remove all HTML comments

isRemoveMultiSpaces

public boolean isRemoveMultiSpaces()
Returns true if all multiple whitespace characters will be replaced with single spaces.

Returns:
true if all multiple whitespace characters will be replaced with single spaces.

setRemoveMultiSpaces

public void setRemoveMultiSpaces(boolean removeMultiSpaces)
If set to true all multiple whitespace characters will be replaced with single spaces. Default is true.

Parameters:
removeMultiSpaces - set true to replace all multiple whitespace characters will single spaces.

isRemoveIntertagSpaces

public boolean isRemoveIntertagSpaces()
Returns true if all inter-tag whitespace characters will be removed.

Returns:
true if all inter-tag whitespace characters will be removed.

setRemoveIntertagSpaces

public void setRemoveIntertagSpaces(boolean removeIntertagSpaces)
If set to true all inter-tag whitespace characters will be removed. Default is false.

Note: It is fairly safe to turn this option on unless you rely on spaces for page formatting. Even if you do, you can always preserve required spaces with &nbsp;. This option has no performance impact.

Parameters:
removeIntertagSpaces - set true to remove all inter-tag whitespace characters

getPreservePatterns

public java.util.List<java.util.regex.Pattern> getPreservePatterns()
Returns a list of Patterns defining custom preserving block rules

Returns:
list of Pattern objects defining rules for preserving block rules

setPreservePatterns

public void setPreservePatterns(java.util.List<java.util.regex.Pattern> preservePatterns)
This method allows setting custom block preservation rules defined by regular expression patterns. Blocks that match provided patterns will be skipped during HTML compression.

Custom preservation rules have higher priority than default rules. Priority between custom rules are defined by their position in a list (beginning of a list has higher priority).

Parameters:
preservePatterns - List of Pattern objects that will be used to skip matched blocks during compression

getYuiErrorReporter

public org.mozilla.javascript.ErrorReporter getYuiErrorReporter()
Returns ErrorReporter used by YUI Compressor to log error messages during JavasSript compression

Returns:
ErrorReporter used by YUI Compressor to log error messages during JavasSript compression
See Also:
Yahoo YUI Compressor, Error Reporter Interface

setYuiErrorReporter

public void setYuiErrorReporter(org.mozilla.javascript.ErrorReporter yuiErrorReporter)
Sets ErrorReporter that YUI Compressor will use for reporting errors during JavaScript compression. If no ErrorReporter was provided a NullPointerException will be throuwn in case of an error during the compression.

For simple error reporting that uses System.err stream DefaultErrorReporter can be used.

Parameters:
yuiErrorReporter - ErrorReporter that will be used by YUI Compressor
See Also:
DefaultErrorReporter, Yahoo YUI Compressor, ErrorReporter Interface