Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

IgnoreList.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Copyright (c) 2006-2008 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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;
  23. import com.dmdirc.parser.irc.RegexStringList;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. /**
  27. * Wraps around a RegexStringList to allow "simple" expressions to be used
  28. * instead of more complex regular expressions.
  29. *
  30. * @author chris
  31. */
  32. public class IgnoreList extends RegexStringList {
  33. /**
  34. * Creates a new instance of IgnoreList.
  35. */
  36. public IgnoreList() {
  37. super();
  38. }
  39. /**
  40. * Creates a new instance of IgnoreList containing the specified items.
  41. *
  42. * @param items The items to be added
  43. */
  44. public IgnoreList(final List<String> items) {
  45. super(items);
  46. }
  47. /**
  48. * Adds the specified simple pattern to this ignore list.
  49. *
  50. * @param pattern The simple pattern to be added
  51. */
  52. public void addSimple(final String pattern) {
  53. add(simpleToRegex(pattern));
  54. }
  55. /**
  56. * Determines if this list can be converted to a simple list.
  57. *
  58. * @return True if this list can be converted, false otherwise.
  59. */
  60. public boolean canConvert() {
  61. try {
  62. getSimpleList();
  63. return true;
  64. } catch (UnsupportedOperationException ex) {
  65. return false;
  66. }
  67. }
  68. /**
  69. * Retrieves a list of regular expressions in this ignore list.
  70. *
  71. * @return All expressions in this ignore list
  72. */
  73. public List<String> getRegexList() {
  74. return new ArrayList<String>(ignoreInfo);
  75. }
  76. /**
  77. * Retrieves a list of simple expressions in this ignore list.
  78. *
  79. * @return All expressions in this ignore list, converted to simple expressions
  80. * @throws UnsupportedOperationException if an expression can't be converted
  81. */
  82. public List<String> getSimpleList() throws UnsupportedOperationException {
  83. final List<String> res = new ArrayList<String>();
  84. for (String regex : ignoreInfo) {
  85. res.add(regexToSimple(regex));
  86. }
  87. return res;
  88. }
  89. /**
  90. * Converts a regular expression into a simple expression.
  91. *
  92. * @param regex The regular expression to be converted
  93. * @return A simple expression corresponding to the regex
  94. * @throws UnsupportedOperationException if the regex cannot be converted
  95. */
  96. protected static String regexToSimple(final String regex)
  97. throws UnsupportedOperationException {
  98. final StringBuilder res = new StringBuilder(regex.length());
  99. boolean escaped = false;
  100. boolean inchar = false;
  101. for (char part : regex.toCharArray()) {
  102. if (inchar) {
  103. inchar = false;
  104. if (part == '*') {
  105. res.append(part);
  106. continue;
  107. } else {
  108. res.append('?');
  109. }
  110. }
  111. if (escaped) {
  112. if (part == '?' || part == '*') {
  113. throw new UnsupportedOperationException("Cannot convert to"
  114. + " simple expression: ? or * is escaped.");
  115. }
  116. res.append(part);
  117. escaped = false;
  118. } else if (part == '\\') {
  119. escaped = true;
  120. } else if (part == '.') {
  121. inchar = true;
  122. } else if (part == '.' || part == '^' || part == '$' || part == '['
  123. || part == ']' || part == '\\' || part == '(' || part == ')'
  124. || part == '{' || part == '}' || part == '|' || part == '+'
  125. || part == '*' || part == '?') {
  126. throw new UnsupportedOperationException("Cannot convert to"
  127. + " simple expression: unescaped special char: " + part);
  128. } else {
  129. res.append(part);
  130. }
  131. }
  132. if (escaped) {
  133. throw new UnsupportedOperationException("Cannot convert to "
  134. + "simple expression: trailing backslash");
  135. } else if (inchar) {
  136. res.append('?');
  137. }
  138. return res.toString();
  139. }
  140. /**
  141. * Converts a simple expression to a regular expression.
  142. *
  143. * @param regex The simple expression to be converted
  144. * @return A corresponding regular expression
  145. */
  146. @SuppressWarnings("fallthrough")
  147. protected static String simpleToRegex(final String regex) {
  148. final StringBuilder res = new StringBuilder(regex.length());
  149. for (char part : regex.toCharArray()) {
  150. switch (part) {
  151. case '.': case '^': case '$': case '[': case ']': case '\\':
  152. case '(': case ')': case '{': case '}': case '|': case '+':
  153. res.append('\\');
  154. res.append(part);
  155. break;
  156. case '?':
  157. res.append('.');
  158. break;
  159. case '*':
  160. res.append('.');
  161. default:
  162. res.append(part);
  163. break;
  164. }
  165. }
  166. return res.toString();
  167. }
  168. }