KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jsp > AbstractBodyContent


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.jsp;
31
32 import java.io.IOException JavaDoc;
33 import java.io.Reader JavaDoc;
34 import java.io.Writer JavaDoc;
35
36 /**
37  * A buffered JSP writer encapsulating a Writer.
38  */

39 abstract class AbstractBodyContent extends AbstractJspWriter {
40   private static final char []_trueChars = "true".toCharArray();
41   private static final char []_falseChars = "false".toCharArray();
42   private static final char []_nullChars = "null".toCharArray();
43
44   private final char []_tempCharBuffer = new char[256];
45
46   private boolean _isPrintNullAsBlank;
47
48   public void setPrintNullAsBlank(boolean enable)
49   {
50     _isPrintNullAsBlank = enable;
51   }
52
53   /**
54    * Writes a character array to the writer.
55    *
56    * @param buf the buffer to write.
57    * @param off the offset into the buffer
58    * @param len the number of characters to write
59    */

60   abstract public void write(char []buf, int offset, int length)
61     throws IOException JavaDoc;
62   
63   /**
64    * Writes a character to the output.
65    *
66    * @param buf the buffer to write.
67    */

68   abstract public void write(int ch) throws IOException JavaDoc;
69
70   /**
71    * Writes a char buffer to the output.
72    *
73    * @param buf the buffer to write.
74    */

75   final public void write(char []buf) throws IOException JavaDoc
76   {
77     write(buf, 0, buf.length);
78   }
79
80   /**
81    * Writes a string to the output.
82    */

83   final public void write(String JavaDoc s) throws IOException JavaDoc
84   {
85     write(s, 0, s.length());
86   }
87
88   /**
89    * Writes a subsection of a string to the output.
90    */

91   public void write(String JavaDoc s, int off, int len) throws IOException JavaDoc
92   {
93     while (len > 0) {
94       int sublen = _tempCharBuffer.length;
95       
96       if (len < sublen)
97     sublen = len;
98       
99       s.getChars(off, off + sublen, _tempCharBuffer, 0);
100
101       write(_tempCharBuffer, 0, sublen);
102
103       len -= sublen;
104       off += sublen;
105     }
106   }
107
108   /**
109    * Writes the newline character.
110    */

111   public void newLine() throws IOException JavaDoc
112   {
113     write('\n');
114   }
115   
116   /**
117    * Prints a boolean.
118    */

119   final public void print(boolean b) throws IOException JavaDoc
120   {
121     write(b ? _trueChars : _falseChars);
122   }
123
124   /**
125    * Prints a character.
126    */

127   public void print(char ch) throws IOException JavaDoc
128   {
129     write(ch);
130   }
131   
132   public void print(int i) throws IOException JavaDoc
133   {
134     if (i == 0x80000000) {
135       print("-2147483648");
136       return;
137     }
138
139     if (i < 0) {
140       write('-');
141       i = -i;
142     } else if (i < 9) {
143       write('0' + i);
144       return;
145     }
146
147     int length = 0;
148     int exp = 10;
149
150     if (i >= 1000000000)
151       length = 9;
152     else {
153       for (; i >= exp; length++)
154         exp = 10 * exp;
155     }
156
157     int j = 31;
158     
159     while (i > 0) {
160       _tempCharBuffer[--j] = (char) ((i % 10) + '0');
161       i /= 10;
162     }
163
164     write(_tempCharBuffer, j, 31 - j);
165   }
166   
167   public void print(long v) throws IOException JavaDoc
168   {
169     if (v == 0x8000000000000000L) {
170       print("-9223372036854775808");
171       return;
172     }
173
174     if (v < 0) {
175       write('-');
176       v = -v;
177     } else if (v == 0) {
178       write('0');
179       return;
180     }
181
182     int j = 31;
183     
184     while (v > 0) {
185       _tempCharBuffer[--j] = (char) ((v % 10) + '0');
186       v /= 10;
187     }
188
189     write(_tempCharBuffer, j, 31 - j);
190   }
191   
192   final public void print(float f) throws IOException JavaDoc
193   {
194     write(String.valueOf(f));
195   }
196   
197   final public void print(double d) throws IOException JavaDoc
198   {
199     write(String.valueOf(d));
200   }
201
202   /**
203    * Prints a character array
204    */

205   final public void print(char []s) throws IOException JavaDoc
206   {
207     write(s, 0, s.length);
208   }
209
210   /**
211    * Prints a string.
212    */

213   final public void print(String JavaDoc s) throws IOException JavaDoc
214   {
215     if (s != null)
216       write(s, 0, s.length());
217     else if (_isPrintNullAsBlank) {
218     }
219     else
220       write(_nullChars, 0, _nullChars.length);
221   }
222
223   /**
224    * Prints the value of the object.
225    */

226   final public void print(Object JavaDoc v) throws IOException JavaDoc
227   {
228     if (v != null) {
229       String JavaDoc s = v.toString();
230       
231       write(s, 0, s.length());
232     }
233     else if (_isPrintNullAsBlank) {
234     }
235     else
236       write(_nullChars, 0, _nullChars.length);
237   }
238
239   /**
240    * Prints the newline.
241    */

242   public void println() throws IOException JavaDoc
243   {
244     write('\n');
245   }
246   
247   /**
248    * Prints the boolean followed by a newline.
249    *
250    * @param v the value to print
251    */

252   final public void println(boolean v) throws IOException JavaDoc
253   {
254     print(v);
255     println();
256   }
257
258   /**
259    * Prints a character followed by a newline.
260    *
261    * @param v the value to print
262    */

263   final public void println(char v) throws IOException JavaDoc
264   {
265     print(v);
266     println();
267   }
268   
269   /**
270    * Prints an integer followed by a newline.
271    *
272    * @param v the value to print
273    */

274   final public void println(int v) throws IOException JavaDoc
275   {
276     print(v);
277     println();
278   }
279   
280   /**
281    * Prints a long followed by a newline.
282    *
283    * @param v the value to print
284    */

285   final public void println(long v) throws IOException JavaDoc
286   {
287     print(v);
288     println();
289   }
290   
291   /**
292    * Prints a float followed by a newline.
293    *
294    * @param v the value to print
295    */

296   final public void println(float v) throws IOException JavaDoc
297   {
298     String JavaDoc s = String.valueOf(v);
299     
300     write(s, 0, s.length());
301     println();
302   }
303   
304   
305   /**
306    * Prints a double followed by a newline.
307    *
308    * @param v the value to print
309    */

310   final public void println(double v) throws IOException JavaDoc
311   {
312     String JavaDoc s = String.valueOf(v);
313     
314     write(s, 0, s.length());
315     
316     println();
317   }
318
319   /**
320    * Writes a character array followed by a newline.
321    */

322   final public void println(char []s) throws IOException JavaDoc
323   {
324     write(s, 0, s.length);
325     println();
326   }
327
328   /**
329    * Writes a string followed by a newline.
330    */

331   final public void println(String JavaDoc s) throws IOException JavaDoc
332   {
333     if (s != null)
334       write(s, 0, s.length());
335     else if (_isPrintNullAsBlank) {
336     }
337     else
338       write(_nullChars, 0, _nullChars.length);
339
340     println();
341   }
342   
343   /**
344    * Writes an object followed by a newline.
345    */

346   final public void println(Object JavaDoc v) throws IOException JavaDoc
347   {
348     if (v != null) {
349       String JavaDoc s = String.valueOf(v);
350
351       write(s, 0, s.length());
352     }
353     else if (_isPrintNullAsBlank) {
354     }
355     else
356       write(_nullChars, 0, _nullChars.length);
357     
358     println();
359   }
360
361   abstract public void clear() throws IOException JavaDoc;
362
363   abstract public void clearBuffer() throws IOException JavaDoc;
364
365   abstract public void flushBuffer()
366     throws IOException JavaDoc;
367
368   abstract public void flush() throws IOException JavaDoc;
369
370   abstract public void close() throws IOException JavaDoc;
371
372   abstract public int getBufferSize();
373
374   abstract public int getRemaining();
375
376   public void writeOut(Writer JavaDoc writer) throws IOException JavaDoc
377   {
378     throw new UnsupportedOperationException JavaDoc();
379   }
380
381   public String JavaDoc getString()
382   {
383     throw new UnsupportedOperationException JavaDoc();
384   }
385
386   public Reader JavaDoc getReader()
387   {
388     throw new UnsupportedOperationException JavaDoc();
389   }
390
391   public void clearBody()
392   {
393     throw new UnsupportedOperationException JavaDoc();
394   }
395 }
396
Popular Tags