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