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