001    /**
002     * Copyright (c) 2000-2010 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.PortalIncludeUtil;
018    import com.liferay.portal.kernel.servlet.taglib.BaseBodyTagSupport;
019    import com.liferay.portal.kernel.servlet.taglib.aui.ScriptData;
020    import com.liferay.portal.kernel.util.ParamUtil;
021    import com.liferay.portal.kernel.util.ServerDetector;
022    import com.liferay.portal.kernel.util.StringBundler;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.kernel.util.WebKeys;
025    import com.liferay.portal.theme.ThemeDisplay;
026    
027    import javax.servlet.http.HttpServletRequest;
028    import javax.servlet.jsp.JspException;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     */
033    public class ScriptTag extends BaseBodyTagSupport {
034    
035            public static final String PAGE = "/html/taglib/aui/script/page.jsp";
036    
037            public int doEndTag() throws JspException {
038                    HttpServletRequest request =
039                            (HttpServletRequest)pageContext.getRequest();
040    
041                    String position = _position;
042    
043                    String fragmentId = ParamUtil.getString(request, "p_f_id");
044    
045                    if (Validator.isNotNull(fragmentId)) {
046                            position = _POSITION_INLINE;
047                    }
048    
049                    try {
050                            if (Validator.isNull(position)) {
051                                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
052                                            WebKeys.THEME_DISPLAY);
053    
054                                    if (themeDisplay.isIsolated() ||
055                                            themeDisplay.isLifecycleResource() ||
056                                            themeDisplay.isStateExclusive()) {
057    
058                                            position = _POSITION_INLINE;
059                                    }
060                                    else {
061                                            position = _POSITION_AUTO;
062                                    }
063                            }
064    
065                            StringBundler bodyContentSB = getBodyContentAsStringBundler();
066    
067                            if (position.equals(_POSITION_INLINE)) {
068                                    ScriptData scriptData = new ScriptData();
069    
070                                    request.setAttribute(ScriptTag.class.getName(), scriptData);
071    
072                                    scriptData.append(bodyContentSB, _use);
073    
074                                    PortalIncludeUtil.include(pageContext, PAGE);
075                            }
076                            else {
077                                    ScriptData scriptData = (ScriptData)request.getAttribute(
078                                            WebKeys.AUI_SCRIPT_DATA);
079    
080                                    if (scriptData == null) {
081                                            scriptData = new ScriptData();
082    
083                                            request.setAttribute(WebKeys.AUI_SCRIPT_DATA, scriptData);
084                                    }
085    
086                                    scriptData.append(bodyContentSB, _use);
087                            }
088    
089                            return EVAL_PAGE;
090                    }
091                    catch (Exception e) {
092                            throw new JspException(e);
093                    }
094                    finally {
095                            if (position.equals(_POSITION_INLINE)) {
096                                    request.removeAttribute(ScriptTag.class.getName());
097                            }
098    
099                            if (!ServerDetector.isResin()) {
100                                    cleanUp();
101                            }
102                    }
103            }
104    
105            protected void cleanUp() {
106                    _position = null;
107                    _use = null;
108            }
109    
110            public void setPosition(String position) {
111                    _position = position;
112            }
113    
114            public void setUse(String use) {
115                    _use = use;
116            }
117    
118            private static final String _POSITION_AUTO = "auto";
119    
120            private static final String _POSITION_INLINE = "inline";
121    
122            private String _position;
123            private String _use;
124    
125    }