001    /**
002     * Copyright (c) 2000-2011 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.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    }