1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.springmodules.validation.commons.validwhen;
24
25 import java.math.BigDecimal;
26 import java.util.Stack;
27
28 import antlr.*;
29 import antlr.collections.impl.BitSet;
30 import org.apache.commons.validator.util.ValidatorUtils;
31
32 public class ValidWhenParser extends antlr.LLkParser implements ValidWhenParserTokenTypes {
33
34 Stack argStack = new Stack();
35
36 Object form;
37
38 int index;
39
40 String value;
41
42 public void setForm(Object f) {
43 form = f;
44 }
45
46 ;
47
48 public void setIndex(int i) {
49 index = i;
50 }
51
52 ;
53
54 public void setValue(String v) {
55 value = v;
56 }
57
58 ;
59
60 public boolean getResult() {
61 return ((Boolean) argStack.peek()).booleanValue();
62 }
63
64 private final int LESS_EQUAL = 0;
65
66 private final int LESS_THAN = 1;
67
68 private final int EQUAL = 2;
69
70 private final int GREATER_THAN = 3;
71
72 private final int GREATER_EQUAL = 4;
73
74 private final int NOT_EQUAL = 5;
75
76 private final int AND = 6;
77
78 private final int OR = 7;
79
80 private boolean evaluateComparison(Object v1, Object compare, Object v2) {
81 if ((v1 == null) || (v2 == null)) {
82 if (String.class.isInstance(v1)) {
83 if (((String) v1).length() == 0) {
84 v1 = null;
85 }
86 }
87 if (String.class.isInstance(v2)) {
88 if (((String) v2).length() == 0) {
89 v2 = null;
90 }
91 }
92 switch (((Integer) compare).intValue()) {
93 case LESS_EQUAL:
94 case GREATER_THAN:
95 case LESS_THAN:
96 case GREATER_EQUAL:
97 return false;
98 case EQUAL:
99 return (v1 == v2);
100 case NOT_EQUAL:
101 return (v1 != v2);
102 }
103 }
104
105 boolean numberCompare = true;
106 if ((BigDecimal.class.isInstance(v1) || String.class.isInstance(v1)) &&
107 (BigDecimal.class.isInstance(v2) || String.class.isInstance(v2))) {
108 numberCompare = true;
109 } else {
110 numberCompare = false;
111 }
112
113 if (numberCompare) {
114 try {
115 BigDecimal number1, number2;
116 if (BigDecimal.class.isInstance(v1)) {
117 number1 = (BigDecimal) v1;
118 } else {
119 number1 = new BigDecimal((String) v1);
120 }
121 if (BigDecimal.class.isInstance(v2)) {
122 number2 = (BigDecimal) v2;
123 } else {
124 number2 = new BigDecimal((String) v2);
125 }
126 int compareResult = number1.compareTo(number2);
127
128 switch (((Integer) compare).intValue()) {
129 case LESS_EQUAL:
130 return (compareResult <= 0);
131
132 case LESS_THAN:
133 return (compareResult < 0);
134
135 case EQUAL:
136 return (compareResult == 0);
137
138 case GREATER_THAN:
139 return (compareResult > 0);
140
141 case GREATER_EQUAL:
142 return (compareResult >= 0);
143
144 case NOT_EQUAL:
145 return (compareResult != 0);
146 }
147 } catch (NumberFormatException ex) {
148 }
149 ;
150 }
151
152 String string1 = String.valueOf(v1);
153 String string2 = String.valueOf(v2);
154
155 int res = string1.compareTo(string2);
156
157 switch (((Integer) compare).intValue()) {
158 case LESS_EQUAL:
159 return (res <= 0);
160
161 case LESS_THAN:
162 return (res < 0);
163
164 case EQUAL:
165 return (res == 0);
166
167 case GREATER_THAN:
168 return (res > 0);
169
170 case GREATER_EQUAL:
171 return (res >= 0);
172
173 case NOT_EQUAL:
174 return (res != 0);
175 }
176
177 return true;
178 }
179
180
181 protected ValidWhenParser(TokenBuffer tokenBuf, int k) {
182 super(tokenBuf, k);
183 tokenNames = _tokenNames;
184 }
185
186 public ValidWhenParser(TokenBuffer tokenBuf) {
187 this(tokenBuf, 6);
188 }
189
190 protected ValidWhenParser(TokenStream lexer, int k) {
191 super(lexer, k);
192 tokenNames = _tokenNames;
193 }
194
195 public ValidWhenParser(TokenStream lexer) {
196 this(lexer, 6);
197 }
198
199 public ValidWhenParser(ParserSharedInputState state) {
200 super(state, 6);
201 tokenNames = _tokenNames;
202 }
203
204 public final void number() throws RecognitionException, TokenStreamException {
205
206 Token d = null;
207 Token h = null;
208
209 switch (LA(1)) {
210 case DECIMAL_LITERAL: {
211 d = LT(1);
212 match(DECIMAL_LITERAL);
213 argStack.push(new BigDecimal(d.getText()));
214 break;
215 }
216 case HEX_LITERAL: {
217 h = LT(1);
218 match(HEX_LITERAL);
219 argStack.push(new BigDecimal(Integer.decode(h.getText()).toString()));
220 break;
221 }
222 default: {
223 throw new NoViableAltException(LT(1), getFilename());
224 }
225 }
226 }
227
228 public final void string() throws RecognitionException, TokenStreamException {
229
230 Token str = null;
231
232 str = LT(1);
233 match(STRING_LITERAL);
234 argStack.push(str.getText().substring(1, str.getText().length() - 1));
235 }
236
237 public final void identifier() throws RecognitionException, TokenStreamException {
238
239 Token str = null;
240
241 str = LT(1);
242 match(IDENTIFIER);
243 argStack.push(str.getText());
244 }
245
246 public final void field() throws RecognitionException, TokenStreamException {
247
248
249 if ((LA(1) == IDENTIFIER) && (LA(2) == LBRACKET) && (LA(3) == RBRACKET) && (LA(4) == IDENTIFIER)) {
250 identifier();
251 match(LBRACKET);
252 match(RBRACKET);
253 identifier();
254
255 Object i2 = argStack.pop();
256 Object i1 = argStack.pop();
257 argStack.push(ValidatorUtils.getValueAsString(form, i1 + "[" + index + "]" + i2));
258
259 } else
260 if ((LA(1) == IDENTIFIER) && (LA(2) == LBRACKET) && (LA(3) == DECIMAL_LITERAL || LA(3) == HEX_LITERAL) && (LA(4) == RBRACKET) && (LA(5) == IDENTIFIER))
261 {
262 identifier();
263 match(LBRACKET);
264 number();
265 match(RBRACKET);
266 identifier();
267
268 Object i5 = argStack.pop();
269 Object i4 = argStack.pop();
270 Object i3 = argStack.pop();
271 argStack.push(ValidatorUtils.getValueAsString(form, i3 + "[" + i4 + "]" + i5));
272
273 } else
274 if ((LA(1) == IDENTIFIER) && (LA(2) == LBRACKET) && (LA(3) == DECIMAL_LITERAL || LA(3) == HEX_LITERAL) && (LA(4) == RBRACKET) && (LA(5) == LBRACKET))
275 {
276 identifier();
277 match(LBRACKET);
278 number();
279 match(RBRACKET);
280 match(LBRACKET);
281
282 Object i7 = argStack.pop();
283 Object i6 = argStack.pop();
284 argStack.push(ValidatorUtils.getValueAsString(form, i6 + "[" + i7 + "]"));
285
286 } else if ((LA(1) == IDENTIFIER) && (LA(2) == LBRACKET) && (LA(3) == RBRACKET) && (_tokenSet_0.member(LA(4)))) {
287 identifier();
288 match(LBRACKET);
289 match(RBRACKET);
290
291 Object i8 = argStack.pop();
292 argStack.push(ValidatorUtils.getValueAsString(form, i8 + "[" + index + "]"));
293
294 } else if ((LA(1) == IDENTIFIER) && (_tokenSet_0.member(LA(2)))) {
295 identifier();
296
297 Object i9 = argStack.pop();
298 argStack.push(ValidatorUtils.getValueAsString(form, (String) i9));
299
300 } else {
301 throw new NoViableAltException(LT(1), getFilename());
302 }
303
304 }
305
306 public final void literal() throws RecognitionException, TokenStreamException {
307
308
309 switch (LA(1)) {
310 case DECIMAL_LITERAL:
311 case HEX_LITERAL: {
312 number();
313 break;
314 }
315 case STRING_LITERAL: {
316 string();
317 break;
318 }
319 case LITERAL_null: {
320 match(LITERAL_null);
321 argStack.push(null);
322 break;
323 }
324 case THIS: {
325 match(THIS);
326 argStack.push(value);
327 break;
328 }
329 default: {
330 throw new NoViableAltException(LT(1), getFilename());
331 }
332 }
333 }
334
335 public final void value() throws RecognitionException, TokenStreamException {
336
337
338 switch (LA(1)) {
339 case IDENTIFIER: {
340 field();
341 break;
342 }
343 case DECIMAL_LITERAL:
344 case HEX_LITERAL:
345 case STRING_LITERAL:
346 case LITERAL_null:
347 case THIS: {
348 literal();
349 break;
350 }
351 default: {
352 throw new NoViableAltException(LT(1), getFilename());
353 }
354 }
355 }
356
357 public final void expression() throws RecognitionException, TokenStreamException {
358
359
360 expr();
361 match(Token.EOF_TYPE);
362 }
363
364 public final void expr() throws RecognitionException, TokenStreamException {
365
366
367 if ((LA(1) == LPAREN) && (_tokenSet_1.member(LA(2)))) {
368 match(LPAREN);
369 comparisonExpression();
370 match(RPAREN);
371 } else if ((LA(1) == LPAREN) && (LA(2) == LPAREN)) {
372 match(LPAREN);
373 joinedExpression();
374 match(RPAREN);
375 } else {
376 throw new NoViableAltException(LT(1), getFilename());
377 }
378
379 }
380
381 public final void comparisonExpression() throws RecognitionException, TokenStreamException {
382
383
384 value();
385 comparison();
386 value();
387
388 Object v2 = argStack.pop();
389 Object comp = argStack.pop();
390 Object v1 = argStack.pop();
391 argStack.push(new Boolean(evaluateComparison(v1, comp, v2)));
392
393 }
394
395 public final void joinedExpression() throws RecognitionException, TokenStreamException {
396
397
398 expr();
399 join();
400 expr();
401
402 Boolean v1 = (Boolean) argStack.pop();
403 Integer join = (Integer) argStack.pop();
404 Boolean v2 = (Boolean) argStack.pop();
405 if (join.intValue() == AND) {
406 argStack.push(new Boolean(v1.booleanValue() && v2.booleanValue()));
407 } else {
408 argStack.push(new Boolean(v1.booleanValue() || v2.booleanValue()));
409 }
410
411 }
412
413 public final void join() throws RecognitionException, TokenStreamException {
414
415
416 switch (LA(1)) {
417 case ANDSIGN: {
418 match(ANDSIGN);
419 argStack.push(new Integer(AND));
420 break;
421 }
422 case ORSIGN: {
423 match(ORSIGN);
424 argStack.push(new Integer(OR));
425 break;
426 }
427 default: {
428 throw new NoViableAltException(LT(1), getFilename());
429 }
430 }
431 }
432
433 public final void comparison() throws RecognitionException, TokenStreamException {
434
435
436 switch (LA(1)) {
437 case EQUALSIGN: {
438 match(EQUALSIGN);
439 argStack.push(new Integer(EQUAL));
440 break;
441 }
442 case GREATERTHANSIGN: {
443 match(GREATERTHANSIGN);
444 argStack.push(new Integer(GREATER_THAN));
445 break;
446 }
447 case GREATEREQUALSIGN: {
448 match(GREATEREQUALSIGN);
449 argStack.push(new Integer(GREATER_EQUAL));
450 break;
451 }
452 case LESSTHANSIGN: {
453 match(LESSTHANSIGN);
454 argStack.push(new Integer(LESS_THAN));
455 break;
456 }
457 case LESSEQUALSIGN: {
458 match(LESSEQUALSIGN);
459 argStack.push(new Integer(LESS_EQUAL));
460 break;
461 }
462 case NOTEQUALSIGN: {
463 match(NOTEQUALSIGN);
464 argStack.push(new Integer(NOT_EQUAL));
465 break;
466 }
467 default: {
468 throw new NoViableAltException(LT(1), getFilename());
469 }
470 }
471 }
472
473
474 public static final String[] _tokenNames = {
475 "<0>",
476 "EOF",
477 "<2>",
478 "NULL_TREE_LOOKAHEAD",
479 "DECIMAL_LITERAL",
480 "HEX_LITERAL",
481 "STRING_LITERAL",
482 "IDENTIFIER",
483 "LBRACKET",
484 "RBRACKET",
485 "\"null\"",
486 "THIS",
487 "LPAREN",
488 "RPAREN",
489 "\"and\"",
490 "\"or\"",
491 "EQUALSIGN",
492 "GREATERTHANSIGN",
493 "GREATEREQUALSIGN",
494 "LESSTHANSIGN",
495 "LESSEQUALSIGN",
496 "NOTEQUALSIGN",
497 "WS"
498 };
499
500 private static final long[] mk_tokenSet_0() {
501 long[] data = {4136960L, 0L};
502 return data;
503 }
504
505 public static final BitSet _tokenSet_0 = new BitSet(mk_tokenSet_0());
506
507 private static final long[] mk_tokenSet_1() {
508 long[] data = {3312L, 0L};
509 return data;
510 }
511
512 public static final BitSet _tokenSet_1 = new BitSet(mk_tokenSet_1());
513
514 }