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 517553 2007-03-13 06:09:58Z wglass $ 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 41 42 /** 43 * 44 */ 45 public class ASTIfStatement extends SimpleNode 46 { 47 /** 48 * @param id 49 */ 50 public ASTIfStatement(int id) 51 { 52 super(id); 53 } 54 55 /** 56 * @param p 57 * @param id 58 */ 59 public ASTIfStatement(Parser p, int id) 60 { 61 super(p, id); 62 } 63 64 65 /** 66 * @see org.apache.velocity.runtime.parser.node.SimpleNode#jjtAccept(org.apache.velocity.runtime.parser.node.ParserVisitor, java.lang.Object) 67 */ 68 public Object jjtAccept(ParserVisitor visitor, Object data) 69 { 70 return visitor.visit(this, data); 71 } 72 73 /** 74 * @see org.apache.velocity.runtime.parser.node.SimpleNode#render(org.apache.velocity.context.InternalContextAdapter, java.io.Writer) 75 */ 76 public boolean render( InternalContextAdapter context, Writer writer) 77 throws IOException,MethodInvocationException, 78 ResourceNotFoundException, ParseErrorException 79 { 80 /* 81 * Check if the #if(expression) construct evaluates to true: 82 * if so render and leave immediately because there 83 * is nothing left to do! 84 */ 85 if (jjtGetChild(0).evaluate(context)) 86 { 87 jjtGetChild(1).render(context, writer); 88 return true; 89 } 90 91 int totalNodes = jjtGetNumChildren(); 92 93 /* 94 * Now check the remaining nodes left in the 95 * if construct. The nodes are either elseif 96 * nodes or else nodes. Each of these node 97 * types knows how to evaluate themselves. If 98 * a node evaluates to true then the node will 99 * render itself and this method will return 100 * as there is nothing left to do. 101 */ 102 for (int i = 2; i < totalNodes; i++) 103 { 104 if (jjtGetChild(i).evaluate(context)) 105 { 106 jjtGetChild(i).render(context, writer); 107 return true; 108 } 109 } 110 111 /* 112 * This is reached when an ASTIfStatement 113 * consists of an if/elseif sequence where 114 * none of the nodes evaluate to true. 115 */ 116 return true; 117 } 118 119 /** 120 * @param context 121 * @param visitor 122 */ 123 public void process( InternalContextAdapter context, ParserVisitor visitor) 124 { 125 } 126 } 127 128 129 130 131 132