001: /*
002: * Copyright 2002-2006 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.jexl.parser;
017:
018: import org.apache.commons.jexl.JexlContext;
019: import org.apache.commons.jexl.util.Coercion;
020:
021: /**
022: * LE : a <= b.
023: *
024: * Follows A.3.6.1 of the JSTL 1.0 specification
025: *
026: * @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
027: * @author <a href="mailto:proyal@apache.org">Peter Royal</a>
028: * @version $Id: ASTLENode.java 398203 2006-04-29 16:44:55Z dion $
029: */
030: public class ASTLENode extends SimpleNode {
031: /**
032: * Create the node given an id.
033: *
034: * @param id node id.
035: */
036: public ASTLENode(int id) {
037: super (id);
038: }
039:
040: /**
041: * Create a node with the given parser and id.
042: *
043: * @param p a parser.
044: * @param id node id.
045: */
046: public ASTLENode(Parser p, int id) {
047: super (p, id);
048: }
049:
050: /** {@inheritDoc} */
051: public Object jjtAccept(ParserVisitor visitor, Object data) {
052: return visitor.visit(this , data);
053: }
054:
055: /** {@inheritDoc} */
056: public Object value(JexlContext jc) throws Exception {
057: /*
058: * now get the values
059: */
060:
061: Object left = ((SimpleNode) jjtGetChild(0)).value(jc);
062: Object right = ((SimpleNode) jjtGetChild(1)).value(jc);
063:
064: if (left == right) {
065: return Boolean.TRUE;
066: } else if ((left == null) || (right == null)) {
067: return Boolean.FALSE;
068: } else if (Coercion.isFloatingPoint(left)
069: || Coercion.isFloatingPoint(right)) {
070: double leftDouble = Coercion.coerceDouble(left)
071: .doubleValue();
072: double rightDouble = Coercion.coerceDouble(right)
073: .doubleValue();
074:
075: return leftDouble <= rightDouble ? Boolean.TRUE
076: : Boolean.FALSE;
077: } else if (Coercion.isNumberable(left)
078: || Coercion.isNumberable(right)) {
079: long leftLong = Coercion.coerceLong(left).longValue();
080: long rightLong = Coercion.coerceLong(right).longValue();
081:
082: return leftLong <= rightLong ? Boolean.TRUE : Boolean.FALSE;
083: } else if (left instanceof String || right instanceof String) {
084: String leftString = left.toString();
085: String rightString = right.toString();
086:
087: return leftString.compareTo(rightString) <= 0 ? Boolean.TRUE
088: : Boolean.FALSE;
089: } else if (left instanceof Comparable) {
090: return ((Comparable) left).compareTo(right) <= 0 ? Boolean.TRUE
091: : Boolean.FALSE;
092: } else if (right instanceof Comparable) {
093: return ((Comparable) right).compareTo(left) >= 0 ? Boolean.TRUE
094: : Boolean.FALSE;
095: }
096:
097: throw new Exception("Invalid comparison : LE ");
098: }
099:
100: }
|