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 doStartTag() {
39          return EVAL_BODY_BUFFERED;
40      }
41  
42      public int doAfterBody() {
43          _bodyContentString = getBodyContent().getString();
44  
45          return SKIP_BODY;
46      }
47  
48      public int doEndTag() throws JspException {
49          HttpServletRequest request =
50              (HttpServletRequest)pageContext.getRequest();
51  
52          try {
53              if (Validator.isNull(_position)) {
54                  ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
55                      WebKeys.THEME_DISPLAY);
56  
57                  if (themeDisplay.isIsolated() ||
58                      themeDisplay.isLifecycleResource() ||
59                      themeDisplay.isStateExclusive()) {
60  
61                      _position = _POSITION_INLINE;
62                  }
63                  else {
64                      _position = _POSITION_AUTO;
65                  }
66              }
67  
68              if (_position.equals(_POSITION_INLINE)) {
69                  ScriptData scriptData = new ScriptData();
70  
71                  request.setAttribute(ScriptTag.class.getName(), scriptData);
72  
73                  scriptData.append(_bodyContentString, _use);
74  
75                  PortalIncludeUtil.include(pageContext, PAGE);
76              }
77              else {
78                  ScriptData scriptData = (ScriptData)request.getAttribute(
79                      WebKeys.AUI_SCRIPT_DATA);
80  
81                  if (scriptData == null) {
82                      scriptData = new ScriptData();
83  
84                      request.setAttribute(WebKeys.AUI_SCRIPT_DATA, scriptData);
85                  }
86  
87                  scriptData.append(_bodyContentString, _use);
88              }
89  
90              return EVAL_PAGE;
91          }
92          catch (Exception e) {
93              throw new JspException(e);
94          }
95          finally {
96              if (_position.equals(_POSITION_INLINE)) {
97                  request.removeAttribute(ScriptTag.class.getName());
98              }
99  
100             if (!ServerDetector.isResin()) {
101                 _bodyContentString = StringPool.BLANK;
102                 _position = null;
103                 _use = null;
104             }
105         }
106     }
107 
108     public void setPosition(String position) {
109         _position = position;
110     }
111 
112     public void setUse(String use) {
113         _use = use;
114     }
115 
116     private static final String _POSITION_AUTO = "auto";
117 
118     private static final String _POSITION_INLINE = "inline";
119 
120     private String _bodyContentString = StringPool.BLANK;
121     private String _position;
122     private String _use;
123 
124 }