View Javadoc

    1   /*
    2    * Copyright 2002,2004 The Apache Software Foundation.
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *      http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  package org.apache.commons.jexl.parser;
    17
    18  import org.apache.commons.jexl.JexlContext;
    19  import org.apache.commons.jexl.util.Coercion;
    20
    21  /***
    22   *  LE : a <= b
    23   *
    24   *  Follows A.3.6.1 of the JSTL 1.0 specification
    25   *
    26   *  @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
    27   *  @author <a href="mailto:proyal@apache.org">Peter Royal</a>
    28   *  @version $Id: ASTLENode.java,v 1.4 2004/02/28 13:45:20 yoavs Exp $
    29   */
    30  public class ASTLENode extends SimpleNode
    31  {
    32      public ASTLENode(int id)
    33      {
    34          super(id);
    35      }
    36
    37      public ASTLENode(Parser p, int id)
    38      {
    39          super(p, id);
    40      }
    41
    42      /*** Accept the visitor. **/
    43      public Object jjtAccept(ParserVisitor visitor, Object data)
    44      {
    45          return visitor.visit(this, data);
    46      }
    47
    48      public Object value(JexlContext jc)
    49          throws Exception
    50      {
    51          /*
    52           * now get the values
    53           */
    54
    55          Object left = ( (SimpleNode) jjtGetChild(0)).value(jc);
    56          Object right = ( (SimpleNode) jjtGetChild(1)).value(jc);
    57
    58          if( left == right )
    59          {
    60              return Boolean.TRUE;
    61          }
    62          else if ( ( left == null ) || ( right == null ) )
    63          {
    64              return Boolean.FALSE;
    65          }
    66          else if( Coercion.isFloatingPoint( left ) || Coercion.isFloatingPoint( right ) )
    67          {
    68              double leftDouble = Coercion.coerceDouble( left ).doubleValue();
    69              double rightDouble = Coercion.coerceDouble( right ).doubleValue();
    70
    71              return leftDouble <= rightDouble
    72                  ? Boolean.TRUE
    73                  : Boolean.FALSE;
    74          }
    75          else if( Coercion.isNumberable( left ) || Coercion.isNumberable( right ) )
    76          {
    77              long leftLong = Coercion.coerceLong( left ).longValue();
    78              long rightLong = Coercion.coerceLong( right ).longValue();
    79
    80              return leftLong <= rightLong
    81                  ? Boolean.TRUE
    82                  : Boolean.FALSE;
    83          }
    84          else if( left instanceof String || right instanceof String )
    85          {
    86              String leftString = left.toString();
    87              String rightString = right.toString();
    88
    89              return leftString.compareTo( rightString ) <= 0
    90                  ? Boolean.TRUE
    91                  : Boolean.FALSE;
    92          }
    93          else if( left instanceof Comparable )
    94          {
    95              return ( (Comparable)left ).compareTo( right ) <= 0
    96                  ? Boolean.TRUE
    97                  : Boolean.FALSE;
    98          }
    99          else if( right instanceof Comparable )
    100         {
    101             return ( (Comparable)right ).compareTo( left ) >= 0
    102                 ? Boolean.TRUE
    103                 : Boolean.FALSE;
    104         }
    105
    106         throw new Exception("Invalid comparison : LE ");
    107     }
    108
    109 }