Source code: org/alicebot/server/core/util/PatternArbiter.java
1 // Decompiled by Jad v1.5.8c. Copyright 2001 Pavel Kouznetsov.
2 // Jad home page: http://www.geocities.com/kpdus/jad.html
3 // Decompiler options: packimports(3)
4
5 package org.alicebot.server.core.util;
6
7 import java.io.*;
8 import java.text.StringCharacterIterator;
9 import java.util.*;
10
11 // Referenced classes of package org.alicebot.server.core.util:
12 // NotAnAIMLPatternException
13
14 public class PatternArbiter
15 {
16
17 public PatternArbiter()
18 {
19 }
20
21 public static boolean matches(String s, String s1, boolean flag)
22 throws NotAnAIMLPatternException
23 {
24 checkAIMLPattern(s1, flag);
25 StringCharacterIterator stringcharacteriterator;
26 StringCharacterIterator stringcharacteriterator1;
27 if(flag)
28 {
29 stringcharacteriterator = new StringCharacterIterator(s1.toUpperCase().trim());
30 stringcharacteriterator1 = new StringCharacterIterator(s.toUpperCase().trim());
31 } else
32 {
33 stringcharacteriterator = new StringCharacterIterator(s1.trim());
34 stringcharacteriterator1 = new StringCharacterIterator(s.trim());
35 }
36 char c = stringcharacteriterator.first();
37 char c1 = stringcharacteriterator1.first();
38 int i = 1;
39 int j = 1;
40 int k = 0;
41 int l = 0;
42 int i1;
43 for(i1 = 1; (i1 & 2) != 2;)
44 {
45 if((i1 & 1) == 1)
46 {
47 if((i1 & 0x10) == 16)
48 c = stringcharacteriterator.next();
49 if((i1 & 8) == 8)
50 c1 = stringcharacteriterator1.next();
51 i1 = 1;
52 }
53 if((i1 & 1) == 1)
54 {
55 if(!Character.isLetterOrDigit(c1))
56 {
57 j = 32;
58 if(Character.isWhitespace(c1))
59 {
60 j |= 0x10;
61 if(c1 == ' ')
62 j |= 8;
63 else
64 i1 = 6;
65 }
66 } else
67 {
68 j = 4;
69 }
70 if(!Character.isLetterOrDigit(c))
71 {
72 i = 32;
73 if(Character.isWhitespace(c))
74 {
75 i |= 0x10;
76 if(c == ' ')
77 i |= 8;
78 else
79 i1 = 6;
80 }
81 if(c == '*' || c == '_')
82 i |= 2;
83 } else
84 {
85 i = 4;
86 }
87 }
88 if((i1 & 1) == 1)
89 {
90 if(c == '\uFFFF')
91 {
92 k = 2;
93 i1 |= 2;
94 } else
95 if(stringcharacteriterator.getEndIndex() == stringcharacteriterator.getIndex() + 1)
96 k = 1;
97 if(c1 == '\uFFFF')
98 {
99 l = 2;
100 i1 |= 2;
101 } else
102 if(stringcharacteriterator1.getEndIndex() == stringcharacteriterator1.getIndex() + 1)
103 l = 1;
104 }
105 if((i1 & 1) == 1)
106 if(i == 4)
107 {
108 if(j == 4)
109 {
110 if(c == c1)
111 i1 = i1 | 8 | 0x10;
112 else
113 i1 = 6;
114 } else
115 {
116 i1 = 6;
117 }
118 if(k == 1)
119 i1 |= 2;
120 } else
121 if((i & 2) == 2)
122 {
123 if(j == 4)
124 i1 |= 8;
125 else
126 if((j & 0x20) == 32)
127 if(k == 1)
128 {
129 i1 |= 8;
130 } else
131 {
132 char c2 = stringcharacteriterator.next();
133 if(c2 != ' ')
134 i1 = 6;
135 else
136 i1 = i1 | 0x10 | 8;
137 }
138 } else
139 if((i & 8) == 8)
140 if((j & 0x20) == 32)
141 i1 = i1 | 0x10 | 8;
142 else
143 i1 = 6;
144 }
145
146 if((i1 & 4) == 4)
147 return false;
148 if(l == 1 || l == 2)
149 return k == 1 || k == 2;
150 else
151 return false;
152 }
153
154 public static void checkAIMLPattern(String s, boolean flag)
155 throws NotAnAIMLPatternException
156 {
157 StringCharacterIterator stringcharacteriterator = new StringCharacterIterator(s);
158 boolean flag1 = true;
159 int j = 1;
160 for(char c = stringcharacteriterator.first(); c != '\uFFFF'; c = stringcharacteriterator.next())
161 {
162 int i;
163 if(!Character.isLetterOrDigit(c))
164 {
165 i = 32;
166 if(Character.isWhitespace(c))
167 {
168 i |= 0x10;
169 if(c == ' ')
170 i |= 8;
171 else
172 throw new NotAnAIMLPatternException("The only allowed whitespace is a space ( ).", s);
173 }
174 if(c == '*' || c == '_')
175 {
176 i |= 2;
177 if(j != 1 && (j == 4 || (j & 2) == 2))
178 throw new NotAnAIMLPatternException("A wildcard cannot be preceded by a wildcard, a letter or a digit.", s);
179 }
180 if(c == '<')
181 {
182 int k = stringcharacteriterator.getIndex();
183 if(s.regionMatches(false, k, "<bot name=\"", 0, 11))
184 {
185 stringcharacteriterator.setIndex(k + 11);
186 for(c = stringcharacteriterator.next(); c != '\uFFFF' && c != '"' && (Character.isLetterOrDigit(c) || c == ' ' || c == '_'); c = stringcharacteriterator.next());
187 k = stringcharacteriterator.getIndex();
188 if(!s.regionMatches(false, k, "\"/>", 0, 3))
189 throw new NotAnAIMLPatternException("Invalid or malformed <bot/> element.", s);
190 stringcharacteriterator.setIndex(k + 3);
191 } else
192 {
193 throw new NotAnAIMLPatternException("Invalid or malformed inner element.", s);
194 }
195 }
196 } else
197 {
198 i = 4;
199 if(!flag && Character.toUpperCase(c) != c)
200 throw new NotAnAIMLPatternException("Characters with case mappings must be uppercase.", s);
201 if(j != 1 && (j & 2) == 2)
202 throw new NotAnAIMLPatternException("A letter or digit may not be preceded by a wildcard.", s);
203 }
204 j = i;
205 }
206
207 }
208
209 public static void main(String args[])
210 {
211 BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(System.in));
212 Object obj = null;
213 Object obj1 = null;
214 boolean flag = false;
215 boolean flag2 = false;
216 int i = 0;
217 int j = 0;
218 do
219 {
220 String s2 = null;
221 try
222 {
223 s2 = bufferedreader.readLine();
224 }
225 catch(IOException ioexception)
226 {
227 System.out.println("Cannot read from console!");
228 return;
229 }
230 if(s2 == null)
231 break;
232 if(s2.toLowerCase().equals("exit"))
233 {
234 System.out.println("Exiting.");
235 System.exit(0);
236 }
237 if(!s2.startsWith(";") && s2.trim().length() > 0)
238 {
239 StringTokenizer stringtokenizer = new StringTokenizer(s2, "|");
240 String s;
241 String s1;
242 boolean flag1;
243 boolean flag3;
244 try
245 {
246 s = stringtokenizer.nextToken();
247 s1 = stringtokenizer.nextToken();
248 flag1 = stringtokenizer.nextToken().equals("y");
249 flag3 = stringtokenizer.nextToken().equals("t");
250 }
251 catch(NoSuchElementException nosuchelementexception)
252 {
253 System.out.println("Improperly formatted input. Use: literal|PATTERN|(y/n)|(t/f)");
254 continue;
255 }
256 long l = (new Date()).getTime();
257 boolean flag4;
258 try
259 {
260 flag4 = matches(s, s1, flag1);
261 }
262 catch(NotAnAIMLPatternException notanaimlpatternexception)
263 {
264 System.out.println("Exception: " + notanaimlpatternexception.getMessage());
265 flag4 = false;
266 }
267 l = (new Date()).getTime() - l;
268 if(flag4 == flag3)
269 {
270 j++;
271 System.out.print("TEST PASSED] ");
272 } else
273 {
274 i++;
275 System.out.print("TEST FAILED] ");
276 }
277 if(flag4)
278 System.out.print("match: " + s + " | " + s1 + (flag1 ? " (ignoreCase)" : ""));
279 else
280 System.out.print("no match: " + s + " | " + s1 + (flag1 ? " (ignoreCase)" : ""));
281 System.out.println(" (" + l + " ms)");
282 } else
283 {
284 System.out.println(s2);
285 }
286 } while(true);
287 System.out.println((j + i) + " tests: " + j + " successes, " + i + " failures");
288 }
289
290 private static final char ASTERISK = 42;
291 private static final char UNDERSCORE = 95;
292 private static final char SPACE = 32;
293 private static final char TAG_START = 60;
294 private static final char QUOTE_MARK = 34;
295 private static final String BOT_NAME_EQUALS = "<bot name=\"";
296 private static final String ATOMIC_ELEMENT_END = "\"/>";
297 private static final int UNKNOWN = 1;
298 private static final int IS_WILDCARD = 2;
299 private static final int IS_LETTERDIGIT = 4;
300 private static final int IS_SPACE = 8;
301 private static final int IS_WHITESPACE = 16;
302 private static final int IS_NON_LETTERDIGIT = 32;
303 private static final int NOT_PAST_END = 0;
304 private static final int AT_END = 1;
305 private static final int PAST_END = 2;
306 private static final int CONTINUE_MATCHING = 1;
307 private static final int STOP_MATCHING = 2;
308 private static final int MATCH_FAILURE = 4;
309 private static final int ADVANCE_LITERAL = 8;
310 private static final int ADVANCE_PATTERN = 16;
311 }