View Javadoc

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