|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.ibm.icu.text.SearchIterator
public abstract class SearchIterator
SearchIterator is an abstract base class that defines a protocol for text searching. Subclasses provide concrete implementations of various search algorithms. A concrete subclass, StringSearch, is provided that implements language-sensitive pattern matching based on the comparison rules defined in a RuleBasedCollator object. Instances of SearchIterator maintain a current position and scan over the target text, returning the indices where a match is found and the length of each match. Generally, the sequence of forward matches will be equivalent to the sequence of backward matches.One case where this statement may not hold is when non-overlapping mode is set on and there are continuous repetitive patterns in the text. Consider the case searching for pattern "aba" in the text "ababababa", setting overlapping mode off will produce forward matches at offsets 0, 4. However when a backwards search is done, the results will be at offsets 6 and 2.
If matches searched for have boundary restrictions. BreakIterators can be used to define the valid boundaries of such a match. Once a BreakIterator is set, potential matches will be tested against the BreakIterator to determine if the boundaries are valid and that all characters in the potential match are equivalent to the pattern searched for. For example, looking for the pattern "fox" in the text "foxy fox" will produce match results at offset 0 and 5 with length 3 if no BreakIterators were set. However if a WordBreakIterator is set, the only match that would be found will be at the offset 5. Since, the SearchIterator guarantees that if a BreakIterator is set, all its matches will match the given pattern exactly, a potential match that passes the BreakIterator might still not produce a valid match. For instance the pattern "e" will not be found in the string "\u00e9" (latin small letter e with acute) if a CharacterBreakIterator is used. Even though "e" is a part of the character "\u00e9" and the potential match at offset 0 length 1 passes the CharacterBreakIterator test, "\u00e9" is not equivalent to "e", hence the SearchIterator rejects the potential match. By default, the SearchIterator does not impose any boundary restriction on the matches, it will return all results that match the pattern. Illustrating with the above example, "e" will be found in the string "\u00e9" if no BreakIterator is specified.
SearchIterator also provides a means to handle overlapping matches via the API setOverlapping(boolean). For example, if overlapping mode is set, searching for the pattern "abab" in the text "ababab" will match at positions 0 and 2, whereas if overlapping is not set, SearchIterator will only match at position 0. By default, overlapping mode is not set.
The APIs in SearchIterator are similar to that of other text iteration classes such as BreakIterator. Using this class, it is easy to scan through text looking for all occurances of a match.
Example of use:
String target = "The quick brown fox jumped over the lazy fox"; String pattern = "fox"; SearchIterator iter = new StringSearch(pattern, target); for (int pos = iter.first(); pos != SearchIterator.DONE; pos = iter.next()) { // println matches at offset 16 and 41 with length 3 System.out.println("Found match at " + pos + ", length is " + iter.getMatchLength()); } target = "ababababa"; pattern = "aba"; iter.setTarget(new StringCharacterIterator(pattern)); iter.setOverlapping(false); System.out.println("Overlapping mode set to false"); System.out.println("Forward matches of pattern " + pattern + " in text " + text + ": "); for (int pos = iter.first(); pos != SearchIterator.DONE; pos = iter.next()) { // println matches at offset 0 and 4 with length 3 System.out.println("offset " + pos + ", length " + iter.getMatchLength()); } System.out.println("Backward matches of pattern " + pattern + " in text " + text + ": "); for (int pos = iter.last(); pos != SearchIterator.DONE; pos = iter.previous()) { // println matches at offset 6 and 2 with length 3 System.out.println("offset " + pos + ", length " + iter.getMatchLength()); } System.out.println("Overlapping mode set to true"); System.out.println("Index set to 2"); iter.setIndex(2); iter.setOverlapping(true); System.out.println("Forward matches of pattern " + pattern + " in text " + text + ": "); for (int pos = iter.first(); pos != SearchIterator.DONE; pos = iter.next()) { // println matches at offset 2, 4 and 6 with length 3 System.out.println("offset " + pos + ", length " + iter.getMatchLength()); } System.out.println("Index set to 2"); iter.setIndex(2); System.out.println("Backward matches of pattern " + pattern + " in text " + text + ": "); for (int pos = iter.last(); pos != SearchIterator.DONE; pos = iter.previous()) { // println matches at offset 0 with length 3 System.out.println("offset " + pos + ", length " + iter.getMatchLength()); }
BreakIterator
Field Summary | |
---|---|
protected BreakIterator |
breakIterator
The BreakIterator to define the boundaries of a logical match. |
static int |
DONE
DONE is returned by previous() and next() after all valid matches have been returned, and by first() and last() if there are no matches at all. |
protected int |
matchLength
Length of the most current match in target text. |
protected CharacterIterator |
targetText
Target text for searching. |
Constructor Summary | |
---|---|
protected |
SearchIterator(CharacterIterator target,
BreakIterator breaker)
Protected constructor for use by subclasses. |
Method Summary | |
---|---|
int |
first()
Return the index of the first forward match in the target text. |
int |
following(int position)
Return the index of the first forward match in target text that is at or after argument position. |
BreakIterator |
getBreakIterator()
Returns the BreakIterator that is used to restrict the indexes at which matches are detected. |
abstract int |
getIndex()
Return the index in the target text at which the iterator is currently positioned. |
String |
getMatchedText()
Returns the text that was matched by the most recent call to first() , next() , previous() , or last() . |
int |
getMatchLength()
Returns the length of the most recent match in the target text. |
int |
getMatchStart()
Returns the index of the most recent match in the target text. |
CharacterIterator |
getTarget()
Return the target text that is being searched. |
protected abstract int |
handleNext(int start)
Abstract method that subclasses override to provide the mechanism for finding the next forwards match in the target text. |
protected abstract int |
handlePrevious(int startAt)
Abstract method which subclasses override to provide the mechanism for finding the next backwards match in the target text. |
boolean |
isOverlapping()
Return true if the overlapping property has been set. |
int |
last()
Return the index of the first backward match in target text. |
int |
next()
Search forwards in the target text for the next valid match, starting the search from the current iterator position. |
int |
preceding(int position)
Return the index of the first backwards match in target text that ends at or before argument position. |
int |
previous()
Search backwards in the target text for the next valid match, starting the search from the current iterator position. |
void |
reset()
Resets the search iteration. |
void |
setBreakIterator(BreakIterator breakiter)
Set the BreakIterator that is used to restrict the points at which matches are detected. |
void |
setIndex(int position)
Sets the position in the target text at which the next search will start. |
protected void |
setMatchLength(int length)
Sets the length of the most recent match in the target text. |
void |
setOverlapping(boolean allowOverlap)
Determines whether overlapping matches are returned. |
void |
setTarget(CharacterIterator text)
Set the target text to be searched. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final int DONE
previous()
,
next()
,
Constant Field Valuesprotected BreakIterator breakIterator
setBreakIterator(BreakIterator)
,
getBreakIterator()
,
BreakIterator
protected CharacterIterator targetText
setTarget(CharacterIterator)
,
getTarget()
protected int matchLength
setMatchLength(int)
,
getMatchLength()
Constructor Detail |
---|
protected SearchIterator(CharacterIterator target, BreakIterator breaker)
target
- The target text to be searched.breaker
- A BreakIterator
that is used to determine the
boundaries of a logical match. This argument can be null.
IllegalArgumentException
- thrown when argument target is null,
or of length 0BreakIterator
Method Detail |
---|
public void setIndex(int position)
Sets the position in the target text at which the next search will start. This method clears any previous match.
position
- position from which to start the next search
IndexOutOfBoundsException
- thrown if argument position is out
of the target text range.getIndex()
public void setOverlapping(boolean allowOverlap)
Determines whether overlapping matches are returned. See the class documentation for more information about overlapping matches.
The default setting of this property is false
allowOverlap
- flag indicator if overlapping matches are allowedisOverlapping()
public void setBreakIterator(BreakIterator breakiter)
breakiter
- A BreakIterator that will be used to restrict the
points at which matches are detected.getBreakIterator()
,
BreakIterator
public void setTarget(CharacterIterator text)
text
- new text iterator to look for match,
IllegalArgumentException
- thrown when text is null or has
0 lengthgetTarget()
public int getMatchStart()
Returns the index of the most recent match in the target text.
This call returns a valid result only after a successful call to
first()
, next()
, previous()
, or last()
.
Just after construction, or after a searching method returns
DONE, this method will return DONE.
Use getMatchLength to get the length of the matched text. getMatchedText will return the subtext in the searched target text from index getMatchStart() with length getMatchLength().
getMatchLength()
,
getMatchedText()
,
first()
,
next()
,
previous()
,
last()
,
DONE
public abstract int getIndex()
DONE
is returned.
first()
,
next()
,
previous()
,
last()
,
DONE
public int getMatchLength()
Returns the length of the most recent match in the target text.
This call returns a valid result only after a successful
call to first()
, next()
, previous()
, or
last()
.
Just after construction, or after a searching method returns
DONE, this method will return 0. See getMatchStart() for
more details.
getMatchStart()
,
getMatchedText()
,
first()
,
next()
,
previous()
,
last()
,
DONE
public BreakIterator getBreakIterator()
setBreakIterator
.
If the BreakIterator has not been set, null will be returned.
See setBreakIterator for more information.
setBreakIterator(com.ibm.icu.text.BreakIterator)
,
BreakIterator
public CharacterIterator getTarget()
setTarget(java.text.CharacterIterator)
public String getMatchedText()
first()
, next()
, previous()
, or last()
.
If the iterator is not pointing at a valid match, for instance just
after construction or after DONE has been returned, an empty
String will be returned. See getMatchStart for more information
getMatchStart()
,
getMatchLength()
,
first()
,
next()
,
previous()
,
last()
,
DONE
public int next()
getIndex()
,
is the starting position of the match if one was found. If a match is
found, the index of the match is returned, otherwise DONE is
returned. If overlapping mode is set, the beginning of the found match
can be before the end of the current match, if any.
getMatchStart()
,
getMatchLength()
,
getMatchedText()
,
following(int)
,
preceding(int)
,
previous()
,
first()
,
last()
,
DONE
public int previous()
getIndex()
,
is the starting position of the match if one was found. If a match is
found, the index is returned, otherwise DONE is returned. If
overlapping mode is set, the end of the found match can be after the
beginning of the previous match, if any.
getMatchStart()
,
getMatchLength()
,
getMatchedText()
,
following(int)
,
preceding(int)
,
next()
,
first()
,
last()
,
DONE
public boolean isOverlapping()
setOverlapping(boolean)
public void reset()
Resets the search iteration. All properties will be reset to their default values.
If a forward iteration is initiated, the next search will begin at the start of the target text. Otherwise, if a backwards iteration is initiated, the next search will begin at the end of the target text.
public final int first()
DONE
if there are no matches.getMatchStart()
,
getMatchLength()
,
getMatchedText()
,
following(int)
,
preceding(int)
,
next()
,
previous()
,
last()
,
DONE
public final int following(int position)
DONE
if there are no matches.getMatchStart()
,
getMatchLength()
,
getMatchedText()
,
first()
,
preceding(int)
,
next()
,
previous()
,
last()
,
DONE
public final int last()
DONE
if there are no matches.getMatchStart()
,
getMatchLength()
,
getMatchedText()
,
first()
,
preceding(int)
,
next()
,
previous()
,
following(int)
,
DONE
public final int preceding(int position)
DONE
if there are no matches.getMatchStart()
,
getMatchLength()
,
getMatchedText()
,
first()
,
following(int)
,
next()
,
previous()
,
last()
,
DONE
protected void setMatchLength(int length)
length
- new length to sethandleNext(int)
,
handlePrevious(int)
protected abstract int handleNext(int start)
Abstract method that subclasses override to provide the mechanism for finding the next forwards match in the target text. This allows different subclasses to provide different search algorithms.
If a match is found, this function must call setMatchLength(int) to
set the length of the result match.
The iterator is adjusted so that its current index, as returned by
getIndex()
, is the starting position of the match if one was
found. If a match is not found, DONE will be returned.
start
- index in the target text at which the forwards search
should begin.
setMatchLength(int)
,
handlePrevious(int)
,
DONE
protected abstract int handlePrevious(int startAt)
Abstract method which subclasses override to provide the mechanism for finding the next backwards match in the target text. This allows different subclasses to provide different search algorithms.
If a match is found, this function must call setMatchLength(int) to
set the length of the result match.
The iterator is adjusted so that its current index, as returned by
getIndex()
, is the starting position of the match if one was
found. If a match is not found, DONE will be returned.
startAt
- index in the target text at which the backwards search
should begin.
setMatchLength(int)
,
handleNext(int)
,
DONE
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |