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