Source code: com/tripi/asp/CaseNode.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 java.util.Vector;
28
29 /**
30 * Implements the case part of the select..case statement.
31 *
32 * @author Terence Haddock
33 * @version 0.9
34 */
35 public class CaseNode extends DefaultNode
36 {
37 /** List of tests for this case */
38 VarListNode tests;
39
40 /** Code to execute if one of the tests evaluate to true */
41 BlockNode exec;
42
43 /**
44 * Constructor to create a new case node.
45 * @param tests Tests to evaluate, <b>null</b> for the ELSE statement.
46 * @param exec Block to execute.
47 */
48 public CaseNode(VarListNode tests, BlockNode exec)
49 {
50 this.tests = tests;
51 this.exec = exec;
52 }
53
54 /**
55 * Dumps this case node.
56 * @see Node#dump()
57 * @throws AspException if an error occurs
58 */
59 public void dump() throws AspException
60 {
61 System.out.print("CASE ");
62 if (tests == null)
63 {
64 System.out.print("ELSE");
65 } else {
66 tests.dump();
67 }
68 System.out.println();
69 exec.dump();
70 }
71
72 /**
73 * Checks if the test matches the case node.
74 * @return <b>true</b> if the case node matches, <b>false</b> otherwise.
75 * @throws AspException if an error occurs
76 */
77 public boolean matches(Object value, AspContext context) throws AspException
78 {
79 if (tests == null) {
80 return true;
81 }
82 String strValue = Types.coerceToString(value);
83 Vector vecValues = (Vector)tests.execute(context);
84 for (int i = 0; i < vecValues.size(); i++)
85 {
86 String testValue = Types.coerceToString(vecValues.get(i));
87 if (testValue.equals(strValue)) {
88 return true;
89 }
90 }
91 return false;
92 }
93
94 /**
95 * Prepares this node. Calls prepare on children nodes.
96 *
97 * @param context Global context
98 * @see Node#prepare(AspContext)
99 * @throws AspException if an error occurs.
100 */
101 public void prepare(AspContext context) throws AspException
102 {
103 exec.prepare(context);
104 }
105
106 /**
107 * Executes this case node.
108 * @param context AspContext under which to execute this case node.
109 * @return the result of this block node.
110 * @see Node#execute(AspContext)
111 * @throws AspException if an error occurs
112 */
113 public Object execute(AspContext context) throws AspException
114 {
115 return exec.execute(context);
116 }
117 };
118