001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.taglib.util;
016    
017    import com.liferay.portal.kernel.servlet.DynamicServletRequest;
018    import com.liferay.portal.kernel.servlet.taglib.BaseBodyTagSupport;
019    import com.liferay.portal.kernel.util.WebKeys;
020    
021    import java.util.HashSet;
022    import java.util.LinkedHashMap;
023    import java.util.Map;
024    import java.util.Set;
025    
026    import javax.servlet.ServletContext;
027    import javax.servlet.http.HttpServletRequest;
028    import javax.servlet.http.HttpServletResponse;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     * @author Shuyang Zhou
033     */
034    public class ParamAndPropertyAncestorTagImpl
035            extends BaseBodyTagSupport
036            implements ParamAncestorTag, PropertyAncestorTag {
037    
038            public void addParam(String name, String value) {
039                    if (_params == null) {
040                            _params = new LinkedHashMap<String, String[]>();
041                    }
042    
043                    // PLT.26.6
044    
045                    if (!_allowEmptyParam && ((value == null) || (value.length() == 0))) {
046                            _params.remove(name);
047    
048                            if (_removedParameterNames == null) {
049                                    _removedParameterNames = new HashSet<String>();
050                            }
051    
052                            _removedParameterNames.add(name);
053    
054                            return;
055                    }
056    
057                    String[] values = _params.get(name);
058    
059                    if (values == null) {
060                            values = new String[] {value};
061                    }
062                    else {
063                            String[] newValues = new String[values.length + 1];
064    
065                            System.arraycopy(values, 0, newValues, 0, values.length);
066    
067                            newValues[newValues.length - 1] = value;
068    
069                            values = newValues;
070                    }
071    
072                    _params.put(name, values);
073            }
074    
075            public void addProperty(String name, String value) {
076                    if (_properties == null) {
077                            _properties = new LinkedHashMap<String, String[]>();
078                    }
079    
080                    String[] values = _properties.get(name);
081    
082                    if (values == null) {
083                            values = new String[] {value};
084                    }
085                    else {
086                            String[] newValues = new String[values.length + 1];
087    
088                            System.arraycopy(values, 0, newValues, 0, values.length);
089    
090                            newValues[newValues.length - 1] = value;
091    
092                            values = newValues;
093                    }
094    
095                    _properties.put(name, values);
096            }
097    
098            public void clearParams() {
099                    if (_params != null) {
100                            _params.clear();
101                    }
102    
103                    if (_removedParameterNames != null) {
104                            _removedParameterNames.clear();
105                    }
106            }
107    
108            public void clearProperties() {
109                    if (_properties != null) {
110                            _properties.clear();
111                    }
112            }
113    
114            public Map<String, String[]> getParams() {
115                    return _params;
116            }
117    
118            public Map<String, String[]> getProperties() {
119                    return _properties;
120            }
121    
122            public Set<String> getRemovedParameterNames() {
123                    return _removedParameterNames;
124            }
125    
126            public ServletContext getServletContext() {
127                    if (_servletContext != null) {
128                            return _servletContext;
129                    }
130    
131                    HttpServletRequest request =
132                            (HttpServletRequest)pageContext.getRequest();
133    
134                    ServletContext servletContext = (ServletContext)request.getAttribute(
135                            WebKeys.CTX);
136    
137                    if (servletContext == null) {
138                            servletContext = pageContext.getServletContext();
139                    }
140    
141                    return servletContext;
142            }
143    
144            public HttpServletRequest getServletRequest() {
145                    HttpServletRequest request =
146                            (HttpServletRequest)pageContext.getRequest();
147    
148                    if (_params != null) {
149                            request = new DynamicServletRequest(request, _params);
150                    }
151    
152                    return request;
153            }
154    
155            public HttpServletResponse getServletResponse() {
156                    HttpServletResponse response =
157                            (HttpServletResponse)pageContext.getResponse();
158    
159                    return response;
160            }
161    
162            public boolean isAllowEmptyParam() {
163                    return _allowEmptyParam;
164            }
165    
166            public void setAllowEmptyParam(boolean allowEmptyParam) {
167                    _allowEmptyParam = allowEmptyParam;
168            }
169    
170            public void setServletContext(ServletContext servletContext) {
171                    _servletContext = servletContext;
172            }
173    
174            private boolean _allowEmptyParam;
175            private Map<String, String[]> _params;
176            private Map<String, String[]> _properties;
177            private Set<String> _removedParameterNames;
178            private ServletContext _servletContext;
179    
180    }