KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.caucho.log.Log;
33 import com.caucho.util.CharBuffer;
34 import com.caucho.util.FreeList;
35 import com.caucho.util.L10N;
36 import com.caucho.vfs.TempCharBuffer;
37 import com.caucho.vfs.TempCharReader;
38 import com.caucho.vfs.TempCharStream;
39
40 import javax.servlet.jsp.JspWriter JavaDoc;
41 import java.io.IOException JavaDoc;
42 import java.io.PrintWriter JavaDoc;
43 import java.io.Reader JavaDoc;
44 import java.io.Writer JavaDoc;
45 import java.util.logging.Logger JavaDoc;
46
47 /**
48  * Implementation of the JSP BodyContent interface.
49  */

50 public class BodyContentImpl extends AbstractBodyContent {
51   static final L10N L = new L10N(BodyContentImpl.class);
52   static final Logger JavaDoc log = Log.open(BodyContentImpl.class);
53   
54   private static final FreeList<BodyContentImpl> _freeList
55     = new FreeList<BodyContentImpl>(32);
56
57   private TempCharStream _tempStream = new TempCharStream();
58   private TempCharReader _charReader;
59   private JspPrintWriter _printWriter;
60   private JspWriter JavaDoc _prev;
61
62   /**
63    * Creates a new QBodyContent instance.
64    *
65    * @param prev the encloding writer.
66    */

67   private BodyContentImpl(JspWriter JavaDoc prev)
68   {
69     setParent(prev);
70   }
71
72   /**
73    * Allocates a new BodyContent instance.
74    */

75   static BodyContentImpl allocate()
76   {
77     BodyContentImpl body = (BodyContentImpl) _freeList.allocate();
78     
79     if (body == null)
80       body = new BodyContentImpl(null);
81
82     return body;
83   }
84
85   /**
86    * Initializes the BodyContent object.
87    *
88    * @param prev the enclosing writer
89    */

90   void init(JspWriter JavaDoc prev)
91   {
92     setParent(prev);
93
94     _tempStream.openWrite();
95   }
96
97   /**
98    * Writes characters to the stream.
99    *
100    * @param buf character buffer
101    * @param off starting offset into the buffer
102    * @param len length of valid bytes in the buffer.
103    */

104   final public void write(char []buf, int off, int len) throws IOException JavaDoc
105   {
106     _tempStream.write(buf, off, len);
107   }
108
109   /**
110    * Writes characters to the stream.
111    *
112    * @param s string
113    * @param off starting offset into the buffer
114    * @param len length of valid bytes in the buffer.
115    */

116   final public void write(String JavaDoc s, int off, int len) throws IOException JavaDoc
117   {
118     _tempStream.write(s, off, len);
119   }
120
121   /**
122    * Writes characters to the stream.
123    *
124    * @param ch character to write.
125    */

126   final public void write(int ch) throws IOException JavaDoc
127   {
128     _tempStream.write(ch);
129   }
130
131   final public void clear() throws IOException JavaDoc
132   {
133     _tempStream.clearWrite();
134   }
135
136   final public void clearBuffer() throws IOException JavaDoc
137   {
138     clear();
139   }
140
141   final public void flush() throws IOException JavaDoc
142   {
143     // jsp/18kg
144
throw new IOException JavaDoc(L.l("flush() may not be called in a body"));
145   }
146
147   final public void close() throws IOException JavaDoc
148   {
149   }
150
151   final public int getBufferSize()
152   {
153     return -1;
154   }
155
156   final public int getRemaining()
157   {
158     return 0;
159   }
160
161   /**
162    * Clears the body contents.
163    */

164   public void clearBody()
165   {
166     _tempStream.clearWrite();
167   }
168
169   /**
170    * Returns a reader to the body content.
171    */

172   public Reader getReader()
173   {
174     _charReader = new TempCharReader();
175     _charReader.init(_tempStream.getHead());
176     
177     return _charReader;
178   }
179
180   public CharBuffer getCharBuffer()
181   {
182     CharBuffer cb = new CharBuffer();
183       
184     TempCharBuffer head = _tempStream.getHead();
185
186     for (; head != null; head = head.getNext()) {
187       char []cbuf = head.getBuffer();
188
189       cb.append(cbuf, 0, head.getLength());
190     }
191       
192     return cb;
193   }
194
195   /**
196    * Returns a string representing the body content.
197    */

198   public String JavaDoc getString()
199   {
200     TempCharBuffer head = _tempStream.getHead();
201
202     if (head == null)
203       return "";
204
205     if (head.getNext() == null)
206       return new String JavaDoc(head.getBuffer(), 0, head.getLength());
207
208     int length = 0;
209     for (; head != null; head = head.getNext())
210       length += head.getLength();
211
212     char []buf = new char[length];
213
214     int offset = 0;
215     for (head = _tempStream.getHead(); head != null; head = head.getNext()) {
216       char []cbuf = head.getBuffer();
217       int sublen = head.getLength();
218
219       System.arraycopy(cbuf, 0, buf, offset, sublen);
220
221       offset += sublen;
222     }
223       
224     return new String JavaDoc(buf, 0, length);
225   }
226
227   /**
228    * Returns a string representing the body content.
229    */

230   public String JavaDoc getTrimString()
231   {
232     TempCharBuffer head = _tempStream.getHead();
233
234     boolean hasData = false;
235
236     char []buf = null;
237     int totalLength = 0;
238     for (; head != null; head = head.getNext()) {
239       char []cbuf = head.getBuffer();
240       int end = head.getLength();
241       int offset = 0;
242
243       if (! hasData) {
244     for (offset = 0; offset < end; offset++) {
245       if (! Character.isWhitespace(cbuf[offset])) {
246         hasData = true;
247         break;
248       }
249     }
250       }
251
252       if (head.getNext() == null) {
253     for (; offset < end; end--) {
254       if (! Character.isWhitespace(cbuf[end - 1]))
255         break;
256     }
257
258     if (buf != null) {
259       System.arraycopy(cbuf, offset, buf, totalLength, end - offset);
260       totalLength += end - offset;
261       
262       return new String JavaDoc(buf, 0, totalLength);
263     }
264     else if (offset == end)
265       return "";
266     else
267       return new String JavaDoc(cbuf, offset, end - offset);
268     
269       }
270       else if (buf == null) {
271     int length = 0;
272
273     for (TempCharBuffer ptr = head; ptr != null; ptr = ptr.getNext())
274       length += ptr.getLength();
275
276     buf = new char[length];
277
278     System.arraycopy(cbuf, offset, buf, 0, end - offset);
279     totalLength += end - offset;
280       }
281       else {
282     System.arraycopy(cbuf, offset, buf, totalLength, end - offset);
283     totalLength += end - offset;
284       }
285     }
286
287     return "";
288   }
289
290   /**
291    * Writes the body contents out to the named writer.
292    */

293   public void writeOut(Writer JavaDoc out) throws IOException JavaDoc
294   {
295     try {
296       TempCharBuffer head = _tempStream.getHead();
297
298       for (; head != null; head = head.getNext()) {
299     char []cbuf = head.getBuffer();
300
301     out.write(cbuf, 0, head.getLength());
302       }
303     } catch (IOException JavaDoc e) {
304     }
305   }
306
307   /**
308    * Writes the body contents out to the named writer.
309    */

310   public void flushBuffer() throws IOException JavaDoc
311   {
312   }
313
314   /**
315    * Returns the print writer.
316    */

317   public PrintWriter JavaDoc getWriter()
318   {
319     if (_printWriter == null)
320       _printWriter = new JspPrintWriter(this);
321
322     _printWriter.init(this);
323
324     return _printWriter;
325   }
326   
327   /**
328    * Releases the body content at the end of the tag.
329    */

330   public void release()
331   {
332     releaseNoFree();
333
334     _freeList.free(this);
335   }
336   
337   void releaseNoFree()
338   {
339     if (_charReader != null && ! _charReader.isEmpty()) {
340       _charReader.setFree(true);
341       _tempStream.discard();
342     }
343     else
344       _tempStream.destroy();
345
346     _charReader = null;
347     _prev = null;
348   }
349
350   AbstractJspWriter popWriter()
351   {
352     AbstractJspWriter parent = super.popWriter();
353     
354     release();
355
356     return parent;
357   }
358 }
359
Popular Tags