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.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    }