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