| 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 | public class ASTNENode extends SimpleNode |
| 25 | { |
| 26 | public ASTNENode(int id) |
| 27 | { |
| 28 | super(id); |
| 29 | } |
| 30 | |
| 31 | public ASTNENode(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 | Object left = jjtGetChild(0).value( context ); |
| 46 | Object right = jjtGetChild(1).value( context ); |
| 47 | |
| 48 | /* |
| 49 | * null check |
| 50 | */ |
| 51 | |
| 52 | if ( left == null || right == null) |
| 53 | { |
| 54 | rsvc.error( ( left == null ? "Left" : "Right" ) + " side (" |
| 55 | + jjtGetChild( (left == null? 0 : 1) ).literal() |
| 56 | + ") of '!=' operation has null value." |
| 57 | + " Operation not possible. " |
| 58 | + context.getCurrentTemplateName() + " [line " + getLine() |
| 59 | + ", column " + getColumn() + "]"); |
| 60 | return false; |
| 61 | |
| 62 | } |
| 63 | |
| 64 | |
| 65 | /* |
| 66 | * check to see if they are the same class. I don't think this is slower |
| 67 | * as I don't think that getClass() results in object creation, and we can |
| 68 | * extend == to handle all classes |
| 69 | */ |
| 70 | |
| 71 | if (left.getClass().equals( right.getClass() ) ) |
| 72 | { |
| 73 | return !(left.equals( right )); |
| 74 | } |
| 75 | else |
| 76 | { |
| 77 | rsvc.error("Error in evaluation of != expression." |
| 78 | + " Both arguments must be of the same Class." |
| 79 | + " Currently left = " + left.getClass() + ", right = " |
| 80 | + right.getClass() + ". " |
| 81 | + context.getCurrentTemplateName() + " [line " + getLine() |
| 82 | + ", column " + getColumn() + "] (ASTEQNode)"); |
| 83 | |
| 84 | return false; |
| 85 | } |
| 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 | } |