001: package org.apache.velocity.runtime.parser.node;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import org.apache.velocity.runtime.parser.Parser;
023: import org.apache.velocity.runtime.parser.ParserVisitor;
024: import org.apache.velocity.context.InternalContextAdapter;
025:
026: import org.apache.velocity.exception.MethodInvocationException;
027:
028: /**
029: * Please look at the Parser.jjt file which is
030: * what controls the generation of this class.
031: *
032: * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
033: * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
034: * @version $Id: ASTOrNode.java 463298 2006-10-12 16:10:32Z henning $
035: */
036: public class ASTOrNode extends SimpleNode {
037: /**
038: * @param id
039: */
040: public ASTOrNode(int id) {
041: super (id);
042: }
043:
044: /**
045: * @param p
046: * @param id
047: */
048: public ASTOrNode(Parser p, int id) {
049: super (p, id);
050: }
051:
052: /**
053: * @see org.apache.velocity.runtime.parser.node.SimpleNode#jjtAccept(org.apache.velocity.runtime.parser.ParserVisitor, java.lang.Object)
054: */
055: public Object jjtAccept(ParserVisitor visitor, Object data) {
056: return visitor.visit(this , data);
057: }
058:
059: /**
060: * Returns the value of the expression.
061: * Since the value of the expression is simply the boolean
062: * result of evaluate(), lets return that.
063: * @param context
064: * @return The Expression value.
065: * @throws MethodInvocationException
066: */
067: public Object value(InternalContextAdapter context)
068: throws MethodInvocationException {
069: // TODO: JDK 1.4+ -> valueOf()
070: return new Boolean(evaluate(context));
071: }
072:
073: /**
074: * the logical or :
075: * the rule :
076: * left || null -> left
077: * null || right -> right
078: * null || null -> false
079: * left || right -> left || right
080: * @param context
081: * @return The evaluation result.
082: * @throws MethodInvocationException
083: */
084: public boolean evaluate(InternalContextAdapter context)
085: throws MethodInvocationException {
086: Node left = jjtGetChild(0);
087: Node right = jjtGetChild(1);
088:
089: /*
090: * if the left is not null and true, then true
091: */
092:
093: if (left != null && left.evaluate(context))
094: return true;
095:
096: /*
097: * same for right
098: */
099:
100: if (right != null && right.evaluate(context))
101: return true;
102:
103: return false;
104: }
105: }
|