1 16 package javax.faces.component; 17 18 import java.beans.BeanInfo ; 19 import java.beans.IntrospectionException ; 20 import java.beans.Introspector ; 21 import java.beans.PropertyDescriptor ; 22 import java.io.Serializable ; 23 import java.lang.reflect.Method ; 24 import java.util.Collection ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.Map ; 28 import java.util.Set ; 29 30 import javax.faces.FacesException; 31 import javax.faces.context.FacesContext; 32 import javax.faces.el.ValueBinding; 33 34 48 class _ComponentAttributesMap 49 implements Map , Serializable 50 { 51 private static final Object [] EMPTY_ARGS = new Object [0]; 52 53 private UIComponent _component; 54 private Map _attributes = null; private transient Map _propertyDescriptorMap = null; 56 57 _ComponentAttributesMap(UIComponent component) 58 { 59 _component = component; 60 _attributes = new HashMap (); 61 } 62 63 _ComponentAttributesMap(UIComponent component, Map attributes) 64 { 65 _component = component; 66 _attributes = attributes; 67 } 68 69 public int size() 70 { 71 return _attributes.size(); 72 } 73 74 public void clear() 75 { 76 _attributes.clear(); 77 } 78 79 public boolean isEmpty() 80 { 81 return _attributes.isEmpty(); 82 } 83 84 public boolean containsKey(Object key) 85 { 86 checkKey(key); 87 if (getPropertyDescriptor((String )key) == null) 88 { 89 return _attributes.containsKey(key); 90 } 91 else 92 { 93 return false; 94 } 95 } 96 97 100 public boolean containsValue(Object value) 101 { 102 return _attributes.containsValue(value); 103 } 104 105 public Collection values() 106 { 107 return _attributes.values(); 108 } 109 110 public void putAll(Map t) 111 { 112 for (Iterator it = t.entrySet().iterator(); it.hasNext(); ) 113 { 114 Map.Entry entry = (Entry)it.next(); 115 put(entry.getKey(), entry.getValue()); 116 } 117 } 118 119 public Set entrySet() 120 { 121 return _attributes.entrySet(); 122 } 123 124 public Set keySet() 125 { 126 return _attributes.keySet(); 127 } 128 129 public Object get(Object key) 130 { 131 checkKey(key); 132 PropertyDescriptor propertyDescriptor = getPropertyDescriptor((String )key); 133 if (propertyDescriptor != null) 134 { 135 Object value = getComponentProperty(propertyDescriptor); 136 if (value != null) 137 { 138 return value; 139 } 140 141 ValueBinding vb = _component.getValueBinding((String ) key); 142 return vb != null ? vb.getValue(FacesContext.getCurrentInstance()) : null; 143 } 144 else 145 { 146 return _attributes.get(key); 147 } 148 } 149 150 public Object remove(Object key) 151 { 152 checkKey(key); 153 PropertyDescriptor propertyDescriptor = getPropertyDescriptor((String )key); 154 if (propertyDescriptor != null) 155 { 156 throw new IllegalArgumentException ("Cannot remove component property attribute"); 157 } 158 return _attributes.remove(key); 159 } 160 161 165 public Object put(Object key, Object value) 166 { 167 checkKey(key); 168 PropertyDescriptor propertyDescriptor = getPropertyDescriptor((String )key); 169 if (propertyDescriptor != null) 170 { 171 if (propertyDescriptor.getReadMethod() != null) 172 { 173 Object oldValue = getComponentProperty(propertyDescriptor); 174 setComponentProperty(propertyDescriptor, value); 175 return oldValue; 176 } 177 else 178 { 179 setComponentProperty(propertyDescriptor, value); 180 return null; 181 } 182 } 183 else 184 { 185 return _attributes.put(key, value); 186 } 187 } 188 189 190 private PropertyDescriptor getPropertyDescriptor(String key) 191 { 192 if (_propertyDescriptorMap == null) 193 { 194 BeanInfo beanInfo; 195 try 196 { 197 beanInfo = Introspector.getBeanInfo(_component.getClass()); 198 } 199 catch (IntrospectionException e) 200 { 201 throw new FacesException(e); 202 } 203 PropertyDescriptor [] propertyDescriptors = beanInfo.getPropertyDescriptors(); 204 _propertyDescriptorMap = new HashMap (); 205 for (int i = 0; i < propertyDescriptors.length; i++) 206 { 207 PropertyDescriptor propertyDescriptor = propertyDescriptors[i]; 208 if (propertyDescriptor.getReadMethod() != null) 209 { 210 _propertyDescriptorMap.put(propertyDescriptor.getName(), 211 propertyDescriptor); 212 } 213 } 214 } 215 return (PropertyDescriptor )_propertyDescriptorMap.get(key); 216 } 217 218 219 private Object getComponentProperty(PropertyDescriptor propertyDescriptor) 220 { 221 Method readMethod = propertyDescriptor.getReadMethod(); 222 if (readMethod == null) 223 { 224 throw new IllegalArgumentException ("Component property " + propertyDescriptor.getName() + " is not readable"); 225 } 226 try 227 { 228 return readMethod.invoke(_component, EMPTY_ARGS); 229 } 230 catch (Exception e) 231 { 232 FacesContext facesContext = FacesContext.getCurrentInstance(); 233 throw new FacesException("Could not get property " + propertyDescriptor.getName() + " of component " + _component.getClientId(facesContext), e); 234 } 235 } 236 237 238 private void setComponentProperty(PropertyDescriptor propertyDescriptor, Object value) 239 { 240 Method writeMethod = propertyDescriptor.getWriteMethod(); 241 if (writeMethod == null) 242 { 243 throw new IllegalArgumentException ("Component property " + propertyDescriptor.getName() + " is not writable"); 244 } 245 try 246 { 247 writeMethod.invoke(_component, new Object [] {value}); 248 } 249 catch (Exception e) 250 { 251 FacesContext facesContext = FacesContext.getCurrentInstance(); 252 throw new FacesException("Could not set property " + propertyDescriptor.getName() + " of component " + _component.getClientId(facesContext), e); 253 } 254 } 255 256 257 private void checkKey(Object key) 258 { 259 if (key == null) throw new NullPointerException ("key"); 260 if (!(key instanceof String )) throw new ClassCastException ("key is not a String"); 261 } 262 263 Map getUnderlyingMap() 264 { 265 return _attributes; 266 } 267 } | Popular Tags |