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

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