You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IRCDocumentSearcher.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.ui.messages;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. import java.util.regex.Matcher;
  26. import java.util.regex.Pattern;
  27. /**
  28. * Searches the textpane for specified phrases.
  29. */
  30. public class IRCDocumentSearcher {
  31. /** Document to search. */
  32. private final Document document;
  33. /** Phrase to search for. */
  34. private final String phrase;
  35. /** Textpane position. */
  36. private LinePosition position;
  37. /** Case sensitive? */
  38. private final boolean caseSensitive;
  39. /**
  40. * Constructs a new IRC Document searcher.
  41. *
  42. * @param phrase Phrase to search for
  43. * @param document Document to search
  44. * @param caseSensitive Whether or not this searcher is case sensitive
  45. */
  46. public IRCDocumentSearcher(final String phrase, final Document document,
  47. final boolean caseSensitive) {
  48. this.phrase = phrase;
  49. this.document = document;
  50. this.position = getEndPosition();
  51. this.caseSensitive = caseSensitive;
  52. }
  53. /**
  54. * Returns the end position in the document.
  55. *
  56. * @return End position
  57. */
  58. private LinePosition getEndPosition() {
  59. final int documentSize = document.getNumLines() - 1;
  60. final int lineLength;
  61. if (documentSize >= 0) {
  62. lineLength = document.getLine(documentSize).getLength();
  63. } else {
  64. lineLength = 0;
  65. }
  66. return new LinePosition(documentSize, lineLength, documentSize,
  67. lineLength);
  68. }
  69. /**
  70. * Sets the position of the current match
  71. *
  72. * @param position New match position
  73. */
  74. public void setPosition(final LinePosition position) {
  75. this.position = position;
  76. }
  77. /**
  78. * Searches up in the document.
  79. *
  80. * @return Line position of the next match
  81. */
  82. public LinePosition searchUp() {
  83. if (position == null) {
  84. position = getEndPosition();
  85. }
  86. int line = position.getEndLine();
  87. for (int remaining = document.getNumLines(); remaining > 0; remaining--) {
  88. if (line < 0) {
  89. line = 0;
  90. }
  91. final String lineText = document.getLine(line).getText();
  92. final List<LinePosition> matches = searchLine(line, lineText);
  93. for (int i = matches.size() - 1; i >= 0; i--) {
  94. if (position.getEndLine() != line
  95. || matches.get(i).getEndPos() < position.getEndPos()) {
  96. return matches.get(i);
  97. }
  98. }
  99. line--;
  100. if (line < 0) {
  101. line += document.getNumLines();
  102. }
  103. }
  104. return null;
  105. }
  106. /**
  107. * Searches down in the document.
  108. *
  109. * @return Line position of the next match
  110. */
  111. public LinePosition searchDown() {
  112. if (position == null) {
  113. position = getEndPosition();
  114. }
  115. int line = position.getStartLine();
  116. for (int remaining = document.getNumLines(); remaining > 0; remaining--) {
  117. if (line < 0) {
  118. line = 0;
  119. }
  120. final String lineText = document.getLine(line).getText();
  121. final List<LinePosition> matches = searchLine(line, lineText);
  122. for (LinePosition match : matches) {
  123. if (position.getStartLine() != line
  124. || match.getStartPos() > position.getStartPos()) {
  125. return match;
  126. }
  127. }
  128. line++;
  129. if (line >= document.getNumLines()) {
  130. line -= document.getNumLines();
  131. }
  132. }
  133. return null;
  134. }
  135. /**
  136. * Searches a line and returns all matches on a line.
  137. *
  138. * @param lineNum the line number of the line we're searching
  139. * @param line Line to search
  140. *
  141. * @return List of matches
  142. */
  143. private List<LinePosition> searchLine(final int lineNum, final String line) {
  144. final List<LinePosition> matches = new ArrayList<>();
  145. final Matcher matcher = Pattern.compile((caseSensitive ? "" : "(?i)")
  146. + "\\Q" + phrase + "\\E").matcher(line);
  147. while (matcher.find()) {
  148. matches.add(new LinePosition(lineNum, matcher.start(), lineNum, matcher.end()));
  149. }
  150. return matches;
  151. }
  152. }