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.BrowserSnifferUtil;
019    import com.liferay.portal.kernel.servlet.PortalIncludeUtil;
020    import com.liferay.portal.kernel.servlet.taglib.FileAvailabilityUtil;
021    import com.liferay.portal.kernel.servlet.taglib.aui.ScriptData;
022    import com.liferay.portal.kernel.util.ServerDetector;
023    import com.liferay.portal.kernel.util.StringBundler;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.util.WebKeys;
026    import com.liferay.portal.model.Portlet;
027    import com.liferay.taglib.aui.base.BaseScriptTag;
028    
029    import java.util.Set;
030    
031    import javax.servlet.http.HttpServletRequest;
032    import javax.servlet.jsp.JspException;
033    import javax.servlet.jsp.JspWriter;
034    import javax.servlet.jsp.PageContext;
035    import javax.servlet.jsp.tagext.BodyContent;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     * @author Shuyang Zhou
040     */
041    public class ScriptTag extends BaseScriptTag {
042    
043            public static void doTag(
044                            String position, String use, String bodyContentString,
045                            BodyContent previousBodyContent, PageContext pageContext)
046                    throws Exception {
047    
048                    String previousBodyContentString = null;
049    
050                    if ((previousBodyContent != null) &&
051                            !(previousBodyContent instanceof BodyContentWrapper)) {
052    
053                            // LPS-22413
054    
055                            previousBodyContentString = previousBodyContent.getString();
056                    }
057    
058                    ScriptTag scriptTag = new ScriptTag();
059    
060                    scriptTag.setPageContext(pageContext);
061                    scriptTag.setPosition(position);
062                    scriptTag.setUse(use);
063    
064                    BodyContent bodyContent = pageContext.pushBody();
065    
066                    scriptTag.setBodyContent(bodyContent);
067    
068                    bodyContent.write(bodyContentString);
069    
070                    pageContext.popBody();
071    
072                    scriptTag.doEndTag();
073    
074                    scriptTag.release();
075    
076                    if (previousBodyContentString != null) {
077    
078                            // LPS-22413
079    
080                            previousBodyContent.clear();
081    
082                            previousBodyContent.append(previousBodyContentString);
083                    }
084            }
085    
086            public static void flushScriptData(PageContext pageContext)
087                    throws Exception {
088    
089                    HttpServletRequest request =
090                            (HttpServletRequest)pageContext.getRequest();
091    
092                    ScriptData scriptData = (ScriptData)request.getAttribute(
093                            ScriptTag.class.getName());
094    
095                    if (scriptData == null) {
096                            scriptData = (ScriptData)request.getAttribute(
097                                    WebKeys.AUI_SCRIPT_DATA);
098    
099                            if (scriptData != null) {
100                                    request.removeAttribute(WebKeys.AUI_SCRIPT_DATA);
101                            }
102                    }
103    
104                    if (scriptData != null) {
105                            ScriptTag scriptTag = new ScriptTag();
106    
107                            scriptTag.setPageContext(pageContext);
108    
109                            scriptTag.processEndTag(scriptData);
110                    }
111            }
112    
113            @Override
114            public int doEndTag() throws JspException {
115                    HttpServletRequest request =
116                            (HttpServletRequest)pageContext.getRequest();
117    
118                    boolean positionInline = isPositionInLine();
119    
120                    try {
121                            String portletId = null;
122    
123                            Portlet portlet = (Portlet)request.getAttribute(
124                                    WebKeys.RENDER_PORTLET);
125    
126                            if (portlet != null) {
127                                    portletId = portlet.getPortletId();
128                            }
129    
130                            StringBundler bodyContentSB = getBodyContentAsStringBundler();
131    
132                            String use = getUse();
133    
134                            if (positionInline) {
135                                    ScriptData scriptData = new ScriptData();
136    
137                                    request.setAttribute(ScriptTag.class.getName(), scriptData);
138    
139                                    scriptData.append(portletId, bodyContentSB, use);
140    
141                                    String page = getPage();
142    
143                                    if (FileAvailabilityUtil.isAvailable(
144                                                    pageContext.getServletContext(), page)) {
145    
146                                            PortalIncludeUtil.include(pageContext, page);
147                                    }
148                                    else {
149                                            processEndTag(scriptData);
150                                    }
151                            }
152                            else {
153                                    ScriptData scriptData = (ScriptData)request.getAttribute(
154                                            WebKeys.AUI_SCRIPT_DATA);
155    
156                                    if (scriptData == null) {
157                                            scriptData = new ScriptData();
158    
159                                            request.setAttribute(WebKeys.AUI_SCRIPT_DATA, scriptData);
160                                    }
161    
162                                    scriptData.append(portletId, bodyContentSB, use);
163                            }
164    
165                            return EVAL_PAGE;
166                    }
167                    catch (Exception e) {
168                            throw new JspException(e);
169                    }
170                    finally {
171                            if (positionInline) {
172                                    request.removeAttribute(ScriptTag.class.getName());
173                            }
174    
175                            if (!ServerDetector.isResin()) {
176                                    cleanUp();
177                            }
178    
179                            request.removeAttribute(WebKeys.JAVASCRIPT_CONTEXT);
180                    }
181            }
182    
183            @Override
184            public int doStartTag() throws JspException {
185                    HttpServletRequest request =
186                            (HttpServletRequest)pageContext.getRequest();
187    
188                    request.setAttribute(WebKeys.JAVASCRIPT_CONTEXT, Boolean.TRUE);
189    
190                    return super.doStartTag();
191            }
192    
193            @Override
194            protected void cleanUp() {
195                    setPosition(null);
196                    setUse(null);
197            }
198    
199            protected void processEndTag(ScriptData scriptData) throws Exception {
200                    JspWriter jspWriter = pageContext.getOut();
201    
202                    jspWriter.write("<script type=\"text/javascript\">\n// <![CDATA[\n");
203    
204                    StringBundler rawSB = scriptData.getRawSB();
205    
206                    rawSB.writeTo(jspWriter);
207    
208                    StringBundler callbackSB = scriptData.getCallbackSB();
209    
210                    if (callbackSB.index() > 0) {
211                            String loadMethod = "use";
212    
213                            HttpServletRequest request =
214                                    (HttpServletRequest)pageContext.getRequest();
215    
216                            if (BrowserSnifferUtil.isIe(request) &&
217                                    (BrowserSnifferUtil.getMajorVersion(request) < 8)) {
218    
219                                    loadMethod = "ready";
220                            }
221    
222                            jspWriter.write("AUI().");
223                            jspWriter.write( loadMethod );
224                            jspWriter.write("(");
225    
226                            Set<String> useSet = scriptData.getUseSet();
227    
228                            for (String use : useSet) {
229                                    jspWriter.write(StringPool.APOSTROPHE);
230                                    jspWriter.write(use);
231                                    jspWriter.write(StringPool.APOSTROPHE);
232                                    jspWriter.write(StringPool.COMMA_AND_SPACE);
233                            }
234    
235                            jspWriter.write("function(A) {");
236    
237                            callbackSB.writeTo(jspWriter);
238    
239                            jspWriter.write("});");
240                    }
241    
242                    jspWriter.write("\n// ]]>\n</script>");
243            }
244    
245    }