Package com.day.text
Class Replace
java.lang.Object
com.day.text.Replace
The
Replace class implements the string replacement
functionality formerly coded in the base/base_replace.c C
file. This class acts as a data container for the concrete replacement
definition and also does the actual replacement for the String/UString ECMA
host object and also for Java clients.
The usage of this class is relatively simple :
- Acquire an instance through the default constructor
- Add Pattern strings through the
addPattern(String, String),addPattern(int, String, String)oraddPatterns(String[][])calls - Optionally set replacement flags
- call
replace(String, int)for each string you want to work on
The flags for the replacement are defined as follows :
REPLACE_IGNORE_HTML- Don't replace anything contained within a tag enclosed in < and >, resp.
REPLACE_WHOLE_WORD- Only replace occurrences of patterns when they form standalone words.
The replacement algorithm works in three steps :
- All occurrences of the patterns are looked for in the string
- Within theses occurrences, collisions are resolved using a weighting algorithm which is based on the pattern length using a weight reference.
- For each occurrence not invalidated through collision detection, the pattern is replaced by the replacement. The rest of the input String is copied to the destination unmodified.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intReplacement flag to ignore any strings occuring within HTML tagsstatic final intReplacment flag indicating no special treatmentstatic final intReplacement flag to only replace search words occuring as stand- alone words -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddPattern(int pos, String pattern, String replacement) Insert the pattern at the indicated position in the internal pattern listvoidaddPattern(String pattern, String replacement) Append the pattern to the internal pattern listvoidaddPatterns(String[][] prPairs) Append all the pattern/replacement String pairs to the list.intgetFlags()Returns the current flagsReturns an iterator over the existing patternscallsreplace(String, int)with wref=0The real replacement of the patterns within the input takes place here.voidsetFlags(int flags) Set the indicated replacement flags
-
Field Details
-
REPLACE_NONE
public static final int REPLACE_NONEReplacment flag indicating no special treatment- See Also:
-
REPLACE_IGNORE_HTML
public static final int REPLACE_IGNORE_HTMLReplacement flag to ignore any strings occuring within HTML tags- See Also:
-
REPLACE_WHOLE_WORD
public static final int REPLACE_WHOLE_WORDReplacement flag to only replace search words occuring as stand- alone words- See Also:
-
-
Constructor Details
-
Replace
public Replace()Default constructor for the replacement object
-
-
Method Details
-
replace
callsreplace(String, int)with wref=0 -
replace
The real replacement of the patterns within the input takes place here.- Parameters:
source- The String to do the work inwref- Reference weight (?)
-
setFlags
public void setFlags(int flags) Set the indicated replacement flags- Parameters:
flags- The flags to set
-
getFlags
public int getFlags()Returns the current flags- Returns:
- the current flags
-
addPattern
Insert the pattern at the indicated position in the internal pattern list- Parameters:
pos- The position to insert the pattern atpattern- The pattern String for the new patternreplacement- The replacement String for the new pattern
-
addPattern
Append the pattern to the internal pattern list- Parameters:
pattern- The pattern String for the new patternreplacement- The replacement String for the new pattern
-
addPatterns
Append all the pattern/replacement String pairs to the list. Each entry is supposed to contain at least two entries.- Parameters:
prPairs- An Array of pattern/replacement String pairs.
-
getPatterns
Returns an iterator over the existing patterns- Returns:
- an iterator over the existing patterns
-