001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.StringBundler;
021    import com.liferay.portal.kernel.util.StringPool;
022    
023    import java.io.IOException;
024    import java.io.Writer;
025    
026    import javax.servlet.jsp.tagext.BodyContent;
027    import javax.servlet.jsp.tagext.BodyTagSupport;
028    
029    /**
030     * @author Shuyang Zhou
031     */
032    public class BaseBodyTagSupport extends BodyTagSupport {
033    
034            public StringBundler getBodyContentAsStringBundler() {
035                    BodyContent bodyContent = getBodyContent();
036    
037                    if (bodyContent instanceof BodyContentWrapper) {
038                            BodyContentWrapper bodyContentWrapper =
039                                    (BodyContentWrapper)bodyContent;
040    
041                            return bodyContentWrapper.getStringBundler();
042                    }
043                    else {
044                            if (_log.isWarnEnabled()) {
045                                    _log.warn(
046                                            "BodyContent is not BodyContentWrapper. Check " +
047                                                    "JspFactorySwapper.");
048                            }
049    
050                            String bodyContentString = bodyContent.getString();
051    
052                            if (bodyContentString == null) {
053                                    bodyContentString = StringPool.BLANK;
054                            }
055    
056                            return new StringBundler(bodyContentString);
057                    }
058            }
059    
060            public void writeBodyContent(Writer writer) throws IOException {
061                    StringBundler sb = getBodyContentAsStringBundler();
062    
063                    sb.writeTo(writer);
064            }
065    
066            private static Log _log = LogFactoryUtil.getLog(BaseBodyTagSupport.class);
067    
068    }