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: import org.apache.velocity.context.InternalContextAdapter;
023: import org.apache.velocity.exception.MethodInvocationException;
024: import org.apache.velocity.runtime.parser.Parser;
025: import org.apache.velocity.runtime.parser.ParserVisitor;
026: import org.apache.velocity.util.TemplateNumber;
027:
028: /**
029: * Handles subtraction of nodes (in #set() )<br><br>
030: *
031: * Please look at the Parser.jjt file which is
032: * what controls the generation of this class.
033: *
034: * @author <a href="mailto:wglass@forio.com">Will Glass-Husain</a>
035: * @author <a href="mailto:pero@antaramusic.de">Peter Romianowski</a>
036: * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
037: * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
038: * @version $Id: ASTSubtractNode.java 463298 2006-10-12 16:10:32Z henning $
039: */
040: public class ASTSubtractNode extends SimpleNode {
041: /**
042: * @param id
043: */
044: public ASTSubtractNode(int id) {
045: super (id);
046: }
047:
048: /**
049: * @param p
050: * @param id
051: */
052: public ASTSubtractNode(Parser p, int id) {
053: super (p, id);
054: }
055:
056: /**
057: * @see org.apache.velocity.runtime.parser.node.SimpleNode#jjtAccept(org.apache.velocity.runtime.parser.ParserVisitor, java.lang.Object)
058: */
059: public Object jjtAccept(ParserVisitor visitor, Object data) {
060: return visitor.visit(this , data);
061: }
062:
063: /**
064: * computes the value of the subtraction.
065: * @param context
066: * @return result or null
067: * @throws MethodInvocationException
068: */
069: public Object value(InternalContextAdapter context)
070: throws MethodInvocationException {
071: /*
072: * get the two args
073: */
074:
075: Object left = jjtGetChild(0).value(context);
076: Object right = jjtGetChild(1).value(context);
077:
078: /*
079: * if either is null, lets log and bail
080: */
081:
082: if (left == null || right == null) {
083: log.error((left == null ? "Left" : "Right") + " side ("
084: + jjtGetChild((left == null ? 0 : 1)).literal()
085: + ") of subtraction operation has null value."
086: + " Operation not possible. "
087: + context.getCurrentTemplateName() + " [line "
088: + getLine() + ", column " + getColumn() + "]");
089: return null;
090: }
091:
092: /*
093: * convert to Number if applicable
094: */
095: if (left instanceof TemplateNumber) {
096: left = ((TemplateNumber) left).getAsNumber();
097: }
098: if (right instanceof TemplateNumber) {
099: right = ((TemplateNumber) right).getAsNumber();
100: }
101:
102: /*
103: * if not a Number, not much we can do either
104: */
105: if (!(left instanceof Number) || !(right instanceof Number)) {
106: log
107: .error((!(left instanceof Number) ? "Left"
108: : "Right")
109: + " side of subtraction operation is not a Number. "
110: + context.getCurrentTemplateName()
111: + " [line "
112: + getLine()
113: + ", column "
114: + getColumn() + "]");
115: return null;
116: }
117:
118: return MathUtils.subtract((Number) left, (Number) right);
119: }
120: }
|