KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > util > ServletContextWriter


1 /*
2  * $Id: ServletContextWriter.java 54929 2004-10-16 16:38:42Z germuska $
3  *
4  * Copyright 2000-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 package org.apache.struts.util;
20
21 import java.io.PrintWriter JavaDoc;
22 import java.io.StringWriter JavaDoc;
23 import javax.servlet.ServletContext JavaDoc;
24
25 /**
26  * A PrintWriter implementation that uses the logging facilities of a
27  * <code>javax.servlet.ServletContext</code> to output its results. Output
28  * will be buffered until a newline character is output, <code>flush()</code>
29  * is called, or until one of the <code>println()</code> methods is called.
30  * Along the way, carriage return characters are skipped.
31  *
32  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
33  */

34 public class ServletContextWriter extends PrintWriter JavaDoc {
35
36
37     // ----------------------------------------------------------- Constructors
38

39
40     /**
41      * Construct a ServletContextWriter associated with the specified
42      * ServletContext instance.
43      *
44      * @param context The associated servlet context
45      */

46     public ServletContextWriter(ServletContext JavaDoc context) {
47
48         super(new StringWriter JavaDoc());
49         this.context = context;
50
51     }
52
53
54     // ------------------------------------------------------------- Properties
55

56
57     /**
58      * The buffer into which we accumulate lines to be logged.
59      */

60     protected StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
61
62
63     /**
64      * The servlet context with which we are associated.
65      */

66     protected ServletContext JavaDoc context = null;
67
68
69     /**
70      * The error state for this stream.
71      */

72     protected boolean error = false;
73
74
75     // --------------------------------------------------------- Public Methods
76

77
78     /**
79      * Flush the stream and check for its error state. <strong>IMPLEMENTATION
80      * NOTE</strong> - our associated servlet context gives no indication of
81      * problems with logging, so the only way this method will return
82      * <code>true</code> is if <code>setError()</code> is called.
83      */

84     public boolean checkError() {
85
86         flush();
87         return (error);
88
89     }
90
91
92     /**
93      * Close the stream.
94      */

95     public void close() {
96
97         flush();
98
99     }
100
101
102     /**
103      * Flush the stream.
104      */

105     public void flush() {
106
107         if (buffer.length() > 0) {
108             context.log(buffer.toString());
109             buffer.setLength(0);
110         }
111
112     }
113
114
115     /**
116      * Print a boolean value.
117      *
118      * @param b The value to be printed
119      */

120     public void print(boolean b) {
121
122         write(String.valueOf(b));
123
124     }
125
126
127     /**
128      * Print a character value.
129      *
130      * @param c The value to be printed
131      */

132     public void print(char c) {
133
134         write(c);
135
136     }
137
138
139     /**
140      * Print a character array.
141      *
142      * @param c The character array to be printed
143      */

144     public void print(char c[]) {
145
146         for (int i = 0; i < c.length; i++)
147             write(c[i]);
148
149     }
150
151
152     /**
153      * Print a double value.
154      *
155      * @param d The value to be printed
156      */

157     public void print(double d) {
158
159         write(String.valueOf(d));
160
161     }
162
163
164     /**
165      * Print a float value.
166      *
167      * @param f The value to be printed
168      */

169     public void print(float f) {
170
171         write(String.valueOf(f));
172
173     }
174
175
176     /**
177      * Print an integer value.
178      *
179      * @param i The value to be printed
180      */

181     public void print(int i) {
182
183         write(String.valueOf(i));
184
185     }
186
187
188     /**
189      * Print a long value.
190      *
191      * @param l The value to be printed
192      */

193     public void print(long l) {
194
195         write(String.valueOf(l));
196
197     }
198
199
200     /**
201      * Print an object.
202      *
203      * @param o The value to be printed
204      */

205     public void print(Object JavaDoc o) {
206
207         write(o.toString());
208
209     }
210
211
212     /**
213      * Print a String value.
214      *
215      * @param s The value to be printed
216      */

217     public void print(String JavaDoc s) {
218
219         int len = s.length();
220         for (int i = 0; i < len; i++)
221             write(s.charAt(i));
222
223     }
224
225
226     /**
227      * Terminate the current line and flush the buffer.
228      */

229     public void println() {
230
231         flush();
232
233     }
234
235
236     /**
237      * Print a boolean value and terminate the line.
238      *
239      * @param b The value to be printed
240      */

241     public void println(boolean b) {
242
243         println(String.valueOf(b));
244
245     }
246
247
248     /**
249      * Print a character value and terminate the line.
250      *
251      * @param c The value to be printed
252      */

253     public void println(char c) {
254
255         write(c);
256         println();
257
258     }
259
260
261     /**
262      * Print a character array and terminate the line.
263      *
264      * @param c The character array to be printed
265      */

266     public void println(char c[]) {
267
268         for (int i = 0; i < c.length; i++)
269             print(c[i]);
270         println();
271
272     }
273
274
275     /**
276      * Print a double value and terminate the line.
277      *
278      * @param d The value to be printed
279      */

280     public void println(double d) {
281
282         println(String.valueOf(d));
283
284     }
285
286
287     /**
288      * Print a float value and terminate the line.
289      *
290      * @param f The value to be printed
291      */

292     public void println(float f) {
293
294         println(String.valueOf(f));
295
296     }
297
298
299     /**
300      * Print an integer value and terminate the line.
301      *
302      * @param i The value to be printed
303      */

304     public void println(int i) {
305
306         println(String.valueOf(i));
307
308     }
309
310
311     /**
312      * Print a long value and terminate the line.
313      *
314      * @param l The value to be printed
315      */

316     public void println(long l) {
317
318         println(String.valueOf(l));
319
320     }
321
322
323     /**
324      * Print an object and terminate the line.
325      *
326      * @param o The value to be printed
327      */

328     public void println(Object JavaDoc o) {
329
330         println(o.toString());
331
332     }
333
334
335     /**
336      * Print a String value and terminate the line.
337      *
338      * @param s The value to be printed
339      */

340     public void println(String JavaDoc s) {
341
342         int len = s.length();
343         for (int i = 0; i < len; i++)
344             print(s.charAt(i));
345         println();
346
347     }
348
349
350     /**
351      * Set the error state for this stream.
352      */

353     public void setError() {
354
355         this.error = true;
356
357     }
358
359
360     /**
361      * Write a single character to this stream.
362      *
363      * @param c The character to be written
364      */

365     public void write(char c) {
366
367         if (c == '\n')
368             flush();
369         else if (c != '\r')
370             buffer.append(c);
371
372     }
373
374
375     /**
376      * Write a single character to this stream.
377      *
378      * @param c The character to be written
379      */

380     public void write(int c) {
381
382         write((char) c);
383
384     }
385
386
387     /**
388      * Write an array of charaters to this stream.
389      *
390      * @param buf The character array to be written
391      */

392     public void write(char buf[]) {
393
394         for (int i = 0; i < buf.length; i++)
395             write(buf[i]);
396
397     }
398
399
400     /**
401      * Write the specified subset of an array of characters to this stream.
402      *
403      * @param buf The character array from which to write
404      * @param off The zero-relative starting offset to write
405      * @param len The number of characters to write
406      */

407     public void write(char buf[], int off, int len) {
408
409         for (int i = off; i < len; i++)
410             write(buf[i]);
411
412     }
413
414
415     /**
416      * Write a String to this stream.
417      *
418      * @param s The string to be written
419      */

420     public void write(String JavaDoc s) {
421
422         int len = s.length();
423         for (int i = 0; i < len; i++)
424             write(s.charAt(i));
425
426     }
427
428
429     /**
430      * Write the specified portion of a String to this stream.
431      *
432      * @param s The String from which to write
433      * @param off The zero-relative starting offset to write
434      * @param len The number of characters to write
435      */

436     public void write(String JavaDoc s, int off, int len) {
437
438         for (int i = off; i < len; i++)
439             write(s.charAt(i));
440
441     }
442
443
444 }
445
Popular Tags