Package com.day.text

Class Replace


  • public class Replace
    extends java.lang.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 int REPLACE_IGNORE_HTML
      Replacement flag to ignore any strings occuring within HTML tags
      static int REPLACE_NONE
      Replacment flag indicating no special treatment
      static 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 list
      void addPattern​(java.lang.String pattern, java.lang.String replacement)
      Append the pattern to the internal pattern list
      void addPatterns​(java.lang.String[][] prPairs)
      Append all the pattern/replacement String pairs to the list.
      int getFlags()
      Returns the current flags
      java.util.Iterator getPatterns()
      Returns an iterator over the existing patterns
      java.lang.String replace​(java.lang.String source)
      calls replace(String, int) with wref=0
      java.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
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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
    • Constructor Detail

      • Replace

        public Replace()
        Default constructor for the replacement object
    • Method Detail

      • replace

        public java.lang.String replace​(java.lang.String source)
        calls replace(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 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,
                               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 at
        pattern - The pattern String for the new pattern
        replacement - 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 pattern
        replacement - 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