1 package org.sapia.console; 2 3 import java.io.*; 4 5 6 26 public class CommandConsole extends Console { 27 private CommandFactory _fac; 28 private ConsoleListener _listener = new ConsoleListenerImpl(); 29 30 35 public CommandConsole(CommandFactory fac) { 36 super(); 37 _fac = fac; 38 } 39 40 51 public CommandConsole(InputStream in, OutputStream out, CommandFactory fac) { 52 super(in, out); 53 _fac = fac; 54 } 55 56 59 public void setCommandListener(ConsoleListener listener) { 60 _listener = listener; 61 } 62 63 68 public void start() { 69 int idx = 0; 70 String line; 71 String name = null; 72 String args = null; 73 _listener.onStart(this); 74 75 while (true) { 76 try { 77 prompt(); 78 line = readLine(); 79 80 if (line.length() == 0) { 81 continue; 82 } else { 83 CmdLine cmdLine = CmdLine.parse(line); 84 85 if (cmdLine.size() == 0) { 86 continue; 87 } 88 89 if (cmdLine.isNextArg()) { 90 Command cmd = _fac.getCommandFor(name = cmdLine.chopArg().getName()); 91 Context ctx = newContext(); 92 ctx.setUp(this, cmdLine); 93 cmd.execute(ctx); 94 } else { 95 println("Command name expected"); 96 } 97 } 98 } catch (InputException e) { 99 this.println(e.getMessage()); 100 } catch (AbortException e) { 101 _listener.onAbort(this); 102 103 break; 104 } catch (IOException e) { 105 e.printStackTrace(); 106 107 break; 108 } catch (CommandNotFoundException e) { 109 _listener.onCommandNotFound(this, name); 110 } 111 } 112 } 113 114 118 protected Context newContext() { 119 return new Context(); 120 } 121 122 } 134 | Popular Tags |