001: // $ANTLR 2.7.2: "ValidWhenParser.g" -> "ValidWhenParser.java"$
002:
003: /*
004: * $Id: ValidWhenParser.java 504715 2007-02-07 22:10:26Z bayard $
005: *
006: * Licensed to the Apache Software Foundation (ASF) under one
007: * or more contributor license agreements. See the NOTICE file
008: * distributed with this work for additional information
009: * regarding copyright ownership. The ASF licenses this file
010: * to you under the Apache License, Version 2.0 (the
011: * "License"); you may not use this file except in compliance
012: * with the License. You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing,
017: * software distributed under the License is distributed on an
018: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
019: * KIND, either express or implied. See the License for the
020: * specific language governing permissions and limitations
021: * under the License.
022: */
023:
024: package org.apache.struts.validator.validwhen;
025:
026: import java.util.Stack;
027: import org.apache.commons.validator.util.ValidatorUtils;
028:
029: import antlr.TokenBuffer;
030: import antlr.TokenStreamException;
031: import antlr.TokenStreamIOException;
032: import antlr.ANTLRException;
033: import antlr.LLkParser;
034: import antlr.Token;
035: import antlr.TokenStream;
036: import antlr.RecognitionException;
037: import antlr.NoViableAltException;
038: import antlr.MismatchedTokenException;
039: import antlr.SemanticException;
040: import antlr.ParserSharedInputState;
041: import antlr.collections.impl.BitSet;
042:
043: public class ValidWhenParser extends antlr.LLkParser implements
044: ValidWhenParserTokenTypes {
045: Stack argStack = new Stack();
046: Object form;
047: int index;
048: String value;
049:
050: public void setForm(Object f) {
051: form = f;
052: };
053:
054: public void setIndex(int i) {
055: index = i;
056: };
057:
058: public void setValue(String v) {
059: value = v;
060: };
061:
062: public boolean getResult() {
063: return ((Boolean) argStack.peek()).booleanValue();
064: }
065:
066: private final int LESS_EQUAL = 0;
067: private final int LESS_THAN = 1;
068: private final int EQUAL = 2;
069: private final int GREATER_THAN = 3;
070: private final int GREATER_EQUAL = 4;
071: private final int NOT_EQUAL = 5;
072: private final int AND = 6;
073: private final int OR = 7;
074:
075: private boolean evaluateComparison(Object v1, Object compare,
076: Object v2) {
077: boolean intCompare = true;
078: if ((v1 == null) || (v2 == null)) {
079: if (String.class.isInstance(v1)) {
080: if (((String) v1).length() == 0) {
081: v1 = null;
082: }
083: }
084: if (String.class.isInstance(v2)) {
085: if (((String) v2).length() == 0) {
086: v2 = null;
087: }
088: }
089: switch (((Integer) compare).intValue()) {
090: case LESS_EQUAL:
091: case GREATER_THAN:
092: case LESS_THAN:
093: case GREATER_EQUAL:
094: return false;
095: case EQUAL:
096: return (v1 == v2);
097: case NOT_EQUAL:
098: return (v1 != v2);
099: }
100: }
101: if ((Integer.class.isInstance(v1) || String.class
102: .isInstance(v1))
103: && (Integer.class.isInstance(v2) || String.class
104: .isInstance(v2))) {
105: intCompare = true;
106: } else {
107: intCompare = false;
108: }
109: if (intCompare) {
110: try {
111: int v1i = 0, v2i = 0;
112: if (Integer.class.isInstance(v1)) {
113: v1i = ((Integer) v1).intValue();
114: } else {
115: v1i = Integer.parseInt((String) v1);
116: }
117: if (Integer.class.isInstance(v2)) {
118: v2i = ((Integer) v2).intValue();
119: } else {
120: v2i = Integer.parseInt((String) v2);
121: }
122: switch (((Integer) compare).intValue()) {
123: case LESS_EQUAL:
124: return (v1i <= v2i);
125:
126: case LESS_THAN:
127: return (v1i < v2i);
128:
129: case EQUAL:
130: return (v1i == v2i);
131:
132: case GREATER_THAN:
133: return (v1i > v2i);
134:
135: case GREATER_EQUAL:
136: return (v1i >= v2i);
137:
138: case NOT_EQUAL:
139: return (v1i != v2i);
140: }
141: } catch (NumberFormatException ex) {
142: }
143: ;
144: }
145: String v1s = "", v2s = "";
146:
147: if (Integer.class.isInstance(v1)) {
148: v1s = ((Integer) v1).toString();
149: } else {
150: v1s = (String) v1;
151: }
152:
153: if (Integer.class.isInstance(v2)) {
154: v2s = ((Integer) v2).toString();
155: } else {
156: v2s = (String) v2;
157: }
158:
159: int res = v1s.compareTo(v2s);
160: switch (((Integer) compare).intValue()) {
161: case LESS_EQUAL:
162: return (res <= 0);
163:
164: case LESS_THAN:
165: return (res < 0);
166:
167: case EQUAL:
168: return (res == 0);
169:
170: case GREATER_THAN:
171: return (res > 0);
172:
173: case GREATER_EQUAL:
174: return (res >= 0);
175:
176: case NOT_EQUAL:
177: return (res != 0);
178: }
179: return true;
180: }
181:
182: protected ValidWhenParser(TokenBuffer tokenBuf, int k) {
183: super (tokenBuf, k);
184: tokenNames = _tokenNames;
185: }
186:
187: public ValidWhenParser(TokenBuffer tokenBuf) {
188: this (tokenBuf, 6);
189: }
190:
191: protected ValidWhenParser(TokenStream lexer, int k) {
192: super (lexer, k);
193: tokenNames = _tokenNames;
194: }
195:
196: public ValidWhenParser(TokenStream lexer) {
197: this (lexer, 6);
198: }
199:
200: public ValidWhenParser(ParserSharedInputState state) {
201: super (state, 6);
202: tokenNames = _tokenNames;
203: }
204:
205: public final void integer() throws RecognitionException,
206: TokenStreamException {
207:
208: Token d = null;
209: Token h = null;
210: Token o = null;
211:
212: switch (LA(1)) {
213: case DECIMAL_LITERAL: {
214: d = LT(1);
215: match(DECIMAL_LITERAL);
216: argStack.push(Integer.decode(d.getText()));
217: break;
218: }
219: case HEX_LITERAL: {
220: h = LT(1);
221: match(HEX_LITERAL);
222: argStack.push(Integer.decode(h.getText()));
223: break;
224: }
225: case OCTAL_LITERAL: {
226: o = LT(1);
227: match(OCTAL_LITERAL);
228: argStack.push(Integer.decode(o.getText()));
229: break;
230: }
231: default: {
232: throw new NoViableAltException(LT(1), getFilename());
233: }
234: }
235: }
236:
237: public final void string() throws RecognitionException,
238: TokenStreamException {
239:
240: Token str = null;
241:
242: str = LT(1);
243: match(STRING_LITERAL);
244: argStack.push(str.getText().substring(1,
245: str.getText().length() - 1));
246: }
247:
248: public final void identifier() throws RecognitionException,
249: TokenStreamException {
250:
251: Token str = null;
252:
253: str = LT(1);
254: match(IDENTIFIER);
255: argStack.push(str.getText());
256: }
257:
258: public final void field() throws RecognitionException,
259: TokenStreamException {
260:
261: if ((LA(1) == IDENTIFIER) && (LA(2) == LBRACKET)
262: && (LA(3) == RBRACKET) && (LA(4) == IDENTIFIER)) {
263: identifier();
264: match(LBRACKET);
265: match(RBRACKET);
266: identifier();
267:
268: Object i2 = argStack.pop();
269: Object i1 = argStack.pop();
270: argStack.push(ValidatorUtils.getValueAsString(form, i1
271: + "[" + index + "]" + i2));
272:
273: } else if ((LA(1) == IDENTIFIER)
274: && (LA(2) == LBRACKET)
275: && ((LA(3) >= DECIMAL_LITERAL && LA(3) <= OCTAL_LITERAL))
276: && (LA(4) == RBRACKET) && (LA(5) == IDENTIFIER)) {
277: identifier();
278: match(LBRACKET);
279: integer();
280: match(RBRACKET);
281: identifier();
282:
283: Object i5 = argStack.pop();
284: Object i4 = argStack.pop();
285: Object i3 = argStack.pop();
286: argStack.push(ValidatorUtils.getValueAsString(form, i3
287: + "[" + i4 + "]" + i5));
288:
289: } else if ((LA(1) == IDENTIFIER)
290: && (LA(2) == LBRACKET)
291: && ((LA(3) >= DECIMAL_LITERAL && LA(3) <= OCTAL_LITERAL))
292: && (LA(4) == RBRACKET) && (LA(5) == LBRACKET)) {
293: identifier();
294: match(LBRACKET);
295: integer();
296: match(RBRACKET);
297: match(LBRACKET);
298:
299: Object i7 = argStack.pop();
300: Object i6 = argStack.pop();
301: argStack.push(ValidatorUtils.getValueAsString(form, i6
302: + "[" + i7 + "]"));
303:
304: } else if ((LA(1) == IDENTIFIER) && (LA(2) == LBRACKET)
305: && (LA(3) == RBRACKET) && (_tokenSet_0.member(LA(4)))) {
306: identifier();
307: match(LBRACKET);
308: match(RBRACKET);
309:
310: Object i8 = argStack.pop();
311: argStack.push(ValidatorUtils.getValueAsString(form, i8
312: + "[" + index + "]"));
313:
314: } else if ((LA(1) == IDENTIFIER) && (_tokenSet_0.member(LA(2)))) {
315: identifier();
316:
317: Object i9 = argStack.pop();
318: argStack.push(ValidatorUtils.getValueAsString(form,
319: (String) i9));
320:
321: } else {
322: throw new NoViableAltException(LT(1), getFilename());
323: }
324:
325: }
326:
327: public final void literal() throws RecognitionException,
328: TokenStreamException {
329:
330: switch (LA(1)) {
331: case DECIMAL_LITERAL:
332: case HEX_LITERAL:
333: case OCTAL_LITERAL: {
334: integer();
335: break;
336: }
337: case STRING_LITERAL: {
338: string();
339: break;
340: }
341: case LITERAL_null: {
342: match(LITERAL_null);
343: argStack.push(null);
344: break;
345: }
346: case THIS: {
347: match(THIS);
348: argStack.push(value);
349: break;
350: }
351: default: {
352: throw new NoViableAltException(LT(1), getFilename());
353: }
354: }
355: }
356:
357: public final void value() throws RecognitionException,
358: TokenStreamException {
359:
360: switch (LA(1)) {
361: case IDENTIFIER: {
362: field();
363: break;
364: }
365: case DECIMAL_LITERAL:
366: case HEX_LITERAL:
367: case OCTAL_LITERAL:
368: case STRING_LITERAL:
369: case LITERAL_null:
370: case THIS: {
371: literal();
372: break;
373: }
374: default: {
375: throw new NoViableAltException(LT(1), getFilename());
376: }
377: }
378: }
379:
380: public final void expression() throws RecognitionException,
381: TokenStreamException {
382:
383: expr();
384: match(Token.EOF_TYPE);
385: }
386:
387: public final void expr() throws RecognitionException,
388: TokenStreamException {
389:
390: if ((LA(1) == LPAREN) && (_tokenSet_1.member(LA(2)))) {
391: match(LPAREN);
392: comparisonExpression();
393: match(RPAREN);
394: } else if ((LA(1) == LPAREN) && (LA(2) == LPAREN)) {
395: match(LPAREN);
396: joinedExpression();
397: match(RPAREN);
398: } else {
399: throw new NoViableAltException(LT(1), getFilename());
400: }
401:
402: }
403:
404: public final void comparisonExpression()
405: throws RecognitionException, TokenStreamException {
406:
407: value();
408: comparison();
409: value();
410:
411: Object v2 = argStack.pop();
412: Object comp = argStack.pop();
413: Object v1 = argStack.pop();
414: argStack.push(new Boolean(evaluateComparison(v1, comp, v2)));
415:
416: }
417:
418: public final void joinedExpression() throws RecognitionException,
419: TokenStreamException {
420:
421: expr();
422: join();
423: expr();
424:
425: Boolean v1 = (Boolean) argStack.pop();
426: Integer join = (Integer) argStack.pop();
427: Boolean v2 = (Boolean) argStack.pop();
428: if (join.intValue() == AND) {
429: argStack.push(new Boolean(v1.booleanValue()
430: && v2.booleanValue()));
431: } else {
432: argStack.push(new Boolean(v1.booleanValue()
433: || v2.booleanValue()));
434: }
435:
436: }
437:
438: public final void join() throws RecognitionException,
439: TokenStreamException {
440:
441: switch (LA(1)) {
442: case ANDSIGN: {
443: match(ANDSIGN);
444: argStack.push(new Integer(AND));
445: break;
446: }
447: case ORSIGN: {
448: match(ORSIGN);
449: argStack.push(new Integer(OR));
450: break;
451: }
452: default: {
453: throw new NoViableAltException(LT(1), getFilename());
454: }
455: }
456: }
457:
458: public final void comparison() throws RecognitionException,
459: TokenStreamException {
460:
461: switch (LA(1)) {
462: case EQUALSIGN: {
463: match(EQUALSIGN);
464: argStack.push(new Integer(EQUAL));
465: break;
466: }
467: case GREATERTHANSIGN: {
468: match(GREATERTHANSIGN);
469: argStack.push(new Integer(GREATER_THAN));
470: break;
471: }
472: case GREATEREQUALSIGN: {
473: match(GREATEREQUALSIGN);
474: argStack.push(new Integer(GREATER_EQUAL));
475: break;
476: }
477: case LESSTHANSIGN: {
478: match(LESSTHANSIGN);
479: argStack.push(new Integer(LESS_THAN));
480: break;
481: }
482: case LESSEQUALSIGN: {
483: match(LESSEQUALSIGN);
484: argStack.push(new Integer(LESS_EQUAL));
485: break;
486: }
487: case NOTEQUALSIGN: {
488: match(NOTEQUALSIGN);
489: argStack.push(new Integer(NOT_EQUAL));
490: break;
491: }
492: default: {
493: throw new NoViableAltException(LT(1), getFilename());
494: }
495: }
496: }
497:
498: public static final String[] _tokenNames = { "<0>", "EOF", "<2>",
499: "NULL_TREE_LOOKAHEAD", "DECIMAL_LITERAL", "HEX_LITERAL",
500: "OCTAL_LITERAL", "STRING_LITERAL", "IDENTIFIER",
501: "LBRACKET", "RBRACKET", "\"null\"", "THIS", "LPAREN",
502: "RPAREN", "\"and\"", "\"or\"", "EQUALSIGN",
503: "GREATERTHANSIGN", "GREATEREQUALSIGN", "LESSTHANSIGN",
504: "LESSEQUALSIGN", "NOTEQUALSIGN", "WS" };
505:
506: private static final long[] mk_tokenSet_0() {
507: long[] data = { 8273920L, 0L };
508: return data;
509: }
510:
511: public static final BitSet _tokenSet_0 = new BitSet(mk_tokenSet_0());
512:
513: private static final long[] mk_tokenSet_1() {
514: long[] data = { 6640L, 0L };
515: return data;
516: }
517:
518: public static final BitSet _tokenSet_1 = new BitSet(mk_tokenSet_1());
519:
520: }
|