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.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            public void setDynamicAttribute(
058                    String uri, String localName, Object value) {
059    
060                    _dynamicAttributes.put(localName, value);
061            }
062    
063            public void setNamespacedAttribute(
064                    HttpServletRequest request, String key, Object value) {
065    
066                    if (value instanceof Boolean) {
067                            value = String.valueOf(value);
068                    }
069                    else if (value instanceof Number) {
070                            value = String.valueOf(value);
071                    }
072    
073                    request.setAttribute(_encodeKey(key), value);
074            }
075    
076            public void setScopedAttribute(String name, Object value) {
077                    _scopedAttributes.put(name, value);
078            }
079    
080            protected Map<String, Object> getDynamicAttributes() {
081                    return _dynamicAttributes;
082            }
083    
084            private String _encodeKey(String key) {
085                    if (_attributeNamespace.length() == 0) {
086                            return key;
087                    }
088                    else {
089                            return _attributeNamespace.concat(key);
090                    }
091            }
092    
093            private String _attributeNamespace = StringPool.BLANK;
094            private Map<String, Object> _dynamicAttributes =
095                    new HashMap<String, Object>();
096            private Map<String, Object> _scopedAttributes =
097                    new HashMap<String, Object>();
098    
099    }