001    /**
002     * Copyright (c) 2000-2012 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.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    /**
033     * @author Brian Wing Shun Chan
034     * @author Shuyang Zhou
035     */
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                            // LPS-22413
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                            // LPS-22413
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                    AUIUtil.outputScriptData(request, pageContext.getOut());
088            }
089    
090            @Override
091            public int doEndTag() throws JspException {
092                    HttpServletRequest request =
093                            (HttpServletRequest)pageContext.getRequest();
094    
095                    boolean positionInline = isPositionInLine();
096    
097                    try {
098                            String portletId = null;
099    
100                            Portlet portlet = (Portlet)request.getAttribute(
101                                    WebKeys.RENDER_PORTLET);
102    
103                            if (portlet != null) {
104                                    portletId = portlet.getPortletId();
105                            }
106    
107                            StringBundler bodyContentSB = getBodyContentAsStringBundler();
108    
109                            String use = getUse();
110    
111                            if (positionInline) {
112                                    ScriptData scriptData = new ScriptData();
113    
114                                    request.setAttribute(ScriptTag.class.getName(), scriptData);
115    
116                                    scriptData.append(portletId, bodyContentSB, use);
117    
118                                    String page = getPage();
119    
120                                    if (FileAvailabilityUtil.isAvailable(
121                                                    pageContext.getServletContext(), page)) {
122    
123                                            PortalIncludeUtil.include(pageContext, page);
124                                    }
125                                    else {
126                                            AUIUtil.outputScriptData(request, pageContext.getOut());
127                                    }
128                            }
129                            else {
130                                    ScriptData scriptData = (ScriptData)request.getAttribute(
131                                            WebKeys.AUI_SCRIPT_DATA);
132    
133                                    if (scriptData == null) {
134                                            scriptData = new ScriptData();
135    
136                                            request.setAttribute(WebKeys.AUI_SCRIPT_DATA, scriptData);
137                                    }
138    
139                                    scriptData.append(portletId, bodyContentSB, use);
140                            }
141    
142                            return EVAL_PAGE;
143                    }
144                    catch (Exception e) {
145                            throw new JspException(e);
146                    }
147                    finally {
148                            if (positionInline) {
149                                    request.removeAttribute(ScriptTag.class.getName());
150                            }
151    
152                            if (!ServerDetector.isResin()) {
153                                    cleanUp();
154                            }
155    
156                            request.removeAttribute(WebKeys.JAVASCRIPT_CONTEXT);
157                    }
158            }
159    
160            @Override
161            public int doStartTag() throws JspException {
162                    HttpServletRequest request =
163                            (HttpServletRequest)pageContext.getRequest();
164    
165                    request.setAttribute(WebKeys.JAVASCRIPT_CONTEXT, Boolean.TRUE);
166    
167                    return super.doStartTag();
168            }
169    
170            @Override
171            protected void cleanUp() {
172                    setPosition(null);
173                    setUse(null);
174            }
175    
176    }