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:
017: package org.apache.commons.jexl.parser;
018:
019: import org.apache.commons.jexl.JexlContext;
020: import org.apache.commons.jexl.util.Coercion;
021:
022: /**
023: * Addition : either integer addition or string concatenation.
024: *
025: * @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
026: * @version $Id: ASTAddNode.java 398180 2006-04-29 15:40:35Z dion $
027: */
028: public class ASTAddNode extends SimpleNode {
029: /**
030: * Create the node given an id.
031: *
032: * @param id node id.
033: */
034: public ASTAddNode(int id) {
035: super (id);
036: }
037:
038: /**
039: * Create a node with the given parser and id.
040: *
041: * @param p a parser.
042: * @param id node id.
043: */
044: public ASTAddNode(Parser p, int id) {
045: super (p, id);
046: }
047:
048: /**
049: * {@inheritDoc}
050: */
051: public Object jjtAccept(ParserVisitor visitor, Object data) {
052: return visitor.visit(this , data);
053: }
054:
055: /**
056: * {@inheritDoc}
057: */
058: public Object value(JexlContext context) throws Exception {
059: Object left = ((SimpleNode) jjtGetChild(0)).value(context);
060: Object right = ((SimpleNode) jjtGetChild(1)).value(context);
061:
062: /*
063: * the spec says 'and'
064: */
065: if (left == null && right == null) {
066: return new Long(0);
067: }
068:
069: /*
070: * if anything is float, double or string with ( "." | "E" | "e")
071: * coerce all to doubles and do it
072: */
073: if (left instanceof Float
074: || left instanceof Double
075: || right instanceof Float
076: || right instanceof Double
077: || (left instanceof String && (((String) left)
078: .indexOf(".") != -1
079: || ((String) left).indexOf("e") != -1 || ((String) left)
080: .indexOf("E") != -1))
081: || (right instanceof String && (((String) right)
082: .indexOf(".") != -1
083: || ((String) right).indexOf("e") != -1 || ((String) right)
084: .indexOf("E") != -1))) {
085:
086: /*
087: * in the event that either is null and not both, then just make the
088: * null a 0
089: */
090:
091: try {
092: Double l = Coercion.coerceDouble(left);
093: Double r = Coercion.coerceDouble(right);
094: return new Double(l.doubleValue() + r.doubleValue());
095: } catch (java.lang.NumberFormatException nfe) {
096: /*
097: * Well, use strings!
098: */
099: return left.toString().concat(right.toString());
100: }
101: }
102:
103: /*
104: * attempt to use Longs
105: */
106: try {
107: Long l = Coercion.coerceLong(left);
108: Long r = Coercion.coerceLong(right);
109: return new Long(l.longValue() + r.longValue());
110: } catch (java.lang.NumberFormatException nfe) {
111: /*
112: * Well, use strings!
113: */
114: return left.toString().concat(right.toString());
115: }
116: }
117: }
|