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.runtime.parser.Parser;
24  import org.apache.velocity.runtime.parser.ParserVisitor;
25  import org.apache.velocity.exception.MethodInvocationException;
26  
27  /**
28   * Please look at the Parser.jjt file which is
29   * what controls the generation of this class.
30   *
31   * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
32   * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
33   * @version $Id: ASTAndNode.java 463298 2006-10-12 16:10:32Z henning $
34   */
35  public class ASTAndNode extends SimpleNode
36  {
37      /**
38       * @param id
39       */
40      public ASTAndNode(int id)
41      {
42          super(id);
43      }
44  
45      /**
46       * @param p
47       * @param id
48       */
49      public ASTAndNode(Parser p, int id)
50      {
51          super(p, id);
52      }
53  
54      /**
55       * @see org.apache.velocity.runtime.parser.node.SimpleNode#jjtAccept(org.apache.velocity.runtime.parser.ParserVisitor, java.lang.Object)
56       */
57      public Object jjtAccept(ParserVisitor visitor, Object data)
58      {
59          return visitor.visit(this, data);
60      }
61  
62      /**
63       *  Returns the value of the expression.
64       *  Since the value of the expression is simply the boolean
65       *  result of evaluate(), lets return that.
66       * @param context
67       * @return The value of the expression.
68       * @throws MethodInvocationException
69       */
70      public Object value(InternalContextAdapter context)
71          throws MethodInvocationException
72      {
73          // TODO: JDK 1.4+ -> valueOf()
74          return new Boolean(evaluate(context));
75      }
76  
77      /**
78       * logical and :
79       *   null && right = false
80       *   left && null = false
81       *   null && null = false
82       * @param context
83       * @return True if both sides are true.
84       * @throws MethodInvocationException
85       */
86      public boolean evaluate( InternalContextAdapter context)
87          throws MethodInvocationException
88      {
89          Node left = jjtGetChild(0);
90          Node right = jjtGetChild(1);
91  
92          /*
93           *  if either is null, lets log and bail
94           */
95  
96          if (left == null || right == null)
97          {
98              log.error((left == null ? "Left" : "Right") + " side of '&&' operation is null."
99                             + " Operation not possible. "
100                            + context.getCurrentTemplateName() + " [line " + getLine()
101                            + ", column " + getColumn() + "]");
102             return false;
103         }
104 
105         /*
106          *  short circuit the test.  Don't eval the RHS if the LHS is false
107          */
108 
109         if( left.evaluate( context ) )
110         {
111             if ( right.evaluate( context ) )
112             {
113                 return true;
114             }
115         }
116 
117         return false;
118     }
119 }
120