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   *  GT : 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: ASTGTNode.java,v 1.5 2004/02/28 13:45:20 yoavs Exp $
    29   */
    30  public class ASTGTNode extends SimpleNode
    31  {
    32      public ASTGTNode( int id )
    33      {
    34          super( id );
    35      }
    36
    37      public ASTGTNode( 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 ) || ( left == null ) || ( right == null ) )
    59          {
    60              return Boolean.FALSE;
    61          }
    62          else if( Coercion.isFloatingPoint( left ) || Coercion.isFloatingPoint( right ) )
    63          {
    64              double leftDouble = Coercion.coerceDouble( left ).doubleValue();
    65              double rightDouble = Coercion.coerceDouble( right ).doubleValue();
    66
    67              return leftDouble > rightDouble
    68                  ? Boolean.TRUE
    69                  : Boolean.FALSE;
    70          }
    71          else if( Coercion.isNumberable( left ) || Coercion.isNumberable( right ) )
    72          {
    73              long leftLong = Coercion.coerceLong( left ).longValue();
    74              long rightLong = Coercion.coerceLong( right ).longValue();
    75
    76              return leftLong > rightLong
    77                  ? Boolean.TRUE
    78                  : Boolean.FALSE;
    79          }
    80          else if( left instanceof String || right instanceof String )
    81          {
    82              String leftString = left.toString();
    83              String rightString = right.toString();
    84
    85              return leftString.compareTo( rightString ) > 0
    86                  ? Boolean.TRUE
    87                  : Boolean.FALSE;
    88          }
    89          else if( left instanceof Comparable )
    90          {
    91              return ( (Comparable)left ).compareTo( right ) > 0
    92                  ? Boolean.TRUE
    93                  : Boolean.FALSE;
    94          }
    95          else if( right instanceof Comparable )
    96          {
    97              return ( (Comparable)right ).compareTo( left ) < 0
    98                  ? Boolean.TRUE
    99                  : Boolean.FALSE;
    100         }
    101
    102         throw new Exception( "Invalid comparison : GT " );
    103     }
    104 }