Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@apache.org Received: (qmail 27312 invoked from network); 9 Feb 2003 20:18:00 -0000 Received: from exchange.sun.com (192.18.33.10) by daedalus.apache.org with SMTP; 9 Feb 2003 20:18:00 -0000 Received: (qmail 4179 invoked by uid 97); 9 Feb 2003 20:19:34 -0000 Delivered-To: qmlist-jakarta-archive-commons-dev@nagoya.betaversion.org Received: (qmail 4172 invoked from network); 9 Feb 2003 20:19:33 -0000 Received: from daedalus.apache.org (HELO apache.org) (208.185.179.12) by nagoya.betaversion.org with SMTP; 9 Feb 2003 20:19:33 -0000 Received: (qmail 27048 invoked by uid 500); 9 Feb 2003 20:17:58 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 27037 invoked by uid 500); 9 Feb 2003 20:17:58 -0000 Received: (qmail 27034 invoked from network); 9 Feb 2003 20:17:58 -0000 Received: from icarus.apache.org (208.185.179.13) by daedalus.apache.org with SMTP; 9 Feb 2003 20:17:58 -0000 Received: (qmail 70297 invoked by uid 1197); 9 Feb 2003 20:17:57 -0000 Date: 9 Feb 2003 20:17:57 -0000 Message-ID: <20030209201757.70295.qmail@icarus.apache.org> From: geirm@apache.org To: jakarta-commons-cvs@apache.org Subject: cvs commit: jakarta-commons/jexl/src/java/org/apache/commons/jexl/parser ASTDivNode.java ASTModNode.java X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N geirm 2003/02/09 12:17:56 Modified: jexl/src/java/org/apache/commons/jexl/parser ASTDivNode.java ASTModNode.java Log: Make them work :) Revision Changes Path 1.2 +51 -13 jakarta-commons/jexl/src/java/org/apache/commons/jexl/parser/ASTDivNode.java Index: ASTDivNode.java =================================================================== RCS file: /home/cvs/jakarta-commons/jexl/src/java/org/apache/commons/jexl/parser/ASTDivNode.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- ASTDivNode.java 26 Apr 2002 04:23:14 -0000 1.1 +++ ASTDivNode.java 9 Feb 2003 20:17:55 -0000 1.2 @@ -1,7 +1,7 @@ /* * The Apache Software License, Version 1.1 * - * Copyright (c) 2002 The Apache Software Foundation. All rights + * Copyright (c) 2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without @@ -53,18 +53,56 @@ */ package org.apache.commons.jexl.parser; -public class ASTDivNode extends SimpleNode { - public ASTDivNode(int id) { - super(id); - } - - public ASTDivNode(Parser p, int id) { - super(p, id); - } +import org.apache.commons.jexl.util.Coercion; +import org.apache.commons.jexl.JexlContext; +/** + * / + * + * @author Geir Magnusson Jr. + * @version $Id$ + */ +public class ASTDivNode extends SimpleNode +{ + public ASTDivNode(int id) + { + super(id); + } + + public ASTDivNode(Parser p, int id) + { + super(p, id); + } + + + /** Accept the visitor. **/ + public Object jjtAccept(ParserVisitor visitor, Object data) + { + return visitor.visit(this, data); + } + + public Object value(JexlContext jc) + throws Exception + { + Object left = ((SimpleNode) jjtGetChild(0)).value(jc); + Object right = ((SimpleNode) jjtGetChild(1)).value(jc); + + /* + * the spec says 'and', I think 'or' + */ + if (left == null && right == null) + return new Integer(0); + + Double l = Coercion.coerceDouble(left); + Double r = Coercion.coerceDouble(right); + + /* + * catch div/0 + */ + if (r.doubleValue() == 0.0) + return new Double(0.0); + + return new Double(l.doubleValue() / r.doubleValue()); - /** Accept the visitor. **/ - public Object jjtAccept(ParserVisitor visitor, Object data) { - return visitor.visit(this, data); - } + } } 1.2 +137 -13 jakarta-commons/jexl/src/java/org/apache/commons/jexl/parser/ASTModNode.java Index: ASTModNode.java =================================================================== RCS file: /home/cvs/jakarta-commons/jexl/src/java/org/apache/commons/jexl/parser/ASTModNode.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- ASTModNode.java 26 Apr 2002 04:23:14 -0000 1.1 +++ ASTModNode.java 9 Feb 2003 20:17:55 -0000 1.2 @@ -1,19 +1,143 @@ -/* Generated By:JJTree: Do not edit this line. ASTModNode.java */ +/* + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2003 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", "Commons", "Jexl" 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 + * . + */ package org.apache.commons.jexl.parser; -public class ASTModNode extends SimpleNode { - public ASTModNode(int id) { - super(id); - } - - public ASTModNode(Parser p, int id) { - super(p, id); - } +import org.apache.commons.jexl.JexlContext; +import org.apache.commons.jexl.util.Coercion; +/** + * % + * + * @author Geir Magnusson Jr. + * @version $Id$ + */ +public class ASTModNode extends SimpleNode +{ + public ASTModNode(int id) + { + super(id); + } + + public ASTModNode(Parser p, int id) + { + super(p, id); + } + + + /** Accept the visitor. **/ + public Object jjtAccept(ParserVisitor visitor, Object data) + { + return visitor.visit(this, data); + } + + public Object value(JexlContext jc) + throws Exception + { + Object left = ((SimpleNode) jjtGetChild(0)).value(jc); + Object right = ((SimpleNode) jjtGetChild(1)).value(jc); + + /* + * the spec says 'and', I think 'or' + */ + if (left == null && right == null) + return new Integer(0); + + /* + * if anything is float, double or string with ( "." | "E" | "e") + * coerce all to doubles and do it + */ + if ( left instanceof Float || left instanceof Double + || right instanceof Float || right instanceof Double + || ( left instanceof String + && ( ((String) left).indexOf(".") != -1 || + ((String) left).indexOf("e") != -1 || + ((String) left).indexOf("E") != -1 ) + ) + || ( right instanceof String + && ( ((String) right).indexOf(".") != -1 || + ((String) right).indexOf("e") != -1 || + ((String) right).indexOf("E") != -1 ) + ) + ) + { + Double l = Coercion.coerceDouble(left); + Double r = Coercion.coerceDouble(right); + + /* + * catch div/0 + */ + if (r.doubleValue() == 0.0) + return new Double(0.0); + + return new Double(l.doubleValue() % r.doubleValue()); + } + + /* + * otherwise to longs with thee! + */ + + Long l = Coercion.coerceLong(left); + Long r = Coercion.coerceLong(right); + + /* + * catch the div/0 + */ + if (r.longValue() == 0) + return new Long(0); + + return new Long(l.longValue() % r.longValue()); + } - /** Accept the visitor. **/ - public Object jjtAccept(ParserVisitor visitor, Object data) { - return visitor.visit(this, data); - } } --------------------------------------------------------------------- To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-dev-help@jakarta.apache.org