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 /** 23 * Please look at the Parser.jjt file which is 24 * what controls the generation of this class. 25 * 26 * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a> 27 * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a> 28 * @version $Id: ASTIfStatement.java 463298 2006-10-12 16:10:32Z henning $ 29 */ 30 31 32 import java.io.IOException; 33 import java.io.Writer; 34 35 import org.apache.velocity.context.InternalContextAdapter; 36 import org.apache.velocity.exception.MethodInvocationException; 37 import org.apache.velocity.exception.ParseErrorException; 38 import org.apache.velocity.exception.ResourceNotFoundException; 39 import org.apache.velocity.runtime.parser.Parser; 40 import org.apache.velocity.runtime.parser.ParserVisitor; 41 42 43 /** 44 * 45 */ 46 public class ASTIfStatement extends SimpleNode 47 { 48 /** 49 * @param id 50 */ 51 public ASTIfStatement(int id) 52 { 53 super(id); 54 } 55 56 /** 57 * @param p 58 * @param id 59 */ 60 public ASTIfStatement(Parser p, int id) 61 { 62 super(p, id); 63 } 64 65 66 /** 67 * @see org.apache.velocity.runtime.parser.node.SimpleNode#jjtAccept(org.apache.velocity.runtime.parser.ParserVisitor, java.lang.Object) 68 */ 69 public Object jjtAccept(ParserVisitor visitor, Object data) 70 { 71 return visitor.visit(this, data); 72 } 73 74 /** 75 * @see org.apache.velocity.runtime.parser.node.SimpleNode#render(org.apache.velocity.context.InternalContextAdapter, java.io.Writer) 76 */ 77 public boolean render( InternalContextAdapter context, Writer writer) 78 throws IOException,MethodInvocationException, 79 ResourceNotFoundException, ParseErrorException 80 { 81 /* 82 * Check if the #if(expression) construct evaluates to true: 83 * if so render and leave immediately because there 84 * is nothing left to do! 85 */ 86 if (jjtGetChild(0).evaluate(context)) 87 { 88 jjtGetChild(1).render(context, writer); 89 return true; 90 } 91 92 int totalNodes = jjtGetNumChildren(); 93 94 /* 95 * Now check the remaining nodes left in the 96 * if construct. The nodes are either elseif 97 * nodes or else nodes. Each of these node 98 * types knows how to evaluate themselves. If 99 * a node evaluates to true then the node will 100 * render itself and this method will return 101 * as there is nothing left to do. 102 */ 103 for (int i = 2; i < totalNodes; i++) 104 { 105 if (jjtGetChild(i).evaluate(context)) 106 { 107 jjtGetChild(i).render(context, writer); 108 return true; 109 } 110 } 111 112 /* 113 * This is reached when an ASTIfStatement 114 * consists of an if/elseif sequence where 115 * none of the nodes evaluate to true. 116 */ 117 return true; 118 } 119 120 /** 121 * @param context 122 * @param visitor 123 */ 124 public void process( InternalContextAdapter context, ParserVisitor visitor) 125 { 126 } 127 } 128 129 130 131 132 133