001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.runtime.writer;
020
021import java.io.IOException;
022import java.io.Reader;
023import java.io.StringReader;
024import java.io.Writer;
025
026import javax.servlet.jsp.JspWriter;
027import javax.servlet.jsp.tagext.BodyContent;
028
029import lucee.commons.lang.CharBuffer;
030
031
032/**
033 * implementation of the BodyContent
034 */
035public class BodyContentImpl extends BodyContent {
036        
037        CharBuffer charBuffer=new CharBuffer(128);
038        JspWriter enclosingWriter;
039
040        /**
041         * default constructor
042         * @param jspWriter
043         */
044        public BodyContentImpl(JspWriter jspWriter) {
045                super(jspWriter);
046                enclosingWriter=jspWriter;
047                
048        }
049
050        /**
051         * initialize the BodyContent with the enclosing jsp writer
052         * @param jspWriter
053         */
054        public void init(JspWriter jspWriter) {
055                enclosingWriter=jspWriter;
056                clearBuffer();
057                
058        }
059
060        /**
061         * @see javax.servlet.jsp.tagext.BodyContent#getReader()
062         */
063        public Reader getReader() {
064                return new StringReader(charBuffer.toString());
065        }
066
067        /**
068         * @see javax.servlet.jsp.tagext.BodyContent#getString()
069         */
070        public String getString() {
071                return charBuffer.toString();
072        }
073
074        /**
075         * @see javax.servlet.jsp.tagext.BodyContent#writeOut(java.io.Writer)
076         */
077        public void writeOut(Writer writer) throws IOException {
078                charBuffer.writeOut(writer);
079        }
080
081        /**
082         * @see javax.servlet.jsp.JspWriter#newLine()
083         */
084        public void newLine() {
085                println();
086        }
087
088        /**
089         * @see javax.servlet.jsp.JspWriter#print(boolean)
090         */
091        public void print(boolean arg) {
092                print(arg?"true":"false");
093        }
094
095        /**
096         * @see javax.servlet.jsp.JspWriter#print(char)
097         */
098        public void print(char arg) {
099                charBuffer.append(String.valueOf(arg));
100        }
101
102        /**
103         * @see javax.servlet.jsp.JspWriter#print(int)
104         */
105        public void print(int arg) {
106                charBuffer.append(String.valueOf(arg));
107        }
108
109        /**
110         * @see javax.servlet.jsp.JspWriter#print(long)
111         */
112        public void print(long arg) {
113                charBuffer.append(String.valueOf(arg));
114        }
115
116        /**
117         * @see javax.servlet.jsp.JspWriter#print(float)
118         */
119        public void print(float arg) {
120                charBuffer.append(String.valueOf(arg));
121        }
122
123        /**
124         * @see javax.servlet.jsp.JspWriter#print(double)
125         */
126        public void print(double arg) {
127                charBuffer.append(String.valueOf(arg));
128        }
129
130        /**
131         * @see javax.servlet.jsp.JspWriter#print(char[])
132         */
133        public void print(char[] arg) {
134                charBuffer.append(arg);
135        }
136
137        /**
138         * @see javax.servlet.jsp.JspWriter#print(java.lang.String)
139         */
140        public void print(String arg) {
141                charBuffer.append(arg);
142        }
143
144        /**
145         * @see javax.servlet.jsp.JspWriter#print(java.lang.Object)
146         */
147        public void print(Object arg) {         
148                charBuffer.append(String.valueOf(arg));
149        }
150
151        /**
152         * @see javax.servlet.jsp.JspWriter#println()
153         */
154        public void println() {
155                charBuffer.append("\n");
156        }
157
158        /**
159         * @see javax.servlet.jsp.JspWriter#println(boolean)
160         */
161        public void println(boolean arg) {
162                print(arg);
163                println();
164        }
165
166        /**
167         * @see javax.servlet.jsp.JspWriter#println(char)
168         */
169        public void println(char arg) {
170                print(arg);
171                println();
172        }
173
174        /**
175         * @see javax.servlet.jsp.JspWriter#println(int)
176         */
177        public void println(int arg) {
178                print(arg);
179                println();
180        }
181
182        /**
183         * @see javax.servlet.jsp.JspWriter#println(long)
184         */
185        public void println(long arg) {
186                print(arg);
187                println();
188        }
189
190        /**
191         * @see javax.servlet.jsp.JspWriter#println(float)
192         */
193        public void println(float arg) {
194                print(arg);
195                println();
196        }
197
198        /**
199         * @see javax.servlet.jsp.JspWriter#println(double)
200         */
201        public void println(double arg) {
202                print(arg);
203                println();
204        }
205
206        /**
207         * @see javax.servlet.jsp.JspWriter#println(char[])
208         */
209        public void println(char[] arg) {
210                print(arg);
211                println();
212        }
213
214        /**
215         * @see javax.servlet.jsp.JspWriter#println(java.lang.String)
216         */
217        public void println(String arg) {
218                print(arg);
219                println();
220        }
221
222        /**
223         * @see javax.servlet.jsp.JspWriter#println(java.lang.Object)
224         */
225        public void println(Object arg) {
226                print(arg);
227                println();
228        }
229
230        /**
231         * @throws IOException 
232         * @see javax.servlet.jsp.JspWriter#clear()
233         */
234        public void clear() throws IOException {
235                charBuffer.clear();
236        enclosingWriter.clear();
237        }
238
239        /**
240         * @see javax.servlet.jsp.JspWriter#clearBuffer()
241         */
242        public void clearBuffer() {
243                charBuffer.clear();
244        }
245        
246        /**
247         * @see java.io.Writer#flush()
248         */
249        public void flush() throws IOException {
250                enclosingWriter.write(charBuffer.toCharArray());
251                charBuffer.clear();
252        }
253
254        /**
255         * @see java.io.Writer#close()
256         */
257        public void close() throws IOException {
258                flush();
259                enclosingWriter.close();
260        }
261
262        /**
263         * @see javax.servlet.jsp.JspWriter#getRemaining()
264         */
265        public int getRemaining() {
266                return bufferSize-charBuffer.size();
267        }
268
269        /**
270         * @see java.io.Writer#write(char[], int, int)
271         */
272        public void write(char[] cbuf, int off, int len) {
273                charBuffer.append(cbuf,off,len);
274        }
275
276        /**
277         * @see java.io.Writer#write(char[])
278         */
279        public void write(char[] cbuf) {
280                charBuffer.append(cbuf);
281        }
282
283        /**
284         * @see java.io.Writer#write(int)
285         */
286        public void write(int c) {
287                print(c);
288        }
289
290        /**
291         * @see java.io.Writer#write(java.lang.String, int, int)
292         */
293        public void write(String str, int off, int len) {
294                charBuffer.append(str,off,len);
295        }
296
297        /**
298         * @see java.io.Writer#write(java.lang.String)
299         */
300        public void write(String str) {
301                charBuffer.append(str);
302        }
303
304        /**
305         * @see java.lang.Object#toString()
306         */
307        public String toString() {
308                return charBuffer.toString();
309        }
310
311        /**
312         * @see javax.servlet.jsp.tagext.BodyContent#clearBody()
313         */
314        public void clearBody() {
315                charBuffer.clear();
316        }
317
318        /**
319         * @see javax.servlet.jsp.tagext.BodyContent#getEnclosingWriter()
320         */
321        public JspWriter getEnclosingWriter() {
322                return enclosingWriter;
323        }
324
325        /**
326         * returns the inner char buffer
327         * @return intern CharBuffer
328         */
329        public CharBuffer getCharBuffer() {
330                return charBuffer;
331        }
332
333        /**
334         * sets the inner Charbuffer
335         * @param charBuffer
336         */
337        public void setCharBuffer(CharBuffer charBuffer) {
338                this.charBuffer=charBuffer;
339        }
340
341        /**
342         * @see javax.servlet.jsp.JspWriter#getBufferSize()
343         */
344        public int getBufferSize() {
345                return charBuffer.size();
346        }
347
348        /**
349         * @see javax.servlet.jsp.JspWriter#isAutoFlush()
350         */
351        public boolean isAutoFlush() {
352                return super.isAutoFlush();
353        }
354
355        /**
356         * @see java.io.Writer#append(java.lang.CharSequence)
357         */
358        public Writer append(CharSequence csq) throws IOException {
359                write(csq.toString());
360                return this;
361        }
362
363        /**
364         * @see java.io.Writer#append(java.lang.CharSequence, int, int)
365         */
366        public Writer append(CharSequence csq, int start, int end) throws IOException {
367                write(csq.subSequence(start, end).toString());
368                return this;
369        }
370
371        /**
372         * @see java.io.Writer#append(char)
373         */
374        public Writer append(char c) throws IOException {
375                write(c);
376                return this;
377        }
378        
379
380
381        
382}