com.cleancode.format
Class JSArchive.JSCompactor

java.lang.Object
  extended by com.cleancode.format.JSArchive.JSCompactor
Enclosing class:
JSArchive

public class JSArchive.JSCompactor
extends Object

Compacts a series of text strings, discarding any unnecessary whitespace as well as all comments. Typically, you pass each line of a file to the trimLine method, generating a revised, compacted line. However, you may also pass an entire file stored as a single string. Whatever you pass in, the result will come back as a single line. So if you pass individual lines, they will retain their "line-nature". If you pass a block of lines, it becomes a single line.

Note that all lines are terminated with a single newline character regardless of the system on which you are executing. This delimiter works fine in all browsers, and is obviously more compact than the two-character Windows line ending.


Constructor Summary
JSArchive.JSCompactor()
          Creates a JSCompactor, the workhorse compaction engine.
 
Method Summary
 String trimLine(List<String> lines)
          Compacts a list of strings, removing any unnecessary whitespace and comments.
 String trimLine(String s)
           Compacts a String removing any unnecessary whitespace and comments.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JSArchive.JSCompactor

public JSArchive.JSCompactor()
Creates a JSCompactor, the workhorse compaction engine.

Method Detail

trimLine

public String trimLine(String s)

Compacts a String removing any unnecessary whitespace and comments. This method is designed to be called repeatedly for each line of input within a single file. It retains state across calls so that block comments (/* ... ) may span multiple lines.

The input string is tokenized by examining the following delimiters: whitespace, single quote ('), double quote ("), block comment start (/*), and inline comment (//). trimLine handles any mixture of these as JavaScript semantics require, i.e. putting an inline comment (//...) inside a block comment will be ignored, so that even if the ending block comment marker is on the same line, it will still be seen. Also handles escaped quotes inside quotes (i.e. \' inside 'string' or \" inside "string").

The only known anomaly is a minor issue with regular expressions. Just as an apostrophe (') is a string delimiter, JavaScript uses the virgule (/) as a regular expression delimiter. Inside a regular expression, one should be able to use a single or double quote character uninhibited, but JSCompactor will see these as starting a string. Simply escape these characters inside the regular expression to avoid this issue. That is, instead of something like /ab*c'd/ use /ab*c\'d/.

In the event of a parsing error, this is marked with a block comment (/* ...) at the end of the string.

Parameters:
s - input string
Returns:
trimmed (optimally-reduced) string
See Also:
trimLine(List)

trimLine

public String trimLine(List<String> lines)
Compacts a list of strings, removing any unnecessary whitespace and comments. This method takes a list of strings (typically each one representing a line of input) and concatenates their optimized forms into a single output string, so there is only a single newline at the end of the returned string. In the event of a parsing error, a block comment (/* ...) at the end of the particular string, which may or may not be at the end of the final, concatenated string.

Parameters:
lines - list of input strings to optimize on one line
Returns:
trimmed (optimally-reduced) string
See Also:
trimLine(String)


CleanCode Java Libraries Copyright © 2001-2012 Michael Sorens - Revised 2012.12.10 Get CleanCode at SourceForge.net. Fast, secure and Free Open Source software downloads