Package com.day.text
Class Replace
- java.lang.Object
-
- com.day.text.Replace
-
public class Replace extends java.lang.Object
TheReplace
class implements the string replacement functionality formerly coded in thebase/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
Fields Modifier and Type Field Description static int
REPLACE_IGNORE_HTML
Replacement flag to ignore any strings occuring within HTML tagsstatic int
REPLACE_NONE
Replacment flag indicating no special treatmentstatic int
REPLACE_WHOLE_WORD
Replacement flag to only replace search words occuring as stand- alone words
-
Constructor Summary
Constructors Constructor Description Replace()
Default constructor for the replacement object
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addPattern(int pos, java.lang.String pattern, java.lang.String replacement)
Insert the pattern at the indicated position in the internal pattern listvoid
addPattern(java.lang.String pattern, java.lang.String replacement)
Append the pattern to the internal pattern listvoid
addPatterns(java.lang.String[][] prPairs)
Append all the pattern/replacement String pairs to the list.int
getFlags()
Returns the current flagsjava.util.Iterator
getPatterns()
Returns an iterator over the existing patternsjava.lang.String
replace(java.lang.String source)
callsreplace(String, int)
with wref=0java.lang.String
replace(java.lang.String source, int wref)
The real replacement of the patterns within the input takes place here.void
setFlags(int flags)
Set the indicated replacement flags
-
-
-
Field Detail
-
REPLACE_NONE
public static final int REPLACE_NONE
Replacment flag indicating no special treatment- See Also:
- Constant Field Values
-
REPLACE_IGNORE_HTML
public static final int REPLACE_IGNORE_HTML
Replacement flag to ignore any strings occuring within HTML tags- See Also:
- Constant Field Values
-
REPLACE_WHOLE_WORD
public static final int REPLACE_WHOLE_WORD
Replacement flag to only replace search words occuring as stand- alone words- See Also:
- Constant Field Values
-
-
Method Detail
-
replace
public java.lang.String replace(java.lang.String source)
callsreplace(String, int)
with wref=0
-
replace
public java.lang.String replace(java.lang.String source, int wref)
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
public void addPattern(int pos, java.lang.String pattern, java.lang.String replacement)
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
public void addPattern(java.lang.String pattern, java.lang.String replacement)
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
public void addPatterns(java.lang.String[][] prPairs)
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
public java.util.Iterator getPatterns()
Returns an iterator over the existing patterns- Returns:
- an iterator over the existing patterns
-
-