Package com.day.text

Class Replace

java.lang.Object
com.day.text.Replace

public class Replace extends Object
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 :

  1. Acquire an instance through the default constructor
  2. Add Pattern strings through the addPattern(String, String), addPattern(int, String, String) or addPatterns(String[][]) calls
  3. Optionally set replacement flags
  4. 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 :

  1. All occurrences of the patterns are looked for in the string
  2. Within theses occurrences, collisions are resolved using a weighting algorithm which is based on the pattern length using a weight reference.
  3. 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 final int
    Replacement flag to ignore any strings occuring within HTML tags
    static final int
    Replacment flag indicating no special treatment
    static final int
    Replacement flag to only replace search words occuring as stand- alone words
  • Constructor Summary

    Constructors
    Constructor
    Description
    Default constructor for the replacement object
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addPattern(int pos, String pattern, String replacement)
    Insert the pattern at the indicated position in the internal pattern list
    void
    addPattern(String pattern, String replacement)
    Append the pattern to the internal pattern list
    void
    addPatterns(String[][] prPairs)
    Append all the pattern/replacement String pairs to the list.
    int
    Returns the current flags
    Returns an iterator over the existing patterns
    replace(String source)
    calls replace(String, int) with wref=0
    replace(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

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • REPLACE_NONE

      public static final int REPLACE_NONE
      Replacment flag indicating no special treatment
      See Also:
    • REPLACE_IGNORE_HTML

      public static final int REPLACE_IGNORE_HTML
      Replacement flag to ignore any strings occuring within HTML tags
      See Also:
    • REPLACE_WHOLE_WORD

      public static final int REPLACE_WHOLE_WORD
      Replacement 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

      public String replace(String source)
      calls replace(String, int) with wref=0
    • replace

      public String replace(String source, int wref)
      The real replacement of the patterns within the input takes place here.
      Parameters:
      source - The String to do the work in
      wref - 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, String pattern, String replacement)
      Insert the pattern at the indicated position in the internal pattern list
      Parameters:
      pos - The position to insert the pattern at
      pattern - The pattern String for the new pattern
      replacement - The replacement String for the new pattern
    • addPattern

      public void addPattern(String pattern, String replacement)
      Append the pattern to the internal pattern list
      Parameters:
      pattern - The pattern String for the new pattern
      replacement - The replacement String for the new pattern
    • addPatterns

      public void addPatterns(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 Iterator getPatterns()
      Returns an iterator over the existing patterns
      Returns:
      an iterator over the existing patterns