1 | package org.apache.velocity.runtime.parser.node; |
2 | |
3 | /* |
4 | * Copyright 2000-2001,2004 The Apache Software Foundation. |
5 | * |
6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
7 | * you may not use this file except in compliance with the License. |
8 | * You may obtain a copy of the License at |
9 | * |
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
11 | * |
12 | * Unless required by applicable law or agreed to in writing, software |
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 | * See the License for the specific language governing permissions and |
16 | * limitations under the License. |
17 | */ |
18 | |
19 | |
20 | /** |
21 | * Please look at the Parser.jjt file which is |
22 | * what controls the generation of this class. |
23 | * |
24 | * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a> |
25 | * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a> |
26 | * @version $Id: ASTIfStatement.java,v 1.9.8.1 2004/03/03 23:22:58 geirm Exp $ |
27 | */ |
28 | |
29 | |
30 | import java.io.Writer; |
31 | import java.io.IOException; |
32 | |
33 | import org.apache.velocity.context.InternalContextAdapter; |
34 | import org.apache.velocity.runtime.parser.*; |
35 | |
36 | import org.apache.velocity.exception.MethodInvocationException; |
37 | import org.apache.velocity.exception.ParseErrorException; |
38 | import org.apache.velocity.exception.ResourceNotFoundException; |
39 | |
40 | |
41 | public class ASTIfStatement extends SimpleNode |
42 | { |
43 | public ASTIfStatement(int id) |
44 | { |
45 | super(id); |
46 | } |
47 | |
48 | public ASTIfStatement(Parser p, int id) |
49 | { |
50 | super(p, id); |
51 | } |
52 | |
53 | /** Accept the visitor. **/ |
54 | public Object jjtAccept(ParserVisitor visitor, Object data) |
55 | { |
56 | return visitor.visit(this, data); |
57 | } |
58 | |
59 | public boolean render( InternalContextAdapter context, Writer writer) |
60 | throws IOException,MethodInvocationException, |
61 | ResourceNotFoundException, ParseErrorException |
62 | { |
63 | /* |
64 | * Check if the #if(expression) construct evaluates to true: |
65 | * if so render and leave immediately because there |
66 | * is nothing left to do! |
67 | */ |
68 | if (jjtGetChild(0).evaluate(context)) |
69 | { |
70 | jjtGetChild(1).render(context, writer); |
71 | return true; |
72 | } |
73 | |
74 | int totalNodes = jjtGetNumChildren(); |
75 | |
76 | /* |
77 | * Now check the remaining nodes left in the |
78 | * if construct. The nodes are either elseif |
79 | * nodes or else nodes. Each of these node |
80 | * types knows how to evaluate themselves. If |
81 | * a node evaluates to true then the node will |
82 | * render itself and this method will return |
83 | * as there is nothing left to do. |
84 | */ |
85 | for (int i = 2; i < totalNodes; i++) |
86 | { |
87 | if (jjtGetChild(i).evaluate(context)) |
88 | { |
89 | jjtGetChild(i).render(context, writer); |
90 | return true; |
91 | } |
92 | } |
93 | |
94 | /* |
95 | * This is reached when an ASTIfStatement |
96 | * consists of an if/elseif sequence where |
97 | * none of the nodes evaluate to true. |
98 | */ |
99 | return true; |
100 | } |
101 | |
102 | public void process( InternalContextAdapter context, ParserVisitor visitor) |
103 | { |
104 | } |
105 | } |
106 | |
107 | |
108 | |
109 | |
110 | |
111 | |