KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > validator > validwhen > ValidWhenParser


1 // $ANTLR 2.7.2: "ValidWhenParser.g" -> "ValidWhenParser.java"$
2

3 /*
4  * $Id: ValidWhenParser.java 54929 2004-10-16 16:38:42Z germuska $
5  *
6  * Copyright 2003-2004 The Apache Software Foundation.
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */

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