| 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 | * Please look at the Parser.jjt file which is |
| 25 | * what controls the generation of this class. |
| 26 | * |
| 27 | * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a> |
| 28 | * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a> |
| 29 | * @version $Id: ASTAndNode.java,v 1.8.8.1 2004/03/03 23:22:58 geirm Exp $ |
| 30 | */ |
| 31 | public class ASTAndNode extends SimpleNode |
| 32 | { |
| 33 | public ASTAndNode(int id) |
| 34 | { |
| 35 | super(id); |
| 36 | } |
| 37 | |
| 38 | public ASTAndNode(Parser p, int id) |
| 39 | { |
| 40 | super(p, id); |
| 41 | } |
| 42 | |
| 43 | /** Accept the visitor. **/ |
| 44 | public Object jjtAccept(ParserVisitor visitor, Object data) |
| 45 | { |
| 46 | return visitor.visit(this, data); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Returns the value of the expression. |
| 51 | * Since the value of the expression is simply the boolean |
| 52 | * result of evaluate(), lets return that. |
| 53 | */ |
| 54 | public Object value(InternalContextAdapter context ) |
| 55 | throws MethodInvocationException |
| 56 | { |
| 57 | return new Boolean( evaluate( context ) ); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * logical and : |
| 62 | * null && right = false |
| 63 | * left && null = false |
| 64 | * null && null = false |
| 65 | */ |
| 66 | public boolean evaluate( InternalContextAdapter context) |
| 67 | throws MethodInvocationException |
| 68 | { |
| 69 | Node left = jjtGetChild(0); |
| 70 | Node right = jjtGetChild(1); |
| 71 | |
| 72 | /* |
| 73 | * if either is null, lets log and bail |
| 74 | */ |
| 75 | |
| 76 | if (left == null || right == null) |
| 77 | { |
| 78 | rsvc.error( ( left == null ? "Left" : "Right" ) + " side of '&&' operation is null." |
| 79 | + " Operation not possible. " |
| 80 | + context.getCurrentTemplateName() + " [line " + getLine() |
| 81 | + ", column " + getColumn() + "]"); |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | /* |
| 86 | * short circuit the test. Don't eval the RHS if the LHS is false |
| 87 | */ |
| 88 | |
| 89 | if( left.evaluate( context ) ) |
| 90 | { |
| 91 | if ( right.evaluate( context ) ) |
| 92 | { |
| 93 | return true; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | return false; |
| 98 | } |
| 99 | } |
| 100 | |