1 4 package org.infoglue.deliver.util.webservices; 5 6 import java.util.ArrayList ; 7 import java.util.Collection ; 8 import java.util.HashMap ; 9 import java.util.Iterator ; 10 import java.util.List ; 11 import java.util.Map ; 12 13 16 public class DynamicWebserviceSerializer 17 { 18 21 private static final Object MARKER = null; 22 23 26 private Map legalTypes; 27 28 31 public DynamicWebserviceSerializer() 32 { 33 super(); 34 } 35 36 39 public DynamicWebserviceSerializer(final Map legalTypes) 40 { 41 super(); 42 this.legalTypes = legalTypes; 43 } 44 45 48 public List serializeCollection(final Collection collection) 49 { 50 final Collection c = (collection == null) ? new ArrayList () : collection; 51 52 final List result = createResultList(c.getClass(), c.size()); 53 for(final Iterator i = collection.iterator(); i.hasNext(); ) 54 { 55 result.addAll(serializeObject(i.next())); 56 } 57 return result; 58 } 59 60 63 public List serializeMap(final Map map) { 64 final Map m = (map == null) ? new HashMap () : map; 65 66 final List result = createResultList(m.getClass(), m.size()); 67 for(final Iterator i = map.keySet().iterator(); i.hasNext(); ) 68 { 69 final Object key = i.next(); 70 final Object value = map.get(key); 71 result.addAll(serializeObject(key)); 72 result.addAll(serializeObject(value)); 73 } 74 return result; 75 } 76 77 80 public List serializeDynamicWebserviceElement(final DynamicWebserviceElement element) { 81 final List arguments = element.serialize(); 82 for(final Iterator i = arguments.iterator(); i.hasNext(); ) 83 { 84 checkType(i.next()); 85 } 86 final List result = createResultList(element.getClass(), arguments.size()); 87 result.addAll(arguments); 88 return result; 89 } 90 91 94 private List serializeObject(final Object o) { 95 if(o instanceof Map ) 96 { 97 return serializeMap((Map ) o); 98 } 99 if(o instanceof Collection ) 100 { 101 return serializeCollection((Collection ) o); 102 } 103 if(o instanceof DynamicWebserviceElement) 104 { 105 return serializeDynamicWebserviceElement((DynamicWebserviceElement) o); 106 } 107 checkType(o); 108 final List dummy = new ArrayList (); 109 dummy.add(o); 110 return dummy; 111 } 112 113 116 private List createResultList(final Class c, final int size) 117 { 118 final List list = new ArrayList (); 119 list.add(MARKER); 120 list.add(c.getName()); 121 list.add(new Integer (size).toString()); 122 return list; 123 } 124 125 128 private void checkType(final Object o) 129 { 130 if(o == null) 131 { 132 throw new IllegalArgumentException ("Null objects are not allowed in collections..."); 133 } 134 if(!legalTypes.containsKey(o.getClass())) 135 { 136 throw new IllegalArgumentException ("Non-standard elements not allowed in collections..."); 137 } 138 } 139 140 143 public Object deserialize(final Object [] array) throws Throwable 144 { 145 final List list = new ArrayList (); 146 for(int i=0; i<array.length; ++i) 147 { 148 list.add(array[i]); 149 } 150 return deserializeElement(list); 151 } 152 153 156 private Object deserializeMap(final List c, final Map map, final int count) throws Throwable 157 { 158 for(int i=0; i<count; ++i) 159 { 160 final Object key = deserializeElement(c); 161 final Object value = deserializeElement(c); 162 map.put(key, value); 163 } 164 return map; 165 } 166 167 170 private Object deserializeCollection(final List c, final Collection collection, final int count) throws Throwable 171 { 172 for(int i=0; i<count; ++i) 173 { 174 Object o = deserializeElement(c); 175 collection.add(o); 176 } 177 return collection; 178 } 179 180 183 private Object deserializeDynamicWebserviceElement(final List c, final DynamicWebserviceElement element, final int count) throws Throwable 184 { 185 final List arguments = new ArrayList (); 186 for(int i=0; i<count; ++i) 187 { 188 arguments.add(c.remove(0)); 189 } 190 element.deserialize(arguments); 191 return element; 192 } 193 194 197 private Object deserializeElement(final List c) throws Throwable 198 { 199 Object o = c.remove(0); 200 if(o == MARKER) 201 { 202 final Object oo = Class.forName(c.remove(0).toString()).newInstance(); 203 int count = new Integer (c.remove(0).toString()).intValue(); 204 if(oo instanceof Map ) 205 { 206 return deserializeMap(c, (Map ) oo, count); 207 } 208 if(oo instanceof Collection ) 209 { 210 return deserializeCollection(c, (Collection ) oo, count); 211 } 212 return deserializeDynamicWebserviceElement(c, (DynamicWebserviceElement) oo, count); 213 } 214 return o; 215 } 216 } 217 | Popular Tags |