jsutil.compress
Class JSCReplace

java.lang.Object
  |
  +--xbn.XBNObject
        |
        +--jsutil.compress.JSCReplace

public class JSCReplace
extends XBNObject

Random string replacement functions, tailored to JavaScript compression.

Source code:  JSCReplace.java


Field Summary
protected static String sLS
           
 
Fields inherited from class xbn.XBNObject
bFALSE_IN_PRODUCTION, bTRUE_IN_PRODUCTION, sCNSTR, sES, sLINE_SEP
 
Constructor Summary
JSCReplace(StringOrBuffer sob_source)
          Create a JSCReplace.
JSCReplace(StringOrBuffer sob_source, Outputter optr_log)
          Create a JSCReplace.
 
Method Summary
 void elimComments()
          Eliminate all comments.
 void elimEmptyLines()
          Eliminates all empty lines from the source.
 void elimLinesWithToken(String s_token)
          Eliminate every line containing the provided token.
 void elimMLCs()
          Eliminate all multiple-line comments from the provided source code, using default mlc-delimiters.
 void elimMLCs(String s_mlcStart, String s_mlcEnd)
          Eliminate all multiple-line comments from the provided source code.
 void elimNextToToken(boolean b_beforeAfter, String s_token, String[] a_string)
          Eliminate all tokens in the source code that precedes/follows any of the provided strings.
 void elimNextToToken(boolean b_beforeAfter, String s_token, String[] a_string, String[] as_lineExceptions)
          Eliminate all tokens in the source code that precedes/follows any of the provided strings.
 void elimSLCs()
          Eliminate all single-line comments from the provided source code, using the default slc-delimiter.
 void elimSLCs(String s_slcDelim)
          Eliminate all single-line comments from the provided source code.
 void elimSpaceAfter(String[] as_spaceAfter)
          Eliminate all spaces in the source code that follows any of the provided strings.
 void elimSpaceAfter(String[] as_spaceAfter, String[] as_lineExceptions)
          Eliminate all spaces in the source code that follows any of the provided strings.
 void elimSpaceBefore(String[] as_spaceBefore)
          Eliminate all spaces in the source code that precedes any of the provided strings.
 void elimSpaceBefore(String[] as_spaceBefore, String[] as_lineExceptions)
          Eliminate all spaces in the source code that precedes any of the provided strings.
 Outputter getOptrLog()
          Get the Outputter through which logging should be written.
 StringOrBuffer getSOBSource()
          Get the source code as an StringOrBuffer.
 void log(String s_tring)
          Write some output to the log file, with an ending line break.
 void lognl(String s_tring)
          Write some output to the log file, without an ending line break.
 void replaceAll(String s_searchFor, String s_replaceWith)
          Replace all instances of something with something else, with one pass through the source only.
 void replaceAll(String s_searchFor, String s_replaceWith, String[] as_lineExceptions)
          Replace all instances of something with something else, with one pass through the source only.
 void replaceAllOrUntil(boolean b_allOrUntil, String s_searchFor, String s_replaceWith)
          Replace all instances of something with something else.
 void replaceAllOrUntil(boolean b_allOrUntil, String s_searchFor, String s_replaceWith, String[] as_lineExceptions)
          Replace all instances of something with something else.
 void replaceIdentifiers(String[] as_searchFor, String[] as_replaceWith)
          Replace all instances of the search-for strings.
 void replaceIdentifiers(String[] as_searchFor, String[] as_replaceWith, String[] as_lineExceptions)
          Replace all instances of the search-for strings.
 void replaceUntil(String s_searchFor, String s_replaceWith)
          Replace all instances of something with something else, repeatedly, until no more instances exist.
 void replaceUntil(String s_searchFor, String s_replaceWith, String[] as_lineExceptions)
          Replace all instances of something with something else, repeatedly, until no more instances exist.
 void setOptrLog(Outputter optr_log)
          Set the Outputter to use for debugging output.
 void trimAllLines()
          Trim all whitespace from the start and ends of each line.
 void trimAndElimEmptyLines()
          Trim every line, and then eliminate all empty lines in the source.
 
Methods inherited from class xbn.XBNObject
getXMsgPrefix, sop, sopl, sopl, throwAX, throwAXIfBadStr, throwAXIfNull, throwAXSpoof
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

sLS

protected static final String sLS
Constructor Detail

JSCReplace

public JSCReplace(StringOrBuffer sob_source)

Create a JSCReplace.

Equal to JSCReplace(sob_source, (new Outputter()))


JSCReplace

public JSCReplace(StringOrBuffer sob_source,
                  Outputter optr_log)

Create a JSCReplace.

Parameters:
sob_source - The JavaScript source code needing replacements.
optr_log - Provided directly to setOptrLog.
Method Detail

getOptrLog

public final Outputter getOptrLog()

Get the Outputter through which logging should be written. See log and lognl

Set with setOptrLog.

Returns:
optr_log, exactly as provided to setOptrLog.

setOptrLog

public final void setOptrLog(Outputter optr_log)

Set the Outputter to use for debugging output.

Get with getOptrLog.

Parameters:
optr_log - The Outputter through which logging should be written. May not be null.

getSOBSource

public final StringOrBuffer getSOBSource()

Get the source code as an StringOrBuffer.

Returns:
sob_source, exactly as provided to the constructor, although it's value may have changed.

log

public final void log(String s_tring)

Write some output to the log file, with an ending line break.

See lognl

Equal to getOptrLog().write(s_tring)


lognl

public final void lognl(String s_tring)

Write some output to the log file, without an ending line break.

See log

Equal to getOptrLog().writeNoln(s_tring)


elimMLCs

public final void elimMLCs()

Eliminate all multiple-line comments from the provided source code, using default mlc-delimiters.

Equal to elimMLCs("/" + "**", "**" + "/")


elimMLCs

public final void elimMLCs(String s_mlcStart,
                           String s_mlcEnd)

Eliminate all multiple-line comments from the provided source code. Logs activity. Delimiters inside strings are deleted. Change them to "/" + "**" and "**" + "/"

Parameters:
s_mlcStart - Provided directly to [Utility].stripMLCs
s_mlcEnd - Provided directly to [Utility].stripMLCs

elimSLCs

public final void elimSLCs()

Eliminate all single-line comments from the provided source code, using the default slc-delimiter.

Equal to elimSLCs("/" + "/")


elimSLCs

public final void elimSLCs(String s_slcDelim)

Eliminate all single-line comments from the provided source code. Logs activity. Delimiters inside strings are deleted. Change them to "/" + "/"

Parameters:
s_slcDelim - Provided directly to [Utility].stripSLCs

elimComments

public final void elimComments()

Eliminate all comments.

This calls
   trimAllLines()
then
   elimEmptyLines()


elimSpaceBefore

public final void elimSpaceBefore(String[] as_spaceBefore)

Eliminate all spaces in the source code that precedes any of the provided strings.

Equal to elimSpaceBefore(as_spaceBefore, null)


elimSpaceBefore

public final void elimSpaceBefore(String[] as_spaceBefore,
                                  String[] as_lineExceptions)

Eliminate all spaces in the source code that precedes any of the provided strings. Logs activity.

Equal to elimNextToToken(true, " ", as_spaceAfter, as_lineExceptions)


elimSpaceAfter

public final void elimSpaceAfter(String[] as_spaceAfter)

Eliminate all spaces in the source code that follows any of the provided strings.

Equal to elimSpaceAfter(as_spaceAfter, null)


elimSpaceAfter

public final void elimSpaceAfter(String[] as_spaceAfter,
                                 String[] as_lineExceptions)

Eliminate all spaces in the source code that follows any of the provided strings. Logs activity.

Equal to elimNextToToken(false, " ", as_spaceAfter, as_lineExceptions)


elimNextToToken

public final void elimNextToToken(boolean b_beforeAfter,
                                  String s_token,
                                  String[] a_string)

Eliminate all tokens in the source code that precedes/follows any of the provided strings.

Equal to elimNextToToken(b_beforeAfter, s_token, as_spaceAfter, null)


elimNextToToken

public final void elimNextToToken(boolean b_beforeAfter,
                                  String s_token,
                                  String[] a_string,
                                  String[] as_lineExceptions)

Eliminate all tokens in the source code that precedes/follows any of the provided strings. Logs activity.

When as_lineExceptions is null, this does a replaceUntil() replaceUntil:

Parameters:
b_beforeAfter - Eliminate tokens preceding or following the strings? For each element in a_string, if true, this calls
   replaceUntil(s_token + a_string[i], a_string[i], as_lineExceptions)
If false,
   replaceUntil(a_string[i] + s_token, a_string[i], as_lineExceptions)

.
s_token - The token to eliminate, when it precedes/follows any of the strings in a_string. May not be null or zero characters in length.
a_string - List of strings that, in at least one instance in the source, has a token preceding/following it. May not be null or zero elements in length. Each element must be non-null, at least one character in length, and must have at least one instance of preceding/following token, outside of exception lines.

trimAllLines

public final void trimAllLines()

Trim all whitespace from the start and ends of each line. Logs activity.

Equal to [TrimChars].trimAllLines(getSOBSource())


elimEmptyLines

public final void elimEmptyLines()

Eliminates all empty lines from the source. Logs activity.


trimAndElimEmptyLines

public final void trimAndElimEmptyLines()

Trim every line, and then eliminate all empty lines in the source.

This calls
   trimAllLines()
then
   elimEmptyLines()


replaceAll

public final void replaceAll(String s_searchFor,
                             String s_replaceWith)

Replace all instances of something with something else, with one pass through the source only.

Equal to replaceAll(s_searchFor, s_replaceWith, null)


replaceAll

public final void replaceAll(String s_searchFor,
                             String s_replaceWith,
                             String[] as_lineExceptions)

Replace all instances of something with something else, with one pass through the source only.

Equal to replaceAllOrUntil(false, s_searchFor, s_replaceWith, null)


replaceUntil

public final void replaceUntil(String s_searchFor,
                               String s_replaceWith)

Replace all instances of something with something else, repeatedly, until no more instances exist.

Equal to replaceUntil(s_searchFor, s_replaceWith, null)


replaceUntil

public final void replaceUntil(String s_searchFor,
                               String s_replaceWith,
                               String[] as_lineExceptions)

Replace all instances of something with something else, repeatedly, until no more instances exist.

Equal to replaceAllOrUntil(false, s_searchFor, s_replaceWith, null)


replaceAllOrUntil

public final void replaceAllOrUntil(boolean b_allOrUntil,
                                    String s_searchFor,
                                    String s_replaceWith)

Replace all instances of something with something else.

Equal to replaceAllOrUntil(b_allOrUntil, s_searchFor, s_replaceWith, null)


replaceAllOrUntil

public final void replaceAllOrUntil(boolean b_allOrUntil,
                                    String s_searchFor,
                                    String s_replaceWith,
                                    String[] as_lineExceptions)

Replace all instances of something with something else. Logs activity.

Parameters:
b_allOrUntil - Should the replacements be done with [UtilSOB].replaceAll or [UtilSOB].replaceUntil? If true, 'all', if false, 'until'.
s_searchFor - The string to search for. Must exist at least once in getSOBSource. Provided directly to replaceAll or replaceUntil.
s_replaceWith - The string to replace s_searchFor with. Provided directly to replaceAll or replaceUntil.
as_lineExceptions - List of strings which, if found in any line in the source code, should be ignored (ie, no replacements made). If non-null, may not be zero elements in length, and no element may be null or zero characters in length. If null, no lines are ignored.

replaceIdentifiers

public final void replaceIdentifiers(String[] as_searchFor,
                                     String[] as_replaceWith)

Replace all instances of the search-for strings.

Equal to replaceIdentifiers(as_searchFor, as_replaceWith, null)


replaceIdentifiers

public final void replaceIdentifiers(String[] as_searchFor,
                                     String[] as_replaceWith,
                                     String[] as_lineExceptions)

Replace all instances of the search-for strings. Logs activity.

For each string in as_replaceWith, this calls
   replaceAll(as_searchFor[i], as_replaceWith[i], as_lineExceptions)

Warnings

Parameters:
as_searchFor - The list of identifiers that should be replaced with the values in as_replaceWith. May not be zero elements in length, and each element must be at least three characters in length, unique, contain only characters legal for an identifier (letters, digits, underscores and dollar signs) and no element may be a subset of any preceding element ('hello' may not precede 'hellothere').
as_replaceWith - The list of character variable names to replace the identifiers (in as_replaceWith) with. May not be null or a length different than as_searchFor. Each element must be at least one character in length, and unique. When as_searchFor is null, this must be as well.

elimLinesWithToken

public final void elimLinesWithToken(String s_token)

Eliminate every line containing the provided token. Logs activity.

Parameters:
s_token - The token that, if found on any line, causes that line to be deleted. May not be null or zero characters in length.



Copyright 2003-2005, Jeff Epstein, All Rights Reserved. See top of source code files for copyright notice.

 

http://sourceforge.net/projects/jsutiljava

 

SourceForge.net Logo