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