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