[1] | 1 | package de.ugoe.cs.util.console;
|
---|
| 2 |
|
---|
| 3 | import java.io.IOException;
|
---|
| 4 |
|
---|
[199] | 5 | import de.ugoe.cs.util.console.listener.IErrorListener;
|
---|
| 6 | import de.ugoe.cs.util.console.listener.IExceptionListener;
|
---|
| 7 | import de.ugoe.cs.util.console.listener.IOutputListener;
|
---|
| 8 | import de.ugoe.cs.util.console.listener.ITraceListener;
|
---|
| 9 |
|
---|
[1] | 10 | /**
|
---|
| 11 | * <p>
|
---|
| 12 | * Implements a simple console observer that prints normal text to
|
---|
[175] | 13 | * {@code stdout}, errors to {@code stderr} and reads from {@code stdin}.
|
---|
[1] | 14 | * </p>
|
---|
| 15 | *
|
---|
| 16 | * @author Steffen Herbold
|
---|
[175] | 17 | * @version 1.0
|
---|
[1] | 18 | */
|
---|
[199] | 19 | public class TextConsole implements IOutputListener, IErrorListener,
|
---|
| 20 | ITraceListener, IExceptionListener {
|
---|
[1] | 21 |
|
---|
| 22 | /**
|
---|
| 23 | * <p>
|
---|
| 24 | * In the debug mode, trace messages will be printed.
|
---|
| 25 | * </p>
|
---|
| 26 | */
|
---|
| 27 | private boolean debugMode = true;
|
---|
| 28 |
|
---|
| 29 | /**
|
---|
| 30 | * <p>
|
---|
| 31 | * Creates a new text console and automatically registers it as observer.
|
---|
| 32 | * </p>
|
---|
| 33 | */
|
---|
| 34 | public TextConsole() {
|
---|
[199] | 35 | Console.getInstance().registerOutputListener(this);
|
---|
| 36 | Console.getInstance().registerErrorListener(this);
|
---|
| 37 | Console.getInstance().registerTraceListener(this);
|
---|
| 38 | Console.getInstance().registerExceptionListener(this);
|
---|
[1] | 39 | }
|
---|
| 40 |
|
---|
| 41 | /**
|
---|
| 42 | * <p>
|
---|
[175] | 43 | * Prints messages to {@code stdout}.
|
---|
[1] | 44 | * </p>
|
---|
| 45 | *
|
---|
[201] | 46 | * @see ConsoleObserver#outputMsg(java.lang.String)
|
---|
[1] | 47 | */
|
---|
[201] | 48 | public void outputMsg(String newMessage) {
|
---|
[1] | 49 | System.out.print(newMessage);
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | /**
|
---|
| 53 | * <p>
|
---|
[175] | 54 | * Prints messages to {@code stderr}.
|
---|
[1] | 55 | * </p>
|
---|
| 56 | *
|
---|
[201] | 57 | * @see ConsoleObserver#errorMsg(String)
|
---|
[1] | 58 | */
|
---|
| 59 | @Override
|
---|
[201] | 60 | public void errorMsg(String errMessage) {
|
---|
[1] | 61 | System.err.print(errMessage);
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | /**
|
---|
| 65 | * <p>
|
---|
[175] | 66 | * Prints the stacktrace of an exception to {@code stderr}.
|
---|
[1] | 67 | * </p>
|
---|
| 68 | *
|
---|
[210] | 69 | * @see ConsoleObserver#logException(Exception)
|
---|
[1] | 70 | */
|
---|
| 71 | @Override
|
---|
[210] | 72 | public void logException(Exception e) {
|
---|
| 73 | System.err.println(e.getMessage());
|
---|
[1] | 74 | }
|
---|
| 75 |
|
---|
| 76 | /**
|
---|
| 77 | * <p>
|
---|
[175] | 78 | * Prints messages to {@code stdout}. These messages are only printed, if
|
---|
| 79 | * the console is run in debug mode.
|
---|
[1] | 80 | * </p>
|
---|
| 81 | */
|
---|
| 82 | @Override
|
---|
[201] | 83 | public void traceMsg(String traceMessage) {
|
---|
[1] | 84 | if (debugMode) {
|
---|
| 85 | System.out.print(traceMessage);
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | /**
|
---|
| 90 | * <p>
|
---|
| 91 | * Starts a new TextConsole. If the text console is started, it can be used
|
---|
| 92 | * not only to print message, but also to execute commands by reading
|
---|
[175] | 93 | * {@code stdin}.
|
---|
[1] | 94 | * </p>
|
---|
| 95 | *
|
---|
| 96 | * @param debugMode
|
---|
| 97 | * true, if the application is to run in debug mode, i.e. trace
|
---|
| 98 | * messages will be printed
|
---|
| 99 | */
|
---|
| 100 | public void run(boolean debugMode) {
|
---|
| 101 | this.debugMode = debugMode;
|
---|
| 102 | CommandExecuter exec = CommandExecuter.getInstance();
|
---|
| 103 | while (true) {
|
---|
| 104 | System.out.print("> ");
|
---|
| 105 | String command = getCommand().trim();
|
---|
| 106 | if (!command.equals("")) {
|
---|
| 107 | exec.exec(command);
|
---|
| 108 | }
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | /**
|
---|
| 113 | * <p>
|
---|
[175] | 114 | * Reads a new command from {@code stdin}.
|
---|
[1] | 115 | * </p>
|
---|
| 116 | *
|
---|
| 117 | * @return a string with a command
|
---|
| 118 | */
|
---|
| 119 | protected String getCommand() {
|
---|
| 120 | byte[] buffer = new byte[1024];
|
---|
| 121 | try {
|
---|
| 122 | System.in.read(buffer);
|
---|
| 123 | } catch (IOException e) {
|
---|
| 124 |
|
---|
| 125 | }
|
---|
| 126 | return new String(buffer);
|
---|
| 127 | }
|
---|
[185] | 128 |
|
---|
[1] | 129 | }
|
---|