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.util;
16  
17  import com.liferay.portal.kernel.servlet.taglib.BaseBodyTagSupport;
18  import com.liferay.portal.kernel.util.WebKeys;
19  import com.liferay.util.servlet.DynamicServletRequest;
20  
21  import java.util.LinkedHashMap;
22  import java.util.Map;
23  
24  import javax.servlet.ServletContext;
25  import javax.servlet.http.HttpServletRequest;
26  
27  /**
28   * <a href="ParamAndPropertyAncestorTagImpl.java.html"><b><i>View Source</i></b>
29   * </a>
30   *
31   * @author Brian Wing Shun Chan
32   */
33  public class ParamAndPropertyAncestorTagImpl
34      extends BaseBodyTagSupport
35      implements ParamAncestorTag, PropertyAncestorTag {
36  
37      public void addParam(String name, String value) {
38          if (_params == null) {
39              _params = new LinkedHashMap<String, String[]>();
40          }
41  
42          String[] values = _params.get(name);
43  
44          if (values == null) {
45              values = new String[] {value};
46          }
47          else {
48              String[] newValues = new String[values.length + 1];
49  
50              System.arraycopy(values, 0, newValues, 0, values.length);
51  
52              newValues[newValues.length - 1] = value;
53  
54              values = newValues;
55          }
56  
57          _params.put(name, values);
58      }
59  
60      public void addProperty(String name, String value) {
61          if (_properties == null) {
62              _properties = new LinkedHashMap<String, String[]>();
63          }
64  
65          String[] values = _properties.get(name);
66  
67          if (values == null) {
68              values = new String[] {value};
69          }
70          else {
71              String[] newValues = new String[values.length + 1];
72  
73              System.arraycopy(values, 0, newValues, 0, values.length);
74  
75              newValues[newValues.length - 1] = value;
76  
77              values = newValues;
78          }
79  
80          _properties.put(name, values);
81      }
82  
83      public void clearParams() {
84          if (_params != null) {
85              _params.clear();
86          }
87      }
88  
89      public void clearProperties() {
90          if (_properties != null) {
91              _properties.clear();
92          }
93      }
94  
95      public Map<String, String[]> getParams() {
96          return _params;
97      }
98  
99      public Map<String, String[]> getProperties() {
100         return _properties;
101     }
102 
103     public ServletContext getServletContext() {
104         if (_servletContext != null) {
105             return _servletContext;
106         }
107 
108         HttpServletRequest request =
109             (HttpServletRequest)pageContext.getRequest();
110 
111         ServletContext servletContext = (ServletContext)request.getAttribute(
112             WebKeys.CTX);
113 
114         if (servletContext == null) {
115             servletContext = pageContext.getServletContext();
116         }
117 
118         return servletContext;
119     }
120 
121     public HttpServletRequest getServletRequest() {
122         HttpServletRequest request =
123             (HttpServletRequest)pageContext.getRequest();
124 
125         if (_params != null) {
126             request = new DynamicServletRequest(request, _params);
127         }
128 
129         return request;
130     }
131 
132     public void setServletContext(ServletContext servletContext) {
133         _servletContext = servletContext;
134     }
135 
136     private Map<String, String[]> _params;
137     private Map<String, String[]> _properties;
138     private ServletContext _servletContext;
139 
140 }