1 57 58 package org.apache.soap.encoding.soapenc; 59 60 import java.beans.*; 61 import java.io.*; 62 import java.util.*; 63 import java.lang.reflect.*; 64 import org.w3c.dom.*; 65 import org.apache.soap.util.*; 66 import org.apache.soap.util.xml.*; 67 import org.apache.soap.*; 68 import org.apache.soap.rpc.*; 69 70 79 public class HashtableSerializer implements Serializer, Deserializer 80 { 81 private static final String STR_KEY = "key"; 82 private static final String STR_ITEM = "item"; 83 private static final String STR_VALUE = "value"; 84 85 public void marshall(String inScopeEncStyle, Class javaType, Object src, 86 Object context, Writer sink, NSStack nsStack, 87 XMLJavaMappingRegistry xjmr, SOAPContext ctx) 88 throws IllegalArgumentException , IOException 89 { 90 if (src == null) 91 { 92 SoapEncUtils.generateNullStructure(inScopeEncStyle, 93 javaType, 94 context, 95 sink, 96 nsStack, 97 xjmr); 98 } 99 else if (src instanceof Hashtable) 100 { 101 SoapEncUtils.generateStructureHeader(inScopeEncStyle, 102 javaType, 103 context, 104 sink, 105 nsStack, 106 xjmr); 107 108 sink.write(StringUtils.lineSeparator); 109 110 Hashtable hash = (Hashtable)src; 111 112 for (Enumeration e = hash.keys(); e.hasMoreElements(); ) 113 { 114 Object key = e.nextElement(); 115 Object value = hash.get(key); 116 117 sink.write("<" + STR_ITEM + ">"); 118 sink.write(StringUtils.lineSeparator); 119 120 xjmr.marshall(Constants.NS_URI_SOAP_ENC, key.getClass(), key, STR_KEY, 122 sink, nsStack, ctx); 123 sink.write(StringUtils.lineSeparator); 124 125 if (value == null) 126 { 127 SoapEncUtils.generateNullStructure(Constants.NS_URI_SOAP_ENC, 128 Object .class, STR_VALUE, sink, 129 nsStack, xjmr); 130 } 131 else 132 { 133 Class actualComponentType = value.getClass(); 134 135 xjmr.marshall(Constants.NS_URI_SOAP_ENC, actualComponentType, value, 136 STR_VALUE, sink, nsStack, ctx); 137 } 138 139 sink.write(StringUtils.lineSeparator); 140 sink.write("</" + STR_ITEM + ">"); 141 sink.write(StringUtils.lineSeparator); 142 } 143 144 sink.write("</" + context + '>'); 145 } 146 else 147 { 148 throw new IllegalArgumentException ("Tried to pass a '" + 149 src.getClass().toString() + 150 "' to HashtableSerializer"); 151 } 152 } 153 154 public Bean unmarshall(String inScopeEncStyle, QName elementType, Node src, 155 XMLJavaMappingRegistry xjmr, SOAPContext ctx) 156 throws IllegalArgumentException 157 { 158 Element root = (Element)src; 159 String name = root.getTagName(); 160 161 if (SoapEncUtils.isNull(root)) 162 { 163 return new Bean(Hashtable.class, null); 164 } 165 166 Hashtable hash = new Hashtable(); 167 Element tempEl = DOMUtils.getFirstChildElement(root); 168 169 while (tempEl != null) { 170 Element keyEl = DOMUtils.getFirstChildElement(tempEl); 172 String tagName = keyEl.getTagName(); 173 174 if (!tagName.equalsIgnoreCase(STR_KEY)) 175 { 176 throw new IllegalArgumentException ("Got <" + tagName + 177 "> tag when expecting <" + 178 STR_KEY + ">"); 179 } 180 181 Element valEl = DOMUtils.getNextSiblingElement(keyEl); 182 183 tagName = valEl.getTagName(); 184 185 if (!tagName.equalsIgnoreCase("value")) 186 { 187 throw new IllegalArgumentException ("Got <" + tagName + 188 "> tag when expecting <" + 189 STR_VALUE + ">"); 190 } 191 192 Bean keyBean = unmarshallEl(inScopeEncStyle, xjmr, keyEl, ctx); 193 Bean valBean = unmarshallEl(inScopeEncStyle, xjmr, valEl, ctx); 194 195 hash.put(keyBean.value, valBean.value); 196 197 tempEl = DOMUtils.getNextSiblingElement(tempEl); 198 } 199 200 return new Bean(Hashtable.class, hash); 201 } 202 203 private Bean unmarshallEl(String inScopeEncStyle, 204 XMLJavaMappingRegistry xjmr, 205 Element targetEl, SOAPContext ctx) 206 { 207 String declEncStyle = DOMUtils.getAttributeNS(targetEl, 208 Constants.NS_URI_SOAP_ENV, Constants.ATTR_ENCODING_STYLE); 209 String actualEncStyle = (declEncStyle != null) 210 ? declEncStyle 211 : inScopeEncStyle; 212 QName declItemType = SoapEncUtils.getTypeQName(targetEl); 213 214 return xjmr.unmarshall(actualEncStyle, declItemType, targetEl, ctx); 215 } 216 } 217 | Popular Tags |