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