001: /*
002: * $Id: ServletContextWriter.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts.util;
022:
023: import javax.servlet.ServletContext;
024:
025: import java.io.PrintWriter;
026: import java.io.StringWriter;
027:
028: /**
029: * A PrintWriter implementation that uses the logging facilities of a
030: * <code>javax.servlet.ServletContext</code> to output its results. Output
031: * will be buffered until a newline character is output, <code>flush()</code>
032: * is called, or until one of the <code>println()</code> methods is called.
033: * Along the way, carriage return characters are skipped.
034: *
035: * @version $Rev: 471754 $ $Date: 2005-05-07 12:11:38 -0400 (Sat, 07 May 2005)
036: * $
037: */
038: public class ServletContextWriter extends PrintWriter {
039: // ------------------------------------------------------------- Properties
040:
041: /**
042: * The buffer into which we accumulate lines to be logged.
043: */
044: protected StringBuffer buffer = new StringBuffer();
045:
046: /**
047: * The servlet context with which we are associated.
048: */
049: protected ServletContext context = null;
050:
051: /**
052: * The error state for this stream.
053: */
054: protected boolean error = false;
055:
056: // ----------------------------------------------------------- Constructors
057:
058: /**
059: * Construct a ServletContextWriter associated with the specified
060: * ServletContext instance.
061: *
062: * @param context The associated servlet context
063: */
064: public ServletContextWriter(ServletContext context) {
065: super (new StringWriter());
066: this .context = context;
067: }
068:
069: // --------------------------------------------------------- Public Methods
070:
071: /**
072: * Flush the stream and check for its error state. <strong>IMPLEMENTATION
073: * NOTE</strong> - our associated servlet context gives no indication of
074: * problems with logging, so the only way this method will return
075: * <code>true</code> is if <code>setError()</code> is called.
076: */
077: public boolean checkError() {
078: flush();
079:
080: return (error);
081: }
082:
083: /**
084: * Close the stream.
085: */
086: public void close() {
087: flush();
088: }
089:
090: /**
091: * Flush the stream.
092: */
093: public void flush() {
094: if (buffer.length() > 0) {
095: context.log(buffer.toString());
096: buffer.setLength(0);
097: }
098: }
099:
100: /**
101: * Print a boolean value.
102: *
103: * @param b The value to be printed
104: */
105: public void print(boolean b) {
106: write(String.valueOf(b));
107: }
108:
109: /**
110: * Print a character value.
111: *
112: * @param c The value to be printed
113: */
114: public void print(char c) {
115: write(c);
116: }
117:
118: /**
119: * Print a character array.
120: *
121: * @param c The character array to be printed
122: */
123: public void print(char[] c) {
124: for (int i = 0; i < c.length; i++) {
125: write(c[i]);
126: }
127: }
128:
129: /**
130: * Print a double value.
131: *
132: * @param d The value to be printed
133: */
134: public void print(double d) {
135: write(String.valueOf(d));
136: }
137:
138: /**
139: * Print a float value.
140: *
141: * @param f The value to be printed
142: */
143: public void print(float f) {
144: write(String.valueOf(f));
145: }
146:
147: /**
148: * Print an integer value.
149: *
150: * @param i The value to be printed
151: */
152: public void print(int i) {
153: write(String.valueOf(i));
154: }
155:
156: /**
157: * Print a long value.
158: *
159: * @param l The value to be printed
160: */
161: public void print(long l) {
162: write(String.valueOf(l));
163: }
164:
165: /**
166: * Print an object.
167: *
168: * @param o The value to be printed
169: */
170: public void print(Object o) {
171: write(o.toString());
172: }
173:
174: /**
175: * Print a String value.
176: *
177: * @param s The value to be printed
178: */
179: public void print(String s) {
180: int len = s.length();
181:
182: for (int i = 0; i < len; i++) {
183: write(s.charAt(i));
184: }
185: }
186:
187: /**
188: * Terminate the current line and flush the buffer.
189: */
190: public void println() {
191: flush();
192: }
193:
194: /**
195: * Print a boolean value and terminate the line.
196: *
197: * @param b The value to be printed
198: */
199: public void println(boolean b) {
200: println(String.valueOf(b));
201: }
202:
203: /**
204: * Print a character value and terminate the line.
205: *
206: * @param c The value to be printed
207: */
208: public void println(char c) {
209: write(c);
210: println();
211: }
212:
213: /**
214: * Print a character array and terminate the line.
215: *
216: * @param c The character array to be printed
217: */
218: public void println(char[] c) {
219: for (int i = 0; i < c.length; i++) {
220: print(c[i]);
221: }
222:
223: println();
224: }
225:
226: /**
227: * Print a double value and terminate the line.
228: *
229: * @param d The value to be printed
230: */
231: public void println(double d) {
232: println(String.valueOf(d));
233: }
234:
235: /**
236: * Print a float value and terminate the line.
237: *
238: * @param f The value to be printed
239: */
240: public void println(float f) {
241: println(String.valueOf(f));
242: }
243:
244: /**
245: * Print an integer value and terminate the line.
246: *
247: * @param i The value to be printed
248: */
249: public void println(int i) {
250: println(String.valueOf(i));
251: }
252:
253: /**
254: * Print a long value and terminate the line.
255: *
256: * @param l The value to be printed
257: */
258: public void println(long l) {
259: println(String.valueOf(l));
260: }
261:
262: /**
263: * Print an object and terminate the line.
264: *
265: * @param o The value to be printed
266: */
267: public void println(Object o) {
268: println(o.toString());
269: }
270:
271: /**
272: * Print a String value and terminate the line.
273: *
274: * @param s The value to be printed
275: */
276: public void println(String s) {
277: int len = s.length();
278:
279: for (int i = 0; i < len; i++) {
280: print(s.charAt(i));
281: }
282:
283: println();
284: }
285:
286: /**
287: * Set the error state for this stream.
288: */
289: public void setError() {
290: this .error = true;
291: }
292:
293: /**
294: * Write a single character to this stream.
295: *
296: * @param c The character to be written
297: */
298: public void write(char c) {
299: if (c == '\n') {
300: flush();
301: } else if (c != '\r') {
302: buffer.append(c);
303: }
304: }
305:
306: /**
307: * Write a single character to this stream.
308: *
309: * @param c The character to be written
310: */
311: public void write(int c) {
312: write((char) c);
313: }
314:
315: /**
316: * Write an array of charaters to this stream.
317: *
318: * @param buf The character array to be written
319: */
320: public void write(char[] buf) {
321: for (int i = 0; i < buf.length; i++) {
322: write(buf[i]);
323: }
324: }
325:
326: /**
327: * Write the specified subset of an array of characters to this stream.
328: *
329: * @param buf The character array from which to write
330: * @param off The zero-relative starting offset to write
331: * @param len The number of characters to write
332: */
333: public void write(char[] buf, int off, int len) {
334: for (int i = off; i < len; i++) {
335: write(buf[i]);
336: }
337: }
338:
339: /**
340: * Write a String to this stream.
341: *
342: * @param s The string to be written
343: */
344: public void write(String s) {
345: int len = s.length();
346:
347: for (int i = 0; i < len; i++) {
348: write(s.charAt(i));
349: }
350: }
351:
352: /**
353: * Write the specified portion of a String to this stream.
354: *
355: * @param s The String from which to write
356: * @param off The zero-relative starting offset to write
357: * @param len The number of characters to write
358: */
359: public void write(String s, int off, int len) {
360: for (int i = off; i < len; i++) {
361: write(s.charAt(i));
362: }
363: }
364: }
|