1 4 package org.oddjob.values.types; 5 6 7 import java.io.ByteArrayInputStream ; 8 import java.io.InputStream ; 9 import java.util.Collection ; 10 import java.util.Iterator ; 11 import java.util.LinkedHashMap ; 12 import java.util.Map ; 13 import java.util.Properties ; 14 import java.util.Set ; 15 16 import org.oddjob.arooa.ArooaHandler; 17 import org.oddjob.arooa.ArooaContext; 18 import org.oddjob.arooa.ArooaConstants; 19 import org.oddjob.arooa.handlers.MappedPropertyHandler; 20 import org.oddjob.arooa.reflect.IntrospectionHelper; 21 22 63 public class MapType implements Map { 64 65 private Map map = new LinkedHashMap (); 66 67 public void setValue(String name, Object value) { 68 map.put(name, value); 69 } 70 71 87 public Object valueFor(Class requested) { 88 Map resolved = resolveMap(requested); 89 if (requested.isAssignableFrom(Object .class)) { 90 return resolved; 91 } 92 if (requested.isAssignableFrom(String .class)) { 93 return propertiesFrom(resolved); 94 } 95 if (requested.isAssignableFrom(InputStream .class)) { 96 return new ByteArrayInputStream (propertiesFrom(resolved).getBytes()); 97 } 98 if (requested.isAssignableFrom(Properties .class)) { 99 Properties results = new Properties (); 100 for (Iterator it = resolved.entrySet().iterator(); it.hasNext(); ) { 101 Map.Entry entry = (Map.Entry ) it.next(); 102 results.setProperty(entry.getKey().toString(), entry.getValue().toString()); 103 } 104 return results; 105 } 106 return resolved; 107 } 108 109 private Map resolveMap(Class required) { 110 Map results = new LinkedHashMap (); 111 for (Iterator it = map.entrySet().iterator(); it.hasNext(); ) { 112 Map.Entry entry = (Map.Entry ) it.next(); 113 Object o = IntrospectionHelper.valueFor(entry.getValue()); 114 results.put(entry.getKey(), o); 115 } 116 return results; 117 } 118 119 public ArooaHandler handlerFor(ArooaContext context) { 120 context.set(ArooaConstants.ELEMENT_NAME, "value"); 121 return new MappedPropertyHandler(); 122 } 123 124 127 public void clear() { 128 throw new UnsupportedOperationException ("This map is read only!"); 129 } 130 131 134 public boolean containsKey(Object key) { 135 return map.containsKey(key); 136 } 137 138 141 public boolean containsValue(Object value) { 142 return map.containsValue(value); 143 } 144 145 148 public Set entrySet() { 149 return map.entrySet(); 150 } 151 152 155 public Object get(Object key) { 156 return map.get(key); 157 } 158 159 162 public boolean isEmpty() { 163 return map.isEmpty(); 164 } 165 166 169 public Set keySet() { 170 return map.keySet(); 171 } 172 173 176 public Object put(Object key, Object value) { 177 throw new UnsupportedOperationException ("This map is read only!"); 178 } 179 180 183 public void putAll(Map t) { 184 throw new UnsupportedOperationException ("This map is read only!"); 185 } 186 187 190 public Object remove(Object key) { 191 return map.remove(key); 192 } 193 194 197 public int size() { 198 return map.size(); 199 } 200 201 204 public Collection values() { 205 return map.values(); 206 } 207 208 215 public static String propertiesFrom(Map map) { 216 String sep = System.getProperty(SystemType.LINE_SEPARATOR); 217 StringBuffer buf = new StringBuffer (); 218 for (Iterator it = map.entrySet().iterator(); it.hasNext(); ) { 219 Map.Entry entry = (Map.Entry ) it.next(); 220 buf.append(entry.getKey()); 221 buf.append("="); 222 buf.append(entry.getValue()); 223 buf.append(sep); 224 } 225 return buf.toString(); 226 } 227 } 228 | Popular Tags |