KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > applications > StringPrintWriter


1 /*
2  * ____.
3  * __/\ ______| |__/\. _______
4  * __ .____| | \ | +----+ \
5  * _______| /--| | | - \ _ | : - \_________
6  * \\______: :---| : : | : | \________>
7  * |__\---\_____________:______: :____|____:_____\
8  * /_____|
9  *
10  * . . . i n j a h i a w e t r u s t . . .
11  *
12  *
13  *
14  * ----- BEGIN LICENSE BLOCK -----
15  * Version: JCSL 1.0
16  *
17  * The contents of this file are subject to the Jahia Community Source License
18  * 1.0 or later (the "License"); you may not use this file except in
19  * compliance with the License. You may obtain a copy of the License at
20  * http://www.jahia.org/license
21  *
22  * Software distributed under the License is distributed on an "AS IS" basis,
23  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
24  * for the rights, obligations and limitations governing use of the contents
25  * of the file. The Original and Upgraded Code is the Jahia CMS and Portal
26  * Server. The developer of the Original and Upgraded Code is JAHIA Ltd. JAHIA
27  * Ltd. owns the copyrights in the portions it created. All Rights Reserved.
28  *
29  * The Shared Modifications are Jahia View Helper.
30  *
31  * The Developer of the Shared Modifications is Jahia Solution S�rl.
32  * Portions created by the Initial Developer are Copyright (C) 2002 by the
33  * Initial Developer. All Rights Reserved.
34  *
35  * Contributor(s):
36  * 22-AUG-2003, Jahia Solutions Sarl, Fulco Houkes
37  *
38  * ----- END LICENSE BLOCK -----
39  */

40
41
42 package org.jahia.services.applications;
43
44 import java.io.PrintWriter JavaDoc;
45 import java.io.Writer JavaDoc;
46
47
48 /**
49  * A simple PrintWriter based class that writes to a String rather than a OutputStream or such...
50  * Personal note : this isn't the most fascinating code I've ever written, but it is a sort of
51  * emulator we are writing here so... :)
52  * Note : none of this is supposed to be optimised in anyway, it was done a quick and dirty way
53  * using mostly already available functions calls to make this reliable.
54  * CHECKME : should be complete, bugs may subsist in some conversion, see CHECKMEs below
55  *
56  * @author : Serge Huber
57  */

58 public class StringPrintWriter extends PrintWriter JavaDoc {
59
60     /** logging */
61     private static org.apache.log4j.Logger logger =
62             org.apache.log4j.Logger.getLogger (StringPrintWriter.class);
63
64     private StringBuffer JavaDoc strBuffer = new StringBuffer JavaDoc (4096);
65     private static boolean debugPrintWriter = false;
66
67     public StringPrintWriter (java.io.OutputStream JavaDoc out) {
68         super (out);
69     }
70
71     public StringPrintWriter (Writer JavaDoc out) {
72         super (out);
73     }
74
75     public StringPrintWriter (java.io.OutputStream JavaDoc out, boolean autoFlush) {
76         super (out, autoFlush);
77     }
78
79     public StringPrintWriter (Writer JavaDoc writer, boolean autoFlush) {
80         super (writer, autoFlush);
81     }
82
83     public StringPrintWriter (Writer JavaDoc writer, StringBuffer JavaDoc newBuffer) {
84         super (writer);
85         strBuffer = newBuffer;
86     }
87
88     public void print (boolean b) {
89         strBuffer.append (b);
90     }
91
92     public void print (char c) {
93         // debugLog("StringPrintWriter.print", "char");
94
strBuffer.append (c);
95     }
96
97     public void print (char[] s) {
98         // debugLog("StringPrintWriter.print", "char[]");
99
strBuffer.append (s);
100     }
101
102     public void print (double d) {
103         strBuffer.append (d);
104     }
105
106     public void print (float f) {
107         strBuffer.append (f);
108     }
109
110     public void print (int i) {
111         strBuffer.append (i);
112     }
113
114     public void print (long l) {
115         strBuffer.append (l);
116     }
117
118     public void print (Object JavaDoc obj) {
119         strBuffer.append (obj);
120     }
121
122     public void print (String JavaDoc s) {
123         // debugLog("StringPrintWriter.print", "String");
124
strBuffer.append (s);
125     }
126
127     public void println () {
128         // debugLog("StringPrintWriter.println", "(no arg)");
129
strBuffer.append ("\n");
130     }
131
132     public void println (boolean b) {
133         strBuffer.append (b);
134         strBuffer.append ("\n");
135     }
136
137     public void println (char c) {
138         // debugLog("StringPrintWriter.println", "char");
139
strBuffer.append (c);
140         strBuffer.append ("\n");
141     }
142
143     public void println (char[] s) {
144         // debugLog("StringPrintWriter.println", "char[]");
145
strBuffer.append (s);
146         strBuffer.append ("\n");
147     }
148
149     public void println (double d) {
150         strBuffer.append (d);
151         strBuffer.append ("\n");
152     }
153
154     public void println (float f) {
155         strBuffer.append (f);
156         strBuffer.append ("\n");
157     }
158
159     public void println (int i) {
160         strBuffer.append (i);
161         strBuffer.append ("\n");
162     }
163
164     public void println (long l) {
165         strBuffer.append (l);
166         strBuffer.append ("\n");
167     }
168
169     public void println (Object JavaDoc obj) {
170         strBuffer.append (obj);
171         strBuffer.append ("\n");
172     }
173
174     public void println (String JavaDoc x) {
175         // debugLog("StringPrintWriter.println", "String");
176
strBuffer.append (x);
177         strBuffer.append ("\n");
178     }
179
180     public void write (char[] cbuf) {
181         // debugLog("StringPrintWriter.write", "char[]");
182
strBuffer.append (cbuf);
183     }
184
185     public void write (char[] cbuf, int off, int len) {
186         // debugLog("StringPrintWriter.write", "char[], offset+length");
187
strBuffer.append (cbuf, off, len);
188     }
189
190     public void write (int c) {
191         // CHECKME : why is this an int and not a char ??
192
// debugLog("StringPrintWriter.write", "int");
193
strBuffer.append ((char) c);
194     }
195
196     public void write (String JavaDoc s) {
197         // debugLog("StringPrintWriter.write", "String[" + s + "]");
198
strBuffer.append (s);
199     }
200
201     public void write (String JavaDoc str, int off, int len) {
202         // debugLog("StringPrintWriter.write", "String, offset+length");
203
strBuffer.append (str.substring (off, off + len)); // CHECKME : are the indexes correct here ?
204
}
205
206     public String JavaDoc getBuffer () {
207         return strBuffer.toString ();
208     }
209
210     public void flush () {
211     }
212
213     public void close () {
214     }
215
216     public boolean checkError () {
217         // debugLog("StringPrinterWriter.checkError()", "called.");
218
return false;
219     }
220
221     private static void debugLog (String JavaDoc message) {
222         if (debugPrintWriter) {
223             logger.debug (message);
224         }
225     }
226
227 }
228
Popular Tags