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.

IgnoreList.java 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * Copyright (c) 2006-2013 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.parser.common;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. import java.util.regex.PatternSyntaxException;
  26. /**
  27. * Parser Ignore list.
  28. *
  29. * @author Shane Mc Cormack
  30. */
  31. public class IgnoreList {
  32. /** Arraylist storing ignore patterns. */
  33. protected final List<String> ignoreInfo = new ArrayList<String>();
  34. /**
  35. * Creates a new instance of RegexStringList.
  36. */
  37. public IgnoreList() {
  38. // Do nothing
  39. }
  40. /**
  41. * Creates a new instance of RegexStringList, with the specified items.
  42. *
  43. * @param items Items to add to this RegexStringList
  44. */
  45. public IgnoreList(final List<String> items) {
  46. addAll(items);
  47. }
  48. /**
  49. * Add a new ignore pattern to the ignore list.
  50. *
  51. * @param pattern Regex syntax for the ignore (Pattern is matched case-insensitively as ^pattern$)
  52. */
  53. public void add(final String pattern) {
  54. for (String target : ignoreInfo) {
  55. if (pattern.equalsIgnoreCase(target)) {
  56. return;
  57. }
  58. }
  59. ignoreInfo.add(pattern);
  60. }
  61. /**
  62. * Adds a set of patterns to the list.
  63. *
  64. * @param patterns A list of patterns to be added
  65. */
  66. public void addAll(final List<String> patterns) {
  67. for (String pattern : patterns) {
  68. add(pattern);
  69. }
  70. }
  71. /**
  72. * Delete an ignore from the list.
  73. *
  74. * @param position Position in the list to remove
  75. */
  76. public void remove(final int position) {
  77. if (position < this.count()) {
  78. ignoreInfo.remove(position);
  79. }
  80. }
  81. /**
  82. * Clear the ignore list.
  83. */
  84. public void clear() {
  85. ignoreInfo.clear();
  86. }
  87. /**
  88. * Check if a string matches any of the ignores in the list.
  89. *
  90. * @param check String to check (Patterns are matched case-insensitively as ^pattern$)
  91. * @return integer showing the position of the first match in the ignore list (-1 if none)
  92. * @throws PatternSyntaxException if one of the items in the list is an invalid regex
  93. */
  94. public int matches(final String check) throws PatternSyntaxException {
  95. for (int i = 0; i < this.count(); ++i) {
  96. if (check.matches("(?i)" + this.get(i))) {
  97. return i;
  98. }
  99. }
  100. return -1;
  101. }
  102. /**
  103. * Check if a string matches a specific ignore in the list.
  104. *
  105. * @param position Position to check
  106. * @param check String to check (Patterns are matched case-insensitively as ^pattern$)
  107. * @return boolean true/false
  108. * @throws PatternSyntaxException if the item is an invalid regex
  109. */
  110. public boolean matches(final int position, final String check) throws
  111. PatternSyntaxException {
  112. if (position < this.count()) {
  113. return check.matches("(?i)" + this.get(position));
  114. } else {
  115. return false;
  116. }
  117. }
  118. /**
  119. * Get the ignore pattern in a given position in the list.
  120. *
  121. * @param position Position to check
  122. * @return String showing the pattern. ("" if position isn't valid)
  123. */
  124. public String get(final int position) {
  125. if (position < this.count()) {
  126. return ignoreInfo.get(position);
  127. } else {
  128. return "";
  129. }
  130. }
  131. /**
  132. * Change the ignore pattern in a given position in the list.
  133. *
  134. * @param position Position to change
  135. * @param pattern New pattern
  136. */
  137. public void set(final int position, final String pattern) {
  138. if (position < this.count()) {
  139. ignoreInfo.set(position, pattern);
  140. }
  141. }
  142. /**
  143. * Get the amount of ignores in the list.
  144. *
  145. * @return int showing the number of ignores
  146. */
  147. public int count() {
  148. return ignoreInfo.size();
  149. }
  150. /**
  151. * Adds the specified simple pattern to this ignore list.
  152. *
  153. * @param pattern The simple pattern to be added
  154. */
  155. public void addSimple(final String pattern) {
  156. add(simpleToRegex(pattern));
  157. }
  158. /**
  159. * Determines if this list can be converted to a simple list.
  160. *
  161. * @return True if this list can be converted, false otherwise.
  162. */
  163. public boolean canConvert() {
  164. try {
  165. getSimpleList();
  166. return true;
  167. } catch (UnsupportedOperationException ex) {
  168. return false;
  169. }
  170. }
  171. /**
  172. * Retrieves a list of regular expressions in this ignore list.
  173. *
  174. * @return All expressions in this ignore list
  175. */
  176. public List<String> getRegexList() {
  177. return new ArrayList<String>(ignoreInfo);
  178. }
  179. /**
  180. * Retrieves a list of simple expressions in this ignore list.
  181. *
  182. * @return All expressions in this ignore list, converted to simple expressions
  183. * @throws UnsupportedOperationException if an expression can't be converted
  184. */
  185. public List<String> getSimpleList() throws UnsupportedOperationException {
  186. final List<String> res = new ArrayList<String>();
  187. for (String regex : ignoreInfo) {
  188. res.add(regexToSimple(regex));
  189. }
  190. return res;
  191. }
  192. /**
  193. * Converts a regular expression into a simple expression.
  194. *
  195. * @param regex The regular expression to be converted
  196. * @return A simple expression corresponding to the regex
  197. * @throws UnsupportedOperationException if the regex cannot be converted
  198. */
  199. protected static String regexToSimple(final String regex)
  200. throws UnsupportedOperationException {
  201. final StringBuilder res = new StringBuilder(regex.length());
  202. boolean escaped = false;
  203. boolean inchar = false;
  204. for (char part : regex.toCharArray()) {
  205. if (inchar) {
  206. inchar = false;
  207. if (part == '*') {
  208. res.append(part);
  209. continue;
  210. } else {
  211. res.append('?');
  212. }
  213. }
  214. if (escaped) {
  215. if (part == '?' || part == '*') {
  216. throw new UnsupportedOperationException("Cannot convert to"
  217. + " simple expression: ? or * is escaped.");
  218. }
  219. res.append(part);
  220. escaped = false;
  221. } else if (part == '\\') {
  222. escaped = true;
  223. } else if (part == '.') {
  224. inchar = true;
  225. } else if (part == '.' || part == '^' || part == '$' || part
  226. == '['
  227. || part == ']' || part == '\\' || part == '(' || part == ')'
  228. || part == '{' || part == '}' || part == '|' || part == '+'
  229. || part == '*' || part == '?') {
  230. throw new UnsupportedOperationException("Cannot convert to"
  231. + " simple expression: unescaped special char: " + part);
  232. } else {
  233. res.append(part);
  234. }
  235. }
  236. if (escaped) {
  237. throw new UnsupportedOperationException("Cannot convert to "
  238. + "simple expression: trailing backslash");
  239. } else if (inchar) {
  240. res.append('?');
  241. }
  242. return res.toString();
  243. }
  244. /**
  245. * Converts a simple expression to a regular expression.
  246. *
  247. * @param regex The simple expression to be converted
  248. * @return A corresponding regular expression
  249. */
  250. @SuppressWarnings("fallthrough")
  251. protected static String simpleToRegex(final String regex) {
  252. final StringBuilder res = new StringBuilder(regex.length());
  253. for (char part : regex.toCharArray()) {
  254. switch (part) {
  255. case '.':
  256. case '^':
  257. case '$':
  258. case '[':
  259. case ']':
  260. case '\\':
  261. case '(':
  262. case ')':
  263. case '{':
  264. case '}':
  265. case '|':
  266. case '+':
  267. res.append('\\');
  268. res.append(part);
  269. break;
  270. case '?':
  271. res.append('.');
  272. break;
  273. case '*':
  274. res.append('.');
  275. default:
  276. res.append(part);
  277. break;
  278. }
  279. }
  280. return res.toString();
  281. }
  282. }