source: trunk/java-utils/src/main/java/de/ugoe/cs/util/console/TextConsole.java @ 1297

Last change on this file since 1297 was 927, checked in by sherbold, 12 years ago
  • added copyright under the Apache License, Version 2.0
File size: 4.0 KB
RevLine 
[927]1//   Copyright 2012 Georg-August-Universität Göttingen, Germany
2//
3//   Licensed under the Apache License, Version 2.0 (the "License");
4//   you may not use this file except in compliance with the License.
5//   You may obtain a copy of the License at
6//
7//       http://www.apache.org/licenses/LICENSE-2.0
8//
9//   Unless required by applicable law or agreed to in writing, software
10//   distributed under the License is distributed on an "AS IS" BASIS,
11//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//   See the License for the specific language governing permissions and
13//   limitations under the License.
14
[1]15package de.ugoe.cs.util.console;
16
17import java.io.IOException;
[312]18import java.nio.charset.Charset;
[633]19import java.util.logging.Level;
[1]20
[199]21import de.ugoe.cs.util.console.listener.IErrorListener;
22import de.ugoe.cs.util.console.listener.IExceptionListener;
23import de.ugoe.cs.util.console.listener.IOutputListener;
24import de.ugoe.cs.util.console.listener.ITraceListener;
25
[1]26/**
27 * <p>
28 * Implements a simple console observer that prints normal text to
[175]29 * {@code stdout}, errors to {@code stderr} and reads from {@code stdin}.
[1]30 * </p>
31 *
32 * @author Steffen Herbold
[175]33 * @version 1.0
[1]34 */
[199]35public class TextConsole implements IOutputListener, IErrorListener,
36                ITraceListener, IExceptionListener {
[674]37       
[1]38        /**
[674]39         * <p>Defines the trace level used by this console.</p>
[1]40         */
[674]41        private Level traceLevel;
42       
43        /**
44         * <p>
45         * Creates a new text console and automatically registers it as observer. The trace level is {@link Level#WARNING}.
46         * </p>
47         */
48        public TextConsole() {
49            this(Level.WARNING);
50        }
[1]51
52        /**
53         * <p>
54         * Creates a new text console and automatically registers it as observer.
55         * </p>
[674]56         * @param traceLevel trace level used by this text console
[1]57         */
[674]58        public TextConsole(Level traceLevel) {
[199]59                Console.getInstance().registerOutputListener(this);
60                Console.getInstance().registerErrorListener(this);
61                Console.getInstance().registerTraceListener(this);
62                Console.getInstance().registerExceptionListener(this);
[674]63                this.traceLevel = traceLevel;
[1]64        }
65
66        /**
67         * <p>
[175]68         * Prints messages to {@code stdout}.
[1]69         * </p>
70         *
[201]71         * @see ConsoleObserver#outputMsg(java.lang.String)
[1]72         */
[201]73        public void outputMsg(String newMessage) {
[1]74                System.out.print(newMessage);
75        }
76
77        /**
78         * <p>
[175]79         * Prints messages to {@code stderr}.
[1]80         * </p>
81         *
[201]82         * @see ConsoleObserver#errorMsg(String)
[1]83         */
84        @Override
[201]85        public void errorMsg(String errMessage) {
[1]86                System.err.print(errMessage);
87        }
88
89        /**
90         * <p>
[175]91         * Prints the stacktrace of an exception to {@code stderr}.
[1]92         * </p>
93         *
[210]94         * @see ConsoleObserver#logException(Exception)
[1]95         */
96        @Override
[210]97        public void logException(Exception e) {
98                System.err.println(e.getMessage());
[1]99        }
100
101        /**
102         * <p>
[175]103         * Prints messages to {@code stdout}. These messages are only printed, if
104         * the console is run in debug mode.
[1]105         * </p>
106         */
107        @Override
[633]108        public void traceMsg(String traceMessage, Level level) {
[674]109                if (level.intValue()>=traceLevel.intValue()) {
[633]110                        System.out.print("[" + level.toString() + "] " + traceMessage);
[1]111                }
112        }
113
114        /**
115         * <p>
116         * Starts a new TextConsole. If the text console is started, it can be used
117         * not only to print message, but also to execute commands by reading
[175]118         * {@code stdin}.
[1]119         * </p>
120         */
[674]121        public void run() {
[1]122                CommandExecuter exec = CommandExecuter.getInstance();
123                while (true) {
124                        System.out.print("> ");
125                        String command = getCommand().trim();
126                        if (!command.equals("")) {
127                                exec.exec(command);
128                        }
129                }
130        }
131
132        /**
133         * <p>
[175]134         * Reads a new command from {@code stdin}.
[1]135         * </p>
136         *
137         * @return a string with a command
138         */
139        protected String getCommand() {
140                byte[] buffer = new byte[1024];
[312]141                int bytesRead = 0;
142                String command;
[1]143                try {
[312]144                        bytesRead = System.in.read(buffer);
[1]145                } catch (IOException e) {
146
147                }
[312]148                if (bytesRead == 0) {
149                        command = "";
150                } else {
151                        command = new String(buffer, Charset.defaultCharset());
152                }
153                return command;
[1]154        }
[312]155
[1]156}
Note: See TracBrowser for help on using the repository browser.