001
014
015 package com.liferay.taglib.aui;
016
017 import com.liferay.portal.kernel.servlet.BodyContentWrapper;
018 import com.liferay.portal.kernel.servlet.PortalIncludeUtil;
019 import com.liferay.portal.kernel.servlet.taglib.FileAvailabilityUtil;
020 import com.liferay.portal.kernel.servlet.taglib.aui.ScriptData;
021 import com.liferay.portal.kernel.util.ServerDetector;
022 import com.liferay.portal.kernel.util.StringBundler;
023 import com.liferay.portal.kernel.util.WebKeys;
024 import com.liferay.portal.model.Portlet;
025 import com.liferay.taglib.aui.base.BaseScriptTag;
026
027 import javax.servlet.http.HttpServletRequest;
028 import javax.servlet.jsp.JspException;
029 import javax.servlet.jsp.PageContext;
030 import javax.servlet.jsp.tagext.BodyContent;
031
032
036 public class ScriptTag extends BaseScriptTag {
037
038 public static void doTag(
039 String position, String use, String bodyContentString,
040 BodyContent previousBodyContent, PageContext pageContext)
041 throws Exception {
042
043 String previousBodyContentString = null;
044
045 if ((previousBodyContent != null) &&
046 !(previousBodyContent instanceof BodyContentWrapper)) {
047
048
049
050 previousBodyContentString = previousBodyContent.getString();
051 }
052
053 ScriptTag scriptTag = new ScriptTag();
054
055 scriptTag.setPageContext(pageContext);
056 scriptTag.setPosition(position);
057 scriptTag.setUse(use);
058
059 BodyContent bodyContent = pageContext.pushBody();
060
061 scriptTag.setBodyContent(bodyContent);
062
063 bodyContent.write(bodyContentString);
064
065 pageContext.popBody();
066
067 scriptTag.doEndTag();
068
069 scriptTag.release();
070
071 if (previousBodyContentString != null) {
072
073
074
075 previousBodyContent.clear();
076
077 previousBodyContent.append(previousBodyContentString);
078 }
079 }
080
081 public static void flushScriptData(PageContext pageContext)
082 throws Exception {
083
084 HttpServletRequest request =
085 (HttpServletRequest)pageContext.getRequest();
086
087 ScriptData scriptData = (ScriptData)request.getAttribute(
088 WebKeys.AUI_SCRIPT_DATA);
089
090 if (scriptData == null) {
091 return;
092 }
093
094 request.removeAttribute(WebKeys.AUI_SCRIPT_DATA);
095
096 scriptData.writeTo(request, pageContext.getOut());
097 }
098
099 @Override
100 public int doEndTag() throws JspException {
101 HttpServletRequest request =
102 (HttpServletRequest)pageContext.getRequest();
103
104 boolean positionInline = isPositionInLine();
105
106 try {
107 String portletId = null;
108
109 Portlet portlet = (Portlet)request.getAttribute(
110 WebKeys.RENDER_PORTLET);
111
112 if (portlet != null) {
113 portletId = portlet.getPortletId();
114 }
115
116 StringBundler bodyContentSB = getBodyContentAsStringBundler();
117
118 String use = getUse();
119
120 if (positionInline) {
121 ScriptData scriptData = new ScriptData();
122
123 scriptData.append(portletId, bodyContentSB, use);
124
125 String page = getPage();
126
127 if (FileAvailabilityUtil.isAvailable(
128 pageContext.getServletContext(), page)) {
129
130 PortalIncludeUtil.include(pageContext, page);
131 }
132 else {
133 scriptData.writeTo(request, pageContext.getOut());
134 }
135 }
136 else {
137 ScriptData scriptData = (ScriptData)request.getAttribute(
138 WebKeys.AUI_SCRIPT_DATA);
139
140 if (scriptData == null) {
141 scriptData = new ScriptData();
142
143 request.setAttribute(WebKeys.AUI_SCRIPT_DATA, scriptData);
144 }
145
146 scriptData.append(portletId, bodyContentSB, use);
147 }
148
149 return EVAL_PAGE;
150 }
151 catch (Exception e) {
152 throw new JspException(e);
153 }
154 finally {
155 if (!ServerDetector.isResin()) {
156 cleanUp();
157 }
158
159 request.removeAttribute(WebKeys.JAVASCRIPT_CONTEXT);
160 }
161 }
162
163 @Override
164 public int doStartTag() throws JspException {
165 HttpServletRequest request =
166 (HttpServletRequest)pageContext.getRequest();
167
168 request.setAttribute(WebKeys.JAVASCRIPT_CONTEXT, Boolean.TRUE);
169
170 return super.doStartTag();
171 }
172
173 @Override
174 protected void cleanUp() {
175 setPosition(null);
176 setUse(null);
177 }
178
179 }