Source code: com/tripi/asp/ConditionalNode.java
1 /**
2 * ArrowHead ASP Server
3 * This is a source file for the ArrowHead ASP Server - an 100% Java
4 * VBScript interpreter and ASP server.
5 *
6 * For more information, see http://www.tripi.com/arrowhead
7 *
8 * Copyright (C) 2002 Terence Haddock
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 */
25 package com.tripi.asp;
26
27 import org.apache.log4j.Category;
28
29 /**
30 * Implements a conditional node. IF .. THEN .. ELSE .. END IF
31 *
32 * @author Terence Haddock
33 * @version 0.9
34 */
35 public class ConditionalNode implements Node
36 {
37 /** Debugging category */
38 private static Category DBG = Category.getInstance(ConditionalNode.class);
39
40 /** Condition to test */
41 Node condition;
42
43 /** Code to evaluate if the condition is true */
44 Node trueNode;
45
46 /** Code to evaluate if the condition is false, <b>null</b> if no code. */
47 Node falseNode;
48
49 /** Context of this conditional node */
50 DebugContext context;
51
52 /**
53 * Conditional node constructor.
54 *
55 * @param condition Condition to test.
56 * @param trueNode node to execute if condition is true
57 * @param falseNode node to execute if condition is false
58 * @param context Context of this conditional node
59 */
60 public ConditionalNode(Object condition, Object trueNode, Object falseNode, DebugContext context)
61 {
62 this.condition = (Node)condition;
63 this.trueNode = (Node)trueNode;
64 this.falseNode = (Node)falseNode;
65 this.context = context;
66 }
67
68 /**
69 * Gets the condition this node is based on.
70 * @return condition
71 */
72 public Node getCondition()
73 {
74 return condition;
75 }
76
77 /**
78 * Gets the true node, null for none
79 * @return true node
80 */
81 public Node getTrueNode()
82 {
83 return trueNode;
84 }
85
86 /**
87 * Gets the false node, null for none
88 * @return false node
89 */
90 public Node getFalseNode()
91 {
92 return falseNode;
93 }
94
95 /**
96 * Dumps the output of this conditional node.
97 * @throws AspException if any error occurs.
98 * @see Node#dump()
99 */
100 public void dump() throws AspException
101 {
102 System.out.print("if ");
103 condition.dump();
104 System.out.println(" then");
105 trueNode.dump();
106 if (falseNode != null) {
107 System.out.print("else\n");
108 falseNode.dump();
109 }
110 System.out.println("end if");
111 }
112
113 /**
114 * Prepares the nodes for execution.
115 *
116 * @param context Global context
117 * @throws AspException if any error occurs.
118 * @see Node#prepare(AspContext)
119 */
120 public void prepare(AspContext context) throws AspException
121 {
122 /* Prepare the true node */
123 trueNode.prepare(context);
124 /* Prepare the false node, if non-null */
125 if (falseNode != null) falseNode.prepare(context);
126 }
127
128 /**
129 * Executes this conditional node.
130 * @param context AspContext to evaluate this node under.
131 * @return Result of this conditional node.
132 * @throws AspException if any error occurs.
133 * @see Node#execute(AspContext)
134 */
135 public Object execute(AspContext ctx) throws AspException
136 {
137 try {
138 Object obj = condition.execute(ctx);
139 if (DBG.isDebugEnabled()) {
140 DBG.debug("Condition: " + condition);
141 DBG.debug("Result: " + obj);
142 DBG.debug("Result Class: " + obj.getClass());
143 DBG.debug("Result context: " + context);
144 }
145 Boolean i = Types.coerceToBoolean(obj);
146 if (i.booleanValue()) {
147 return trueNode.execute(ctx);
148 } else if (falseNode != null) {
149 return falseNode.execute(ctx);
150 }
151 } catch (AspException ex) {
152 if (!ex.hasContext()) ex.setContext(context);
153 throw ex;
154 } catch (Exception ex) {
155 DBG.error("Error in execute: ", ex);
156 throw new AspNestedException(ex, context);
157 }
158 return null;
159 }
160 };
161