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
    17  package org.apache.commons.jexl.parser;
    18
    19  import org.apache.commons.jexl.JexlContext;
    20  import org.apache.commons.jexl.util.Coercion;
    21
    22  /***
    23   *  LT : a < b
    24   *
    25   *  Follows A.3.6.1 of the JSTL 1.0 specification
    26   *
    27   *  @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
    28   *  @author <a href="mailto:proyal@apache.org">Peter Royal</a>
    29   *  @version $Id: ASTLTNode.java,v 1.5 2004/02/28 13:45:20 yoavs Exp $
    30   */
    31  public class ASTLTNode extends SimpleNode
    32  {
    33      public ASTLTNode(int id)
    34      {
    35          super(id);
    36      }
    37
    38      public ASTLTNode(Parser p, int id)
    39      {
    40          super(p, id);
    41      }
    42
    43      /*** Accept the visitor. **/
    44      public Object jjtAccept(ParserVisitor visitor, Object data)
    45      {
    46          return visitor.visit(this, data);
    47      }
    48
    49      public Object value(JexlContext jc)
    50          throws Exception
    51      {
    52          /*
    53           * now get the values
    54           */
    55
    56          Object left = ( (SimpleNode) jjtGetChild(0)).value(jc);
    57          Object right = ( (SimpleNode) jjtGetChild(1)).value(jc);
    58
    59          if( ( left == right ) || ( left == null ) || ( right == null ) )
    60          {
    61              return Boolean.FALSE;
    62          }
    63          else if( Coercion.isFloatingPoint( left ) || Coercion.isFloatingPoint( right ) )
    64          {
    65              double leftDouble = Coercion.coerceDouble( left ).doubleValue();
    66              double rightDouble = Coercion.coerceDouble( right ).doubleValue();
    67
    68              return leftDouble < rightDouble
    69                  ? Boolean.TRUE
    70                  : Boolean.FALSE;
    71          }
    72          else if( Coercion.isNumberable( left ) || Coercion.isNumberable( right ) )
    73          {
    74              long leftLong = Coercion.coerceLong( left ).longValue();
    75              long rightLong = Coercion.coerceLong( right ).longValue();
    76
    77              return leftLong < rightLong
    78                  ? Boolean.TRUE
    79                  : Boolean.FALSE;
    80          }
    81          else if( left instanceof String || right instanceof String )
    82          {
    83              String leftString = left.toString();
    84              String rightString = right.toString();
    85
    86              return leftString.compareTo( rightString ) < 0
    87                  ? Boolean.TRUE
    88                  : Boolean.FALSE;
    89          }
    90          else if( left instanceof Comparable )
    91          {
    92              return ( (Comparable)left ).compareTo( right ) < 0
    93                  ? Boolean.TRUE
    94                  : Boolean.FALSE;
    95          }
    96          else if( right instanceof Comparable )
    97          {
    98              return ( (Comparable)right ).compareTo( left ) > 0
    99                  ? Boolean.TRUE
    100                 : Boolean.FALSE;
    101         }
    102
    103         throw new Exception("Invalid comparison : LT ");
    104     }
    105 }