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.CustomAttributes;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.portal.kernel.util.Validator;
020    
021    import java.io.IOException;
022    
023    import java.util.HashMap;
024    import java.util.Map;
025    
026    import javax.servlet.http.HttpServletRequest;
027    import javax.servlet.jsp.JspWriter;
028    import javax.servlet.jsp.tagext.DynamicAttributes;
029    
030    /**
031     * @author Eduardo Lundgren
032     * @author Shuyang Zhou
033     */
034    public class AttributesTagSupport
035            extends ParamAndPropertyAncestorTagImpl implements DynamicAttributes {
036    
037            public void clearDynamicAttributes() {
038                    _dynamicAttributes.clear();
039            }
040    
041            public String getAttributeNamespace() {
042                    return _attributeNamespace;
043            }
044    
045            public CustomAttributes getCustomAttributes() {
046                    return _customAttributes;
047            }
048    
049            public Object getDynamicAttribute(String key) {
050                    return _dynamicAttributes.get(key);
051            }
052    
053            public Object getNamespacedAttribute(
054                    HttpServletRequest request, String key) {
055    
056                    return request.getAttribute(_encodeKey(key));
057            }
058    
059            public Object getScopedAttribute(String key) {
060                    return _scopedAttributes.get(key);
061            }
062    
063            public Map<String, Object> getScopedAttributes() {
064                    return _scopedAttributes;
065            }
066    
067            public void setAttributeNamespace(String attributeNamespace) {
068                    _attributeNamespace = attributeNamespace;
069            }
070    
071            public void setCustomAttributes(CustomAttributes customAttributes) {
072                    _customAttributes = customAttributes;
073            }
074    
075            public void setDynamicAttribute(
076                    String uri, String localName, Object value) {
077    
078                    _dynamicAttributes.put(localName, value);
079            }
080    
081            public void setNamespacedAttribute(
082                    HttpServletRequest request, String key, Object value) {
083    
084                    if (value instanceof Boolean) {
085                            value = String.valueOf(value);
086                    }
087                    else if (value instanceof Number) {
088                            value = String.valueOf(value);
089                    }
090    
091                    request.setAttribute(_encodeKey(key), value);
092            }
093    
094            public void setScopedAttribute(String name, Object value) {
095                    _scopedAttributes.put(name, value);
096            }
097    
098            protected Map<String, Object> getDynamicAttributes() {
099                    return _dynamicAttributes;
100            }
101    
102            protected void writeDynamicAttributes(JspWriter jspWriter)
103                    throws IOException {
104    
105                    String dynamicAttributesString = InlineUtil.buildDynamicAttributes(
106                            getDynamicAttributes());
107    
108                    if (Validator.isNotNull(dynamicAttributesString)) {
109                            jspWriter.write(dynamicAttributesString);
110                    }
111            }
112    
113            private String _encodeKey(String key) {
114                    if (_attributeNamespace.length() == 0) {
115                            return key;
116                    }
117                    else {
118                            return _attributeNamespace.concat(key);
119                    }
120            }
121    
122            private String _attributeNamespace = StringPool.BLANK;
123            private CustomAttributes _customAttributes;
124            private Map<String, Object> _dynamicAttributes =
125                    new HashMap<String, Object>();
126            private Map<String, Object> _scopedAttributes =
127                    new HashMap<String, Object>();
128    
129    }