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;
022
023import javax.servlet.http.HttpServletRequest;
024import javax.servlet.http.HttpServletResponse;
025
026import lucee.runtime.PageContext;
027
028/**
029 * JSP Writer that Remove WhiteSpace from given content
030 */
031public final class CFMLWriterWS extends CFMLWriterImpl implements WhiteSpaceWriter {
032
033        public static final char CHAR_EMPTY=0;
034        public static final char CHAR_NL='\n';
035        public static final char CHAR_SPACE=' ';
036        public static final char CHAR_TAB='\t';
037        public static final char CHAR_BS='\b'; // \x0B\
038        public static final char CHAR_FW='\f';
039        public static final char CHAR_RETURN='\r';
040
041        char charBuffer=CHAR_EMPTY;
042        
043        /**
044         * constructor of the class
045         * @param rsp
046         * @param bufferSize 
047         * @param autoFlush 
048         */
049        public CFMLWriterWS(PageContext pc,HttpServletRequest req, HttpServletResponse rsp, int bufferSize, boolean autoFlush, boolean closeConn, 
050                        boolean showVersion, boolean contentLength) {
051                super(pc,req,rsp, bufferSize, autoFlush,closeConn,showVersion,contentLength);
052        }
053
054        /**
055         * @see lucee.runtime.writer.CFMLWriterImpl#print(char)
056         */
057        public void print(char c) throws IOException {
058                switch(c) {
059                case CHAR_NL:
060                        if(charBuffer!=CHAR_NL)charBuffer=c;
061                break;
062                case CHAR_BS:
063                case CHAR_FW:
064                case CHAR_RETURN:
065                case CHAR_SPACE:
066                case CHAR_TAB:
067                        if(charBuffer==CHAR_EMPTY)charBuffer=c;
068                break;
069                
070                default:
071                        printBuffer();
072                        super.print(c);
073                break;
074                }
075        }
076        
077        synchronized void printBuffer() throws IOException {
078                if(charBuffer!=CHAR_EMPTY) {
079                        char b = charBuffer;// muss so bleiben!
080                        charBuffer=CHAR_EMPTY;
081                        super.print(b);
082                }
083        }
084
085        void printBufferEL() {
086                if(charBuffer!=CHAR_EMPTY) {
087                        try {
088                                char b = charBuffer;
089                                charBuffer=CHAR_EMPTY;
090                                super.print(b);
091                        } 
092                        catch (IOException e) {}
093                }
094        }
095
096        /**
097         * @see lucee.runtime.writer.CFMLWriter#writeRaw(java.lang.String)
098         */
099        public void writeRaw(String str) throws IOException {
100                printBuffer();
101                super.write(str);
102        }    
103        
104        /**
105     * just a wrapper function for ACF
106     * @throws IOException 
107     */
108    public void initHeaderBuffer() throws IOException{
109        resetHTMLHead();
110    }
111        
112    
113    
114
115        /**
116         * @see lucee.runtime.writer.CFMLWriterImpl#clear()
117         */
118        public final void clear() throws IOException {
119                printBuffer();
120                super.clear();
121        }
122
123        /**
124         * @see lucee.runtime.writer.CFMLWriterImpl#clearBuffer()
125         */
126        public final void clearBuffer() {
127                printBufferEL();
128                super.clearBuffer();
129        }
130
131        /**
132         * @see lucee.runtime.writer.CFMLWriterImpl#close()
133         */
134        public final void close() throws IOException {
135                printBuffer();
136                super.close();
137        }
138
139        /**
140         * @see lucee.runtime.writer.CFMLWriterImpl#flush()
141         */
142        public final void flush() throws IOException {
143                printBuffer();
144                super.flush();
145        }
146
147        /**
148         * @see lucee.runtime.writer.CFMLWriterImpl#getRemaining()
149         */
150        public final int getRemaining() {
151                printBufferEL();
152                return super.getRemaining();
153        }
154
155        /**
156         * @see lucee.runtime.writer.CFMLWriterImpl#newLine()
157         */
158        public final void newLine() throws IOException {
159                print(CHAR_NL);
160        }
161
162        /**
163         * @see lucee.runtime.writer.CFMLWriterImpl#print(boolean)
164         */
165        public final void print(boolean b) throws IOException {
166                printBuffer();
167                super.print(b);
168        }
169
170        /**
171         * @see lucee.runtime.writer.CFMLWriterImpl#print(char[])
172         */
173        public final void print(char[] chars) throws IOException {
174                write(chars,0,chars.length);
175        }
176
177        /**
178         * @see lucee.runtime.writer.CFMLWriterImpl#print(double)
179         */
180        public final void print(double d) throws IOException {
181                printBuffer();
182                super.print(d);
183        }
184
185        /**
186         * @see lucee.runtime.writer.CFMLWriterImpl#print(float)
187         */
188        public final void print(float f) throws IOException {
189                printBuffer();
190                super.print(f);
191        }
192
193        /**
194         * @see lucee.runtime.writer.CFMLWriterImpl#print(int)
195         */
196        public final void print(int i) throws IOException {
197                printBuffer();
198                super.print(i);
199        }
200
201        /**
202         * @see lucee.runtime.writer.CFMLWriterImpl#print(long)
203         */
204        public final void print(long l) throws IOException {
205                printBuffer();
206                super.print(l);
207        }
208
209        /**
210         * @see lucee.runtime.writer.CFMLWriterImpl#print(java.lang.Object)
211         */
212        public final void print(Object obj) throws IOException {
213                print(obj.toString());
214        }
215
216        /**
217         * @see lucee.runtime.writer.CFMLWriterImpl#print(java.lang.String)
218         */
219        public final void print(String str) throws IOException {
220                write(str.toCharArray(),0,str.length());
221        }
222
223        /**
224         * @see lucee.runtime.writer.CFMLWriterImpl#println()
225         */
226        public final void println() throws IOException {
227                print(CHAR_NL);
228        }
229
230        /**
231         * @see lucee.runtime.writer.CFMLWriterImpl#println(boolean)
232         */
233        public final void println(boolean b) throws IOException {
234                printBuffer();
235                super.print(b);
236                print(CHAR_NL);
237        }
238
239        /**
240         * @see lucee.runtime.writer.CFMLWriterImpl#println(char)
241         */
242        public final void println(char c) throws IOException {
243                print(c);
244                print(CHAR_NL);
245        }
246
247        /**
248         * @see lucee.runtime.writer.CFMLWriterImpl#println(char[])
249         */
250        public final void println(char[] chars) throws IOException {
251                write(chars,0,chars.length);
252                print(CHAR_NL);
253        }
254
255        /**
256         * @see lucee.runtime.writer.CFMLWriterImpl#println(double)
257         */
258        public final void println(double d) throws IOException {
259                printBuffer();
260                super.print(d);
261                print(CHAR_NL);
262        }
263
264        /**
265         * @see lucee.runtime.writer.CFMLWriterImpl#println(float)
266         */
267        public final void println(float f) throws IOException {
268                printBuffer();
269                super.print(f);
270                print(CHAR_NL);
271        }
272
273        /**
274         * @see lucee.runtime.writer.CFMLWriterImpl#println(int)
275         */
276        public final void println(int i) throws IOException {
277                printBuffer();
278                super.print(i);
279                print(CHAR_NL);
280        }
281
282        /**
283         * @see lucee.runtime.writer.CFMLWriterImpl#println(long)
284         */
285        public final void println(long l) throws IOException {
286                printBuffer();
287                super.print(l);
288                print(CHAR_NL);
289        }
290
291        /**
292         * @see lucee.runtime.writer.CFMLWriterImpl#println(java.lang.Object)
293         */
294        public final void println(Object obj) throws IOException {
295                println(obj.toString());
296        }
297
298        /**
299         * @see lucee.runtime.writer.CFMLWriterImpl#println(java.lang.String)
300         */
301        public final void println(String str) throws IOException {
302                print(str);
303                print(CHAR_NL);
304                
305        }
306
307        /**
308         * @see lucee.runtime.writer.CFMLWriterImpl#write(char[], int, int)
309         */
310        public final void write(char[] chars, int off, int len) throws IOException {
311                for(int i=off;i<len;i++) {
312                        print(chars[i]);
313                }
314        }
315        
316        /**
317         * @see lucee.runtime.writer.CFMLWriterImpl#write(java.lang.String, int, int)
318         */
319        public final void write(String str, int off, int len) throws IOException {
320                write(str.toCharArray(),off,len);
321        }
322        
323
324        /**
325         * @see lucee.runtime.writer.CFMLWriterImpl#write(char[])
326         */
327        public final void write(char[] chars) throws IOException {
328                write(chars,0,chars.length);
329        }
330
331        /**
332         * @see lucee.runtime.writer.CFMLWriterImpl#write(int)
333         */
334        public final void write(int i) throws IOException {
335                print(i);
336        }
337
338        /**
339         * @see lucee.runtime.writer.CFMLWriterImpl#write(java.lang.String)
340         */
341        public final void write(String str) throws IOException {
342        write(str.toCharArray(),0,str.length());
343        }
344}