View Javadoc

1   package org.apache.velocity.runtime.parser.node;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.    
20   */
21  
22  import org.apache.velocity.context.InternalContextAdapter;
23  import org.apache.velocity.exception.MethodInvocationException;
24  import org.apache.velocity.runtime.parser.Parser;
25  
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: ASTAndNode.java 517553 2007-03-13 06:09:58Z wglass $
33   */
34  public class ASTAndNode extends SimpleNode
35  {
36      /**
37       * @param id
38       */
39      public ASTAndNode(int id)
40      {
41          super(id);
42      }
43  
44      /**
45       * @param p
46       * @param id
47       */
48      public ASTAndNode(Parser p, int id)
49      {
50          super(p, id);
51      }
52  
53      /**
54       * @see org.apache.velocity.runtime.parser.node.SimpleNode#jjtAccept(org.apache.velocity.runtime.parser.node.ParserVisitor, java.lang.Object)
55       */
56      public Object jjtAccept(ParserVisitor visitor, Object data)
57      {
58          return visitor.visit(this, data);
59      }
60  
61      /**
62       *  Returns the value of the expression.
63       *  Since the value of the expression is simply the boolean
64       *  result of evaluate(), lets return that.
65       * @param context
66       * @return The value of the expression.
67       * @throws MethodInvocationException
68       */
69      public Object value(InternalContextAdapter context)
70          throws MethodInvocationException
71      {
72          // TODO: JDK 1.4+ -> valueOf()
73          return new Boolean(evaluate(context));
74      }
75  
76      /**
77       * logical and :
78       *   null && right = false
79       *   left && null = false
80       *   null && null = false
81       * @param context
82       * @return True if both sides are true.
83       * @throws MethodInvocationException
84       */
85      public boolean evaluate( InternalContextAdapter context)
86          throws MethodInvocationException
87      {
88          Node left = jjtGetChild(0);
89          Node right = jjtGetChild(1);
90  
91          /*
92           *  if either is null, lets log and bail
93           */
94  
95          if (left == null || right == null)
96          {
97              log.error((left == null ? "Left" : "Right") + " side of '&&' operation is null."
98                             + " Operation not possible. "
99                             + context.getCurrentTemplateName() + " [line " + getLine()
100                            + ", column " + getColumn() + "]");
101             return false;
102         }
103 
104         /*
105          *  short circuit the test.  Don't eval the RHS if the LHS is false
106          */
107 
108         if( left.evaluate( context ) )
109         {
110             if ( right.evaluate( context ) )
111             {
112                 return true;
113             }
114         }
115 
116         return false;
117     }
118 }
119