org.fife.ui.rtextarea
Class SearchEngine

java.lang.Object
  extended by org.fife.ui.rtextarea.SearchEngine

public class SearchEngine
extends Object

A singleton class that can perform advanced find/replace operations in an RTextArea.

Version:
1.0
Author:
Robert Futrell

Method Summary
static boolean find(JTextArea textArea, String text, boolean forward, boolean matchCase, boolean wholeWord, boolean regex)
          Finds the next instance of the string/regular expression specified from the caret position.
protected static String getFindInText(JTextArea textArea, int start, boolean forward)
          Returns the text in which to search, as a string.
protected static List getMatches(Matcher m, String replaceStr)
          This method is called internally by getNextMatchPosRegExImpl and is used to get the locations of all regular-expression matches, and possibly their replacement strings.
static int getNextMatchPos(String searchFor, String searchIn, boolean forward, boolean matchCase, boolean wholeWord)
          Searches searchIn for an occurrence of searchFor either forwards or backwards, matching case or not.
protected static int getNextMatchPosImpl(String searchFor, String searchIn, boolean goForward, boolean matchCase, boolean wholeWord)
          Actually does the work of matching; assumes searchFor and searchIn are already upper/lower-cased appropriately.
static Point getNextMatchPosRegEx(String regEx, CharSequence searchIn, boolean goForward, boolean matchCase, boolean wholeWord)
          Searches searchIn for an occurrence of regEx either forwards or backwards, matching case or not.
protected static Object getNextMatchPosRegExImpl(String regEx, CharSequence searchIn, boolean goForward, boolean matchCase, boolean wholeWord, String replaceStr)
          Searches searchIn for an occurrence of regEx either forwards or backwards, matching case or not.
protected static org.fife.ui.rtextarea.RegExReplaceInfo getRegExReplaceInfo(String regEx, String searchIn, boolean goForward, boolean matchCase, boolean wholeWord, String replacement)
          Returns information on how to implement a regular expression "replace" action in the specified text with the specified replacement string.
static String getReplacementText(Matcher m, CharSequence template)
          Called internally by getMatches().
protected static int makeMarkAndDotEqual(JTextArea textArea, boolean forward)
          Makes the caret's dot and mark the same location so that, for the next search in the specified direction, a match will be found even if it was within the original dot and mark's selection.
protected static boolean regexReplace(JTextArea textArea, String toFind, String replaceWith, boolean forward, boolean matchCase, boolean wholeWord)
          Finds the next instance of the regular expression specified from the caret position.
static boolean replace(RTextArea textArea, String toFind, String replaceWith, boolean forward, boolean matchCase, boolean wholeWord, boolean regex)
          Finds the next instance of the text/regular expression specified from the caret position.
static int replaceAll(RTextArea textArea, String toFind, String replaceWith, boolean matchCase, boolean wholeWord, boolean regex)
          Replaces all instances of the text/regular expression specified in the specified document with the specified replacement.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

find

public static boolean find(JTextArea textArea,
                           String text,
                           boolean forward,
                           boolean matchCase,
                           boolean wholeWord,
                           boolean regex)
                    throws PatternSyntaxException
Finds the next instance of the string/regular expression specified from the caret position. If a match is found, it is selected in this text area.

Parameters:
textArea - The text area in which to search.
text - The string literal or regular expression to search for.
forward - Whether to search forward from the caret position or backward from it.
matchCase - Whether the search should be case-sensitive.
wholeWord - Whether there should be spaces or tabs on either side of the match.
regex - Whether text is a Java regular expression to search for.
Returns:
Whether a match was found (and thus selected).
Throws:
PatternSyntaxException - If regex is true but text is not a valid regular expression.
See Also:
replace(org.fife.ui.rtextarea.RTextArea, java.lang.String, java.lang.String, boolean, boolean, boolean, boolean), regexReplace(javax.swing.JTextArea, java.lang.String, java.lang.String, boolean, boolean, boolean)

getFindInText

protected static String getFindInText(JTextArea textArea,
                                      int start,
                                      boolean forward)
Returns the text in which to search, as a string. This is used internally to grab the smallest buffer possible in which to search.


getMatches

protected static List getMatches(Matcher m,
                                 String replaceStr)
This method is called internally by getNextMatchPosRegExImpl and is used to get the locations of all regular-expression matches, and possibly their replacement strings.

Returns either:

If replacement is null, this method call is assumed to be part of a "find" operation and points are returned. If if is non-null, it is assumed to be part of a "replace" operation and the RegExReplaceInfos are returned.

Parameters:
m - The matcher.
replaceStr - The string to replace matches with. This is a "template" string and can contain captured group references in the form "${digit}".
Returns:
A list of result objects.
Throws:
IndexOutOfBoundsException - If replaceStr references an invalid group (less than zero or greater than the number of groups matched).

getNextMatchPos

public static final int getNextMatchPos(String searchFor,
                                        String searchIn,
                                        boolean forward,
                                        boolean matchCase,
                                        boolean wholeWord)
Searches searchIn for an occurrence of searchFor either forwards or backwards, matching case or not.

Parameters:
searchFor - The string to look for.
searchIn - The string to search in.
forward - Whether to search forward or backward in searchIn.
matchCase - If true, do a case-sensitive search for searchFor.
wholeWord - If true, searchFor occurrences embedded in longer words in searchIn don't count as matches.
Returns:
The starting position of a match, or -1 if no match was found.
See Also:
getNextMatchPosImpl(java.lang.String, java.lang.String, boolean, boolean, boolean), getNextMatchPosRegEx(java.lang.String, java.lang.CharSequence, boolean, boolean, boolean)

getNextMatchPosImpl

protected static final int getNextMatchPosImpl(String searchFor,
                                               String searchIn,
                                               boolean goForward,
                                               boolean matchCase,
                                               boolean wholeWord)
Actually does the work of matching; assumes searchFor and searchIn are already upper/lower-cased appropriately.
The reason this method is here is to attempt to speed up FindInFilesDialog; since it repeatedly calls this method instead of getNextMatchPos, it gets better performance as it no longer has to allocate a lower-cased string for every call.

Parameters:
searchFor - The string to search for.
searchIn - The string to search in.
goForward - Whether the search is forward or backward.
matchCase - Whether the search is case-sensitive.
wholeWord - Whether only whole words should be matched.
Returns:
The location of the next match, or -1 if no match was found.

getNextMatchPosRegEx

public static Point getNextMatchPosRegEx(String regEx,
                                         CharSequence searchIn,
                                         boolean goForward,
                                         boolean matchCase,
                                         boolean wholeWord)
Searches searchIn for an occurrence of regEx either forwards or backwards, matching case or not.

Parameters:
regEx - The regular expression to look for.
searchIn - The string to search in.
goForward - Whether to search forward. If false, search backward.
matchCase - Whether or not to do a case-sensitive search for regEx.
wholeWord - If true, regEx occurrences embedded in longer words in searchIn don't count as matches.
Returns:
A Point representing the starting and ending position of the match, or null if no match was found.
Throws:
PatternSyntaxException - If regEx is an invalid regular expression.
See Also:
getNextMatchPos(java.lang.String, java.lang.String, boolean, boolean, boolean)

getNextMatchPosRegExImpl

protected static Object getNextMatchPosRegExImpl(String regEx,
                                                 CharSequence searchIn,
                                                 boolean goForward,
                                                 boolean matchCase,
                                                 boolean wholeWord,
                                                 String replaceStr)
Searches searchIn for an occurrence of regEx either forwards or backwards, matching case or not.

Parameters:
regEx - The regular expression to look for.
searchIn - The string to search in.
goForward - Whether to search forward. If false, search backward.
matchCase - Whether or not to do a case-sensitive search for regEx.
wholeWord - If true, regEx occurrences embedded in longer words in searchIn don't count as matches.
replaceStr - The string that will replace the match found (if a match is found). The object returned will contain the replacement string with matched groups substituted. If this value is null, it is assumed this call is part of a "find" instead of a "replace" operation.
Returns:
If replaceStr is null, a Point representing the starting and ending points of the match. If it is non-null, an object with information about the match and the morphed string to replace it with. If no match is found, null is returned.
Throws:
PatternSyntaxException - If regEx is an invalid regular expression.
IndexOutOfBoundsException - If replaceStr references an invalid group (less than zero or greater than the number of groups matched).
See Also:
getNextMatchPos(java.lang.String, java.lang.String, boolean, boolean, boolean)

getRegExReplaceInfo

protected static org.fife.ui.rtextarea.RegExReplaceInfo getRegExReplaceInfo(String regEx,
                                                                            String searchIn,
                                                                            boolean goForward,
                                                                            boolean matchCase,
                                                                            boolean wholeWord,
                                                                            String replacement)
Returns information on how to implement a regular expression "replace" action in the specified text with the specified replacement string.

Parameters:
regEx - The regular expression to look for.
searchIn - The string to search in.
goForward - Whether to search forward. If false, search backward.
matchCase - Whether or not to do a case-sensitive search for regEx.
wholeWord - If true, regEx occurrences embedded in longer words in searchIn don't count as matches.
replacement - A template for the replacement string (e.g., this can contain \t and \n to mean tabs and newlines, respectively, as well as group references $n).
Returns:
A RegExReplaceInfo object describing how to implement the replace.
Throws:
PatternSyntaxException - If regEx is an invalid regular expression.
IndexOutOfBoundsException - If replacement references an invalid group (less than zero or greater than the number of groups matched).
See Also:
getNextMatchPos(java.lang.String, java.lang.String, boolean, boolean, boolean)

getReplacementText

public static String getReplacementText(Matcher m,
                                        CharSequence template)
Called internally by getMatches(). This method assumes that the specified matcher has just found a match, and that you want to get the string with which to replace that match.

Parameters:
m - The matcher.
template - The template for the replacement string. For example, "foo" would yield the replacement string "foo", while "$1 is the greatest" would yield different values depending on the value of the first captured group in the match.
Returns:
The string to replace the match with.
Throws:
IndexOutOfBoundsException - If template references an invalid group (less than zero or greater than the number of groups matched).

makeMarkAndDotEqual

protected static int makeMarkAndDotEqual(JTextArea textArea,
                                         boolean forward)
Makes the caret's dot and mark the same location so that, for the next search in the specified direction, a match will be found even if it was within the original dot and mark's selection.

Parameters:
textArea - The text area.
forward - Whether the search will be forward through the document (false means backward).
Returns:
The new dot and mark position.

regexReplace

protected static boolean regexReplace(JTextArea textArea,
                                      String toFind,
                                      String replaceWith,
                                      boolean forward,
                                      boolean matchCase,
                                      boolean wholeWord)
                               throws PatternSyntaxException
Finds the next instance of the regular expression specified from the caret position. If a match is found, it is replaced with the specified replacement string.

Parameters:
textArea - The text area in which to search.
toFind - The regular expression to search for.
replaceWith - The string to replace the found regex with.
forward - Whether to search forward from the caret position or backward from it.
matchCase - Whether the search should be case-sensitive.
wholeWord - Whether there should be spaces or tabs on either side of the match.
Returns:
Whether a match was found (and thus replaced).
Throws:
PatternSyntaxException - If toFind is not a valid regular expression.
IndexOutOfBoundsException - If replaceWith references an invalid group (less than zero or greater than the number of groups matched).
See Also:
replace(org.fife.ui.rtextarea.RTextArea, java.lang.String, java.lang.String, boolean, boolean, boolean, boolean), find(javax.swing.JTextArea, java.lang.String, boolean, boolean, boolean, boolean)

replace

public static boolean replace(RTextArea textArea,
                              String toFind,
                              String replaceWith,
                              boolean forward,
                              boolean matchCase,
                              boolean wholeWord,
                              boolean regex)
                       throws PatternSyntaxException
Finds the next instance of the text/regular expression specified from the caret position. If a match is found, it is replaced with the specified replacement string.

Parameters:
textArea - The text area in which to search.
toFind - The text/regular expression to search for.
replaceWith - The string to replace the found text with.
forward - Whether to search forward from the caret position or backward from it.
matchCase - Whether the search should be case-sensitive.
wholeWord - Whether there should be spaces or tabs on either side of the match.
regex - Whether or not this is a regular expression search.
Returns:
Whether a match was found (and thus replaced).
Throws:
PatternSyntaxException - If regex is true but toFind is not a valid regular expression.
IndexOutOfBoundsException - If regex is true and replaceWith references an invalid group (less than zero or greater than the number of groups matched).
See Also:
regexReplace(javax.swing.JTextArea, java.lang.String, java.lang.String, boolean, boolean, boolean), find(javax.swing.JTextArea, java.lang.String, boolean, boolean, boolean, boolean)

replaceAll

public static int replaceAll(RTextArea textArea,
                             String toFind,
                             String replaceWith,
                             boolean matchCase,
                             boolean wholeWord,
                             boolean regex)
                      throws PatternSyntaxException
Replaces all instances of the text/regular expression specified in the specified document with the specified replacement.

Parameters:
textArea - The text area in which to search.
toFind - The text/regular expression to search for.
replaceWith - The string to replace the found text with.
matchCase - Whether the search should be case-sensitive.
wholeWord - Whether there should be spaces or tabs on either side of the match.
regex - Whether or not this is a regular expression search.
Returns:
The number of replacements done.
Throws:
PatternSyntaxException - If regex is true and toFind is an invalid regular expression.
IndexOutOfBoundsException - If replaceWith references an invalid group (less than zero or greater than the number of groups matched).
See Also:
replace(org.fife.ui.rtextarea.RTextArea, java.lang.String, java.lang.String, boolean, boolean, boolean, boolean), regexReplace(javax.swing.JTextArea, java.lang.String, java.lang.String, boolean, boolean, boolean), find(javax.swing.JTextArea, java.lang.String, boolean, boolean, boolean, boolean)