001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.servlet;
016    
017    import com.liferay.portal.kernel.io.unsync.UnsyncPrintWriter;
018    import com.liferay.portal.kernel.util.CharPool;
019    import com.liferay.portal.kernel.util.StringPool;
020    
021    import java.io.IOException;
022    import java.io.PrintWriter;
023    import java.io.Writer;
024    
025    import javax.servlet.jsp.JspWriter;
026    
027    /**
028     * @author Shuyang Zhou
029     */
030    public class TrimNewLinesJspWriter extends JspWriter {
031    
032            public TrimNewLinesJspWriter(PrintWriter printWriter) {
033                    super(NO_BUFFER, false);
034    
035                    _printWriter = printWriter;
036            }
037    
038            public TrimNewLinesJspWriter(Writer writer) {
039                    super(NO_BUFFER, false);
040    
041                    _printWriter = new UnsyncPrintWriter(writer, true);
042            }
043    
044            public void clear() throws IOException {
045                    throw new IOException();
046            }
047    
048            public void clearBuffer() {
049            }
050    
051            public void close() {
052                    _printWriter.close();
053            }
054    
055            public void flush() {
056                    _printWriter.flush();
057            }
058    
059            public int getRemaining() {
060                    return 0;
061            }
062    
063            public void newLine() {
064                    if (!_lastNewLine) {
065                            _printWriter.println();
066    
067                            _lastNewLine = true;
068                    }
069            }
070    
071            public void print(boolean b) {
072                    _printWriter.print(b);
073    
074                    _lastNewLine = false;
075            }
076    
077            public void print(char c) {
078                    boolean newLine = false;
079    
080                    if ((c == CharPool.NEW_LINE) || (c == CharPool.RETURN)) {
081                            newLine = true;
082                    }
083    
084                    if (!_lastNewLine || !newLine) {
085                            _printWriter.print(c);
086                    }
087    
088                    if (newLine) {
089                            _lastNewLine = true;
090                    }
091            }
092    
093            public void print(char[] charArray) {
094                    _printWriter.print(charArray);
095    
096                    _lastNewLine = false;
097            }
098    
099            public void print(double d) {
100                    _printWriter.print(d);
101    
102                    _lastNewLine = false;
103            }
104    
105            public void print(float f) {
106                    _printWriter.print(f);
107    
108                    _lastNewLine = false;
109            }
110    
111            public void print(int i) {
112                    _printWriter.print(i);
113    
114                    _lastNewLine = false;
115            }
116    
117            public void print(long l) {
118                    _printWriter.print(l);
119    
120                    _lastNewLine = false;
121            }
122    
123            public void print(Object object) {
124                    _printWriter.print(object);
125    
126                    _lastNewLine = false;
127            }
128    
129            public void print(String string) {
130                    String trim = trim(string);
131    
132                    if (trim.length() > 0) {
133                            _printWriter.print(trim);
134    
135                            _lastNewLine = false;
136                    }
137            }
138    
139            public void println() {
140                    if (!_lastNewLine) {
141                            _printWriter.println();
142    
143                            _lastNewLine = true;
144                    }
145            }
146    
147            public void println(boolean b) {
148                    _printWriter.println(b);
149    
150                    _lastNewLine = true;
151            }
152    
153            public void println(char c) {
154                    _printWriter.println(c);
155    
156                    _lastNewLine = true;
157            }
158    
159            public void println(char[] charArray) {
160                    _printWriter.println(charArray);
161    
162                    _lastNewLine = true;
163            }
164    
165            public void println(double d) {
166                    _printWriter.println(d);
167    
168                    _lastNewLine = true;
169            }
170    
171            public void println(float f) {
172                    _printWriter.println(f);
173    
174                    _lastNewLine = true;
175            }
176    
177            public void println(int i) {
178                    _printWriter.println(i);
179    
180                    _lastNewLine = true;
181            }
182    
183            public void println(long l) {
184                    _printWriter.println(l);
185    
186                    _lastNewLine = true;
187            }
188    
189            public void println(Object object) {
190                    _printWriter.println(object);
191    
192                    _lastNewLine = true;
193            }
194    
195            public void println(String string) {
196                    String trim = trim(string);
197    
198                    if (trim.length() > 0) {
199                            _printWriter.println(trim);
200    
201                            _lastNewLine = true;
202                    }
203            }
204    
205            public void write(char[] charArray) {
206                    _printWriter.write(charArray);
207    
208                    _lastNewLine = false;
209            }
210    
211            public void write(char[] charArray, int offset, int length) {
212                    _printWriter.write(charArray, offset, length);
213    
214                    _lastNewLine = false;
215            }
216    
217            public void write(int c) {
218                    boolean newLine = false;
219    
220                    if ((c == CharPool.NEW_LINE) || (c == CharPool.RETURN)) {
221                            newLine = true;
222                    }
223    
224                    if (!_lastNewLine || !newLine) {
225                            _printWriter.write(c);
226                    }
227    
228                    if (newLine) {
229                            _lastNewLine = true;
230                    }
231            }
232    
233            public void write(String string) {
234                    String trim = trim(string);
235    
236                    if (trim.length() > 0) {
237                            _printWriter.write(trim);
238    
239                            _lastNewLine = false;
240                    }
241            }
242    
243            public void write(String string, int offset, int length) {
244                    String trim = trim(string.substring(offset, offset + length));
245    
246                    if (trim.length() > 0) {
247                            _printWriter.write(trim);
248    
249                            _lastNewLine = false;
250                    }
251            }
252    
253            protected String trim(String string) {
254                    int length = string.length();
255    
256                    int start = length;
257    
258                    for(int i = 0; i < length; i++) {
259                            char c = string.charAt(i);
260    
261                            if ((c != CharPool.NEW_LINE) && (c != CharPool.RETURN)) {
262                                    start = i;
263    
264                                    break;
265                            }
266                    }
267    
268                    int end = 0;
269    
270                    for(int i = length - 1; i >= 0 ; i--) {
271                            char c = string.charAt(i);
272    
273                            if ((c != CharPool.NEW_LINE) && (c != CharPool.RETURN)) {
274                                    end = i + 1;
275    
276                                    break;
277                            }
278                    }
279    
280                    if (end > start) {
281                            return string.substring(start, end);
282                    }
283                    else {
284                            return StringPool.BLANK;
285                    }
286            }
287    
288            private boolean _lastNewLine;
289            private PrintWriter _printWriter;
290    
291    }