001
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
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 }