001: package org.apache.velocity.runtime.parser.node;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: /**
023: * Handles number addition of nodes.<br><br>
024: *
025: * Please look at the Parser.jjt file which is
026: * what controls the generation of this class.
027: *
028: * @author <a href="mailto:wglass@forio.com">Will Glass-Husain</a>
029: * @author <a href="mailto:pero@antaramusic.de">Peter Romianowski</a>
030: * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
031: * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
032: * @version $Id: ASTAddNode.java 463298 2006-10-12 16:10:32Z henning $
033: */
034: import org.apache.velocity.context.InternalContextAdapter;
035: import org.apache.velocity.runtime.parser.Parser;
036: import org.apache.velocity.runtime.parser.ParserVisitor;
037:
038: import org.apache.velocity.exception.MethodInvocationException;
039:
040: import org.apache.velocity.util.TemplateNumber;
041:
042: /**
043: *
044: */
045: public class ASTAddNode extends SimpleNode {
046: /**
047: * @param id
048: */
049: public ASTAddNode(int id) {
050: super (id);
051: }
052:
053: /**
054: * @param p
055: * @param id
056: */
057: public ASTAddNode(Parser p, int id) {
058: super (p, id);
059: }
060:
061: /**
062: * @see org.apache.velocity.runtime.parser.node.SimpleNode#jjtAccept(org.apache.velocity.runtime.parser.ParserVisitor, java.lang.Object)
063: */
064: public Object jjtAccept(ParserVisitor visitor, Object data) {
065: return visitor.visit(this , data);
066: }
067:
068: /**
069: * computes the sum of the two nodes.
070: * @param context
071: * @return result or null
072: * @throws MethodInvocationException
073: */
074: public Object value(InternalContextAdapter context)
075: throws MethodInvocationException {
076: /*
077: * get the two addends
078: */
079:
080: Object left = jjtGetChild(0).value(context);
081: Object right = jjtGetChild(1).value(context);
082:
083: /*
084: * if either is null, lets log and bail
085: */
086:
087: if (left == null || right == null) {
088: log.error((left == null ? "Left" : "Right") + " side ("
089: + jjtGetChild((left == null ? 0 : 1)).literal()
090: + ") of addition operation has null value."
091: + " Operation not possible. "
092: + context.getCurrentTemplateName() + " [line "
093: + getLine() + ", column " + getColumn() + "]");
094: return null;
095: }
096:
097: /*
098: * convert to Number if applicable
099: */
100: if (left instanceof TemplateNumber) {
101: left = ((TemplateNumber) left).getAsNumber();
102: }
103: if (right instanceof TemplateNumber) {
104: right = ((TemplateNumber) right).getAsNumber();
105: }
106:
107: /*
108: * Arithmetic operation.
109: */
110: if (left instanceof Number && right instanceof Number) {
111: return MathUtils.add((Number) left, (Number) right);
112: }
113:
114: /*
115: * shall we try for strings?
116: */
117: if (left instanceof String || right instanceof String) {
118: return left.toString().concat(right.toString());
119: }
120: /*
121: * if not a Number or Strings, not much we can do right now
122: */
123: log
124: .error((!(left instanceof Number || left instanceof String) ? "Left"
125: : "Right")
126: + " side of addition operation is not a valid type. "
127: + "Currently only Strings, numbers (1,2,3...) and Number type are supported. "
128: + context.getCurrentTemplateName()
129: + " [line "
130: + getLine() + ", column " + getColumn() + "]");
131:
132: return null;
133: }
134: }
|