001
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
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 @Override
076 public void setDynamicAttribute(
077 String uri, String localName, Object value) {
078
079 _dynamicAttributes.put(localName, value);
080 }
081
082 public void setNamespacedAttribute(
083 HttpServletRequest request, String key, Object value) {
084
085 if (value instanceof Boolean) {
086 value = String.valueOf(value);
087 }
088 else if (value instanceof Number) {
089 value = String.valueOf(value);
090 }
091
092 request.setAttribute(_encodeKey(key), value);
093 }
094
095 public void setScopedAttribute(String name, Object value) {
096 _scopedAttributes.put(name, value);
097 }
098
099 protected Map<String, Object> getDynamicAttributes() {
100 return _dynamicAttributes;
101 }
102
103 protected void writeDynamicAttributes(JspWriter jspWriter)
104 throws IOException {
105
106 String dynamicAttributesString = InlineUtil.buildDynamicAttributes(
107 getDynamicAttributes());
108
109 if (Validator.isNotNull(dynamicAttributesString)) {
110 jspWriter.write(dynamicAttributesString);
111 }
112 }
113
114 private String _encodeKey(String key) {
115 if (_attributeNamespace.length() == 0) {
116 return key;
117 }
118 else {
119 return _attributeNamespace.concat(key);
120 }
121 }
122
123 private String _attributeNamespace = StringPool.BLANK;
124 private CustomAttributes _customAttributes;
125 private Map<String, Object> _dynamicAttributes =
126 new HashMap<String, Object>();
127 private Map<String, Object> _scopedAttributes =
128 new HashMap<String, Object>();
129
130 }