1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.taglib.aui;
16  
17  import com.liferay.portal.kernel.servlet.PortalIncludeUtil;
18  import com.liferay.portal.kernel.servlet.taglib.BaseBodyTagSupport;
19  import com.liferay.portal.kernel.servlet.taglib.aui.ScriptData;
20  import com.liferay.portal.kernel.util.ServerDetector;
21  import com.liferay.portal.kernel.util.StringBundler;
22  import com.liferay.portal.kernel.util.Validator;
23  import com.liferay.portal.kernel.util.WebKeys;
24  import com.liferay.portal.theme.ThemeDisplay;
25  
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.jsp.JspException;
28  
29  /**
30   * <a href="ScriptTag.java.html"><b><i>View Source</i></b></a>
31   *
32   * @author Brian Wing Shun Chan
33   */
34  public class ScriptTag extends BaseBodyTagSupport {
35  
36      public static final String PAGE = "/html/taglib/aui/script/page.jsp";
37  
38      public int doEndTag() throws JspException {
39          HttpServletRequest request =
40              (HttpServletRequest)pageContext.getRequest();
41  
42          String position = _position;
43  
44          try {
45              if (Validator.isNull(position)) {
46                  ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
47                      WebKeys.THEME_DISPLAY);
48  
49                  if (themeDisplay.isIsolated() ||
50                      themeDisplay.isLifecycleResource() ||
51                      themeDisplay.isStateExclusive()) {
52  
53                      position = _POSITION_INLINE;
54                  }
55                  else {
56                      position = _POSITION_AUTO;
57                  }
58              }
59  
60              StringBundler bodyContentSB = getBodyContentAsStringBundler();
61  
62              if (position.equals(_POSITION_INLINE)) {
63                  ScriptData scriptData = new ScriptData();
64  
65                  request.setAttribute(ScriptTag.class.getName(), scriptData);
66  
67                  scriptData.append(bodyContentSB, _use);
68  
69                  PortalIncludeUtil.include(pageContext, PAGE);
70              }
71              else {
72                  ScriptData scriptData = (ScriptData)request.getAttribute(
73                      WebKeys.AUI_SCRIPT_DATA);
74  
75                  if (scriptData == null) {
76                      scriptData = new ScriptData();
77  
78                      request.setAttribute(WebKeys.AUI_SCRIPT_DATA, scriptData);
79                  }
80  
81                  scriptData.append(bodyContentSB, _use);
82              }
83  
84              return EVAL_PAGE;
85          }
86          catch (Exception e) {
87              throw new JspException(e);
88          }
89          finally {
90              if (position.equals(_POSITION_INLINE)) {
91                  request.removeAttribute(ScriptTag.class.getName());
92              }
93  
94              if (!ServerDetector.isResin()) {
95                  cleanUp();
96              }
97          }
98      }
99  
100     protected void cleanUp() {
101         _position = null;
102         _use = null;
103     }
104 
105     public void setPosition(String position) {
106         _position = position;
107     }
108 
109     public void setUse(String use) {
110         _use = use;
111     }
112 
113     private static final String _POSITION_AUTO = "auto";
114 
115     private static final String _POSITION_INLINE = "inline";
116 
117     private String _position;
118     private String _use;
119 
120 }