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