| 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 | import org.apache.velocity.exception.MethodInvocationException; |
| 22 | |
| 23 | |
| 24 | public class ASTGTNode extends SimpleNode |
| 25 | { |
| 26 | public ASTGTNode(int id) |
| 27 | { |
| 28 | super(id); |
| 29 | } |
| 30 | |
| 31 | public ASTGTNode(Parser p, int id) |
| 32 | { |
| 33 | super(p, id); |
| 34 | } |
| 35 | |
| 36 | /** Accept the visitor. **/ |
| 37 | public Object jjtAccept(ParserVisitor visitor, Object data) |
| 38 | { |
| 39 | return visitor.visit(this, data); |
| 40 | } |
| 41 | |
| 42 | public boolean evaluate(InternalContextAdapter context) |
| 43 | throws MethodInvocationException |
| 44 | { |
| 45 | /* |
| 46 | * get the two args |
| 47 | */ |
| 48 | |
| 49 | Object left = jjtGetChild(0).value( context ); |
| 50 | Object right = jjtGetChild(1).value( context ); |
| 51 | |
| 52 | /* |
| 53 | * if either is null, lets log and bail |
| 54 | */ |
| 55 | |
| 56 | if (left == null || right == null) |
| 57 | { |
| 58 | rsvc.error( ( left == null ? "Left" : "Right" ) |
| 59 | + " side (" |
| 60 | + jjtGetChild( (left == null? 0 : 1) ).literal() |
| 61 | + ") of '>' operation has null value." |
| 62 | + " Operation not possible. " |
| 63 | + context.getCurrentTemplateName() + " [line " |
| 64 | + getLine() |
| 65 | + ", column " + getColumn() + "]"); |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | /* |
| 70 | * if not an Integer, not much we can do either |
| 71 | */ |
| 72 | |
| 73 | if ( !( left instanceof Integer ) || !( right instanceof Integer )) |
| 74 | { |
| 75 | rsvc.error( ( !( left instanceof Integer ) ? "Left" : "Right" ) |
| 76 | + " side of '>' operation is not a valid type. " |
| 77 | + " It is a " + ( !( left instanceof Integer ) ? left.getClass() : right.getClass() ) |
| 78 | + ". Currently only integers (1,2,3...) and Integer type is supported. " |
| 79 | + context.getCurrentTemplateName() + " [line " + getLine() |
| 80 | + ", column " + getColumn() + "]"); |
| 81 | |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | return ((Integer) left).intValue() > ((Integer) right).intValue(); |
| 86 | } |
| 87 | |
| 88 | public Object value(InternalContextAdapter context) |
| 89 | throws MethodInvocationException |
| 90 | { |
| 91 | boolean val = evaluate(context); |
| 92 | |
| 93 | return val ? Boolean.TRUE : Boolean.FALSE; |
| 94 | } |
| 95 | |
| 96 | } |