1 package net.sf.tomp.xtcl.command;
2
3 import net.sf.tomp.xtcl.Command;
4 import net.sf.tomp.xtcl.Context;
5
6 import java.io.PrintStream;
7
8 /***
9 * A Sequence of Commands, will be executed sequentially, one-after-another
10 *
11 * @author tomp
12 */
13 public class Sequence extends AbstractCommand {
14
15 private boolean breakOnError = true;
16
17 private Command[] commands = new Command[0];
18
19 /***
20 * Sets whether processing of this Command Sequence should stop if an error
21 * is encountered.
22 *
23 * @param b if true, the processing stops on errors
24 */
25 public void setBreakOnError(boolean b) {
26 breakOnError = b;
27 }
28
29 /***
30 * Set the array of Command into this Sequence
31 *
32 * @param c Commands to set into this
33 */
34 public void setCommands(Command[] c) {
35 commands = c;
36 }
37
38 /***
39 * DOCUMENT ME!
40 *
41 * @param c DOCUMENT ME!
42 * @return DOCUMENT ME!
43 * @throws Exception DOCUMENT ME!
44 */
45 public int execute(Context c) throws Exception {
46 c.put("$_", this);
47
48 int retCode = 0;
49 PrintStream out = c.getOut();
50
51 for (int i = 0; i < commands.length; i++) {
52 Command command = commands[i];
53
54 long start = System.currentTimeMillis();
55
56 if ((out != null) && c.isVerbose()) {
57 out.print(c.getDepth() + "." + i + ": ");
58 }
59
60 if (command instanceof TryBlock) {
61 retCode = command.execute(new Context(c));
62 } else if (command instanceof End) {
63 c = c.getPrevious();
64 retCode = command.execute(c);
65 } else if (command instanceof CatchCommand) {
66 while ((i < commands.length) && !(commands[i] instanceof End)) {
67 i++;
68 }
69
70 if (i < commands.length) {
71 i--;
72 }
73 } else {
74 retCode = command.execute(c);
75 }
76
77 long end = System.currentTimeMillis();
78
79 if ((out != null) && c.isVerbose()) {
80 out.println(" (" + (end - start) + " ms)");
81 }
82
83 if ((retCode > 0) && breakOnError) {
84 while ((i < commands.length)
85 && !(commands[i] instanceof CatchCommand)) {
86 i++;
87 }
88
89 if (i >= commands.length) {
90 break;
91 }
92 }
93 }
94
95 return retCode;
96 }
97
98 /***
99 * DOCUMENT ME!
100 *
101 * @return DOCUMENT ME!
102 */
103 protected String commandsToString() {
104 StringBuffer sb = new StringBuffer();
105
106 for (int i = 0; i < commands.length; i++) {
107 if (i > 0) {
108 sb.append("\n");
109 }
110
111 sb.append(" ");
112 sb.append(commands[i]);
113 }
114
115 return sb.toString();
116 }
117
118 /***
119 * DOCUMENT ME!
120 *
121 * @param c DOCUMENT ME!
122 * @param i DOCUMENT ME!
123 * @return DOCUMENT ME!
124 * @throws Exception DOCUMENT ME!
125 */
126 protected int execute(Context c, int i) throws Exception {
127 int retCode = 0;
128 PrintStream out = c.getOut();
129
130 if (out != null) {
131 out.print("" + i + ": ");
132 }
133
134 retCode = commands[i].execute(c);
135
136 if (out != null) {
137 out.println();
138 }
139
140 return retCode;
141 }
142
143 /***
144 * DOCUMENT ME!
145 *
146 * @return DOCUMENT ME!
147 */
148 public String toString() {
149 return "SEQUENCE ("
150 + (breakOnError ? "breakOnError" : "NoBreakOnError") + ") {\n"
151 + commandsToString() + "\n}";
152 }
153 }
154
155
156
157
158
159
160
161
162
163
164