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 76 public class ArraySerializer implements Serializer, Deserializer 77 { 78 public void marshall(String inScopeEncStyle, Class javaType, Object src, 79 Object context, Writer sink, NSStack nsStack, 80 XMLJavaMappingRegistry xjmr, SOAPContext ctx) 81 throws IllegalArgumentException , IOException 82 { 83 nsStack.pushScope(); 84 85 String lengthStr = src != null 86 ? Array.getLength(src) + "" 87 : ""; 88 Class componentType = javaType.getComponentType(); 89 QName elementType = xjmr.queryElementType(componentType, 90 Constants.NS_URI_SOAP_ENC); 91 92 if (src == null) 93 { 94 SoapEncUtils.generateNullArray(inScopeEncStyle, 95 javaType, 96 context, 97 sink, 98 nsStack, 99 xjmr, 100 elementType, 101 lengthStr); 102 } 103 else 104 { 105 SoapEncUtils.generateArrayHeader(inScopeEncStyle, 106 javaType, 107 context, 108 sink, 109 nsStack, 110 xjmr, 111 elementType, 112 lengthStr); 113 114 sink.write(StringUtils.lineSeparator); 115 116 int length = Array.getLength(src); 117 118 for (int i = 0; i < length; i++) 119 { 120 nsStack.pushScope(); 121 122 Object value = Array.get(src, i); 123 124 if (value == null) 125 { 126 SoapEncUtils.generateNullStructure(inScopeEncStyle, componentType, 127 "item", sink, nsStack, xjmr); 128 } 129 else 130 { 131 Class actualComponentType = value.getClass(); 132 133 xjmr.marshall(Constants.NS_URI_SOAP_ENC, actualComponentType, value, "item", 134 sink, nsStack, ctx); 135 } 136 137 sink.write(StringUtils.lineSeparator); 138 nsStack.popScope(); 139 } 140 141 sink.write("</" + context + '>'); 142 } 143 144 nsStack.popScope(); 145 } 146 147 public Bean unmarshall(String inScopeEncStyle, QName elementType, Node src, 148 XMLJavaMappingRegistry xjmr, SOAPContext ctx) 149 throws IllegalArgumentException 150 { 151 Element root = (Element)src; 152 String name = root.getTagName(); 153 QName arrayItemType = new QName("", ""); 154 Object array = getNewArray(inScopeEncStyle, root, arrayItemType, xjmr); 155 156 if (SoapEncUtils.isNull(root)) 157 { 158 return new Bean(array.getClass(), null); 159 } 160 161 Element tempEl = DOMUtils.getFirstChildElement(root); 162 int length = Array.getLength(array); 163 164 for (int i = 0; i < length; i++) 165 { 166 String declEncStyle = DOMUtils.getAttributeNS(tempEl, 167 Constants.NS_URI_SOAP_ENV, Constants.ATTR_ENCODING_STYLE); 168 String actualEncStyle = declEncStyle != null 169 ? declEncStyle 170 : inScopeEncStyle; 171 QName declItemType = SoapEncUtils.getTypeQName(tempEl); 172 QName actualItemType = declItemType != null 173 ? declItemType 174 : arrayItemType; 175 176 String href = tempEl.getAttribute(Constants.ATTR_REFERENCE); 178 Element actualEl = tempEl; 179 180 if(href != null && !href.equals("") && (href.charAt(0) == '#')) 181 { 182 href = href.substring(1); 183 actualEl = DOMUtils.getElementByID(src.getOwnerDocument().getDocumentElement(),href); 184 if (actualEl == null) { 185 throw new IllegalArgumentException ("No such ID '" + href + "'"); 186 } 187 } 188 189 if (!SoapEncUtils.isNull(actualEl)) 190 { 191 Bean itemBean = xjmr.unmarshall(actualEncStyle, 192 actualItemType, 193 actualEl, 194 ctx); 195 196 Array.set(array, i, itemBean.value); 197 } 198 199 tempEl = DOMUtils.getNextSiblingElement(tempEl); 200 } 201 202 return new Bean(array.getClass(), array); 203 } 204 205 public static Object getNewArray(String inScopeEncStyle, Element arrayEl, 206 QName arrayItemType, 207 XMLJavaMappingRegistry xjmr) 208 throws IllegalArgumentException 209 { 210 QName arrayTypeValue = SoapEncUtils.getAttributeValue(arrayEl, 211 Constants.NS_URI_SOAP_ENC, Constants.ATTR_ARRAY_TYPE, "array", true); 212 String arrayTypeValueNamespaceURI = arrayTypeValue.getNamespaceURI(); 213 String arrayTypeValueLocalPart = arrayTypeValue.getLocalPart(); 214 int leftBracketIndex = arrayTypeValueLocalPart.lastIndexOf('['); 215 int rightBracketIndex = arrayTypeValueLocalPart.lastIndexOf(']'); 216 217 if (leftBracketIndex == -1 218 || rightBracketIndex == -1 219 || rightBracketIndex < leftBracketIndex) 220 { 221 throw new IllegalArgumentException ("Malformed arrayTypeValue '" + 222 arrayTypeValue + "'."); 223 } 224 225 String componentTypeName = 226 arrayTypeValueLocalPart.substring(0, leftBracketIndex); 227 228 if (componentTypeName.endsWith("]")) 229 { 230 throw new IllegalArgumentException ("Arrays of arrays are not " + 231 "supported '" + arrayTypeValue + 232 "'."); 233 } 234 235 arrayItemType.setNamespaceURI(arrayTypeValueNamespaceURI); 236 arrayItemType.setLocalPart(componentTypeName); 237 238 int length = DOMUtils.countKids(arrayEl, Node.ELEMENT_NODE); 239 String lengthStr = 240 arrayTypeValueLocalPart.substring(leftBracketIndex + 1, 241 rightBracketIndex); 242 243 if (lengthStr.length() > 0) 244 { 245 if (lengthStr.indexOf(',') != -1) 246 { 247 throw new IllegalArgumentException ("Multi-dimensional arrays are " + 248 "not supported '" + 249 lengthStr + "'."); 250 } 251 252 try 253 { 254 int explicitLength = Integer.parseInt(lengthStr); 255 256 if (length != explicitLength) 257 { 258 throw new IllegalArgumentException ("Explicit array length is " + 259 "not equal to the number of " + 260 "items '" + explicitLength + 261 " != " + length + "'."); 262 } 263 } 264 catch (NumberFormatException e) 265 { 266 throw new IllegalArgumentException ("Explicit array length is not a " + 267 "valid integer '" + lengthStr + 268 "'."); 269 } 270 } 271 272 Class componentType = xjmr.queryJavaType(arrayItemType, inScopeEncStyle); 273 274 return Array.newInstance(componentType, length); 275 } 276 } 277 | Popular Tags |