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: ASTOrNode.java 685370 2008-08-12 23:36:35Z nbubna $
33 */
34 public class ASTOrNode extends SimpleNode
35 {
36 /**
37 * @param id
38 */
39 public ASTOrNode(int id)
40 {
41 super(id);
42 }
43
44 /**
45 * @param p
46 * @param id
47 */
48 public ASTOrNode(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 Expression value.
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 * the logical or :
79 * the rule :
80 * left || null -> left
81 * null || right -> right
82 * null || null -> false
83 * left || right -> left || right
84 * @param context
85 * @return The evaluation result.
86 * @throws MethodInvocationException
87 */
88 public boolean evaluate( InternalContextAdapter context)
89 throws MethodInvocationException
90 {
91 Node left = jjtGetChild(0);
92 Node right = jjtGetChild(1);
93
94 /*
95 * if the left is not null and true, then true
96 */
97
98 if (left != null && left.evaluate( context ) )
99 return true;
100
101 /*
102 * same for right
103 */
104
105 if ( right != null && right.evaluate( context ) )
106 return true;
107
108 return false;
109 }
110 }
111
112
113
114
115