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