001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.jsp;
031:
032: import java.io.IOException;
033: import java.io.Reader;
034: import java.io.Writer;
035:
036: /**
037: * A buffered JSP writer encapsulating a Writer.
038: */
039: abstract class AbstractBodyContent extends AbstractJspWriter {
040: private static final char[] _trueChars = "true".toCharArray();
041: private static final char[] _falseChars = "false".toCharArray();
042: private static final char[] _nullChars = "null".toCharArray();
043:
044: private final char[] _tempCharBuffer = new char[256];
045:
046: private boolean _isPrintNullAsBlank;
047:
048: public void setPrintNullAsBlank(boolean enable) {
049: _isPrintNullAsBlank = enable;
050: }
051:
052: /**
053: * Writes a character array to the writer.
054: *
055: * @param buf the buffer to write.
056: * @param off the offset into the buffer
057: * @param len the number of characters to write
058: */
059: abstract public void write(char[] buf, int offset, int length)
060: throws IOException;
061:
062: /**
063: * Writes a character to the output.
064: *
065: * @param buf the buffer to write.
066: */
067: abstract public void write(int ch) throws IOException;
068:
069: /**
070: * Writes a char buffer to the output.
071: *
072: * @param buf the buffer to write.
073: */
074: final public void write(char[] buf) throws IOException {
075: write(buf, 0, buf.length);
076: }
077:
078: /**
079: * Writes a string to the output.
080: */
081: final public void write(String s) throws IOException {
082: write(s, 0, s.length());
083: }
084:
085: /**
086: * Writes a subsection of a string to the output.
087: */
088: public void write(String s, int off, int len) throws IOException {
089: while (len > 0) {
090: int sublen = _tempCharBuffer.length;
091:
092: if (len < sublen)
093: sublen = len;
094:
095: s.getChars(off, off + sublen, _tempCharBuffer, 0);
096:
097: write(_tempCharBuffer, 0, sublen);
098:
099: len -= sublen;
100: off += sublen;
101: }
102: }
103:
104: /**
105: * Writes the newline character.
106: */
107: public void newLine() throws IOException {
108: write('\n');
109: }
110:
111: /**
112: * Prints a boolean.
113: */
114: final public void print(boolean b) throws IOException {
115: write(b ? _trueChars : _falseChars);
116: }
117:
118: /**
119: * Prints a character.
120: */
121: public void print(char ch) throws IOException {
122: write(ch);
123: }
124:
125: public void print(int i) throws IOException {
126: if (i == 0x80000000) {
127: print("-2147483648");
128: return;
129: }
130:
131: if (i < 0) {
132: write('-');
133: i = -i;
134: } else if (i < 9) {
135: write('0' + i);
136: return;
137: }
138:
139: int length = 0;
140: int exp = 10;
141:
142: if (i >= 1000000000)
143: length = 9;
144: else {
145: for (; i >= exp; length++)
146: exp = 10 * exp;
147: }
148:
149: int j = 31;
150:
151: while (i > 0) {
152: _tempCharBuffer[--j] = (char) ((i % 10) + '0');
153: i /= 10;
154: }
155:
156: write(_tempCharBuffer, j, 31 - j);
157: }
158:
159: public void print(long v) throws IOException {
160: if (v == 0x8000000000000000L) {
161: print("-9223372036854775808");
162: return;
163: }
164:
165: if (v < 0) {
166: write('-');
167: v = -v;
168: } else if (v == 0) {
169: write('0');
170: return;
171: }
172:
173: int j = 31;
174:
175: while (v > 0) {
176: _tempCharBuffer[--j] = (char) ((v % 10) + '0');
177: v /= 10;
178: }
179:
180: write(_tempCharBuffer, j, 31 - j);
181: }
182:
183: final public void print(float f) throws IOException {
184: write(String.valueOf(f));
185: }
186:
187: final public void print(double d) throws IOException {
188: write(String.valueOf(d));
189: }
190:
191: /**
192: * Prints a character array
193: */
194: final public void print(char[] s) throws IOException {
195: write(s, 0, s.length);
196: }
197:
198: /**
199: * Prints a string.
200: */
201: final public void print(String s) throws IOException {
202: if (s != null)
203: write(s, 0, s.length());
204: else if (_isPrintNullAsBlank) {
205: } else
206: write(_nullChars, 0, _nullChars.length);
207: }
208:
209: /**
210: * Prints the value of the object.
211: */
212: final public void print(Object v) throws IOException {
213: if (v != null) {
214: String s = v.toString();
215:
216: write(s, 0, s.length());
217: } else if (_isPrintNullAsBlank) {
218: } else
219: write(_nullChars, 0, _nullChars.length);
220: }
221:
222: /**
223: * Prints the newline.
224: */
225: public void println() throws IOException {
226: write('\n');
227: }
228:
229: /**
230: * Prints the boolean followed by a newline.
231: *
232: * @param v the value to print
233: */
234: final public void println(boolean v) throws IOException {
235: print(v);
236: println();
237: }
238:
239: /**
240: * Prints a character followed by a newline.
241: *
242: * @param v the value to print
243: */
244: final public void println(char v) throws IOException {
245: print(v);
246: println();
247: }
248:
249: /**
250: * Prints an integer followed by a newline.
251: *
252: * @param v the value to print
253: */
254: final public void println(int v) throws IOException {
255: print(v);
256: println();
257: }
258:
259: /**
260: * Prints a long followed by a newline.
261: *
262: * @param v the value to print
263: */
264: final public void println(long v) throws IOException {
265: print(v);
266: println();
267: }
268:
269: /**
270: * Prints a float followed by a newline.
271: *
272: * @param v the value to print
273: */
274: final public void println(float v) throws IOException {
275: String s = String.valueOf(v);
276:
277: write(s, 0, s.length());
278: println();
279: }
280:
281: /**
282: * Prints a double followed by a newline.
283: *
284: * @param v the value to print
285: */
286: final public void println(double v) throws IOException {
287: String s = String.valueOf(v);
288:
289: write(s, 0, s.length());
290:
291: println();
292: }
293:
294: /**
295: * Writes a character array followed by a newline.
296: */
297: final public void println(char[] s) throws IOException {
298: write(s, 0, s.length);
299: println();
300: }
301:
302: /**
303: * Writes a string followed by a newline.
304: */
305: final public void println(String s) throws IOException {
306: if (s != null)
307: write(s, 0, s.length());
308: else if (_isPrintNullAsBlank) {
309: } else
310: write(_nullChars, 0, _nullChars.length);
311:
312: println();
313: }
314:
315: /**
316: * Writes an object followed by a newline.
317: */
318: final public void println(Object v) throws IOException {
319: if (v != null) {
320: String s = String.valueOf(v);
321:
322: write(s, 0, s.length());
323: } else if (_isPrintNullAsBlank) {
324: } else
325: write(_nullChars, 0, _nullChars.length);
326:
327: println();
328: }
329:
330: abstract public void clear() throws IOException;
331:
332: abstract public void clearBuffer() throws IOException;
333:
334: abstract public void flushBuffer() throws IOException;
335:
336: abstract public void flush() throws IOException;
337:
338: abstract public void close() throws IOException;
339:
340: abstract public int getBufferSize();
341:
342: abstract public int getRemaining();
343:
344: public void writeOut(Writer writer) throws IOException {
345: throw new UnsupportedOperationException();
346: }
347:
348: public String getString() {
349: throw new UnsupportedOperationException();
350: }
351:
352: public Reader getReader() {
353: throw new UnsupportedOperationException();
354: }
355:
356: public void clearBody() {
357: throw new UnsupportedOperationException();
358: }
359: }
|