| 1 | package org.apache.velocity.runtime.parser.node; |
| 2 | |
| 3 | /* |
| 4 | * Copyright 2000-2001,2004 The Apache Software Foundation. |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | import org.apache.velocity.context.InternalContextAdapter; |
| 20 | import org.apache.velocity.runtime.parser.Parser; |
| 21 | |
| 22 | import org.apache.velocity.exception.MethodInvocationException; |
| 23 | |
| 24 | /** |
| 25 | * Handles integer subtraction of nodes (in #set() ) |
| 26 | * |
| 27 | * Please look at the Parser.jjt file which is |
| 28 | * what controls the generation of this class. |
| 29 | * |
| 30 | * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a> |
| 31 | * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a> |
| 32 | * @version $Id: ASTSubtractNode.java,v 1.7.8.1 2004/03/03 23:22:59 geirm Exp $ |
| 33 | */ |
| 34 | public class ASTSubtractNode extends SimpleNode |
| 35 | { |
| 36 | public ASTSubtractNode(int id) |
| 37 | { |
| 38 | super(id); |
| 39 | } |
| 40 | |
| 41 | public ASTSubtractNode(Parser p, int id) |
| 42 | { |
| 43 | super(p, id); |
| 44 | } |
| 45 | |
| 46 | /** Accept the visitor. **/ |
| 47 | public Object jjtAccept(ParserVisitor visitor, Object data) |
| 48 | { |
| 49 | return visitor.visit(this, data); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * computes the value of the subtraction. Currently |
| 54 | * limited to integers |
| 55 | * @return Integer(value) or null |
| 56 | */ |
| 57 | public Object value( InternalContextAdapter context) |
| 58 | throws MethodInvocationException |
| 59 | { |
| 60 | /* |
| 61 | * get the two args |
| 62 | */ |
| 63 | |
| 64 | Object left = jjtGetChild(0).value( context ); |
| 65 | Object right = jjtGetChild(1).value( context ); |
| 66 | |
| 67 | /* |
| 68 | * if either is null, lets log and bail |
| 69 | */ |
| 70 | |
| 71 | if (left == null || right == null) |
| 72 | { |
| 73 | rsvc.error( ( left == null ? "Left" : "Right" ) + " side (" |
| 74 | + jjtGetChild( (left == null? 0 : 1) ).literal() |
| 75 | + ") of subtraction operation has null value." |
| 76 | + " Operation not possible. " |
| 77 | + context.getCurrentTemplateName() + " [line " + getLine() |
| 78 | + ", column " + getColumn() + "]"); |
| 79 | return null; |
| 80 | } |
| 81 | |
| 82 | /* |
| 83 | * if not an Integer, not much we can do either |
| 84 | */ |
| 85 | |
| 86 | if ( !( left instanceof Integer ) || !( right instanceof Integer )) |
| 87 | { |
| 88 | rsvc.error( ( !( left instanceof Integer ) ? "Left" : "Right" ) |
| 89 | + " side of subtraction operation is not a valid type. " |
| 90 | + "Currently only integers (1,2,3...) and Integer type is supported. " |
| 91 | + context.getCurrentTemplateName() + " [line " + getLine() |
| 92 | + ", column " + getColumn() + "]"); |
| 93 | |
| 94 | return null; |
| 95 | } |
| 96 | |
| 97 | return new Integer( ( (Integer) left ).intValue() - ( (Integer) right ).intValue() ); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | |
| 102 | |