001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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            @Override
039            public void addParam(String name, String value) {
040                    if (_params == null) {
041                            _params = new LinkedHashMap<String, String[]>();
042                    }
043    
044                    // PLT.26.6
045    
046                    if (!_allowEmptyParam && ((value == null) || (value.length() == 0))) {
047                            _params.remove(name);
048    
049                            if (_removedParameterNames == null) {
050                                    _removedParameterNames = new HashSet<String>();
051                            }
052    
053                            _removedParameterNames.add(name);
054    
055                            return;
056                    }
057    
058                    String[] values = _params.get(name);
059    
060                    if (values == null) {
061                            values = new String[] {value};
062                    }
063                    else {
064                            String[] newValues = new String[values.length + 1];
065    
066                            System.arraycopy(values, 0, newValues, 0, values.length);
067    
068                            newValues[newValues.length - 1] = value;
069    
070                            values = newValues;
071                    }
072    
073                    _params.put(name, values);
074            }
075    
076            @Override
077            public void addProperty(String name, String value) {
078                    if (_properties == null) {
079                            _properties = new LinkedHashMap<String, String[]>();
080                    }
081    
082                    String[] values = _properties.get(name);
083    
084                    if (values == null) {
085                            values = new String[] {value};
086                    }
087                    else {
088                            String[] newValues = new String[values.length + 1];
089    
090                            System.arraycopy(values, 0, newValues, 0, values.length);
091    
092                            newValues[newValues.length - 1] = value;
093    
094                            values = newValues;
095                    }
096    
097                    _properties.put(name, values);
098            }
099    
100            public void clearParams() {
101                    if (_params != null) {
102                            _params.clear();
103                    }
104    
105                    if (_removedParameterNames != null) {
106                            _removedParameterNames.clear();
107                    }
108            }
109    
110            public void clearProperties() {
111                    if (_properties != null) {
112                            _properties.clear();
113                    }
114            }
115    
116            public Map<String, String[]> getParams() {
117                    return _params;
118            }
119    
120            public Map<String, String[]> getProperties() {
121                    return _properties;
122            }
123    
124            public Set<String> getRemovedParameterNames() {
125                    return _removedParameterNames;
126            }
127    
128            public ServletContext getServletContext() {
129                    if (_servletContext != null) {
130                            return _servletContext;
131                    }
132    
133                    HttpServletRequest request =
134                            (HttpServletRequest)pageContext.getRequest();
135    
136                    ServletContext servletContext = (ServletContext)request.getAttribute(
137                            WebKeys.CTX);
138    
139                    if (servletContext == null) {
140                            servletContext = pageContext.getServletContext();
141                    }
142    
143                    return servletContext;
144            }
145    
146            public HttpServletRequest getServletRequest() {
147                    HttpServletRequest request =
148                            (HttpServletRequest)pageContext.getRequest();
149    
150                    if (_params != null) {
151                            request = new DynamicServletRequest(request, _params);
152                    }
153    
154                    return request;
155            }
156    
157            public HttpServletResponse getServletResponse() {
158                    HttpServletResponse response =
159                            (HttpServletResponse)pageContext.getResponse();
160    
161                    return response;
162            }
163    
164            public boolean isAllowEmptyParam() {
165                    return _allowEmptyParam;
166            }
167    
168            public void setAllowEmptyParam(boolean allowEmptyParam) {
169                    _allowEmptyParam = allowEmptyParam;
170            }
171    
172            public void setServletContext(ServletContext servletContext) {
173                    _servletContext = servletContext;
174            }
175    
176            private boolean _allowEmptyParam;
177            private Map<String, String[]> _params;
178            private Map<String, String[]> _properties;
179            private Set<String> _removedParameterNames;
180            private ServletContext _servletContext;
181    
182    }