2
* $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/util/ServletContextWriter.java,v 1.5 2004/03/14 06:23:51 sraeburn Exp $
4
* $Date: 2004/03/14 06:23:51 $
6
* Copyright 2000-2004 The Apache Software Foundation.
8
* Licensed under the Apache License, Version 2.0 (the "License");
9
* you may not use this file except in compliance with the License.
10
* You may obtain a copy of the License at
12
* http://www.apache.org/licenses/LICENSE-2.0
14
* Unless required by applicable law or agreed to in writing, software
15
* distributed under the License is distributed on an "AS IS" BASIS,
16
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
* See the License for the specific language governing permissions and
18
* limitations under the License.
21
package org.apache.struts.util;
23
import java.io.PrintWriter;
24
import java.io.StringWriter;
25
import javax.servlet.ServletContext;
28
* A PrintWriter implementation that uses the logging facilities of a
29
* <code>javax.servlet.ServletContext</code> to output its results. Output
30
* will be buffered until a newline character is output, <code>flush()</code>
31
* is called, or until one of the <code>println()</code> methods is called.
32
* Along the way, carriage return characters are skipped.
34
* @version $Revision: 1.5 $ $Date: 2004/03/14 06:23:51 $
36
public class ServletContextWriter extends PrintWriter {
39
// ----------------------------------------------------------- Constructors
43
* Construct a ServletContextWriter associated with the specified
44
* ServletContext instance.
46
* @param context The associated servlet context
48
public ServletContextWriter(ServletContext context) {
50
super(new StringWriter());
51
this.context = context;
56
// ------------------------------------------------------------- Properties
60
* The buffer into which we accumulate lines to be logged.
62
protected StringBuffer buffer = new StringBuffer();
66
* The servlet context with which we are associated.
68
protected ServletContext context = null;
72
* The error state for this stream.
74
protected boolean error = false;
77
// --------------------------------------------------------- Public Methods
81
* Flush the stream and check for its error state. <strong>IMPLEMENTATION
82
* NOTE</strong> - our associated servlet context gives no indication of
83
* problems with logging, so the only way this method will return
84
* <code>true</code> is if <code>setError()</code> is called.
86
public boolean checkError() {
107
public void flush() {
109
if (buffer.length() > 0) {
110
context.log(buffer.toString());
118
* Print a boolean value.
120
* @param b The value to be printed
122
public void print(boolean b) {
124
write(String.valueOf(b));
130
* Print a character value.
132
* @param c The value to be printed
134
public void print(char c) {
142
* Print a character array.
144
* @param c The character array to be printed
146
public void print(char c[]) {
148
for (int i = 0; i < c.length; i++)
155
* Print a double value.
157
* @param d The value to be printed
159
public void print(double d) {
161
write(String.valueOf(d));
167
* Print a float value.
169
* @param f The value to be printed
171
public void print(float f) {
173
write(String.valueOf(f));
179
* Print an integer value.
181
* @param i The value to be printed
183
public void print(int i) {
185
write(String.valueOf(i));
191
* Print a long value.
193
* @param l The value to be printed
195
public void print(long l) {
197
write(String.valueOf(l));
205
* @param o The value to be printed
207
public void print(Object o) {
215
* Print a String value.
217
* @param s The value to be printed
219
public void print(String s) {
221
int len = s.length();
222
for (int i = 0; i < len; i++)
229
* Terminate the current line and flush the buffer.
231
public void println() {
239
* Print a boolean value and terminate the line.
241
* @param b The value to be printed
243
public void println(boolean b) {
245
println(String.valueOf(b));
251
* Print a character value and terminate the line.
253
* @param c The value to be printed
255
public void println(char c) {
264
* Print a character array and terminate the line.
266
* @param c The character array to be printed
268
public void println(char c[]) {
270
for (int i = 0; i < c.length; i++)
278
* Print a double value and terminate the line.
280
* @param d The value to be printed
282
public void println(double d) {
284
println(String.valueOf(d));
290
* Print a float value and terminate the line.
292
* @param f The value to be printed
294
public void println(float f) {
296
println(String.valueOf(f));
302
* Print an integer value and terminate the line.
304
* @param i The value to be printed
306
public void println(int i) {
308
println(String.valueOf(i));
314
* Print a long value and terminate the line.
316
* @param l The value to be printed
318
public void println(long l) {
320
println(String.valueOf(l));
326
* Print an object and terminate the line.
328
* @param o The value to be printed
330
public void println(Object o) {
332
println(o.toString());
338
* Print a String value and terminate the line.
340
* @param s The value to be printed
342
public void println(String s) {
344
int len = s.length();
345
for (int i = 0; i < len; i++)
353
* Set the error state for this stream.
355
public void setError() {
363
* Write a single character to this stream.
365
* @param c The character to be written
367
public void write(char c) {
378
* Write a single character to this stream.
380
* @param c The character to be written
382
public void write(int c) {
390
* Write an array of charaters to this stream.
392
* @param buf The character array to be written
394
public void write(char buf[]) {
396
for (int i = 0; i < buf.length; i++)
403
* Write the specified subset of an array of characters to this stream.
405
* @param buf The character array from which to write
406
* @param off The zero-relative starting offset to write
407
* @param len The number of characters to write
409
public void write(char buf[], int off, int len) {
411
for (int i = off; i < len; i++)
418
* Write a String to this stream.
420
* @param s The string to be written
422
public void write(String s) {
424
int len = s.length();
425
for (int i = 0; i < len; i++)
432
* Write the specified portion of a String to this stream.
434
* @param s The String from which to write
435
* @param off The zero-relative starting offset to write
436
* @param len The number of characters to write
438
public void write(String s, int off, int len) {
440
for (int i = off; i < len; i++)