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