package de.ugoe.cs.util.console; import java.io.IOException; import java.nio.charset.Charset; import de.ugoe.cs.util.console.listener.IErrorListener; import de.ugoe.cs.util.console.listener.IExceptionListener; import de.ugoe.cs.util.console.listener.IOutputListener; import de.ugoe.cs.util.console.listener.ITraceListener; /** *
* Implements a simple console observer that prints normal text to * {@code stdout}, errors to {@code stderr} and reads from {@code stdin}. *
* * @author Steffen Herbold * @version 1.0 */ public class TextConsole implements IOutputListener, IErrorListener, ITraceListener, IExceptionListener { /** ** In the debug mode, trace messages will be printed. *
*/ private boolean debugMode = true; /** ** Creates a new text console and automatically registers it as observer. *
*/ public TextConsole() { Console.getInstance().registerOutputListener(this); Console.getInstance().registerErrorListener(this); Console.getInstance().registerTraceListener(this); Console.getInstance().registerExceptionListener(this); } /** ** Prints messages to {@code stdout}. *
* * @see ConsoleObserver#outputMsg(java.lang.String) */ public void outputMsg(String newMessage) { System.out.print(newMessage); } /** ** Prints messages to {@code stderr}. *
* * @see ConsoleObserver#errorMsg(String) */ @Override public void errorMsg(String errMessage) { System.err.print(errMessage); } /** ** Prints the stacktrace of an exception to {@code stderr}. *
* * @see ConsoleObserver#logException(Exception) */ @Override public void logException(Exception e) { System.err.println(e.getMessage()); } /** ** Prints messages to {@code stdout}. These messages are only printed, if * the console is run in debug mode. *
*/ @Override public void traceMsg(String traceMessage) { if (debugMode) { System.out.print(traceMessage); } } /** ** Starts a new TextConsole. If the text console is started, it can be used * not only to print message, but also to execute commands by reading * {@code stdin}. *
* * @param debugMode * true, if the application is to run in debug mode, i.e. trace * messages will be printed */ public void run(boolean debugMode) { this.debugMode = debugMode; CommandExecuter exec = CommandExecuter.getInstance(); while (true) { System.out.print("> "); String command = getCommand().trim(); if (!command.equals("")) { exec.exec(command); } } } /** ** Reads a new command from {@code stdin}. *
* * @return a string with a command */ protected String getCommand() { byte[] buffer = new byte[1024]; int bytesRead = 0; String command; try { bytesRead = System.in.read(buffer); } catch (IOException e) { } if (bytesRead == 0) { command = ""; } else { command = new String(buffer, Charset.defaultCharset()); } return command; } /** ** Configures if the debug mode of the text console is enabled. *
* * @param debug * if true, debug mode is enabled. */ public void setDebug(boolean debug) { debugMode = debug; } }