~ubuntu-branches/ubuntu/trusty/libstruts1.2-java/trusty-proposed

« back to all changes in this revision

Viewing changes to src/share/org/apache/struts/util/ServletContextWriter.java

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Vandyck
  • Date: 2004-11-19 15:35:25 UTC
  • Revision ID: james.westby@ubuntu.com-20041119153525-mdu08a76z4zo67xt
Tags: upstream-1.2.4
ImportĀ upstreamĀ versionĀ 1.2.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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 $
 
3
 * $Revision: 1.5 $
 
4
 * $Date: 2004/03/14 06:23:51 $
 
5
 *
 
6
 * Copyright 2000-2004 The Apache Software Foundation.
 
7
 * 
 
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
 
11
 * 
 
12
 *      http://www.apache.org/licenses/LICENSE-2.0
 
13
 * 
 
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.
 
19
 */
 
20
 
 
21
package org.apache.struts.util;
 
22
 
 
23
import java.io.PrintWriter;
 
24
import java.io.StringWriter;
 
25
import javax.servlet.ServletContext;
 
26
 
 
27
/**
 
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.
 
33
 *
 
34
 * @version $Revision: 1.5 $ $Date: 2004/03/14 06:23:51 $
 
35
 */
 
36
public class ServletContextWriter extends PrintWriter {
 
37
 
 
38
 
 
39
    // ----------------------------------------------------------- Constructors
 
40
 
 
41
 
 
42
    /**
 
43
     * Construct a ServletContextWriter associated with the specified
 
44
     * ServletContext instance.
 
45
     *
 
46
     * @param context The associated servlet context
 
47
     */
 
48
    public ServletContextWriter(ServletContext context) {
 
49
 
 
50
        super(new StringWriter());
 
51
        this.context = context;
 
52
 
 
53
    }
 
54
 
 
55
 
 
56
    // ------------------------------------------------------------- Properties
 
57
 
 
58
 
 
59
    /**
 
60
     * The buffer into which we accumulate lines to be logged.
 
61
     */
 
62
    protected StringBuffer buffer = new StringBuffer();
 
63
 
 
64
 
 
65
    /**
 
66
     * The servlet context with which we are associated.
 
67
     */
 
68
    protected ServletContext context = null;
 
69
 
 
70
 
 
71
    /**
 
72
     * The error state for this stream.
 
73
     */
 
74
    protected boolean error = false;
 
75
 
 
76
 
 
77
    // --------------------------------------------------------- Public Methods
 
78
 
 
79
 
 
80
    /**
 
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.
 
85
     */
 
86
    public boolean checkError() {
 
87
 
 
88
        flush();
 
89
        return (error);
 
90
 
 
91
    }
 
92
 
 
93
 
 
94
    /**
 
95
     * Close the stream.
 
96
     */
 
97
    public void close() {
 
98
 
 
99
        flush();
 
100
 
 
101
    }
 
102
 
 
103
 
 
104
    /**
 
105
     * Flush the stream.
 
106
     */
 
107
    public void flush() {
 
108
 
 
109
        if (buffer.length() > 0) {
 
110
            context.log(buffer.toString());
 
111
            buffer.setLength(0);
 
112
        }
 
113
 
 
114
    }
 
115
 
 
116
 
 
117
    /**
 
118
     * Print a boolean value.
 
119
     *
 
120
     * @param b The value to be printed
 
121
     */
 
122
    public void print(boolean b) {
 
123
 
 
124
        write(String.valueOf(b));
 
125
 
 
126
    }
 
127
 
 
128
 
 
129
    /**
 
130
     * Print a character value.
 
131
     *
 
132
     * @param c The value to be printed
 
133
     */
 
134
    public void print(char c) {
 
135
 
 
136
        write(c);
 
137
 
 
138
    }
 
139
 
 
140
 
 
141
    /**
 
142
     * Print a character array.
 
143
     *
 
144
     * @param c The character array to be printed
 
145
     */
 
146
    public void print(char c[]) {
 
147
 
 
148
        for (int i = 0; i < c.length; i++)
 
149
            write(c[i]);
 
150
 
 
151
    }
 
152
 
 
153
 
 
154
    /**
 
155
     * Print a double value.
 
156
     *
 
157
     * @param d The value to be printed
 
158
     */
 
159
    public void print(double d) {
 
160
 
 
161
        write(String.valueOf(d));
 
162
 
 
163
    }
 
164
 
 
165
 
 
166
    /**
 
167
     * Print a float value.
 
168
     *
 
169
     * @param f The value to be printed
 
170
     */
 
171
    public void print(float f) {
 
172
 
 
173
        write(String.valueOf(f));
 
174
 
 
175
    }
 
176
 
 
177
 
 
178
    /**
 
179
     * Print an integer value.
 
180
     *
 
181
     * @param i The value to be printed
 
182
     */
 
183
    public void print(int i) {
 
184
 
 
185
        write(String.valueOf(i));
 
186
 
 
187
    }
 
188
 
 
189
 
 
190
    /**
 
191
     * Print a long value.
 
192
     *
 
193
     * @param l The value to be printed
 
194
     */
 
195
    public void print(long l) {
 
196
 
 
197
        write(String.valueOf(l));
 
198
 
 
199
    }
 
200
 
 
201
 
 
202
    /**
 
203
     * Print an object.
 
204
     *
 
205
     * @param o The value to be printed
 
206
     */
 
207
    public void print(Object o) {
 
208
 
 
209
        write(o.toString());
 
210
 
 
211
    }
 
212
 
 
213
 
 
214
    /**
 
215
     * Print a String value.
 
216
     *
 
217
     * @param s The value to be printed
 
218
     */
 
219
    public void print(String s) {
 
220
 
 
221
        int len = s.length();
 
222
        for (int i = 0; i < len; i++)
 
223
            write(s.charAt(i));
 
224
 
 
225
    }
 
226
 
 
227
 
 
228
    /**
 
229
     * Terminate the current line and flush the buffer.
 
230
     */
 
231
    public void println() {
 
232
 
 
233
        flush();
 
234
 
 
235
    }
 
236
 
 
237
 
 
238
    /**
 
239
     * Print a boolean value and terminate the line.
 
240
     *
 
241
     * @param b The value to be printed
 
242
     */
 
243
    public void println(boolean b) {
 
244
 
 
245
        println(String.valueOf(b));
 
246
 
 
247
    }
 
248
 
 
249
 
 
250
    /**
 
251
     * Print a character value and terminate the line.
 
252
     *
 
253
     * @param c The value to be printed
 
254
     */
 
255
    public void println(char c) {
 
256
 
 
257
        write(c);
 
258
        println();
 
259
 
 
260
    }
 
261
 
 
262
 
 
263
    /**
 
264
     * Print a character array and terminate the line.
 
265
     *
 
266
     * @param c The character array to be printed
 
267
     */
 
268
    public void println(char c[]) {
 
269
 
 
270
        for (int i = 0; i < c.length; i++)
 
271
            print(c[i]);
 
272
        println();
 
273
 
 
274
    }
 
275
 
 
276
 
 
277
    /**
 
278
     * Print a double value and terminate the line.
 
279
     *
 
280
     * @param d The value to be printed
 
281
     */
 
282
    public void println(double d) {
 
283
 
 
284
        println(String.valueOf(d));
 
285
 
 
286
    }
 
287
 
 
288
 
 
289
    /**
 
290
     * Print a float value and terminate the line.
 
291
     *
 
292
     * @param f The value to be printed
 
293
     */
 
294
    public void println(float f) {
 
295
 
 
296
        println(String.valueOf(f));
 
297
 
 
298
    }
 
299
 
 
300
 
 
301
    /**
 
302
     * Print an integer value and terminate the line.
 
303
     *
 
304
     * @param i The value to be printed
 
305
     */
 
306
    public void println(int i) {
 
307
 
 
308
        println(String.valueOf(i));
 
309
 
 
310
    }
 
311
 
 
312
 
 
313
    /**
 
314
     * Print a long value and terminate the line.
 
315
     *
 
316
     * @param l The value to be printed
 
317
     */
 
318
    public void println(long l) {
 
319
 
 
320
        println(String.valueOf(l));
 
321
 
 
322
    }
 
323
 
 
324
 
 
325
    /**
 
326
     * Print an object and terminate the line.
 
327
     *
 
328
     * @param o The value to be printed
 
329
     */
 
330
    public void println(Object o) {
 
331
 
 
332
        println(o.toString());
 
333
 
 
334
    }
 
335
 
 
336
 
 
337
    /**
 
338
     * Print a String value and terminate the line.
 
339
     *
 
340
     * @param s The value to be printed
 
341
     */
 
342
    public void println(String s) {
 
343
 
 
344
        int len = s.length();
 
345
        for (int i = 0; i < len; i++)
 
346
            print(s.charAt(i));
 
347
        println();
 
348
 
 
349
    }
 
350
 
 
351
 
 
352
    /**
 
353
     * Set the error state for this stream.
 
354
     */
 
355
    public void setError() {
 
356
 
 
357
        this.error = true;
 
358
 
 
359
    }
 
360
 
 
361
 
 
362
    /**
 
363
     * Write a single character to this stream.
 
364
     *
 
365
     * @param c The character to be written
 
366
     */
 
367
    public void write(char c) {
 
368
 
 
369
        if (c == '\n')
 
370
            flush();
 
371
        else if (c != '\r')
 
372
            buffer.append(c);
 
373
 
 
374
    }
 
375
 
 
376
 
 
377
    /**
 
378
     * Write a single character to this stream.
 
379
     *
 
380
     * @param c The character to be written
 
381
     */
 
382
    public void write(int c) {
 
383
 
 
384
        write((char) c);
 
385
 
 
386
    }
 
387
 
 
388
 
 
389
    /**
 
390
     * Write an array of charaters to this stream.
 
391
     *
 
392
     * @param buf The character array to be written
 
393
     */
 
394
    public void write(char buf[]) {
 
395
 
 
396
        for (int i = 0; i < buf.length; i++)
 
397
            write(buf[i]);
 
398
 
 
399
    }
 
400
 
 
401
 
 
402
    /**
 
403
     * Write the specified subset of an array of characters to this stream.
 
404
     *
 
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
 
408
     */
 
409
    public void write(char buf[], int off, int len) {
 
410
 
 
411
        for (int i = off; i < len; i++)
 
412
            write(buf[i]);
 
413
 
 
414
    }
 
415
 
 
416
 
 
417
    /**
 
418
     * Write a String to this stream.
 
419
     *
 
420
     * @param s The string to be written
 
421
     */
 
422
    public void write(String s) {
 
423
 
 
424
        int len = s.length();
 
425
        for (int i = 0; i < len; i++)
 
426
            write(s.charAt(i));
 
427
 
 
428
    }
 
429
 
 
430
 
 
431
    /**
 
432
     * Write the specified portion of a String to this stream.
 
433
     *
 
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
 
437
     */
 
438
    public void write(String s, int off, int len) {
 
439
 
 
440
        for (int i = off; i < len; i++)
 
441
            write(s.charAt(i));
 
442
 
 
443
    }
 
444
 
 
445
 
 
446
}