View Javadoc

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      private boolean breakOnError = true;
15  
16      private Command[] commands = new Command[0];
17  
18      /***
19       * Sets whether processing of this Command Sequence should stop if an error
20       * is encountered.
21       * 
22       * @param b if true, the processing stops on errors
23       */
24      public void setBreakOnError(boolean b) {
25          breakOnError = b;
26      }
27  
28      /***
29       * Set the array of Command into this Sequence
30       * 
31       * @param c Commands to set into this
32       */
33      public void setCommands(Command[] c) {
34          commands = c;
35      }
36  
37      /***
38       * DOCUMENT ME!
39       * 
40       * @param c DOCUMENT ME!
41       * @return DOCUMENT ME!
42       * @throws Exception DOCUMENT ME!
43       */
44      public int execute(Context c) throws Exception {
45          c.put("$_", this);
46  
47          int retCode = 0;
48          PrintStream out = c.getOut();
49  
50          for (int i = 0; i < commands.length; i++) {
51              Command command = commands[i];
52  
53              long start = System.currentTimeMillis();
54  
55              if ((out != null) && c.isVerbose()) {
56                  out.print(c.getDepth() + "." + i + ": ");
57              }
58  
59              if (command instanceof TryBlock) {
60                  retCode = command.execute(new Context(c));
61              } else if (command instanceof End) {
62                  c = c.getPrevious();
63                  retCode = command.execute(c);
64              } else if (command instanceof CatchCommand) {
65                  while ((i < commands.length) && !(commands[i] instanceof End)) {
66                      i++;
67                  }
68  
69                  if (i < commands.length) {
70                      i--;
71                  }
72              } else {
73                  retCode = command.execute(c);
74              }
75  
76              long end = System.currentTimeMillis();
77  
78              if ((out != null) && c.isVerbose()) {
79                  out.println(" (" + (end - start) + " ms)");
80              }
81  
82              if ((retCode > 0) && breakOnError) {
83                  while ((i < commands.length)
84                          && !(commands[i] instanceof CatchCommand)) {
85                      i++;
86                  }
87  
88                  if (i >= commands.length) {
89                      break;
90                  }
91              }
92          }
93  
94          return retCode;
95      }
96  
97      /***
98       * DOCUMENT ME!
99       * 
100      * @return DOCUMENT ME!
101      */
102     protected String commandsToString() {
103         StringBuffer sb = new StringBuffer();
104 
105         for (int i = 0; i < commands.length; i++) {
106             if (i > 0) {
107                 sb.append("\n");
108             }
109 
110             sb.append("   ");
111             sb.append(commands[i]);
112         }
113 
114         return sb.toString();
115     }
116 
117     /***
118      * DOCUMENT ME!
119      * 
120      * @param c DOCUMENT ME!
121      * @param i DOCUMENT ME!
122      * @return DOCUMENT ME!
123      * @throws Exception DOCUMENT ME!
124      */
125     protected int execute(Context c, int i) throws Exception {
126         int retCode = 0;
127         PrintStream out = c.getOut();
128 
129         if (out != null) {
130             out.print("" + i + ": ");
131         }
132 
133         retCode = commands[i].execute(c);
134 
135         if (out != null) {
136             out.println();
137         }
138 
139         return retCode;
140     }
141 
142     /***
143      * DOCUMENT ME!
144      * 
145      * @return DOCUMENT ME!
146      */
147     public String toString() {
148         return "SEQUENCE ("
149                 + (breakOnError ? "breakOnError" : "NoBreakOnError") + ") {\n"
150                 + commandsToString() + "\n}";
151     }
152 }
153 
154 /*
155  * The contents of this file are subject to the Mozilla Public License Version
156  * 1.1 (the "License"); you may not use this file except in compliance with the
157  * License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
158  * Software distributed under the License is distributed on an "AS IS" basis,
159  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
160  * the specific language governing rights and limitations under the License. The
161  * Original Code is: all this file. The Initial Developer of the Original Code
162  * is: Tomas Pitner, Masaryk University in Brno, Czech Republic. Contributor(s):
163  */