Source code: com/tripi/asp/BlockNode.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 import org.apache.log4j.Category;
30
31 /**
32 * BlockNode contains a block of ASP code, one or more lines of code.
33 *
34 * @author Terence Haddock
35 * @version 0.9
36 */
37 public class BlockNode implements Node
38 {
39 /** Debugging code */
40 private Category DBG = Category.getInstance(BlockNode.class);
41
42 /** List of blocks */
43 Vector blocks;
44
45 /** List of debugging contexts corresponding to blocks */
46 Vector linenos;
47
48 /**
49 * Constructor, starting with no lines of code.
50 */
51 public BlockNode()
52 {
53 blocks = new Vector();
54 linenos = new Vector();
55 }
56
57 /**
58 * Obtains the number of blocks in this object.
59 * @return number of blocks
60 */
61 public int size()
62 {
63 return blocks.size();
64 }
65
66 /**
67 * Gets the block at the specified index.
68 * @param i index
69 * @return block
70 */
71 public Node getBlock(int index)
72 {
73 return (Node)blocks.get(index);
74 }
75
76 /**
77 * Appends a line of code with the debugging context.
78 *
79 * @param obj Object to insert
80 * @param lineno Context of this object
81 */
82 public void append(Object obj, DebugContext lineno)
83 {
84 blocks.add(obj);
85 linenos.add(lineno);
86 }
87
88 /**
89 * Dumps the contents of this block.
90 *
91 * @throws AspException if an error occurs
92 * @see Node#dump()
93 */
94 public void dump() throws AspException
95 {
96 int i;
97 System.out.print("{B}");
98 for (i = 0; i < blocks.size(); i++)
99 {
100 Node node = (Node)blocks.get(i);
101 if (node != null) {
102 node.dump();
103 if (!(node instanceof BlockNode)) {
104 System.out.println();
105 }
106 }
107 }
108 }
109
110 /**
111 * Prepares this node for execution.
112 *
113 * @param context Global context
114 * @throws AspException if an error occurs.
115 * @see Node#prepare(AspContext)
116 */
117 public void prepare(AspContext context) throws AspException
118 {
119 int i;
120 for (i = 0; i < blocks.size(); i++)
121 {
122 try {
123 Node node = (Node)blocks.get(i);
124 if (node != null) {
125 node.prepare(context);
126 }
127 } catch (AspException ex) {
128 DebugContext dbg = (DebugContext)linenos.get(i);
129 context.processException(ex, dbg);
130 }
131 }
132 }
133
134 /**
135 * Executes this block of code.
136 *
137 * @param context Scope in which to execute this code.
138 * @return Returns and ExitScope string.
139 * @throws AspException if an error occurs.
140 * @see Node#execute(AspContext)
141 */
142 public Object execute(AspContext context) throws AspException
143 {
144 int i;
145 for (i = 0; i < blocks.size(); i++)
146 {
147 Node node = (Node)blocks.get(i);
148 try {
149 AspThread.checkTimeout(context);
150 if (node != null)
151 {
152 if (DBG.isDebugEnabled()) {
153 DBG.debug("Executing line: " + linenos.get(i));
154 }
155 Object obj = node.execute(context);
156 if (obj instanceof FunctionNode) {
157 obj = ((FunctionNode)obj).execute(
158 new VarListNode(), context);
159 }
160 }
161 } catch (AspExitException ex) {
162 throw ex;
163 } catch (AspException ex) {
164 DebugContext dbg = (DebugContext)linenos.get(i);
165 context.processException(ex, dbg);
166 } catch (Exception ex) {
167 DebugContext dbg = (DebugContext)linenos.get(i);
168 DBG.error("Error in block " + dbg, ex);
169 context.processException(ex, dbg);
170 }
171 }
172 return null;
173 }
174 };
175