001    /**
002     * Copyright (c) 2000-2012 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.taglib.BaseBodyTagSupport;
018    import com.liferay.portal.kernel.util.WebKeys;
019    import com.liferay.util.servlet.DynamicServletRequest;
020    
021    import java.util.LinkedHashMap;
022    import java.util.Map;
023    
024    import javax.servlet.ServletContext;
025    import javax.servlet.http.HttpServletRequest;
026    import javax.servlet.http.HttpServletResponse;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     */
031    public class ParamAndPropertyAncestorTagImpl
032            extends BaseBodyTagSupport
033            implements ParamAncestorTag, PropertyAncestorTag {
034    
035            public void addParam(String name, String value) {
036                    if (_params == null) {
037                            _params = new LinkedHashMap<String, String[]>();
038                    }
039    
040                    String[] values = _params.get(name);
041    
042                    if (values == null) {
043                            values = new String[] {value};
044                    }
045                    else {
046                            String[] newValues = new String[values.length + 1];
047    
048                            System.arraycopy(values, 0, newValues, 0, values.length);
049    
050                            newValues[newValues.length - 1] = value;
051    
052                            values = newValues;
053                    }
054    
055                    _params.put(name, values);
056            }
057    
058            public void addProperty(String name, String value) {
059                    if (_properties == null) {
060                            _properties = new LinkedHashMap<String, String[]>();
061                    }
062    
063                    String[] values = _properties.get(name);
064    
065                    if (values == null) {
066                            values = new String[] {value};
067                    }
068                    else {
069                            String[] newValues = new String[values.length + 1];
070    
071                            System.arraycopy(values, 0, newValues, 0, values.length);
072    
073                            newValues[newValues.length - 1] = value;
074    
075                            values = newValues;
076                    }
077    
078                    _properties.put(name, values);
079            }
080    
081            public void clearParams() {
082                    if (_params != null) {
083                            _params.clear();
084                    }
085            }
086    
087            public void clearProperties() {
088                    if (_properties != null) {
089                            _properties.clear();
090                    }
091            }
092    
093            public Map<String, String[]> getParams() {
094                    return _params;
095            }
096    
097            public Map<String, String[]> getProperties() {
098                    return _properties;
099            }
100    
101            public ServletContext getServletContext() {
102                    if (_servletContext != null) {
103                            return _servletContext;
104                    }
105    
106                    HttpServletRequest request =
107                            (HttpServletRequest)pageContext.getRequest();
108    
109                    ServletContext servletContext = (ServletContext)request.getAttribute(
110                            WebKeys.CTX);
111    
112                    if (servletContext == null) {
113                            servletContext = pageContext.getServletContext();
114                    }
115    
116                    return servletContext;
117            }
118    
119            public HttpServletRequest getServletRequest() {
120                    HttpServletRequest request =
121                            (HttpServletRequest)pageContext.getRequest();
122    
123                    if (_params != null) {
124                            request = new DynamicServletRequest(request, _params);
125                    }
126    
127                    return request;
128            }
129    
130            public HttpServletResponse getServletResponse() {
131                    HttpServletResponse response =
132                            (HttpServletResponse)pageContext.getResponse();
133    
134                    return response;
135            }
136    
137            public void setServletContext(ServletContext servletContext) {
138                    _servletContext = servletContext;
139            }
140    
141            private Map<String, String[]> _params;
142            private Map<String, String[]> _properties;
143            private ServletContext _servletContext;
144    
145    }