001
014
015 package com.liferay.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.taglib.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.http.HttpServletRequest;
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
075 if (bodyContent == null) {
076 return new StringBundler();
077 }
078
079 if (ServerDetector.isTomcat() && _log.isWarnEnabled()) {
080 _log.warn(
081 "BodyContent is not BodyContentWrapper. Check " +
082 "JspFactorySwapper.");
083 }
084
085 String bodyContentString = bodyContent.getString();
086
087 if (bodyContentString == null) {
088 return new StringBundler();
089 }
090
091 return new StringBundler(bodyContentString);
092 }
093
094 public HttpServletRequest getRequest() {
095 return (HttpServletRequest)pageContext.getRequest();
096 }
097
098 @Override
099 public void release() {
100 bodyContent = null;
101
102 super.release();
103 }
104
105 public void setBodyContent(BodyContent bodyContent) {
106 this.bodyContent = bodyContent;
107 }
108
109 public void writeBodyContent(Writer writer) throws IOException {
110 StringBundler sb = getBodyContentAsStringBundler();
111
112 sb.writeTo(writer);
113 }
114
115 protected BodyContent bodyContent;
116
117 private static final Log _log = LogFactoryUtil.getLog(
118 BaseBodyTagSupport.class);
119
120 }