source: trunk/JavaHelperLib/src/de/ugoe/cs/util/console/TextConsole.java @ 312

Last change on this file since 312 was 312, checked in by sherbold, 13 years ago
  • fixed some minor code smells
File size: 3.4 KB
RevLine 
[1]1package de.ugoe.cs.util.console;
2
3import java.io.IOException;
[312]4import java.nio.charset.Charset;
[1]5
[199]6import de.ugoe.cs.util.console.listener.IErrorListener;
7import de.ugoe.cs.util.console.listener.IExceptionListener;
8import de.ugoe.cs.util.console.listener.IOutputListener;
9import de.ugoe.cs.util.console.listener.ITraceListener;
10
[1]11/**
12 * <p>
13 * Implements a simple console observer that prints normal text to
[175]14 * {@code stdout}, errors to {@code stderr} and reads from {@code stdin}.
[1]15 * </p>
16 *
17 * @author Steffen Herbold
[175]18 * @version 1.0
[1]19 */
[199]20public class TextConsole implements IOutputListener, IErrorListener,
21                ITraceListener, IExceptionListener {
[1]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() {
[199]36                Console.getInstance().registerOutputListener(this);
37                Console.getInstance().registerErrorListener(this);
38                Console.getInstance().registerTraceListener(this);
39                Console.getInstance().registerExceptionListener(this);
[1]40        }
41
42        /**
43         * <p>
[175]44         * Prints messages to {@code stdout}.
[1]45         * </p>
46         *
[201]47         * @see ConsoleObserver#outputMsg(java.lang.String)
[1]48         */
[201]49        public void outputMsg(String newMessage) {
[1]50                System.out.print(newMessage);
51        }
52
53        /**
54         * <p>
[175]55         * Prints messages to {@code stderr}.
[1]56         * </p>
57         *
[201]58         * @see ConsoleObserver#errorMsg(String)
[1]59         */
60        @Override
[201]61        public void errorMsg(String errMessage) {
[1]62                System.err.print(errMessage);
63        }
64
65        /**
66         * <p>
[175]67         * Prints the stacktrace of an exception to {@code stderr}.
[1]68         * </p>
69         *
[210]70         * @see ConsoleObserver#logException(Exception)
[1]71         */
72        @Override
[210]73        public void logException(Exception e) {
74                System.err.println(e.getMessage());
[1]75        }
76
77        /**
78         * <p>
[175]79         * Prints messages to {@code stdout}. These messages are only printed, if
80         * the console is run in debug mode.
[1]81         * </p>
82         */
83        @Override
[201]84        public void traceMsg(String traceMessage) {
[1]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
[175]94         * {@code stdin}.
[1]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>
[175]115         * Reads a new command from {@code stdin}.
[1]116         * </p>
117         *
118         * @return a string with a command
119         */
120        protected String getCommand() {
121                byte[] buffer = new byte[1024];
[312]122                int bytesRead = 0;
123                String command;
[1]124                try {
[312]125                        bytesRead = System.in.read(buffer);
[1]126                } catch (IOException e) {
127
128                }
[312]129                if (bytesRead == 0) {
130                        command = "";
131                } else {
132                        command = new String(buffer, Charset.defaultCharset());
133                }
134                return command;
[1]135        }
[312]136
[250]137        /**
138         * <p>
139         * Configures if the debug mode of the text console is enabled.
140         * </p>
[312]141         *
142         * @param debug
143         *            if true, debug mode is enabled.
[250]144         */
145        public void setDebug(boolean debug) {
146                debugMode = debug;
147        }
[185]148
[1]149}
Note: See TracBrowser for help on using the repository browser.