KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > cmdline > lib > DefaultConsole


1 /*====================================================================
2
3 ObjectWeb Util CommandLine Package.
4 Copyright (C) 2003 INRIA & USTL - LIFL - GOAL
5 Contact: architecture@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Philippe Merle.
23 Contributor(s): ______________________________________.
24
25 ====================================================================*/

26
27 package org.objectweb.util.cmdline.lib;
28
29 // Package dependencies.
30
import org.objectweb.util.cmdline.api.Console;
31 import java.io.PrintStream JavaDoc;
32
33 /**
34  * This is the default Console implementation., i.e.
35  * a wrapper class for standard output and error streams.
36  *
37  * This provides the following properties:
38  *
39  * - OutputStream: The standard output stream.
40  *
41  * - ErrorStream: The standard error stream.
42  *
43  * - MessageHeader: The header printed before messages.
44  *
45  * - SilentMessage: Flag to indicate if messages will be really printed or not.
46  *
47  * This provides the following methods:
48  *
49  * - message: Prints messages to the standard output stream.
50  *
51  * - error: Prints messages to the standard error stream.
52  *
53  * @author <a HREF="mailto:Philippe.Merle@lifl.fr">Philippe Merle</a>
54  *
55  * @version 0.1
56  */

57
58 public class DefaultConsole
59   implements Console
60 {
61     // ==================================================================
62
//
63
// Internal state.
64
//
65
// ==================================================================
66

67     /** The standard output stream. */
68     private PrintStream JavaDoc outputStream_;
69
70     /** The standard error stream. */
71     private PrintStream JavaDoc errorStream_;
72
73     /** The message header. */
74     private String JavaDoc messageHeader_;
75
76     /** Boolean for indicating if messages should be displayed. */
77     private boolean silentMessage_;
78
79     // ==================================================================
80
//
81
// Constructors.
82
//
83
// ==================================================================
84

85     /** The default constructor. */
86     public
87     DefaultConsole()
88     {
89         this("", false);
90     }
91
92     /**
93      * The constructor with the message header.
94      *
95      * @param header The message header.
96      */

97     public
98     DefaultConsole(String JavaDoc header)
99     {
100         this(header, false);
101     }
102
103     /**
104      * The constructor with the message header and
105      * the silent message flag.
106      *
107      * @param header The message header.
108      * @param silent The silent message flag.
109      */

110     public
111     DefaultConsole(String JavaDoc header,
112                    boolean silent)
113     {
114         this(System.out, System.err, header, silent);
115     }
116
117     /**
118      * The constructor with all properties.
119      *
120      * @param output The standard output stream.
121      * @param error The standard error stream.
122      * @param header The message header.
123      * @param silent The silent message flag.
124      */

125     public
126     DefaultConsole(PrintStream JavaDoc output,
127                    PrintStream JavaDoc error,
128                    String JavaDoc header,
129                    boolean silent)
130     {
131         // Inits internal state.
132
outputStream_ = output;
133         errorStream_ = error;
134         messageHeader_ = header;
135         silentMessage_ = silent;
136     }
137
138     // ==================================================================
139
//
140
// Internal methods.
141
//
142
// ==================================================================
143

144     // ==================================================================
145
//
146
// Public methods for org.objectweb.util.cmdline.api.Console.
147
//
148
// ==================================================================
149

150     /**
151      * Gets the standard output stream.
152      *
153      * @return The standard output stream.
154      */

155     public PrintStream JavaDoc
156     getOutputStream()
157     {
158         return outputStream_;
159     }
160
161     /**
162      * Sets the standard output stream.
163      *
164      * @param output The standard output stream.
165      */

166     public void
167     setOutputStream(PrintStream JavaDoc output)
168     {
169         outputStream_ = output;
170     }
171
172     /**
173      * Gets the standard error stream.
174      *
175      * @return The standard error stream.
176      */

177     public PrintStream JavaDoc
178     getErrorStream()
179     {
180         return errorStream_;
181     }
182
183     /**
184      * Sets the standard error stream.
185      *
186      * @param error The standard error stream.
187      */

188     public void
189     setErrorStream(PrintStream JavaDoc error)
190     {
191         errorStream_ = error;
192     }
193
194     /**
195      * Gets the message header.
196      *
197      * @return The message header.
198      */

199     public String JavaDoc
200     getMessageHeader()
201     {
202         return messageHeader_;
203     }
204
205     /**
206      * Sets the message header.
207      *
208      * @param header The message header.
209      */

210     public void
211     setMessageHeader(String JavaDoc header)
212     {
213         messageHeader_ = header;
214     }
215
216     /**
217      * Gets the silent flag status.
218      *
219      * @return The silent flag status.
220      */

221     public boolean
222     getSilentMessage()
223     {
224         return silentMessage_;
225     }
226
227     /**
228      * Sets the silent flag status.
229      *
230      * @param flag The silent flag status.
231      */

232     public void
233     setSilentMessage(boolean flag)
234     {
235         silentMessage_ = flag;
236     }
237
238     /**
239      * Prints a message on the standard output.
240      *
241      * @param text The message to print.
242      */

243     public void
244     message(String JavaDoc text)
245     {
246         if(!silentMessage_)
247             getOutputStream().println(messageHeader_ + ": " + text);
248     }
249
250     /**
251      * Displays a message on the error output.
252      *
253      * @param text The message to print.
254      */

255     public void
256     error(String JavaDoc text)
257     {
258         getErrorStream().println(messageHeader_ + ": " + text);
259     }
260 }
261
Popular Tags