001    /**
002     * Copyright (c) 2000-2013 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.util.StringPool;
018    
019    import java.util.HashMap;
020    import java.util.Map;
021    
022    import javax.servlet.http.HttpServletRequest;
023    import javax.servlet.jsp.tagext.DynamicAttributes;
024    
025    /**
026     * @author Eduardo Lundgren
027     * @author Shuyang Zhou
028     */
029    public class AttributesTagSupport
030            extends ParamAndPropertyAncestorTagImpl implements DynamicAttributes {
031    
032            public void clearDynamicAttributes() {
033                    _dynamicAttributes.clear();
034            }
035    
036            public String getAttributeNamespace() {
037                    return _attributeNamespace;
038            }
039    
040            public Map<String, Object> getScopedAttributes() {
041                    return _scopedAttributes;
042            }
043    
044            @Override
045            public void release() {
046                    super.release();
047    
048                    _attributeNamespace = null;
049                    _dynamicAttributes = null;
050                    _scopedAttributes = null;
051            }
052    
053            public void setAttributeNamespace(String attributeNamespace) {
054                    _attributeNamespace = attributeNamespace;
055            }
056    
057            @Override
058            public void setDynamicAttribute(
059                    String uri, String localName, Object value) {
060    
061                    _dynamicAttributes.put(localName, value);
062            }
063    
064            public void setNamespacedAttribute(
065                    HttpServletRequest request, String key, Object value) {
066    
067                    if (value instanceof Boolean) {
068                            value = String.valueOf(value);
069                    }
070                    else if (value instanceof Number) {
071                            value = String.valueOf(value);
072                    }
073    
074                    request.setAttribute(_encodeKey(key), value);
075            }
076    
077            public void setScopedAttribute(String name, Object value) {
078                    _scopedAttributes.put(name, value);
079            }
080    
081            protected Map<String, Object> getDynamicAttributes() {
082                    return _dynamicAttributes;
083            }
084    
085            private String _encodeKey(String key) {
086                    if (_attributeNamespace.length() == 0) {
087                            return key;
088                    }
089                    else {
090                            return _attributeNamespace.concat(key);
091                    }
092            }
093    
094            private String _attributeNamespace = StringPool.BLANK;
095            private Map<String, Object> _dynamicAttributes =
096                    new HashMap<String, Object>();
097            private Map<String, Object> _scopedAttributes =
098                    new HashMap<String, Object>();
099    
100    }