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.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            }
170    
171            @Override
172            protected void cleanUp() {
173                    setPosition(null);
174                    setUse(null);
175            }
176    
177            protected void processEndTag(ScriptData scriptData) throws Exception {
178                    JspWriter jspWriter = pageContext.getOut();
179    
180                    jspWriter.write("<script type=\"text/javascript\">\n// <![CDATA[\n");
181    
182                    StringBundler rawSB = scriptData.getRawSB();
183    
184                    rawSB.writeTo(jspWriter);
185    
186                    StringBundler callbackSB = scriptData.getCallbackSB();
187    
188                    if (callbackSB.index() > 0) {
189                            String loadMethod = "use";
190    
191                            HttpServletRequest request =
192                                    (HttpServletRequest)pageContext.getRequest();
193    
194                            if (BrowserSnifferUtil.isIe(request) &&
195                                    (BrowserSnifferUtil.getMajorVersion(request) < 8)) {
196    
197                                    loadMethod = "ready";
198                            }
199    
200                            jspWriter.write("AUI().");
201                            jspWriter.write( loadMethod );
202                            jspWriter.write("(");
203    
204                            Set<String> useSet = scriptData.getUseSet();
205    
206                            for (String use : useSet) {
207                                    jspWriter.write(StringPool.APOSTROPHE);
208                                    jspWriter.write(use);
209                                    jspWriter.write(StringPool.APOSTROPHE);
210                                    jspWriter.write(StringPool.COMMA_AND_SPACE);
211                            }
212    
213                            jspWriter.write("function(A) {");
214    
215                            callbackSB.writeTo(jspWriter);
216    
217                            jspWriter.write("});");
218                    }
219    
220                    jspWriter.write("\n// ]]>\n</script>");
221            }
222    
223    }