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: /**
023: * Please look at the Parser.jjt file which is
024: * what controls the generation of this class.
025: *
026: * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
027: * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
028: * @version $Id: ASTIfStatement.java 463298 2006-10-12 16:10:32Z henning $
029: */
030:
031: import java.io.IOException;
032: import java.io.Writer;
033:
034: import org.apache.velocity.context.InternalContextAdapter;
035: import org.apache.velocity.exception.MethodInvocationException;
036: import org.apache.velocity.exception.ParseErrorException;
037: import org.apache.velocity.exception.ResourceNotFoundException;
038: import org.apache.velocity.runtime.parser.Parser;
039: import org.apache.velocity.runtime.parser.ParserVisitor;
040:
041: /**
042: *
043: */
044: public class ASTIfStatement extends SimpleNode {
045: /**
046: * @param id
047: */
048: public ASTIfStatement(int id) {
049: super (id);
050: }
051:
052: /**
053: * @param p
054: * @param id
055: */
056: public ASTIfStatement(Parser p, int id) {
057: super (p, id);
058: }
059:
060: /**
061: * @see org.apache.velocity.runtime.parser.node.SimpleNode#jjtAccept(org.apache.velocity.runtime.parser.ParserVisitor, java.lang.Object)
062: */
063: public Object jjtAccept(ParserVisitor visitor, Object data) {
064: return visitor.visit(this , data);
065: }
066:
067: /**
068: * @see org.apache.velocity.runtime.parser.node.SimpleNode#render(org.apache.velocity.context.InternalContextAdapter, java.io.Writer)
069: */
070: public boolean render(InternalContextAdapter context, Writer writer)
071: throws IOException, MethodInvocationException,
072: ResourceNotFoundException, ParseErrorException {
073: /*
074: * Check if the #if(expression) construct evaluates to true:
075: * if so render and leave immediately because there
076: * is nothing left to do!
077: */
078: if (jjtGetChild(0).evaluate(context)) {
079: jjtGetChild(1).render(context, writer);
080: return true;
081: }
082:
083: int totalNodes = jjtGetNumChildren();
084:
085: /*
086: * Now check the remaining nodes left in the
087: * if construct. The nodes are either elseif
088: * nodes or else nodes. Each of these node
089: * types knows how to evaluate themselves. If
090: * a node evaluates to true then the node will
091: * render itself and this method will return
092: * as there is nothing left to do.
093: */
094: for (int i = 2; i < totalNodes; i++) {
095: if (jjtGetChild(i).evaluate(context)) {
096: jjtGetChild(i).render(context, writer);
097: return true;
098: }
099: }
100:
101: /*
102: * This is reached when an ASTIfStatement
103: * consists of an if/elseif sequence where
104: * none of the nodes evaluate to true.
105: */
106: return true;
107: }
108:
109: /**
110: * @param context
111: * @param visitor
112: */
113: public void process(InternalContextAdapter context,
114: ParserVisitor visitor) {
115: }
116: }
|