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