001    /**
002     * Copyright (c) 2000-2013 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.util.CharPool;
018    import com.liferay.portal.kernel.util.StringPool;
019    
020    import java.io.IOException;
021    import java.io.Writer;
022    
023    import javax.servlet.jsp.JspWriter;
024    
025    /**
026     * @author Shuyang Zhou
027     */
028    public class TrimNewLinesJspWriter extends JspWriter {
029    
030            public TrimNewLinesJspWriter(Writer writer) {
031                    super(NO_BUFFER, false);
032    
033                    _writer = writer;
034            }
035    
036            @Override
037            public void clear() throws IOException {
038                    throw new IOException();
039            }
040    
041            @Override
042            public void clearBuffer() {
043            }
044    
045            @Override
046            public void close() throws IOException {
047                    _writer.close();
048            }
049    
050            @Override
051            public void flush() throws IOException {
052                    _writer.flush();
053            }
054    
055            @Override
056            public int getRemaining() {
057                    return 0;
058            }
059    
060            @Override
061            public void newLine() throws IOException {
062                    if (!_lastNewLine) {
063                            _writer.write(_LINE_SEPARATOR);
064    
065                            _lastNewLine = true;
066                    }
067            }
068    
069            @Override
070            public void print(boolean b) throws IOException {
071                    if (b) {
072                            _writer.write(StringPool.TRUE);
073                    }
074                    else {
075                            _writer.write(StringPool.FALSE);
076                    }
077    
078                    _lastNewLine = false;
079            }
080    
081            @Override
082            public void print(char c) throws IOException {
083                    boolean newLine = false;
084    
085                    if ((c == CharPool.NEW_LINE) || (c == CharPool.RETURN)) {
086                            newLine = true;
087                    }
088    
089                    if (!_lastNewLine || !newLine) {
090                            _writer.write(c);
091                    }
092    
093                    if (newLine) {
094                            _lastNewLine = true;
095                    }
096            }
097    
098            @Override
099            public void print(char[] chars) throws IOException {
100                    _writer.write(chars);
101    
102                    _lastNewLine = false;
103            }
104    
105            @Override
106            public void print(double d) throws IOException {
107                    _writer.write(String.valueOf(d));
108    
109                    _lastNewLine = false;
110            }
111    
112            @Override
113            public void print(float f) throws IOException {
114                    _writer.write(String.valueOf(f));
115    
116                    _lastNewLine = false;
117            }
118    
119            @Override
120            public void print(int i) throws IOException {
121                    _writer.write(String.valueOf(i));
122    
123                    _lastNewLine = false;
124            }
125    
126            @Override
127            public void print(long l) throws IOException {
128                    _writer.write(String.valueOf(l));
129    
130                    _lastNewLine = false;
131            }
132    
133            @Override
134            public void print(Object object) throws IOException {
135                    _writer.write(String.valueOf(object));
136    
137                    _lastNewLine = false;
138            }
139    
140            @Override
141            public void print(String string) throws IOException {
142                    if (string == null) {
143                            string = StringPool.NULL;
144                    }
145                    else {
146                            string = trim(string);
147                    }
148    
149                    if (string.length() > 0) {
150                            _writer.write(string);
151    
152                            _lastNewLine = false;
153                    }
154            }
155    
156            @Override
157            public void println() throws IOException {
158                    if (!_lastNewLine) {
159                            _writer.write(_LINE_SEPARATOR);
160    
161                            _lastNewLine = true;
162                    }
163            }
164    
165            @Override
166            public void println(boolean b) throws IOException {
167                    if (b) {
168                            _writer.write(StringPool.TRUE);
169                    }
170                    else {
171                            _writer.write(StringPool.FALSE);
172                    }
173    
174                    _writer.write(_LINE_SEPARATOR);
175    
176                    _lastNewLine = true;
177            }
178    
179            @Override
180            public void println(char c) throws IOException {
181                    _writer.write(c);
182                    _writer.write(_LINE_SEPARATOR);
183    
184                    _lastNewLine = true;
185            }
186    
187            @Override
188            public void println(char[] chars) throws IOException {
189                    _writer.write(chars);
190                    _writer.write(_LINE_SEPARATOR);
191    
192                    _lastNewLine = true;
193            }
194    
195            @Override
196            public void println(double d) throws IOException {
197                    _writer.write(String.valueOf(d));
198                    _writer.write(_LINE_SEPARATOR);
199    
200                    _lastNewLine = true;
201            }
202    
203            @Override
204            public void println(float f) throws IOException {
205                    _writer.write(String.valueOf(f));
206                    _writer.write(_LINE_SEPARATOR);
207    
208                    _lastNewLine = true;
209            }
210    
211            @Override
212            public void println(int i) throws IOException {
213                    _writer.write(String.valueOf(i));
214                    _writer.write(_LINE_SEPARATOR);
215    
216                    _lastNewLine = true;
217            }
218    
219            @Override
220            public void println(long l) throws IOException {
221                    _writer.write(String.valueOf(l));
222                    _writer.write(_LINE_SEPARATOR);
223    
224                    _lastNewLine = true;
225            }
226    
227            @Override
228            public void println(Object object) throws IOException {
229                    _writer.write(String.valueOf(object));
230                    _writer.write(_LINE_SEPARATOR);
231    
232                    _lastNewLine = true;
233            }
234    
235            @Override
236            public void println(String string) throws IOException {
237                    if (string == null) {
238                            string = StringPool.NULL;
239                    }
240                    else {
241                            string = trim(string);
242                    }
243    
244                    if (string.length() > 0) {
245                            _writer.write(string);
246    
247                            _lastNewLine = true;
248                    }
249            }
250    
251            @Override
252            public void write(char[] chars) throws IOException {
253                    _writer.write(chars);
254    
255                    _lastNewLine = false;
256            }
257    
258            @Override
259            public void write(char[] chars, int offset, int length) throws IOException {
260                    _writer.write(chars, offset, length);
261    
262                    _lastNewLine = false;
263            }
264    
265            @Override
266            public void write(int c) throws IOException {
267                    boolean newLine = false;
268    
269                    if ((c == CharPool.NEW_LINE) || (c == CharPool.RETURN)) {
270                            newLine = true;
271                    }
272    
273                    if (!_lastNewLine || !newLine) {
274                            _writer.write(c);
275                    }
276    
277                    if (newLine) {
278                            _lastNewLine = true;
279                    }
280            }
281    
282            @Override
283            public void write(String string) throws IOException {
284                    if (string.length() > 0) {
285                            _writer.write(string);
286    
287                            _lastNewLine = false;
288                    }
289            }
290    
291            @Override
292            public void write(String string, int offset, int length)
293                    throws IOException {
294    
295                    string = trim(string.substring(offset, offset + length));
296    
297                    if (string.length() > 0) {
298                            _writer.write(string);
299    
300                            _lastNewLine = false;
301                    }
302            }
303    
304            protected String trim(String string) {
305                    int length = string.length();
306    
307                    int start = length;
308    
309                    for (int i = 0; i < length; i++) {
310                            char c = string.charAt(i);
311    
312                            if ((c != CharPool.NEW_LINE) && (c != CharPool.RETURN)) {
313                                    start = i;
314    
315                                    break;
316                            }
317                    }
318    
319                    int end = 0;
320    
321                    for (int i = length - 1; i >= 0; i--) {
322                            char c = string.charAt(i);
323    
324                            if ((c != CharPool.NEW_LINE) && (c != CharPool.RETURN)) {
325                                    end = i + 1;
326    
327                                    break;
328                            }
329                    }
330    
331                    if (end > start) {
332                            return string.substring(start, end);
333                    }
334                    else {
335                            return StringPool.BLANK;
336                    }
337            }
338    
339            private static final String _LINE_SEPARATOR = System.getProperty(
340                    "line.separator");
341    
342            private boolean _lastNewLine;
343            private Writer _writer;
344    
345    }