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 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * Copyright (c) 2006-2009 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.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 PatternSyntaxException {
  111. if (position < this.count()) {
  112. return check.matches("(?i)"+this.get(position));
  113. } else {
  114. return false;
  115. }
  116. }
  117. /**
  118. * Get the ignore pattern in a given position in the list.
  119. *
  120. * @param position Position to check
  121. * @return String showing the pattern. ("" if position isn't valid)
  122. */
  123. public String get(final int position) {
  124. if (position < this.count()) {
  125. return ignoreInfo.get(position);
  126. } else {
  127. return "";
  128. }
  129. }
  130. /**
  131. * Change the ignore pattern in a given position in the list.
  132. *
  133. * @param position Position to change
  134. * @param pattern New pattern
  135. */
  136. public void set(final int position, final String pattern) {
  137. if (position < this.count()) {
  138. ignoreInfo.set(position, pattern);
  139. }
  140. }
  141. /**
  142. * Get the amount of ignores in the list.
  143. *
  144. * @return int showing the number of ignores
  145. */
  146. public int count() {
  147. return ignoreInfo.size();
  148. }
  149. /**
  150. * Adds the specified simple pattern to this ignore list.
  151. *
  152. * @param pattern The simple pattern to be added
  153. */
  154. public void addSimple(final String pattern) {
  155. add(simpleToRegex(pattern));
  156. }
  157. /**
  158. * Determines if this list can be converted to a simple list.
  159. *
  160. * @return True if this list can be converted, false otherwise.
  161. */
  162. public boolean canConvert() {
  163. try {
  164. getSimpleList();
  165. return true;
  166. } catch (UnsupportedOperationException ex) {
  167. return false;
  168. }
  169. }
  170. /**
  171. * Retrieves a list of regular expressions in this ignore list.
  172. *
  173. * @return All expressions in this ignore list
  174. */
  175. public List<String> getRegexList() {
  176. return new ArrayList<String>(ignoreInfo);
  177. }
  178. /**
  179. * Retrieves a list of simple expressions in this ignore list.
  180. *
  181. * @return All expressions in this ignore list, converted to simple expressions
  182. * @throws UnsupportedOperationException if an expression can't be converted
  183. */
  184. public List<String> getSimpleList() throws UnsupportedOperationException {
  185. final List<String> res = new ArrayList<String>();
  186. for (String regex : ignoreInfo) {
  187. res.add(regexToSimple(regex));
  188. }
  189. return res;
  190. }
  191. /**
  192. * Converts a regular expression into a simple expression.
  193. *
  194. * @param regex The regular expression to be converted
  195. * @return A simple expression corresponding to the regex
  196. * @throws UnsupportedOperationException if the regex cannot be converted
  197. */
  198. protected static String regexToSimple(final String regex)
  199. throws UnsupportedOperationException {
  200. final StringBuilder res = new StringBuilder(regex.length());
  201. boolean escaped = false;
  202. boolean inchar = false;
  203. for (char part : regex.toCharArray()) {
  204. if (inchar) {
  205. inchar = false;
  206. if (part == '*') {
  207. res.append(part);
  208. continue;
  209. } else {
  210. res.append('?');
  211. }
  212. }
  213. if (escaped) {
  214. if (part == '?' || part == '*') {
  215. throw new UnsupportedOperationException("Cannot convert to"
  216. + " simple expression: ? or * is escaped.");
  217. }
  218. res.append(part);
  219. escaped = false;
  220. } else if (part == '\\') {
  221. escaped = true;
  222. } else if (part == '.') {
  223. inchar = true;
  224. } else if (part == '.' || part == '^' || part == '$' || part == '['
  225. || part == ']' || part == '\\' || part == '(' || part == ')'
  226. || part == '{' || part == '}' || part == '|' || part == '+'
  227. || part == '*' || part == '?') {
  228. throw new UnsupportedOperationException("Cannot convert to"
  229. + " simple expression: unescaped special char: " + part);
  230. } else {
  231. res.append(part);
  232. }
  233. }
  234. if (escaped) {
  235. throw new UnsupportedOperationException("Cannot convert to "
  236. + "simple expression: trailing backslash");
  237. } else if (inchar) {
  238. res.append('?');
  239. }
  240. return res.toString();
  241. }
  242. /**
  243. * Converts a simple expression to a regular expression.
  244. *
  245. * @param regex The simple expression to be converted
  246. * @return A corresponding regular expression
  247. */
  248. @SuppressWarnings("fallthrough")
  249. protected static String simpleToRegex(final String regex) {
  250. final StringBuilder res = new StringBuilder(regex.length());
  251. for (char part : regex.toCharArray()) {
  252. switch (part) {
  253. case '.': case '^': case '$': case '[': case ']': case '\\':
  254. case '(': case ')': case '{': case '}': case '|': case '+':
  255. res.append('\\');
  256. res.append(part);
  257. break;
  258. case '?':
  259. res.append('.');
  260. break;
  261. case '*':
  262. res.append('.');
  263. default:
  264. res.append(part);
  265. break;
  266. }
  267. }
  268. return res.toString();
  269. }
  270. }