Class SearchEngine
java.lang.Object
org.fife.ui.rtextarea.SearchEngine
A singleton class that can perform advanced find/replace operations
in an
RTextArea. Simply create a SearchContext and call
one of the following methods:
- Version:
- 1.0
- See Also:
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprivatePrivate constructor to prevent instantiation. -
Method Summary
Modifier and TypeMethodDescriptionstatic SearchResultfind(JTextArea textArea, SearchContext context) Finds the next instance of the string/regular expression specified from the caret position.private static SearchResultfindImpl(String findIn, SearchContext context) Finds the next instance of the string/regular expression specified from the caret position.private static CharSequencegetFindInCharSequence(RTextArea textArea, int start, boolean forward) Returns aCharSequencefor a text area that doesn't make a copy of its contents for iteration.private static StringgetFindInText(JTextArea textArea, int start, boolean forward) Returns the text in which to search, as a string.private static ListgetMatches(Matcher m, String replaceStr) This method is called internally bygetNextMatchPosRegExImpland is used to get the locations of all regular-expression matches, and possibly their replacement strings.static intgetNextMatchPos(String searchFor, String searchIn, boolean forward, boolean matchCase, boolean wholeWord) SearchessearchInfor an occurrence ofsearchForeither forwards or backwards, matching case or not.private static PointgetNextMatchPosRegEx(String regEx, CharSequence searchIn, boolean goForward, boolean matchCase, boolean wholeWord) SearchessearchInfor an occurrence ofregExeither forwards or backwards, matching case or not.private static ObjectgetNextMatchPosRegExImpl(String regEx, CharSequence searchIn, boolean goForward, boolean matchCase, boolean wholeWord, String replaceStr) SearchessearchInfor an occurrence ofregExeither forwards or backwards, matching case or not.private static RegExReplaceInfogetRegExReplaceInfo(CharSequence searchIn, SearchContext context) Returns information on how to implement a regular expression "replace" action in the specified text with the specified replacement string.static StringgetReplacementText(Matcher m, CharSequence template) Called internally bygetMatches().private static booleanisWholeWord(CharSequence searchIn, int offset, int len) Returns whether the characters on either side ofsubstr(searchIn, startPos, startPos+searchStringLength)are not letters or digits.private static intmakeMarkAndDotEqual(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.static SearchResultmarkAll(RTextArea textArea, SearchContext context) Marks all instances of the specified text in this text area.private static SearchResultmarkAllImpl(RTextArea textArea, SearchContext context) Marks all instances of the specified text in this text area.private static SearchResultregexReplace(RTextArea textArea, SearchContext context) Finds the next instance of the regular expression specified from the caret position.static SearchResultreplace(RTextArea textArea, SearchContext context) Finds the next instance of the text/regular expression specified from the caret position.static SearchResultreplaceAll(RTextArea textArea, SearchContext context) Replaces all instances of the text/regular expression specified in the specified document with the specified replacement.
-
Constructor Details
-
SearchEngine
private SearchEngine()Private constructor to prevent instantiation.
-
-
Method Details
-
find
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.context- What to search for and all search options.- Returns:
- The result of the operation.
- Throws:
PatternSyntaxException- If this is a regular expression search but the search text is an invalid regular expression.- See Also:
-
findImpl
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:
findIn- The text to search in.context- The search context.- Returns:
- The result of the operation. "Mark all" will always be zero, since this method does not perform that operation.
- Throws:
PatternSyntaxException- If this is a regular expression search but the search text is an invalid regular expression.
-
getFindInCharSequence
Returns aCharSequencefor a text area that doesn't make a copy of its contents for iteration. This conserves memory but is likely just a tad slower.- Parameters:
textArea- The text area whose document is the basis for theCharSequence.start- The starting offset of the sequence (or ending offset ifforwardisfalse).forward- Whether we're searching forward or backward.- Returns:
- The character sequence.
-
getFindInText
-
getMatches
This method is called internally bygetNextMatchPosRegExImpland is used to get the locations of all regular-expression matches, and possibly their replacement strings.Returns either:
- A list of points representing the starting and ending positions of all matches returned by the specified matcher, or
- A list of
RegExReplaceInfos describing the matches found by the matcher and the replacement strings for each.
replacementisnull, this method call is assumed to be part of a "find" operation and points are returned. If it is non-null, it is assumed to be part of a "replace" operation and theRegExReplaceInfos 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- IfreplaceStrreferences an invalid group (less than zero or greater than the number of groups matched).
-
getNextMatchPos
public static int getNextMatchPos(String searchFor, String searchIn, boolean forward, boolean matchCase, boolean wholeWord) SearchessearchInfor an occurrence ofsearchForeither forwards or backwards, matching case or not.Most clients will have no need to call this method directly.
- Parameters:
searchFor- The string to look for.searchIn- The string to search in.forward- Whether to search forward or backward insearchIn.matchCase- Iftrue, do a case-sensitive search forsearchFor.wholeWord- Iftrue,searchForoccurrences embedded in longer words insearchIndon't count as matches.- Returns:
- The starting position of a match, or
-1if no match was found.
-
getNextMatchPosRegEx
private static Point getNextMatchPosRegEx(String regEx, CharSequence searchIn, boolean goForward, boolean matchCase, boolean wholeWord) SearchessearchInfor an occurrence ofregExeither 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. Iffalse, search backward.matchCase- Whether to do a case-sensitive search forregEx.wholeWord- Iftrue,regExoccurrences embedded in longer words insearchIndon't count as matches.- Returns:
- A
Pointrepresenting the starting and ending position of the match, ornullif no match was found. - Throws:
PatternSyntaxException- IfregExis an invalid regular expression.- See Also:
-
getNextMatchPosRegExImpl
private static Object getNextMatchPosRegExImpl(String regEx, CharSequence searchIn, boolean goForward, boolean matchCase, boolean wholeWord, String replaceStr) SearchessearchInfor an occurrence ofregExeither 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. Iffalse, search backward.matchCase- Whether to do a case-sensitive search forregEx.wholeWord- Iftrue,regExoccurrences embedded in longer words insearchIndon'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 isnull, it is assumed this call is part of a "find" instead of a "replace" operation.- Returns:
- If
replaceStrisnull, aPointrepresenting 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,nullis returned. - Throws:
PatternSyntaxException- IfregExis an invalid regular expression.IndexOutOfBoundsException- IfreplaceStrreferences an invalid group (less than zero or greater than the number of groups matched).- See Also:
-
getRegExReplaceInfo
Returns information on how to implement a regular expression "replace" action in the specified text with the specified replacement string.- Parameters:
searchIn- The string to search in.context- The search options.- Returns:
- A
RegExReplaceInfoobject describing how to implement the replace. - Throws:
PatternSyntaxException- If the search text is an invalid regular expression.IndexOutOfBoundsException- If the replacement text references an invalid group (less than zero or greater than the number of groups matched).- See Also:
-
getReplacementText
Called internally bygetMatches(). 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.Escapes simply insert the escaped character, except for
\nand\t, which insert a newline and tab respectively. Substrings of the form$\d+are considered to be matched groups. To include a literal dollar sign in your template, escape it (i.e.\$).Most clients will have no need to call this method directly.
- 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- Iftemplatereferences an invalid group (less than zero or greater than the number of groups matched).
-
isWholeWord
Returns whether the characters on either side ofsubstr(searchIn, startPos, startPos+searchStringLength)are not letters or digits. -
makeMarkAndDotEqual
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 (falsemeans backward).- Returns:
- The new dot and mark position.
-
markAll
Marks all instances of the specified text in this text area. This method is typically only called directly in response to search events of typeSearchEvent.Type.MARK_ALL. "Mark all" behavior is automatically performed whenfind(JTextArea, SearchContext)orreplace(RTextArea, SearchContext)is called.- Parameters:
textArea- The text area in which to mark occurrences.context- The search context specifying the text to search for. It is assumed thatcontext.getMarkAll()has already been checked and returnstrue.- Returns:
- The results of the operation.
-
markAllImpl
Marks all instances of the specified text in this text area. This method is typically only called directly in response to search events of typeSearchEvent.Type.MARK_ALL. "Mark all" behavior is automatically performed whenfind(JTextArea, SearchContext)or #replace(RTextArea, SearchContext) is called.- Parameters:
textArea- The text area in which to mark occurrences.context- The search context specifying the text to search for. It is assumed thatcontext.getMarkAll()has already been checked and returnstrue.- Returns:
- The results of the operation.
-
regexReplace
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.context- What to search for and all search options.- Returns:
- The result of the operation.
- Throws:
PatternSyntaxException- If this is a regular expression search but the search text is an invalid regular expression.IndexOutOfBoundsException- If this is a regular expression search but the replacement text references an invalid group (less than zero or greater than the number of groups matched).- See Also:
-
replace
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.context- What to search for and all search options.- Returns:
- The result of the operation.
- Throws:
PatternSyntaxException- If this is a regular expression search but the search text is an invalid regular expression.IndexOutOfBoundsException- If this is a regular expression search but the replacement text references an invalid group (less than zero or greater than the number of groups matched).- See Also:
-
replaceAll
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.context- What to search for and all search options.- Returns:
- The result of the operation.
- Throws:
PatternSyntaxException- If this is a regular expression search but the replacement text is an invalid regular expression.IndexOutOfBoundsException- If this is a regular expression search but the replacement text references an invalid group (less than zero or greater than the number of groups matched).- See Also:
-