|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.cleancode.format.JSArchive
public class JSArchive
Creates a JavaScript archive that consolidates and compresses a collection of JavaScript files. There are three considerations to using such a tool.
The default behavior is to consolidate without compression.
Use the -c
option to compress,
or to promote real obfuscation, use -c
and -l
,
the latter adding line break suppression so that each file
becomes a single line of code in the archive.
When you have enabled compression mode and/or line break suppression mode,
you may selectively choose to leave certain files with line breaks
or certain files completely uncompressed (for example, if you wish
to add a title block in a comment at the top of your archive file).
Precede any file you wish to exclude from being compressed with the
nc:
pseudo-protocol tag, as in nc:myfile.txt
.
Similarly, precede any file you wish to retain line breaks with the
nC:
pseudo-protocol tag.
The combination of -c
and -l
(or -cl
of course) provides the most compression
but may run into issues with certain JavaScript constructs.
When you have a "problem" file, back out the optimization for
just that file by first trying nC:file
to retain its
line breaks. If for whatever reason you still have JavaScript problems,
then you can back out further using nc:file
,
removing compression and retaining line breaks just for that file.
If you specify an archive file (rather than sending the archive to stdout) JSArchive will only rebuild the archive if needed, i.e. if one or more of the dependent files is newer than the archive file. This is a useful optimization most of the time--the exception is when you change your command-line (for instance, adding new files or changing compression on existing files). In that case simply remove the archive file so it will be regenerated.
This utility may be invoked as a standalone tool or as a library component.
Normal operation will operate silently.
Used as a library component, the calling program must handle any exceptions
as needed. Used as a standalone tool, you may enable verbose console
output with the -v
command-line argument.
In case of error, for example, JSArchive will display the first error found,
along with a count of errors.
Invoking in either fashion, you also get a separate log file
that details the construction of the archive.
If you use the -o
command-line argument
(or the setFilename
method) to specify an archive file,
the log file is created in the same directory as the archive;
its name will be the same as the archive name with
LOG_FILE_EXT
appended.
If your archive goes to stdout
, on the other hand,
the log will be named DEFAULT_LOG
(plus the same extension LOG_FILE_EXT
) in the current directory.
Nested Class Summary | |
---|---|
class |
JSArchive.JSCompactor
Compacts a series of text strings, discarding any unnecessary whitespace as well as all comments. |
Field Summary | |
---|---|
static String |
ARCHIVE_ELEM_MARKER
Notation prepended to each file in the archive, allowing for future scanning to identify archive contents. |
static String |
DEFAULT_LOG
Default log file base name when archive generated on stdout. |
static String |
LOG_FILE_EXT
Log file extension. |
static String |
VERSION
Current version of this class. |
Constructor Summary | |
---|---|
JSArchive()
Creates a new JSArchive to compress a set of JavaScript files. |
Method Summary | |
---|---|
void |
generateArchive(String[] args)
Processes a list of files into the archive. |
int |
getErrCount()
Returns the count of errors, if any. |
boolean |
isContentsMode()
Identifies whether contentsMode has been set. |
static void |
main(String[] args)
Command-line support to create a JavaScript archive. |
void |
setCompactMode(boolean compactMode)
Specifies whether generated archive should be compacted or not. |
void |
setContentsMode(boolean contentsMode)
Selects table of contents display mode. |
void |
setDisplayArgs(String[] displayArgs)
Specifies whether command-line should be emitted as a comment in the archive. |
void |
setFilename(String name)
Specifies a target file name for the archive. |
void |
setOneLineMode(boolean oneLineMode)
Sets mode for compacting a file by retaining line breaks or not. |
void |
setVerbose(boolean verbose)
Specifies verbose console output option. |
void |
showContents()
Displays contents of a previously created archive. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final String LOG_FILE_EXT
public static final String DEFAULT_LOG
public static final String ARCHIVE_ELEM_MARKER
public static final String VERSION
Constructor Detail |
---|
public JSArchive()
Method Detail |
---|
public boolean isContentsMode()
public void setFilename(String name)
name
- archive namepublic void setCompactMode(boolean compactMode)
compactMode
- true value indicates to compactpublic void setContentsMode(boolean contentsMode)
contentsMode
- true value displays contents of archivepublic void setOneLineMode(boolean oneLineMode)
oneLineMode
- If true, removes line breaks within a file.public void setVerbose(boolean verbose)
verbose
- true value indicates to display output on stderrpublic void setDisplayArgs(String[] displayArgs)
displayArgs
- array of command line argumentspublic int getErrCount()
public void showContents() throws IOException
ARCHIVE_ELEM_MARKER
) that is used to identify
the individual components.
IOException
- on any file problemspublic void generateArchive(String[] args) throws IOException
Processes a list of files into the archive. The file begins with a preamble that specifies the archive name, the creation date, the name and version of the generator (JSArchive), and the command-line used to generate the archive. Example:
////////////////////////////////////////////////////////////////////// // Archive: ../../../bkmk/MyBkmkFiles/BkArchive.js // Created: Mon Aug 30 15:32:08 PDT 2004 // Generator: JSArchive [1.15] // // The command line arguments to generate this file: // com.cleancode.format.JSArchive -c -o ../../../bkmk/MyBkmkFiles/BkArchive.js ../netscape/sniffer.js ../webmonkey/parseURL.js ../cleancode/Util/Version.js... // // This file was generated automatically and should not be edited manually. //////////////////////////////////////////////////////////////////////
Each file within the archive, then, is preceded with a short
header comment including the original file name,
the compression factor and a time stamp.
Additionally, a marker (ARCHIVE_ELEM_MARKER
) is inserted so that
running the -t
option to JSArchive can display the
contents of the archive.
You may add either local files (via local path name) or
remote files (via URL).
JSArchive uses several nested classes to parcel out the work.
JSReader will read each file and in turn
invoke JSCompactor
to compact each one.
JSClassScanner is an experimental component that
tries to add some additional information about the class structure
in the log file. It does not interfere with the performance of the
compression or consolidation at all.
Note that any parsing exceptions (notably unterminated strings) will output the original line unchanged with a brief comment tacked on the end.
args
- list of names of local files or URLs to process.
IOException
- on any file problemspublic static void main(String[] args)
args
- the command line arguments
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
CleanCode Java Libraries | Copyright © 2001-2012 Michael Sorens - Revised 2012.12.10 |