1 55 56 package org.jboss.axis.encoding.ser; 57 58 import org.jboss.axis.AxisEngine; 59 import org.jboss.axis.Constants; 60 import org.jboss.axis.MessageContext; 61 import org.jboss.axis.encoding.SerializationContext; 62 import org.jboss.axis.encoding.Serializer; 63 import org.jboss.axis.schema.SchemaVersion; 64 import org.jboss.axis.soap.SOAPConstants; 65 import org.jboss.axis.utils.JavaUtils; 66 import org.jboss.axis.utils.Messages; 67 import org.jboss.axis.wsdl.fromJava.Types; 68 import org.jboss.logging.Logger; 69 import org.w3c.dom.Element ; 70 import org.xml.sax.Attributes ; 71 import org.xml.sax.helpers.AttributesImpl ; 72 73 import javax.xml.namespace.QName ; 74 import java.io.IOException ; 75 import java.lang.reflect.Array ; 76 import java.util.Collection ; 77 import java.util.HashMap ; 78 import java.util.Iterator ; 79 80 90 public class ArraySerializer implements Serializer 91 { 92 private static Logger log = Logger.getLogger(ArraySerializer.class.getName()); 93 94 private HashMap componentTypeMap = new HashMap (); 97 98 106 public void serialize(QName name, Attributes attributes, 107 Object value, SerializationContext context) 108 throws IOException 109 { 110 if (value == null) 111 throw new IOException (Messages.getMessage("cantDoNullArray00")); 112 113 MessageContext msgContext = context.getMessageContext(); 114 SchemaVersion schema = SchemaVersion.SCHEMA_2001; 115 SOAPConstants soap = SOAPConstants.SOAP11_CONSTANTS; 116 if (msgContext != null) 117 { 118 schema = msgContext.getSchemaVersion(); 119 soap = msgContext.getSOAPConstants(); 120 } 121 122 Class cls = value.getClass(); 123 Collection list = null; 124 125 if (!cls.isArray()) 126 { 127 if (!(value instanceof Collection )) 128 { 129 throw new IOException (Messages.getMessage("cantSerialize00", cls.getName())); 130 } 131 list = (Collection )value; 132 } 133 134 Class componentType; 136 if (list == null) 137 { 138 componentType = cls.getComponentType(); 139 } 140 else 141 { 142 componentType = Object .class; 143 } 144 145 QName componentQName = (QName )componentTypeMap.get(componentType); 149 150 String dims = ""; 151 152 160 if (componentQName == null) 162 { 163 while (componentType.isArray()) 164 { 165 componentType = componentType.getComponentType(); 166 if (soap == SOAPConstants.SOAP12_CONSTANTS) 167 dims += "* "; 168 else 169 dims += "[]"; 170 } 171 } 172 173 if (componentQName == null) 175 { 176 componentQName = context.getQNameForClass(componentType); 177 } 178 179 if (componentQName == null) 181 { 182 Class searchCls = componentType; 183 while (searchCls != null && componentQName == null) 184 { 185 searchCls = searchCls.getSuperclass(); 186 componentQName = context.getQNameForClass(searchCls); 187 } 188 if (componentQName != null) 189 { 190 componentType = searchCls; 191 } 192 } 193 194 if (componentQName == null) 195 { 196 throw new IOException (Messages.getMessage("noType00", componentType.getName())); 197 } 198 199 String prefix = context.getPrefixForURI(componentQName.getNamespaceURI()); 200 String compType = prefix + ":" + componentQName.getLocalPart(); 201 int len = (list == null) ? Array.getLength(value) : list.size(); 202 203 String arrayType; 204 if (soap == SOAPConstants.SOAP12_CONSTANTS) 205 arrayType = dims + len; 206 else 207 arrayType = dims + "[" + len + "]"; 208 209 236 boolean enable2Dim = false; 239 240 if (msgContext != null) 242 { 243 enable2Dim = 244 JavaUtils.isTrueExplicitly(msgContext.getAxisEngine().getOption(AxisEngine.PROP_TWOD_ARRAY_ENCODING)); 245 } 246 247 int dim2Len = -1; 248 if (enable2Dim && !dims.equals("")) 249 { 250 if (cls.isArray() && len > 0) 251 { 252 boolean okay = true; 253 for (int i = 0; i < len && okay; i++) 255 { 256 257 Object elementValue = Array.get(value, i); 258 if (elementValue == null) 259 okay = false; 260 else if (dim2Len < 0) 261 { 262 dim2Len = Array.getLength(elementValue); 263 if (dim2Len <= 0) 264 { 265 okay = false; 266 } 267 } 268 else if (dim2Len != Array.getLength(elementValue)) 269 { 270 okay = false; 271 } 272 } 273 if (okay) 275 { 276 dims = dims.substring(0, dims.length() - 2); 277 if (soap == SOAPConstants.SOAP12_CONSTANTS) 278 arrayType = dims + len + " " + dim2Len; 279 else 280 arrayType = dims + "[" + len + "," + dim2Len + "]"; 281 } 282 else 283 { 284 dim2Len = -1; 285 } 286 } 287 } 288 289 boolean maxOccursUsage = (msgContext != null && !msgContext.isEncoded()) && 294 componentQName.equals(context.getCurrentXMLType()); 295 296 if (!maxOccursUsage) 297 { 298 AttributesImpl attrs; 299 if (attributes == null) 300 { 301 attrs = new AttributesImpl (); 302 } 303 else if (attributes instanceof AttributesImpl ) 304 { 305 attrs = (AttributesImpl )attributes; 306 } 307 else 308 { 309 attrs = new AttributesImpl (attributes); 310 } 311 312 313 if (attrs.getIndex(soap.getEncodingURI(), soap.getAttrItemType()) == -1) 314 { 315 String encprefix = 316 context.getPrefixForURI(soap.getEncodingURI()); 317 318 if (soap != SOAPConstants.SOAP12_CONSTANTS) 319 { 320 compType = compType + arrayType; 321 322 attrs.addAttribute(soap.getEncodingURI(), 323 soap.getAttrItemType(), 324 encprefix + ":arrayType", 325 "CDATA", 326 compType); 327 328 } 329 else 330 { 331 attrs.addAttribute(soap.getEncodingURI(), 332 soap.getAttrItemType(), 333 encprefix + ":itemType", 334 "CDATA", 335 compType); 336 337 attrs.addAttribute(soap.getEncodingURI(), 338 "arraySize", 339 encprefix + ":arraySize", 340 "CDATA", 341 arrayType); 342 } 343 } 344 345 int typeI = attrs.getIndex(schema.getXsiURI(), 360 "type"); 361 if (typeI != -1) 362 { 363 String qname = 364 context.getPrefixForURI(schema.getXsiURI(), 365 "xsi") + ":type"; 366 QName soapArray; 367 if (soap == SOAPConstants.SOAP12_CONSTANTS) 368 { 369 soapArray = Constants.SOAP_ARRAY12; 370 } 371 else 372 { 373 soapArray = Constants.SOAP_ARRAY; 374 } 375 376 attrs.setAttribute(typeI, 377 schema.getXsiURI(), 378 "type", 379 qname, 380 "CDATA", 381 context.qName2String(soapArray)); 382 } 383 attributes = attrs; 384 } 385 386 QName elementName = name; 390 Attributes serializeAttr = attributes; 391 if (!maxOccursUsage) 392 { 393 serializeAttr = null; context.startElement(name, attributes); 395 elementName = Constants.QNAME_LITERAL_ITEM; 396 } 397 398 if (dim2Len < 0) 399 { 400 if (list == null) 402 { 403 for (int index = 0; index < len; index++) 404 { 405 Object aValue = Array.get(value, index); 406 407 context.serialize(elementName, serializeAttr, aValue, 409 componentQName, true, Boolean.FALSE); } 413 } 414 else 415 { 416 for (Iterator iterator = list.iterator(); iterator.hasNext();) 417 { 418 Object aValue = iterator.next(); 419 420 context.serialize(elementName, serializeAttr, aValue, 422 componentQName, true, Boolean.FALSE); 426 } 427 } 428 } 429 else 430 { 431 for (int index = 0; index < len; index++) 433 { 434 for (int index2 = 0; index2 < dim2Len; index2++) 435 { 436 Object aValue = Array.get(Array.get(value, index), index2); 437 context.serialize(elementName, null, aValue); 438 } 439 } 440 } 441 442 if (!maxOccursUsage) 443 context.endElement(); 444 } 445 446 public String getMechanismType() 447 { 448 return Constants.AXIS_SAX; 449 } 450 451 public void addComponentTypeMapping(Class pomponentType, QName typeName) 452 { 453 componentTypeMap.put(pomponentType, typeName); 454 } 455 456 467 public Element writeSchema(Class javaType, Types types) throws Exception 468 { 469 String componentTypeName = null; 471 Class componentType = null; 472 if (javaType.isArray()) 473 { 474 String dimString = "[]"; 475 componentType = javaType.getComponentType(); 476 if (componentType.isArray()) 477 { 478 while (componentType.isArray()) 479 { 480 dimString += "[]"; 481 componentType = componentType.getComponentType(); 482 } 483 } 484 else 485 { 486 types.writeType(componentType, null); 487 } 488 componentTypeName = 489 types.getQNameString(types.getTypeQName(componentType)) + 490 dimString; 491 } 492 493 return types.createArrayElement(componentTypeName); 495 } 496 } 497 | Popular Tags |