File Source: IgnoreList.java
/*
P/P * Method: com.dmdirc.IgnoreList__static_init
*/
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
23 package com.dmdirc;
24
25 import com.dmdirc.parser.irc.RegexStringList;
26
27 import java.util.ArrayList;
28 import java.util.List;
29
30 /**
31 * Wraps around a RegexStringList to allow "simple" expressions to be used
32 * instead of more complex regular expressions.
33 *
34 * @author chris
35 */
36 public class IgnoreList extends RegexStringList {
37
38 /**
39 * Creates a new instance of IgnoreList.
40 */
41 public IgnoreList() {
/*
P/P * Method: void com.dmdirc.IgnoreList()
*/
42 super();
43 }
44
45 /**
46 * Creates a new instance of IgnoreList containing the specified items.
47 *
48 * @param items The items to be added
49 */
50 public IgnoreList(final List<String> items) {
/*
P/P * Method: void com.dmdirc.IgnoreList(List)
*/
51 super(items);
52 }
53
54 /**
55 * Adds the specified simple pattern to this ignore list.
56 *
57 * @param pattern The simple pattern to be added
58 */
59 public void addSimple(final String pattern) {
/*
P/P * Method: void addSimple(String)
*
* Preconditions:
* pattern != null
*/
60 add(simpleToRegex(pattern));
61 }
62
63 /**
64 * Determines if this list can be converted to a simple list.
65 *
66 * @return True if this list can be converted, false otherwise.
67 */
68 public boolean canConvert() {
69 try {
/*
P/P * Method: bool canConvert()
*
* Preconditions:
* (soft) this.ignoreInfo != null
*
* Postconditions:
* init'ed(return_value)
*/
70 getSimpleList();
71 return true;
72 } catch (UnsupportedOperationException ex) {
73 return false;
74 }
75 }
76
77 /**
78 * Retrieves a list of regular expressions in this ignore list.
79 *
80 * @return All expressions in this ignore list
81 */
82 public List<String> getRegexList() {
/*
P/P * Method: List getRegexList()
*
* Preconditions:
* init'ed(this.ignoreInfo)
*
* Postconditions:
* return_value == &new ArrayList(getRegexList#1)
* new ArrayList(getRegexList#1) num objects == 1
*/
83 return new ArrayList<String>(ignoreInfo);
84 }
85
86 /**
87 * Retrieves a list of simple expressions in this ignore list.
88 *
89 * @return All expressions in this ignore list, converted to simple expressions
90 * @throws UnsupportedOperationException if an expression can't be converted
91 */
92 public List<String> getSimpleList() throws UnsupportedOperationException {
/*
P/P * Method: List getSimpleList()
*
* Preconditions:
* this.ignoreInfo != null
*
* Presumptions:
* java.util.Iterator:next(...)@95 != null
*
* Postconditions:
* return_value == &new ArrayList(getSimpleList#1)
* new ArrayList(getSimpleList#1) num objects == 1
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@95: {0}, {1}
*/
93 final List<String> res = new ArrayList<String>();
94
95 for (String regex : ignoreInfo) {
96 res.add(regexToSimple(regex));
97 }
98
99 return res;
100 }
101
102 /**
103 * Converts a regular expression into a simple expression.
104 *
105 * @param regex The regular expression to be converted
106 * @return A simple expression corresponding to the regex
107 * @throws UnsupportedOperationException if the regex cannot be converted
108 */
109 protected static String regexToSimple(final String regex)
110 throws UnsupportedOperationException {
/*
P/P * Method: String regexToSimple(String)
*
* Preconditions:
* regex != null
*
* Presumptions:
* arr$.length@115 <= 232-1
* arr$[i$]@115 not in {36, 40,41, 43, 63, 91, 93,94, 123..125}
* arr$[i$]@115 not in {36, 40..43, 46, 63, 91..94, 123..125}
*
* Postconditions:
* java.lang.StringBuilder:toString(...)._tainted == 0
* return_value == &java.lang.StringBuilder:toString(...)
*
* Test Vectors:
* arr$[i$]@115: {92}, {42}, {46}, {0..35, 37..39, 44,45, 47..62, 64..90, 95..122, 126..216-1}
*/
111 final StringBuilder res = new StringBuilder(regex.length());
112 boolean escaped = false;
113 boolean inchar = false;
114
115 for (char part : regex.toCharArray()) {
116 if (inchar) {
117 inchar = false;
118
119 if (part == '*') {
120 res.append(part);
121 continue;
122 } else {
123 res.append('?');
124 }
125 }
126
127 if (escaped) {
128 if (part == '?' || part == '*') {
129 throw new UnsupportedOperationException("Cannot convert to"
130 + " simple expression: ? or * is escaped.");
131 }
132
133 res.append(part);
134 escaped = false;
135 } else if (part == '\\') {
136 escaped = true;
137 } else if (part == '.') {
138 inchar = true;
139 } else if (part == '.' || part == '^' || part == '$' || part == '['
140 || part == ']' || part == '\\' || part == '(' || part == ')'
141 || part == '{' || part == '}' || part == '|' || part == '+'
142 || part == '*' || part == '?') {
143 throw new UnsupportedOperationException("Cannot convert to"
144 + " simple expression: unescaped special char: " + part);
145 } else {
146 res.append(part);
147 }
148 }
149
150 if (escaped) {
151 throw new UnsupportedOperationException("Cannot convert to "
152 + "simple expression: trailing backslash");
153 } else if (inchar) {
154 res.append('?');
155 }
156
157 return res.toString();
158 }
159
160 /**
161 * Converts a simple expression to a regular expression.
162 *
163 * @param regex The simple expression to be converted
164 * @return A corresponding regular expression
165 */
166 @SuppressWarnings("fallthrough")
167 protected static String simpleToRegex(final String regex) {
/*
P/P * Method: String simpleToRegex(String)
*
* Preconditions:
* regex != null
*
* Presumptions:
* arr$.length@170 <= 232-1
*
* Postconditions:
* java.lang.StringBuilder:toString(...)._tainted == 0
* return_value == &java.lang.StringBuilder:toString(...)
*
* Test Vectors:
* arr$[i$]@170: {36, 40,41, 43, 46, 91..94, 123..125}, {42}, {63}, {0..35, 37..39, 44,45, 47..62, 64..90, 95..122, 126..216-1}
*/
168 final StringBuilder res = new StringBuilder(regex.length());
169
170 for (char part : regex.toCharArray()) {
171 switch (part) {
172 case '.': case '^': case '$': case '[': case ']': case '\\':
173 case '(': case ')': case '{': case '}': case '|': case '+':
174 res.append('\\');
175 res.append(part);
176 break;
177 case '?':
178 res.append('.');
179 break;
180 case '*':
181 res.append('.');
182 default:
183 res.append(part);
184 break;
185 }
186 }
187
188 return res.toString();
189 }
190
191 }
SofCheck Inspector Build Version : 2.17854
| IgnoreList.java |
2009-Jun-25 01:54:24 |
| IgnoreList.class |
2009-Sep-02 17:04:17 |