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