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            protected void cleanUp() {
114                    setPosition(null);
115                    setUse(null);
116            }
117    
118            @Override
119            public int doEndTag() throws JspException {
120                    HttpServletRequest request =
121                            (HttpServletRequest)pageContext.getRequest();
122    
123                    boolean positionInline = isPositionInLine();
124    
125                    try {
126                            StringBundler bodyContentSB = getBodyContentAsStringBundler();
127    
128                            String use = getUse();
129    
130                            if (positionInline) {
131                                    ScriptData scriptData = new ScriptData();
132    
133                                    request.setAttribute(ScriptTag.class.getName(), scriptData);
134    
135                                    scriptData.append(bodyContentSB, use);
136    
137                                    String page = getPage();
138    
139                                    if (FileAvailabilityUtil.isAvailable(
140                                                    pageContext.getServletContext(), page)) {
141    
142                                            PortalIncludeUtil.include(pageContext, page);
143                                    }
144                                    else {
145                                            processEndTag(scriptData);
146                                    }
147                            }
148                            else {
149                                    ScriptData scriptData = (ScriptData)request.getAttribute(
150                                            WebKeys.AUI_SCRIPT_DATA);
151    
152                                    if (scriptData == null) {
153                                            scriptData = new ScriptData();
154    
155                                            request.setAttribute(WebKeys.AUI_SCRIPT_DATA, scriptData);
156                                    }
157    
158                                    scriptData.append(bodyContentSB, use);
159                            }
160    
161                            return EVAL_PAGE;
162                    }
163                    catch (Exception e) {
164                            throw new JspException(e);
165                    }
166                    finally {
167                            if (positionInline) {
168                                    request.removeAttribute(ScriptTag.class.getName());
169                            }
170    
171                            if (!ServerDetector.isResin()) {
172                                    cleanUp();
173                            }
174                    }
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    }