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 <a href="mailto:[EMAIL PROTECTED]";>Geir Magnusson Jr.</a>
  + *  @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 [EMAIL PROTECTED]
  + *
  + * 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.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 <a href="mailto:[EMAIL PROTECTED]";>Geir Magnusson Jr.</a>
  + *  @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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to