1 25 package com.scalagent.ksoap.marshal; 26 27 import java.io.*; 28 import java.util.*; 29 30 import com.scalagent.ksoap.*; 31 import org.kxml.Xml; 32 import org.kxml.parser.*; 33 34 35 public class MarshalHashtable implements Marshal { 36 public static final PropertyInfo OBJECT_TYPE = 37 new PropertyInfo("object",new Object ()); 38 39 public Object readInstance(SoapReader reader, 40 String namespace, 41 String name, 42 PropertyInfo expected) throws IOException { 43 if (KSoapTracing.dbg) 44 KSoapTracing.log(KSoapTracing.DEBUG, 45 "MarshalHashtable.readInstance(" + reader + "," + 46 namespace + "," + 47 name + "," + 48 expected + ")"); 49 Hashtable instance = new Hashtable(); 50 AbstractXmlParser xmlParser = reader.parser; 51 52 xmlParser.read(); 53 xmlParser.skip(); 54 while (xmlParser.peek().getType() != Xml.END_TAG) { 55 SoapObject item = new ItemSoapObject(instance); 56 57 xmlParser.read(); 58 xmlParser.skip(); 59 60 Object key = 61 reader.read(item,0,null,null,OBJECT_TYPE); 62 reader.parser.skip(); 63 if (key != null) 64 item.setProperty(0,key); 65 66 Object value = 67 reader.read(item,1,null,null,OBJECT_TYPE); 68 reader.parser.skip(); 69 if (value != null) 70 item.setProperty(1,value); 71 72 xmlParser.read(); 73 xmlParser.skip(); 74 } 75 76 reader.parser.read(); 77 78 if (KSoapTracing.dbg) 79 KSoapTracing.log(KSoapTracing.DEBUG, 80 "MarshalHashtable : instance=" + instance); 81 return instance; 82 } 83 84 public void writeInstance(SoapWriter writer, 85 Object instance) throws IOException { 86 if (KSoapTracing.dbg) 87 KSoapTracing.log(KSoapTracing.DEBUG, 88 "MarshalHashtable.writeInstance(" + writer + 89 "," + instance + ")"); 90 91 Hashtable h = (Hashtable) instance; 92 SoapObject item = new SoapObject(null, null); 93 item.addProperty(new PropertyInfo("key",OBJECT_TYPE),null); 94 item.addProperty(new PropertyInfo("value",OBJECT_TYPE),null); 95 for (Enumeration keys = h.keys(); keys.hasMoreElements();) { 96 writer.writer.startTag("item"); 97 Object key = keys.nextElement(); 98 item.setProperty(0,key); 99 item.setProperty(1,h.get(key)); 100 writer.writeObjectBody(item); 101 writer.writer.endTag(); 102 } 103 } 104 105 class ItemSoapObject extends SoapObject { 106 Hashtable h; 107 int resolvedIndex = -1; 108 109 ItemSoapObject(Hashtable h) { 110 super(null, null); 111 this.h = h; 112 addProperty(new PropertyInfo("key",OBJECT_TYPE),null); 113 addProperty(new PropertyInfo("value",OBJECT_TYPE),null); 114 } 115 116 public void setProperty(int index, Object value) { 117 if (resolvedIndex == -1) { 118 super.setProperty(index,value); 119 resolvedIndex = index; 120 } else { 121 Object resolved = 122 resolvedIndex == 0 123 ? getProperty (0) 124 : getProperty (1); 125 126 if (index == 0) 127 h.put(value,resolved); 128 else 129 h.put(resolved,value); 130 } 131 } 132 } 133 134 public void register(ClassMap cm) { 135 cm.addMapping("http://xml.apache.org/xml-soap", 136 "Map", 137 new Hashtable().getClass(), 138 this); 139 } 140 } 141 | Popular Tags |