View Javadoc

1   /*
2    * $Id: ServletContextWriter.java 421119 2006-07-12 04:49:11Z wsmoak $
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  package org.apache.struts.util;
19  
20  import javax.servlet.ServletContext;
21  
22  import java.io.PrintWriter;
23  import java.io.StringWriter;
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: 421119 $ $Date: 2005-05-07 12:11:38 -0400 (Sat, 07 May 2005)
33   *          $
34   */
35  public class ServletContextWriter extends PrintWriter {
36      // ------------------------------------------------------------- Properties
37  
38      /***
39       * The buffer into which we accumulate lines to be logged.
40       */
41      protected StringBuffer buffer = new StringBuffer();
42  
43      /***
44       * The servlet context with which we are associated.
45       */
46      protected ServletContext context = null;
47  
48      /***
49       * The error state for this stream.
50       */
51      protected boolean error = false;
52  
53      // ----------------------------------------------------------- Constructors
54  
55      /***
56       * Construct a ServletContextWriter associated with the specified
57       * ServletContext instance.
58       *
59       * @param context The associated servlet context
60       */
61      public ServletContextWriter(ServletContext context) {
62          super(new StringWriter());
63          this.context = context;
64      }
65  
66      // --------------------------------------------------------- Public Methods
67  
68      /***
69       * Flush the stream and check for its error state.  <strong>IMPLEMENTATION
70       * NOTE</strong> - our associated servlet context gives no indication of
71       * problems with logging, so the only way this method will return
72       * <code>true</code> is if <code>setError()</code> is called.
73       */
74      public boolean checkError() {
75          flush();
76  
77          return (error);
78      }
79  
80      /***
81       * Close the stream.
82       */
83      public void close() {
84          flush();
85      }
86  
87      /***
88       * Flush the stream.
89       */
90      public void flush() {
91          if (buffer.length() > 0) {
92              context.log(buffer.toString());
93              buffer.setLength(0);
94          }
95      }
96  
97      /***
98       * Print a boolean value.
99       *
100      * @param b The value to be printed
101      */
102     public void print(boolean b) {
103         write(String.valueOf(b));
104     }
105 
106     /***
107      * Print a character value.
108      *
109      * @param c The value to be printed
110      */
111     public void print(char c) {
112         write(c);
113     }
114 
115     /***
116      * Print a character array.
117      *
118      * @param c The character array to be printed
119      */
120     public void print(char[] c) {
121         for (int i = 0; i < c.length; i++) {
122             write(c[i]);
123         }
124     }
125 
126     /***
127      * Print a double value.
128      *
129      * @param d The value to be printed
130      */
131     public void print(double d) {
132         write(String.valueOf(d));
133     }
134 
135     /***
136      * Print a float value.
137      *
138      * @param f The value to be printed
139      */
140     public void print(float f) {
141         write(String.valueOf(f));
142     }
143 
144     /***
145      * Print an integer value.
146      *
147      * @param i The value to be printed
148      */
149     public void print(int i) {
150         write(String.valueOf(i));
151     }
152 
153     /***
154      * Print a long value.
155      *
156      * @param l The value to be printed
157      */
158     public void print(long l) {
159         write(String.valueOf(l));
160     }
161 
162     /***
163      * Print an object.
164      *
165      * @param o The value to be printed
166      */
167     public void print(Object o) {
168         write(o.toString());
169     }
170 
171     /***
172      * Print a String value.
173      *
174      * @param s The value to be printed
175      */
176     public void print(String s) {
177         int len = s.length();
178 
179         for (int i = 0; i < len; i++) {
180             write(s.charAt(i));
181         }
182     }
183 
184     /***
185      * Terminate the current line and flush the buffer.
186      */
187     public void println() {
188         flush();
189     }
190 
191     /***
192      * Print a boolean value and terminate the line.
193      *
194      * @param b The value to be printed
195      */
196     public void println(boolean b) {
197         println(String.valueOf(b));
198     }
199 
200     /***
201      * Print a character value and terminate the line.
202      *
203      * @param c The value to be printed
204      */
205     public void println(char c) {
206         write(c);
207         println();
208     }
209 
210     /***
211      * Print a character array and terminate the line.
212      *
213      * @param c The character array to be printed
214      */
215     public void println(char[] c) {
216         for (int i = 0; i < c.length; i++) {
217             print(c[i]);
218         }
219 
220         println();
221     }
222 
223     /***
224      * Print a double value and terminate the line.
225      *
226      * @param d The value to be printed
227      */
228     public void println(double d) {
229         println(String.valueOf(d));
230     }
231 
232     /***
233      * Print a float value and terminate the line.
234      *
235      * @param f The value to be printed
236      */
237     public void println(float f) {
238         println(String.valueOf(f));
239     }
240 
241     /***
242      * Print an integer value and terminate the line.
243      *
244      * @param i The value to be printed
245      */
246     public void println(int i) {
247         println(String.valueOf(i));
248     }
249 
250     /***
251      * Print a long value and terminate the line.
252      *
253      * @param l The value to be printed
254      */
255     public void println(long l) {
256         println(String.valueOf(l));
257     }
258 
259     /***
260      * Print an object and terminate the line.
261      *
262      * @param o The value to be printed
263      */
264     public void println(Object o) {
265         println(o.toString());
266     }
267 
268     /***
269      * Print a String value and terminate the line.
270      *
271      * @param s The value to be printed
272      */
273     public void println(String s) {
274         int len = s.length();
275 
276         for (int i = 0; i < len; i++) {
277             print(s.charAt(i));
278         }
279 
280         println();
281     }
282 
283     /***
284      * Set the error state for this stream.
285      */
286     public void setError() {
287         this.error = true;
288     }
289 
290     /***
291      * Write a single character to this stream.
292      *
293      * @param c The character to be written
294      */
295     public void write(char c) {
296         if (c == '\n') {
297             flush();
298         } else if (c != '\r') {
299             buffer.append(c);
300         }
301     }
302 
303     /***
304      * Write a single character to this stream.
305      *
306      * @param c The character to be written
307      */
308     public void write(int c) {
309         write((char) c);
310     }
311 
312     /***
313      * Write an array of charaters to this stream.
314      *
315      * @param buf The character array to be written
316      */
317     public void write(char[] buf) {
318         for (int i = 0; i < buf.length; i++) {
319             write(buf[i]);
320         }
321     }
322 
323     /***
324      * Write the specified subset of an array of characters to this stream.
325      *
326      * @param buf The character array from which to write
327      * @param off The zero-relative starting offset to write
328      * @param len The number of characters to write
329      */
330     public void write(char[] buf, int off, int len) {
331         for (int i = off; i < len; i++) {
332             write(buf[i]);
333         }
334     }
335 
336     /***
337      * Write a String to this stream.
338      *
339      * @param s The string to be written
340      */
341     public void write(String s) {
342         int len = s.length();
343 
344         for (int i = 0; i < len; i++) {
345             write(s.charAt(i));
346         }
347     }
348 
349     /***
350      * Write the specified portion of a String to this stream.
351      *
352      * @param s   The String from which to write
353      * @param off The zero-relative starting offset to write
354      * @param len The number of characters to write
355      */
356     public void write(String s, int off, int len) {
357         for (int i = off; i < len; i++) {
358             write(s.charAt(i));
359         }
360     }
361 }