1 24 package org.riotfamily.common.beans; 25 26 import java.util.HashMap ; 27 import java.util.Map ; 28 29 import org.springframework.beans.BeanUtils; 30 import org.springframework.beans.BeansException; 31 import org.springframework.beans.PropertyValue; 32 import org.springframework.beans.PropertyValues; 33 import org.springframework.util.Assert; 34 35 38 public class MapWrapper implements ObjectWrapper { 39 40 private Map map; 41 42 private Class valueClass; 43 44 private Class mapClass = HashMap .class; 45 46 public MapWrapper(Map map) { 47 this.map = map; 48 } 49 50 public MapWrapper(Class mapClass) { 51 this.mapClass = mapClass; 52 } 53 54 public void setMapClass(Class mapClass) { 55 this.mapClass = mapClass; 56 } 57 58 public void setValueClass(Class valueClass) { 59 this.valueClass = valueClass; 60 } 61 62 public boolean isReadableProperty(String propertyName) { 63 return true; 64 } 65 66 public boolean isWritableProperty(String propertyName) { 67 return true; 68 } 69 70 public Class getPropertyType(String propertyName) { 71 if (valueClass != null) { 72 return valueClass; 73 } 74 Object value = getPropertyValue(propertyName); 75 if (value != null) { 76 return value.getClass(); 77 } 78 return Object .class; 79 } 80 81 public void setObject(Object object) { 82 Assert.isInstanceOf(Map .class, object); 83 map = (Map ) object; 84 } 85 86 public Object getObject() { 87 return getMap(); 88 } 89 90 public Class getObjectClass() { 91 return mapClass; 92 } 93 94 protected Map getMap() { 95 if (map == null) { 96 map = (Map ) BeanUtils.instantiateClass(mapClass); 97 } 98 return map; 99 } 100 101 public Object getPropertyValue(String propertyName) { 102 return getMap().get(propertyName); 103 } 104 105 public void setPropertyValue(String propertyName, Object value) { 106 getMap().put(propertyName, value); 107 } 108 109 public void setPropertyValue(PropertyValue pv) { 110 setPropertyValue(pv.getName(), pv.getValue()); 111 } 112 113 public void setPropertyValues(Map map) { 114 getMap().putAll(map); 115 } 116 117 public void setPropertyValues(PropertyValues pvs, boolean ignoreUnknown, 118 boolean ignoreInvalid) throws BeansException { 119 120 setPropertyValues(pvs); 121 } 122 123 public void setPropertyValues(PropertyValues pvs) { 124 PropertyValue[] pv = pvs.getPropertyValues(); 125 for (int i = 0; i < pv.length; i++) { 126 setPropertyValue(pv[i]); 127 } 128 } 129 130 public void setPropertyValues(PropertyValues pvs, boolean ignoreUnknown) { 131 setPropertyValues(pvs); 132 } 133 134 } 135 | Popular Tags |