You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by tu...@apache.org on 2003/04/15 08:38:27 UTC
cvs commit: jakarta-struts/src/share/org/apache/struts/validator/validwhen ValidWhenLexer.java ValidWhenParser.g ValidWhenParserTokenTypes.java ValidWhenParserTokenTypes.txt ValidWhen.java ParseException.java SimpleCharStream.java Token.java TokenMgrError.java ValidWhenParser.java ValidWhenParserConstants.java
turner 2003/04/14 23:38:27
Modified: src/share/org/apache/struts/validator/validwhen
ValidWhen.java
Added: src/share/org/apache/struts/validator/validwhen
ValidWhenLexer.java ValidWhenParser.g
ValidWhenParserTokenTypes.java
ValidWhenParserTokenTypes.txt
Removed: src/share/org/apache/struts/validator/validwhen
ParseException.java SimpleCharStream.java
Token.java TokenMgrError.java ValidWhenParser.java
ValidWhenParserConstants.java
Log:
Changing parsers due to look-ahead problems with JavaCC, now using ANTLR
Revision Changes Path
1.2 +15 -3 jakarta-struts/src/share/org/apache/struts/validator/validwhen/ValidWhen.java
Index: ValidWhen.java
===================================================================
RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/validator/validwhen/ValidWhen.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ValidWhen.java 14 Apr 2003 20:21:38 -0000 1.1
+++ ValidWhen.java 15 Apr 2003 06:38:27 -0000 1.2
@@ -59,6 +59,7 @@
import org.apache.struts.action.ActionError;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.validator.Resources;
+import java.io.StringReader;
/**
* <p>
@@ -119,9 +120,20 @@
}
String test = field.getVarValue("test");
if (test == null) return false;
- try {
- valid = ValidWhenParser.evaluateExpression(test, form, index, value);
- } catch (ParseException ex) {
+ ValidWhenLexer l = new ValidWhenLexer(new StringReader(test));
+
+ ValidWhenParser p = new ValidWhenParser(l);
+
+ p.setForm(form);
+ p.setIndex(index);
+ p.setValue(value);
+
+ try {
+ p.expression();
+ valid = p.getResult();
+ } catch (Exception ex) {
+ ex.printStackTrace();
+ errors.add(field.getKey(), Resources.getActionError(request, va, field));
return false;
}
if (!valid) {
1.1 jakarta-struts/src/share/org/apache/struts/validator/validwhen/ValidWhenLexer.java
Index: ValidWhenLexer.java
===================================================================
// $ANTLR 2.7.1: "ValidWhenParser.g" -> "ValidWhenLexer.java"$
/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Struts", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.struts.validator.validwhen;
import java.util.Stack;
import org.apache.commons.validator.ValidatorUtil;
import java.io.InputStream;
import antlr.TokenStreamException;
import antlr.TokenStreamIOException;
import antlr.TokenStreamRecognitionException;
import antlr.CharStreamException;
import antlr.CharStreamIOException;
import antlr.ANTLRException;
import java.io.Reader;
import java.util.Hashtable;
import antlr.CharScanner;
import antlr.InputBuffer;
import antlr.ByteBuffer;
import antlr.CharBuffer;
import antlr.Token;
import antlr.CommonToken;
import antlr.RecognitionException;
import antlr.NoViableAltForCharException;
import antlr.MismatchedCharException;
import antlr.TokenStream;
import antlr.ANTLRHashString;
import antlr.LexerSharedInputState;
import antlr.collections.impl.BitSet;
import antlr.SemanticException;
public class ValidWhenLexer extends antlr.CharScanner implements ValidWhenParserTokenTypes, TokenStream
{
public ValidWhenLexer(InputStream in) {
this(new ByteBuffer(in));
}
public ValidWhenLexer(Reader in) {
this(new CharBuffer(in));
}
public ValidWhenLexer(InputBuffer ib) {
this(new LexerSharedInputState(ib));
}
public ValidWhenLexer(LexerSharedInputState state) {
super(state);
literals = new Hashtable();
literals.put(new ANTLRHashString("null", this), new Integer(11));
literals.put(new ANTLRHashString("or", this), new Integer(16));
literals.put(new ANTLRHashString("and", this), new Integer(15));
caseSensitiveLiterals = true;
setCaseSensitive(false);
}
public Token nextToken() throws TokenStreamException {
Token theRetToken=null;
tryAgain:
for (;;) {
Token _token = null;
int _ttype = Token.INVALID_TYPE;
resetText();
try { // for char stream error handling
try { // for lexical error handling
switch ( LA(1)) {
case '\t': case '\n': case '\r': case ' ':
{
mWS(true);
theRetToken=_returnToken;
break;
}
case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8':
case '9':
{
mDECIMAL_LITERAL(true);
theRetToken=_returnToken;
break;
}
case '"': case '\'':
{
mSTRING_LITERAL(true);
theRetToken=_returnToken;
break;
}
case '[':
{
mLBRACKET(true);
theRetToken=_returnToken;
break;
}
case ']':
{
mRBRACKET(true);
theRetToken=_returnToken;
break;
}
case '(':
{
mLPAREN(true);
theRetToken=_returnToken;
break;
}
case ')':
{
mRPAREN(true);
theRetToken=_returnToken;
break;
}
case '*':
{
mTHIS(true);
theRetToken=_returnToken;
break;
}
case '.': case 'a': case 'b': case 'c':
case 'd': case 'e': case 'f': case 'g':
case 'h': case 'i': case 'j': case 'k':
case 'l': case 'm': case 'n': case 'o':
case 'p': case 'q': case 'r': case 's':
case 't': case 'u': case 'v': case 'w':
case 'x': case 'y': case 'z':
{
mIDENTIFIER(true);
theRetToken=_returnToken;
break;
}
case '=':
{
mEQUALSIGN(true);
theRetToken=_returnToken;
break;
}
case '!':
{
mNOTEQUALSIGN(true);
theRetToken=_returnToken;
break;
}
default:
if ((LA(1)=='0') && (LA(2)=='x')) {
mHEX_LITERAL(true);
theRetToken=_returnToken;
}
else if ((LA(1)=='0') && ((LA(2) >= '0' && LA(2) <= '7'))) {
mOCTAL_LITERAL(true);
theRetToken=_returnToken;
}
else if ((LA(1)=='<') && (LA(2)=='=')) {
mLESSEQUALSIGN(true);
theRetToken=_returnToken;
}
else if ((LA(1)=='>') && (LA(2)=='=')) {
mGREATEREQUALSIGN(true);
theRetToken=_returnToken;
}
else if ((LA(1)=='<') && (true)) {
mLESSTHANSIGN(true);
theRetToken=_returnToken;
}
else if ((LA(1)=='>') && (true)) {
mGREATERTHANSIGN(true);
theRetToken=_returnToken;
}
else {
if (LA(1)==EOF_CHAR) {uponEOF(); _returnToken = makeToken(Token.EOF_TYPE);}
else {throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());}
}
}
if ( _returnToken==null ) continue tryAgain; // found SKIP token
_ttype = _returnToken.getType();
_ttype = testLiteralsTable(_ttype);
_returnToken.setType(_ttype);
return _returnToken;
}
catch (RecognitionException e) {
throw new TokenStreamRecognitionException(e);
}
}
catch (CharStreamException cse) {
if ( cse instanceof CharStreamIOException ) {
throw new TokenStreamIOException(((CharStreamIOException)cse).io);
}
else {
throw new TokenStreamException(cse.getMessage());
}
}
}
}
public final void mWS(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
int _ttype; Token _token=null; int _begin=text.length();
_ttype = WS;
int _saveIndex;
{
int _cnt15=0;
_loop15:
do {
switch ( LA(1)) {
case ' ':
{
match(' ');
break;
}
case '\t':
{
match('\t');
break;
}
case '\n':
{
match('\n');
break;
}
case '\r':
{
match('\r');
break;
}
default:
{
if ( _cnt15>=1 ) { break _loop15; } else {throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());}
}
}
_cnt15++;
} while (true);
}
_ttype = Token.SKIP;
if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
_token = makeToken(_ttype);
_token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
}
_returnToken = _token;
}
public final void mDECIMAL_LITERAL(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
int _ttype; Token _token=null; int _begin=text.length();
_ttype = DECIMAL_LITERAL;
int _saveIndex;
{
matchRange('1','9');
}
{
_loop19:
do {
if (((LA(1) >= '0' && LA(1) <= '9'))) {
matchRange('0','9');
}
else {
break _loop19;
}
} while (true);
}
if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
_token = makeToken(_ttype);
_token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
}
_returnToken = _token;
}
public final void mHEX_LITERAL(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
int _ttype; Token _token=null; int _begin=text.length();
_ttype = HEX_LITERAL;
int _saveIndex;
match('0');
match('x');
{
int _cnt22=0;
_loop22:
do {
switch ( LA(1)) {
case '0': case '1': case '2': case '3':
case '4': case '5': case '6': case '7':
case '8': case '9':
{
matchRange('0','9');
break;
}
case 'a': case 'b': case 'c': case 'd':
case 'e': case 'f':
{
matchRange('a','f');
break;
}
default:
{
if ( _cnt22>=1 ) { break _loop22; } else {throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());}
}
}
_cnt22++;
} while (true);
}
if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
_token = makeToken(_ttype);
_token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
}
_returnToken = _token;
}
public final void mOCTAL_LITERAL(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
int _ttype; Token _token=null; int _begin=text.length();
_ttype = OCTAL_LITERAL;
int _saveIndex;
match('0');
{
int _cnt25=0;
_loop25:
do {
if (((LA(1) >= '0' && LA(1) <= '7'))) {
matchRange('0','7');
}
else {
if ( _cnt25>=1 ) { break _loop25; } else {throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());}
}
_cnt25++;
} while (true);
}
if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
_token = makeToken(_ttype);
_token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
}
_returnToken = _token;
}
public final void mSTRING_LITERAL(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
int _ttype; Token _token=null; int _begin=text.length();
_ttype = STRING_LITERAL;
int _saveIndex;
switch ( LA(1)) {
case '\'':
{
{
match('\'');
{
int _cnt29=0;
_loop29:
do {
if ((_tokenSet_0.member(LA(1)))) {
matchNot('\'');
}
else {
if ( _cnt29>=1 ) { break _loop29; } else {throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());}
}
_cnt29++;
} while (true);
}
match('\'');
}
break;
}
case '"':
{
{
match('\"');
{
int _cnt32=0;
_loop32:
do {
if ((_tokenSet_1.member(LA(1)))) {
matchNot('\"');
}
else {
if ( _cnt32>=1 ) { break _loop32; } else {throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());}
}
_cnt32++;
} while (true);
}
match('\"');
}
break;
}
default:
{
throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());
}
}
if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
_token = makeToken(_ttype);
_token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
}
_returnToken = _token;
}
public final void mLBRACKET(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
int _ttype; Token _token=null; int _begin=text.length();
_ttype = LBRACKET;
int _saveIndex;
match('[');
if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
_token = makeToken(_ttype);
_token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
}
_returnToken = _token;
}
public final void mRBRACKET(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
int _ttype; Token _token=null; int _begin=text.length();
_ttype = RBRACKET;
int _saveIndex;
match(']');
if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
_token = makeToken(_ttype);
_token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
}
_returnToken = _token;
}
public final void mLPAREN(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
int _ttype; Token _token=null; int _begin=text.length();
_ttype = LPAREN;
int _saveIndex;
match('(');
if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
_token = makeToken(_ttype);
_token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
}
_returnToken = _token;
}
public final void mRPAREN(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
int _ttype; Token _token=null; int _begin=text.length();
_ttype = RPAREN;
int _saveIndex;
match(')');
if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
_token = makeToken(_ttype);
_token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
}
_returnToken = _token;
}
public final void mTHIS(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
int _ttype; Token _token=null; int _begin=text.length();
_ttype = THIS;
int _saveIndex;
match("*this*");
if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
_token = makeToken(_ttype);
_token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
}
_returnToken = _token;
}
public final void mIDENTIFIER(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
int _ttype; Token _token=null; int _begin=text.length();
_ttype = IDENTIFIER;
int _saveIndex;
{
switch ( LA(1)) {
case 'a': case 'b': case 'c': case 'd':
case 'e': case 'f': case 'g': case 'h':
case 'i': case 'j': case 'k': case 'l':
case 'm': case 'n': case 'o': case 'p':
case 'q': case 'r': case 's': case 't':
case 'u': case 'v': case 'w': case 'x':
case 'y': case 'z':
{
matchRange('a','z');
break;
}
case '.':
{
match('.');
break;
}
default:
{
throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());
}
}
}
{
int _cnt41=0;
_loop41:
do {
switch ( LA(1)) {
case 'a': case 'b': case 'c': case 'd':
case 'e': case 'f': case 'g': case 'h':
case 'i': case 'j': case 'k': case 'l':
case 'm': case 'n': case 'o': case 'p':
case 'q': case 'r': case 's': case 't':
case 'u': case 'v': case 'w': case 'x':
case 'y': case 'z':
{
matchRange('a','z');
break;
}
case '0': case '1': case '2': case '3':
case '4': case '5': case '6': case '7':
case '8': case '9':
{
matchRange('0','9');
break;
}
case '.':
{
match('.');
break;
}
default:
{
if ( _cnt41>=1 ) { break _loop41; } else {throw new NoViableAltForCharException((char)LA(1), getFilename(), getLine());}
}
}
_cnt41++;
} while (true);
}
if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
_token = makeToken(_ttype);
_token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
}
_returnToken = _token;
}
public final void mEQUALSIGN(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
int _ttype; Token _token=null; int _begin=text.length();
_ttype = EQUALSIGN;
int _saveIndex;
match('=');
match('=');
if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
_token = makeToken(_ttype);
_token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
}
_returnToken = _token;
}
public final void mNOTEQUALSIGN(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
int _ttype; Token _token=null; int _begin=text.length();
_ttype = NOTEQUALSIGN;
int _saveIndex;
match('!');
match('=');
if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
_token = makeToken(_ttype);
_token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
}
_returnToken = _token;
}
public final void mLESSTHANSIGN(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
int _ttype; Token _token=null; int _begin=text.length();
_ttype = LESSTHANSIGN;
int _saveIndex;
match('<');
if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
_token = makeToken(_ttype);
_token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
}
_returnToken = _token;
}
public final void mGREATERTHANSIGN(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
int _ttype; Token _token=null; int _begin=text.length();
_ttype = GREATERTHANSIGN;
int _saveIndex;
match('>');
if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
_token = makeToken(_ttype);
_token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
}
_returnToken = _token;
}
public final void mLESSEQUALSIGN(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
int _ttype; Token _token=null; int _begin=text.length();
_ttype = LESSEQUALSIGN;
int _saveIndex;
match('<');
match('=');
if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
_token = makeToken(_ttype);
_token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
}
_returnToken = _token;
}
public final void mGREATEREQUALSIGN(boolean _createToken) throws RecognitionException, CharStreamException, TokenStreamException {
int _ttype; Token _token=null; int _begin=text.length();
_ttype = GREATEREQUALSIGN;
int _saveIndex;
match('>');
match('=');
if ( _createToken && _token==null && _ttype!=Token.SKIP ) {
_token = makeToken(_ttype);
_token.setText(new String(text.getBuffer(), _begin, text.length()-_begin));
}
_returnToken = _token;
}
private static final long _tokenSet_0_data_[] = { 8358477528813282816L, 576460744384577536L, 0L, 0L };
public static final BitSet _tokenSet_0 = new BitSet(_tokenSet_0_data_);
private static final long _tokenSet_1_data_[] = { 8358478061389227520L, 576460744384577536L, 0L, 0L };
public static final BitSet _tokenSet_1 = new BitSet(_tokenSet_1_data_);
}
1.1 jakarta-struts/src/share/org/apache/struts/validator/validwhen/ValidWhenParser.g
Index: ValidWhenParser.g
===================================================================
header {
/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Struts", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.struts.validator.validwhen;
import java.util.Stack;
import org.apache.commons.validator.ValidatorUtil;
}
class ValidWhenParser extends Parser;
options {
k=6;
defaultErrorHandler=false;
}
{Stack argStack = new Stack();
Object form;
int index;
String value;
public void setForm(Object f) { form = f; };
public void setIndex (int i) { index = i; };
public void setValue (String v) { value = v; };
public boolean getResult() {
return ((Boolean)argStack.peek()).booleanValue();
}
private final int LESS_EQUAL=0;
private final int LESS_THAN=1;
private final int EQUAL=2;
private final int GREATER_THAN=3;
private final int GREATER_EQUAL=4;
private final int NOT_EQUAL=5;
private final int AND=6;
private final int OR=7;
private boolean evaluateComparison (Object v1, Object compare, Object v2) {
boolean intCompare = true;
if ((v1 == null) || (v2 == null)) {
if (String.class.isInstance(v1)) {
if (((String) v1).length() == 0) {
v1 = null;
}
}
if (String.class.isInstance(v2)) {
if (((String) v2).length() == 0) {
v2 = null;
}
}
switch (((Integer)compare).intValue()) {
case LESS_EQUAL:
case GREATER_THAN:
case LESS_THAN:
case GREATER_EQUAL:
return false;
case EQUAL:
return (v1 == v2);
case NOT_EQUAL:
return (v1 != v2);
}
}
if (!Integer.class.isInstance(v1) &&
!Integer.class.isInstance(v2)) {
intCompare = false;
}
if (intCompare) {
try {
int v1i = 0, v2i = 0;
if (Integer.class.isInstance(v1)) {
v1i = ((Integer)v1).intValue();
} else {
v1i = Integer.parseInt((String) v1);
}
if (Integer.class.isInstance(v2)) {
v2i = ((Integer)v2).intValue();
} else {
v2i = Integer.parseInt((String) v2);
}
switch (((Integer)compare).intValue()) {
case LESS_EQUAL:
return (v1i <= v2i);
case LESS_THAN:
return (v1i < v2i);
case EQUAL:
return (v1i == v2i);
case GREATER_THAN:
return (v1i > v2i);
case GREATER_EQUAL:
return (v1i >= v2i);
case NOT_EQUAL:
return (v1i != v2i);
}
} catch (NumberFormatException ex) {};
}
String v1s = "", v2s = "";
if (Integer.class.isInstance(v1)) {
v1s = ((Integer)v1).toString();
} else {
v1s = (String) v1;
}
if (Integer.class.isInstance(v2)) {
v2s = ((Integer)v2).toString();
} else {
v2s = (String) v2;
}
int res = v1s.compareTo(v2s);
switch (((Integer)compare).intValue()) {
case LESS_EQUAL:
return (res <= 0);
case LESS_THAN:
return (res < 0);
case EQUAL:
return (res == 0);
case GREATER_THAN:
return (res > 0);
case GREATER_EQUAL:
return (res >= 0);
case NOT_EQUAL:
return (res != 0);
}
return true;
}
}
integer
: d:DECIMAL_LITERAL { argStack.push(Integer.valueOf(d.getText())); }
| h:HEX_LITERAL { argStack.push(Integer.valueOf(d.getText())); }
| o:OCTAL_LITERAL { argStack.push(Integer.valueOf(d.getText())); } ;
string : str:STRING_LITERAL { argStack.push(str.getText().substring(1, str.getText().length()-1)); };
identifier
: str:IDENTIFIER { argStack.push(str.getText()); } ;
field
: identifier LBRACKET RBRACKET identifier {
Object i2 = argStack.pop();
Object i1 = argStack.pop();
argStack.push(ValidatorUtil.getValueAsString(form, i1 + "[" + index + "]" + i2));
}
| identifier LBRACKET integer RBRACKET identifier {
Object i5 = argStack.pop();
Object i4 = argStack.pop();
Object i3 = argStack.pop();
argStack.push(ValidatorUtil.getValueAsString(form, i3 + "[" + i4 + "]" + i5));
}
| identifier LBRACKET integer RBRACKET LBRACKET {
Object i7 = argStack.pop();
Object i6 = argStack.pop();
argStack.push(ValidatorUtil.getValueAsString(form, i6 + "[" + i7 + "]"));
}
| identifier LBRACKET RBRACKET {
Object i8 = argStack.pop();
argStack.push(ValidatorUtil.getValueAsString(form, i8 + "[" + index + "]"));
}
| identifier {
Object i9 = argStack.pop();
argStack.push(ValidatorUtil.getValueAsString(form, (String)i9));
}
;
literal : integer | string | "null" { argStack.push(null);} | THIS {argStack.push(value);};
value : field | literal ;
expression : expr EOF;
expr: LPAREN comparisonExpression RPAREN | LPAREN joinedExpression RPAREN;
joinedExpression : expr join expr {
Boolean v1 = (Boolean) argStack.pop();
Integer join = (Integer) argStack.pop();
Boolean v2 = (Boolean) argStack.pop();
if (join.intValue() == AND) {
argStack.push(new Boolean(v1.booleanValue() && v2.booleanValue()));
} else {
argStack.push(new Boolean(v1.booleanValue() || v2.booleanValue()));
}
};
join : ANDSIGN { argStack.push(new Integer(AND)); } |
ORSIGN { argStack.push(new Integer(OR)); };
comparison :
EQUALSIGN { argStack.push(new Integer(EQUAL)); } |
GREATERTHANSIGN { argStack.push(new Integer(GREATER_THAN)); } |
GREATEREQUALSIGN { argStack.push(new Integer(GREATER_EQUAL)); } |
LESSTHANSIGN { argStack.push(new Integer(LESS_THAN)); } |
LESSEQUALSIGN { argStack.push(new Integer(LESS_EQUAL)); } |
NOTEQUALSIGN { argStack.push(new Integer(NOT_EQUAL)); } ;
comparisonExpression : value comparison value {
Object v2 = argStack.pop();
Object comp = argStack.pop();
Object v1 = argStack.pop();
argStack.push(new Boolean(evaluateComparison(v1, comp, v2)));
};
class ValidWhenLexer extends Lexer;
options {
k=2;
caseSensitive=false;
defaultErrorHandler=false;
}
tokens {
ANDSIGN="and";
ORSIGN="or";
}
WS : ( ' ' | '\t' | '\n' | '\r' )+
{ $setType(Token.SKIP); }
;
DECIMAL_LITERAL : ('1'..'9') ('0'..'9')* ;
HEX_LITERAL : '0' 'x' ('0'..'9' | 'a'..'f')+ ;
OCTAL_LITERAL : '0' ('0'..'7')+ ;
STRING_LITERAL : ('\'' (~'\'')+ '\'') | ('\"' (~'\"')+ '\"') ;
LBRACKET : '[' ;
RBRACKET : ']' ;
LPAREN : '(' ;
RPAREN : ')' ;
THIS : "*this*" ;
IDENTIFIER : ( 'a'..'z' | '.') ( 'a'..'z' | '0'..'9' | '.')+ ;
EQUALSIGN : '=' '=' ;
NOTEQUALSIGN : '!' '=' ;
LESSTHANSIGN : '<';
GREATERTHANSIGN : '>';
LESSEQUALSIGN : '<' '=';
GREATEREQUALSIGN : '>' '=';
1.1 jakarta-struts/src/share/org/apache/struts/validator/validwhen/ValidWhenParserTokenTypes.java
Index: ValidWhenParserTokenTypes.java
===================================================================
// $ANTLR 2.7.1: "ValidWhenParser.g" -> "ValidWhenParser.java"$
/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Struts", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.struts.validator.validwhen;
import java.util.Stack;
import org.apache.commons.validator.ValidatorUtil;
public interface ValidWhenParserTokenTypes {
int EOF = 1;
int NULL_TREE_LOOKAHEAD = 3;
int DECIMAL_LITERAL = 4;
int HEX_LITERAL = 5;
int OCTAL_LITERAL = 6;
int STRING_LITERAL = 7;
int IDENTIFIER = 8;
int LBRACKET = 9;
int RBRACKET = 10;
int LITERAL_null = 11;
int THIS = 12;
int LPAREN = 13;
int RPAREN = 14;
int ANDSIGN = 15;
int ORSIGN = 16;
int EQUALSIGN = 17;
int GREATERTHANSIGN = 18;
int GREATEREQUALSIGN = 19;
int LESSTHANSIGN = 20;
int LESSEQUALSIGN = 21;
int NOTEQUALSIGN = 22;
int WS = 23;
}
1.1 jakarta-struts/src/share/org/apache/struts/validator/validwhen/ValidWhenParserTokenTypes.txt
Index: ValidWhenParserTokenTypes.txt
===================================================================
// $ANTLR 2.7.1: ValidWhenParser.g -> ValidWhenParserTokenTypes.txt$
ValidWhenParser // output token vocab name
DECIMAL_LITERAL=4
HEX_LITERAL=5
OCTAL_LITERAL=6
STRING_LITERAL=7
IDENTIFIER=8
LBRACKET=9
RBRACKET=10
LITERAL_null="null"=11
THIS=12
LPAREN=13
RPAREN=14
ANDSIGN="and"=15
ORSIGN="or"=16
EQUALSIGN=17
GREATERTHANSIGN=18
GREATEREQUALSIGN=19
LESSTHANSIGN=20
LESSEQUALSIGN=21
NOTEQUALSIGN=22
WS=23
---------------------------------------------------------------------
To unsubscribe, e-mail: struts-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-dev-help@jakarta.apache.org