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 687184 2008-08-19 22:16:39Z nbubna $
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          return evaluate(context) ? Boolean.TRUE : Boolean.FALSE;
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           * null == false
94           */
95          if (left == null || right == null)
96          {
97              return false;
98          }
99  
100         /*
101          *  short circuit the test.  Don't eval the RHS if the LHS is false
102          */
103 
104         if( left.evaluate( context ) )
105         {
106             if ( right.evaluate( context ) )
107             {
108                 return true;
109             }
110         }
111 
112         return false;
113     }
114 }
115