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.taglib;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.servlet.BodyContentWrapper;
020    import com.liferay.portal.kernel.util.ServerDetector;
021    import com.liferay.portal.kernel.util.StringBundler;
022    
023    import java.io.IOException;
024    import java.io.Writer;
025    
026    import javax.servlet.jsp.JspException;
027    import javax.servlet.jsp.tagext.BodyContent;
028    import javax.servlet.jsp.tagext.BodyTag;
029    
030    /**
031     * <p>
032     * See http://issues.liferay.com/browse/LPS-13878.
033     * </p>
034     *
035     * @author Shuyang Zhou
036     */
037    public class BaseBodyTagSupport extends TagSupport {
038    
039            @SuppressWarnings("unused")
040            public int doAfterBody() throws JspException {
041                    return SKIP_BODY;
042            }
043    
044            @SuppressWarnings("unused")
045            public void doInitBody() throws JspException {
046            }
047    
048            @Override
049            @SuppressWarnings("unused")
050            public int doStartTag() throws JspException {
051                    return BodyTag.EVAL_BODY_BUFFERED;
052            }
053    
054            public BodyContent getBodyContent() {
055                    return bodyContent;
056            }
057    
058            public StringBundler getBodyContentAsStringBundler() {
059                    if (!(this instanceof BodyTag)) {
060                            throw new RuntimeException(
061                                    getClass().getName() + " must implement " +
062                                            BodyTag.class.getName());
063                    }
064    
065                    BodyContent bodyContent = getBodyContent();
066    
067                    if (bodyContent instanceof BodyContentWrapper) {
068                            BodyContentWrapper bodyContentWrapper =
069                                    (BodyContentWrapper)bodyContent;
070    
071                            return bodyContentWrapper.getStringBundler();
072                    }
073    
074                    if (bodyContent == null) {
075                            return new StringBundler();
076                    }
077    
078                    if (ServerDetector.isTomcat() && _log.isWarnEnabled()) {
079                            _log.warn(
080                                    "BodyContent is not BodyContentWrapper. Check " +
081                                            "JspFactorySwapper.");
082                    }
083    
084                    String bodyContentString = bodyContent.getString();
085    
086                    if (bodyContentString == null) {
087                            return new StringBundler();
088                    }
089    
090                    return new StringBundler(bodyContentString);
091            }
092    
093            @Override
094            public void release() {
095                    bodyContent = null;
096    
097                    super.release();
098            }
099    
100            public void setBodyContent(BodyContent bodyContent) {
101                    this.bodyContent = bodyContent;
102            }
103    
104            public void writeBodyContent(Writer writer) throws IOException {
105                    StringBundler sb = getBodyContentAsStringBundler();
106    
107                    sb.writeTo(writer);
108            }
109    
110            protected BodyContent bodyContent;
111    
112            private static Log _log = LogFactoryUtil.getLog(BaseBodyTagSupport.class);
113    
114    }