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.util.ServerDetector;
19  import com.liferay.portal.kernel.util.StringPool;
20  import com.liferay.portal.kernel.util.Validator;
21  import com.liferay.portal.kernel.util.WebKeys;
22  import com.liferay.portal.servlet.taglib.aui.ScriptData;
23  import com.liferay.portal.theme.ThemeDisplay;
24  
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.jsp.JspException;
27  import javax.servlet.jsp.tagext.BodyTagSupport;
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 BodyTagSupport {
35  
36      public static final String PAGE = "/html/taglib/aui/script/page.jsp";
37  
38      public int doAfterBody() {
39          _bodyContentString = getBodyContent().getString();
40  
41          return SKIP_BODY;
42      }
43  
44      public int doEndTag() throws JspException {
45          HttpServletRequest request =
46              (HttpServletRequest)pageContext.getRequest();
47  
48          String position = _position;
49  
50          try {
51              if (Validator.isNull(position)) {
52                  ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
53                      WebKeys.THEME_DISPLAY);
54  
55                  if (themeDisplay.isIsolated() ||
56                      themeDisplay.isLifecycleResource() ||
57                      themeDisplay.isStateExclusive()) {
58  
59                      position = _POSITION_INLINE;
60                  }
61                  else {
62                      position = _POSITION_AUTO;
63                  }
64              }
65  
66              if (position.equals(_POSITION_INLINE)) {
67                  ScriptData scriptData = new ScriptData();
68  
69                  request.setAttribute(ScriptTag.class.getName(), scriptData);
70  
71                  scriptData.append(_bodyContentString, _use);
72  
73                  PortalIncludeUtil.include(pageContext, PAGE);
74              }
75              else {
76                  ScriptData scriptData = (ScriptData)request.getAttribute(
77                      WebKeys.AUI_SCRIPT_DATA);
78  
79                  if (scriptData == null) {
80                      scriptData = new ScriptData();
81  
82                      request.setAttribute(WebKeys.AUI_SCRIPT_DATA, scriptData);
83                  }
84  
85                  scriptData.append(_bodyContentString, _use);
86              }
87  
88              return EVAL_PAGE;
89          }
90          catch (Exception e) {
91              throw new JspException(e);
92          }
93          finally {
94              if (position.equals(_POSITION_INLINE)) {
95                  request.removeAttribute(ScriptTag.class.getName());
96              }
97  
98              if (!ServerDetector.isResin()) {
99                  cleanUp();
100             }
101         }
102     }
103 
104     protected void cleanUp() {
105         _bodyContentString = StringPool.BLANK;
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 _bodyContentString = StringPool.BLANK;
123     private String _position;
124     private String _use;
125 
126 }