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