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.*; 32 import org.kxml.io.*; 33 import org.kxml.parser.*; 34 35 36 public class MarshalVector implements Marshal { 37 public static final PropertyInfo OBJECT_TYPE = 38 new PropertyInfo("object",new Object ()); 39 40 public Object readInstance(SoapReader reader, 41 String namespace, 42 String name, 43 PropertyInfo expected) throws IOException { 44 45 if (KSoapTracing.dbg) 46 KSoapTracing.log(KSoapTracing.DEBUG, 47 "MarshalVector.readInstance(" + reader + "," + 48 namespace + "," + 49 name + "," + 50 expected + ")"); 51 52 Vector instance = new Vector(); 53 int i = 0; 54 AbstractXmlParser xmlParser = reader.parser; 55 SoapObject sO = new SoapObject(null,null); 56 sO.addProperty(new PropertyInfo("item",OBJECT_TYPE),null); 57 58 StartTag start = (StartTag) xmlParser.peek(); 59 String toParse = start.getAttribute(ClassMap.enc,"arrayType").getValue(); 60 int cut = toParse.indexOf(":Map["); 61 toParse = toParse.substring(cut+5); 62 cut = toParse.indexOf("]"); 63 toParse = toParse.substring(0,cut); 64 int size = Integer.parseInt(toParse); 65 66 xmlParser.read(); 67 xmlParser.skip(); 68 69 while (xmlParser.peek().getType() != Xml.END_TAG) { 70 start = (StartTag) xmlParser.peek(); 71 Attribute attr = start.getAttribute(ClassMap.xsi,"type"); 72 if (attr != null) { 73 String type = attr.getValue(); 74 cut = type.indexOf(':'); 75 name = type.substring(cut + 1); 76 String prefix = ""; 77 if (cut > 0) 78 prefix = type.substring(0,cut); 79 namespace = start.getPrefixMap().getNamespace(prefix); 80 } 81 82 Object value = 83 reader.read(null,-1,namespace,name,OBJECT_TYPE); 84 reader.parser.skip(); 85 86 if (value != null) { 87 sO.setProperty(i++,value); 88 if (size == i) { 89 xmlParser.read(); 90 xmlParser.skip(); 91 break; 92 } else { 93 continue; 94 } 95 } 96 xmlParser.read(); 97 xmlParser.skip(); 98 } 99 100 for (int j = 0; j < sO.getPropertyCount(); j++) 101 instance.addElement(sO.getProperty(j)); 102 103 if (KSoapTracing.dbg) 104 KSoapTracing.log(KSoapTracing.DEBUG, 105 "MarshalVector : instance=" + instance); 106 return instance; 107 } 108 109 public void writeInstance(SoapWriter writer, 110 Object instance) throws IOException { 111 if (KSoapTracing.dbg) 112 KSoapTracing.log(KSoapTracing.DEBUG, 113 "MarshalVector.writeInstance(" + writer + 114 "," + instance + ")"); 115 116 Vector vector = (Vector) instance; 117 int cnt = vector.size(); 118 119 SoapPrimitive sp = 120 ClassMap.getSoapPrimitive(instance.getClass().getName()); 121 if (cnt > 0) { 122 sp = ClassMap.getSoapPrimitive(vector.firstElement().getClass().getName()); 123 } 124 125 writer.writer.attribute(ClassMap.enc,"arrayType", 126 "xsd:anyType[" + cnt + "]"); 127 128 SoapObject so = new SoapObject(null,null); 129 for (int i = 0; i < cnt; i++) { 130 Object elem = vector.elementAt(i); 131 so.addProperty(new PropertyInfo("item",elem),null); 132 so.setProperty(0,elem); 133 writer.writeObjectBody(so); 134 } 135 } 136 137 public void register(ClassMap cm) { 138 cm.addMapping(ClassMap.enc, 139 "Array", 140 ClassMap.VECTOR_CLASS, 141 this); 142 } 143 } 144 | Popular Tags |