View Javadoc

1   package net.sf.tomp.xtcl;
2   
3   import net.sf.tomp.xtcl.command.End;
4   
5   import java.io.PrintStream;
6   
7   import java.util.ArrayList;
8   import java.util.Iterator;
9   import java.util.List;
10  
11  /***
12   * Simple executor for running XTCL commands from command line, ¨
13   * i.e. console, std. input
14   * 
15   * @author tomp
16   */
17  public class InteractiveExecutor {
18  	
19      /*** Context for commands compilation and execution */
20      protected Context context;
21  
22      /***
23       * Creates a new instance of InteractiveExecutor
24       * 
25       * @param c Context for the Executor
26       */
27      protected InteractiveExecutor(Context c) {
28          context = c;
29      }
30  
31      /***
32       * Executes a command in the current context.
33       * 
34       * Runs interactively in a loop until END command is entered 
35       * 
36       * @return DOCUMENT ME!
37       * @throws Exception DOCUMENT ME!
38       */
39      public int interactive() throws Exception {
40          CommandLineReader clr = new CommandLineReader();
41          Command cs = null;
42          int retCode = 0;
43          int commandNo = 0;
44          PrintStream out = context.getOut();
45          List history = new ArrayList();
46  
47          do {
48              if (out != null) {
49                  out.print("\nXTCL " + commandNo + ">");
50              }
51  
52              String s = clr.nextCommandLine();
53  
54              // nothing entered - end
55              if (s == null) {
56                  break;
57              }
58  
59              // a number entered - execute a command from history
60              if (Character.isDigit(s.charAt(0))) {
61                  // execute command from history
62                  int pos = Integer.parseInt(s);
63  
64                  cs = (Command) history.get(pos);
65                  retCode = context.execute(cs);
66  
67                  // 'hi' entered - shows command history
68              } else if (s.toLowerCase().startsWith("hi")) {
69                  // list history
70                  int c = 0;
71  
72                  for (Iterator i = history.iterator(); i.hasNext(); c++) {
73                      cs = (Command) i.next();
74                      System.out.println(c + "> " + cs);
75                  }
76  
77                  // else - executes a command
78              } else {
79                  // execute new command
80                  Compiler comp = context.getCompiler();
81  
82                  cs = comp.compile(s, clr);
83                  retCode = context.execute(cs);
84  
85                  if (cs != null) {
86                      history.add(cs); 
87                  }
88              }
89  
90              commandNo++;
91          } while (!(cs instanceof End));
92  
93          return retCode;
94      }
95  }
96  
97  /*
98   * The contents of this file are subject to the Mozilla Public License Version
99   * 1.1 (the "License"); you may not use this file except in compliance with the
100  * License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
101  * Software distributed under the License is distributed on an "AS IS" basis,
102  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
103  * the specific language governing rights and limitations under the License. The
104  * Original Code is: all this file. The Initial Developer of the Original Code
105  * is: Tomas Pitner, Masaryk University in Brno, Czech Republic. Contributor(s):
106  */