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.taglib.util;
016    
017    import com.liferay.portal.kernel.util.StringPool;
018    import com.liferay.portal.kernel.util.StringUtil;
019    
020    import javax.servlet.jsp.JspException;
021    import javax.servlet.jsp.JspWriter;
022    import javax.servlet.jsp.tagext.BodyContent;
023    import javax.servlet.jsp.tagext.BodyTagSupport;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     */
028    public class WhitespaceRemoverTag extends BodyTagSupport {
029    
030            @Override
031            public int doEndTag() throws JspException {
032                    try {
033                            JspWriter jspWriter = pageContext.getOut();
034    
035                            jspWriter.write(getBodyContentString());
036                    }
037                    catch (Exception e) {
038                            throw new JspException(e);
039                    }
040    
041                    return EVAL_PAGE;
042            }
043    
044            @Override
045            public int doStartTag() {
046                    return EVAL_BODY_BUFFERED;
047            }
048    
049            protected String getBodyContentString() {
050                    BodyContent bodyContent = getBodyContent();
051    
052                    String bodyContentString = StringUtil.trim(bodyContent.getString());
053    
054                    bodyContentString = StringUtil.replace(
055                            bodyContentString,
056                            new String[] {StringPool.NEW_LINE, StringPool.TAB},
057                            new String[] {StringPool.BLANK, StringPool.BLANK});
058    
059                    return bodyContentString;
060            }
061    
062    }