1 55 56 package org.jboss.axis.encoding.ser; 57 58 import org.jboss.axis.AxisFault; 59 import org.jboss.axis.Constants; 60 import org.jboss.axis.MessageContext; 61 import org.jboss.axis.description.ElementDesc; 62 import org.jboss.axis.description.FieldDesc; 63 import org.jboss.axis.description.OperationDesc; 64 import org.jboss.axis.description.TypeDesc; 65 import org.jboss.axis.encoding.SerializationContext; 66 import org.jboss.axis.encoding.Serializer; 67 import org.jboss.axis.enums.Style; 68 import org.jboss.axis.message.SOAPElementAxisImpl; 69 import org.jboss.axis.soap.SOAPConstants; 70 import org.jboss.axis.utils.BeanPropertyDescriptor; 71 import org.jboss.axis.utils.BeanUtils; 72 import org.jboss.axis.utils.JavaUtils; 73 import org.jboss.axis.utils.Messages; 74 import org.jboss.axis.wsdl.fromJava.Types; 75 import org.jboss.logging.Logger; 76 import org.w3c.dom.Element ; 77 import org.xml.sax.Attributes ; 78 import org.xml.sax.helpers.AttributesImpl ; 79 80 import javax.xml.namespace.QName ; 81 import java.io.IOException ; 82 import java.lang.reflect.Array ; 83 import java.lang.reflect.InvocationTargetException ; 84 import java.lang.reflect.Modifier ; 85 import java.util.List ; 86 87 94 public class BeanSerializer implements Serializer 95 { 96 private static Logger log = Logger.getLogger(BeanSerializer.class.getName()); 97 98 QName xmlType; 99 Class javaType; 100 101 protected BeanPropertyDescriptor[] propertyDescriptor; 102 protected TypeDesc typeDesc; 103 104 105 public BeanSerializer(Class javaType, QName xmlType) 107 { 108 this(javaType, xmlType, TypeDesc.getTypeDescForClass(javaType)); 109 } 110 111 public BeanSerializer(Class javaType, QName xmlType, TypeDesc typeDesc) 113 { 114 this.xmlType = xmlType; 115 this.javaType = javaType; 116 this.typeDesc = typeDesc; 117 118 if (typeDesc != null) 119 { 120 propertyDescriptor = typeDesc.getPropertyDescriptors(); 121 } 122 else 123 { 124 propertyDescriptor = BeanUtils.getPd(javaType, null); 125 } 126 } 127 128 public BeanSerializer(Class javaType, QName xmlType, TypeDesc typeDesc, 130 BeanPropertyDescriptor[] propertyDescriptor) 131 { 132 this.xmlType = xmlType; 133 this.javaType = javaType; 134 this.typeDesc = typeDesc; 135 this.propertyDescriptor = propertyDescriptor; 136 } 137 138 146 public void serialize(QName name, Attributes attributes, 147 Object value, SerializationContext context) 148 throws IOException 149 { 150 151 log.debug("serialize [name=" + name + ",value=" + value + "]"); 152 153 MessageContext messageContext = context.getMessageContext(); 154 OperationDesc operation = messageContext.getOperation(); 155 156 if (operation != null && operation.getStyle() == Style.DOCUMENT) 160 { 161 String uri = name.getNamespaceURI(); 162 String prefix = name.getPrefix(); 163 if (prefix.length() == 0 && uri.length() > 0) 164 { 165 prefix = context.getPrefixForURI(uri); 167 if (prefix.equals("")) prefix = "ns1"; 168 name = new QName (uri, name.getLocalPart(), prefix); 169 context.registerPrefixForURI(prefix, uri); 170 context.setNoDefaultNamespace(true); 171 } 172 } 173 174 Attributes beanAttrs = getObjectAttributes(value, attributes, context); 178 179 String encodingStyle = messageContext.getEncodingStyle(); 181 boolean isEncoded = Constants.isSOAP_ENC(encodingStyle); 182 183 boolean suppressElement = !context.getMessageContext().isEncoded() && 185 name.getNamespaceURI().equals("") && 186 name.getLocalPart().equals("any"); 187 188 if (!suppressElement) 189 { 190 context.startElement(name, beanAttrs); 191 } 192 193 try 194 { 195 for (int i = 0; i < propertyDescriptor.length; i++) 197 { 198 BeanPropertyDescriptor bpDesc = propertyDescriptor[i]; 199 200 String propName = bpDesc.getName(); 201 if (propName.equals("class")) 202 continue; 203 204 QName qname = null; 205 QName xmlType = null; 206 QName itemXmlType = null; 207 boolean isOmittable = false; 208 209 214 if (typeDesc != null) 215 { 216 FieldDesc field = typeDesc.getFieldByName(propName); 217 if (field == null) 218 { 219 String upperName = propName.substring(0, 1).toUpperCase() + propName.substring(1); 220 field = typeDesc.getFieldByName(upperName); 221 } 222 223 if (field != null) 224 { 225 226 if (!field.isElement()) 227 continue; 228 229 if (((ElementDesc)field).isAsContent()) 233 { 234 Object propValue = bpDesc.get(value); 235 String strval = context.getValueAsString(propValue, xmlType); 236 context.writeSafeString(strval); 237 continue; 238 } 239 240 itemXmlType = ((ElementDesc)field).getItemXmlType(); 241 242 if (isEncoded) 246 { 247 qname = new QName (field.getXmlName().getLocalPart()); 248 } 249 else 250 { 251 qname = field.getXmlName(); 252 } 253 isOmittable = field.isMinOccursZero(); 254 xmlType = field.getXmlType(); 255 } 256 } 257 258 if (qname == null) 259 { 260 qname = new QName (propName); 261 } 262 263 if (xmlType == null) 264 { 265 xmlType = context.getQNameForClass(bpDesc.getType()); 267 } 268 269 if (bpDesc.isReadable()) 271 { 272 if (!bpDesc.isIndexed()) 273 { 274 Object propValue = bpDesc.get(value); 276 if (propValue == null && isOmittable && !isEncoded) 280 continue; 281 282 if (!isEncoded && propValue != null && JavaUtils.isArrayClass(propValue.getClass()) 288 && !Constants.XSD_BASE64.equals(xmlType) && !Constants.XSD_HEXBIN.equals(xmlType)) 289 { 290 QName itemType; 291 292 if (itemXmlType == null) 293 { 294 Class componentType = propValue.getClass().getComponentType(); 295 itemType = context.getQNameForClass(componentType); 296 } 297 else 298 { 299 itemType = itemXmlType; 300 } 301 302 log.debug("Item type is " + itemType); 303 for (int j = 0; j < Array.getLength(propValue); j++) 304 { 305 Object item = Array.get(propValue, j); 306 context.serialize(qname, null, item, itemType, true, new Boolean (false)); 307 } 308 } 309 else 310 { 311 context.serialize(qname, null, propValue, xmlType, true, new Boolean (isEncoded)); 312 } 313 } 314 else 315 { 316 int j = 0; 318 while (j >= 0) 319 { 320 Object propValue = null; 321 try 322 { 323 propValue = bpDesc.get(value, j); 324 j++; 325 } 326 catch (Exception e) 327 { 328 j = -1; 329 } 330 if (j >= 0) 331 { 332 context.serialize(qname, null, 333 propValue, xmlType, 334 true, null); 335 } 336 } 337 } 338 } 339 } 340 341 BeanPropertyDescriptor anyDesc = typeDesc == null ? null : typeDesc.getAnyDesc(); 342 343 if (anyDesc != null) 344 { 345 Object anyVal = anyDesc.get(value); 348 if (anyVal != null && anyVal instanceof SOAPElementAxisImpl[]) 349 { 350 SOAPElementAxisImpl[] anyContent = (SOAPElementAxisImpl[])anyVal; 351 for (int i = 0; i < anyContent.length; i++) 352 { 353 SOAPElementAxisImpl element = anyContent[i]; 354 element.output(context); 355 } 356 } 357 } 358 } 359 catch (InvocationTargetException ite) 360 { 361 Throwable target = ite.getTargetException(); 362 log.error(Messages.getMessage("exception00"), target); 363 throw new IOException (target.toString()); 364 } 365 catch (Exception e) 366 { 367 log.error(Messages.getMessage("exception00"), e); 368 throw new IOException (e.toString()); 369 } 370 371 if (!suppressElement) 372 context.endElement(); 373 } 374 375 376 public String getMechanismType() 377 { 378 return Constants.AXIS_SAX; 379 } 380 381 392 public Element writeSchema(Class javaType, Types types) throws Exception 393 { 394 395 Element complexType = types.createElement("complexType"); 397 398 Element e = null; 400 Class superClass = javaType.getSuperclass(); 401 BeanPropertyDescriptor[] superPd = null; 402 List stopClasses = types.getStopClasses(); 403 if (superClass != null && 404 superClass != java.lang.Object .class && 405 superClass != java.lang.Exception .class && 406 superClass != java.lang.Throwable .class && 407 superClass != java.rmi.RemoteException .class && 408 superClass != org.jboss.axis.AxisFault.class && 409 (stopClasses == null || 410 !(stopClasses.contains(superClass.getName())))) 411 { 412 String base = types.writeType(superClass); 414 Element complexContent = types.createElement("complexContent"); 415 complexType.appendChild(complexContent); 416 Element extension = types.createElement("extension"); 417 complexContent.appendChild(extension); 418 extension.setAttribute("base", base); 419 e = extension; 420 TypeDesc superTypeDesc = TypeDesc.getTypeDescForClass(superClass); 422 if (superTypeDesc != null) 423 { 424 superPd = superTypeDesc.getPropertyDescriptors(); 425 } 426 else 427 { 428 superPd = BeanUtils.getPd(superClass, null); 429 } 430 } 431 else 432 { 433 e = complexType; 434 } 435 436 Element all = types.createElement("sequence"); 444 e.appendChild(all); 445 446 if (Modifier.isAbstract(javaType.getModifiers())) 447 { 448 complexType.setAttribute("abstract", "true"); 449 } 450 451 for (int i = 0; i < propertyDescriptor.length; i++) 453 { 454 String propName = propertyDescriptor[i].getName(); 455 456 boolean writeProperty = true; 458 if (propName.equals("class")) 459 { 460 writeProperty = false; 461 } 462 463 if (superPd != null && writeProperty) 466 { 467 for (int j = 0; j < superPd.length && writeProperty; j++) 468 { 469 if (propName.equals(superPd[j].getName())) 470 { 471 writeProperty = false; 472 } 473 } 474 } 475 if (!writeProperty) 476 { 477 continue; 478 } 479 480 Class fieldType = propertyDescriptor[i].getType(); 481 482 487 if (typeDesc != null) 488 { 489 FieldDesc field = typeDesc.getFieldByName(propName); 490 491 if (field != null) 492 { 493 QName qname = field.getXmlName(); 494 QName fieldXmlType = field.getXmlType(); 495 boolean isAnonymous = fieldXmlType != null && fieldXmlType.getLocalPart().startsWith(">"); 496 497 if (qname != null) 498 { 499 504 propName = qname.getLocalPart(); 506 } 507 if (!field.isElement()) 508 { 509 writeAttribute(types, 510 propName, 511 fieldType, 512 fieldXmlType, 513 complexType); 514 } 515 else 516 { 517 writeField(types, 518 propName, 519 fieldType, 520 propertyDescriptor[i].isIndexed(), 521 field.isMinOccursZero(), 522 all, isAnonymous); 523 } 524 } 525 else 526 { 527 writeField(types, 528 propName, 529 fieldType, 530 propertyDescriptor[i].isIndexed(), false, all, false); 531 } 532 } 533 else 534 { 535 writeField(types, 536 propName, 537 fieldType, 538 propertyDescriptor[i].isIndexed(), false, all, false); 539 } 540 } 541 542 return complexType; 544 } 545 546 556 protected void writeField(Types types, String fieldName, 557 Class fieldType, 558 boolean isUnbounded, 559 boolean isOmittable, 560 Element where, 561 boolean isAnonymous) throws Exception 562 { 563 Element elem; 564 if (isAnonymous) 565 { 566 elem = types.createElementWithAnonymousType(fieldName, 567 fieldType, isOmittable, where.getOwnerDocument()); 568 } 569 else 570 { 571 String elementType = types.writeType(fieldType); 572 573 if (elementType == null) 574 { 575 QName anyQN = Constants.XSD_ANYTYPE; 577 String prefix = types.getNamespaces().getCreatePrefix(anyQN.getNamespaceURI()); 578 elementType = prefix + ":" + anyQN.getLocalPart(); 579 } 580 581 elem = types.createElement(fieldName, 582 elementType, 583 types.isNullable(fieldType), 584 isOmittable, 585 where.getOwnerDocument()); 586 } 587 588 if (isUnbounded) 589 { 590 elem.setAttribute("maxOccurs", "unbounded"); 591 } 592 593 where.appendChild(elem); 594 } 595 596 604 protected void writeAttribute(Types types, 605 String fieldName, 606 Class fieldType, 607 QName fieldXmlType, 608 Element where) throws Exception 609 { 610 611 if (!types.isAcceptableAsAttribute(fieldType)) 613 { 614 throw new AxisFault(Messages.getMessage("AttrNotSimpleType00", 615 fieldName, 616 fieldType.getName())); 617 } 618 Element elem = types.createAttributeElement(fieldName, 619 fieldType, fieldXmlType, 620 false, 621 where.getOwnerDocument()); 622 where.appendChild(elem); 623 } 624 625 633 protected Attributes getObjectAttributes(Object value, 634 Attributes attributes, 635 SerializationContext context) 636 { 637 638 if (typeDesc == null || !typeDesc.hasAttributes()) 639 return attributes; 640 641 AttributesImpl attrs; 642 if (attributes == null) 643 { 644 attrs = new AttributesImpl (); 645 } 646 else if (attributes instanceof AttributesImpl ) 647 { 648 attrs = (AttributesImpl )attributes; 649 } 650 else 651 { 652 attrs = new AttributesImpl (attributes); 653 } 654 655 try 656 { 657 for (int i = 0; i < propertyDescriptor.length; i++) 660 { 661 String propName = propertyDescriptor[i].getName(); 662 if (propName.equals("class")) 663 continue; 664 665 FieldDesc field = typeDesc.getFieldByName(propName); 666 if (field == null || field.isElement()) 668 continue; 669 670 QName qname = field.getXmlName(); 671 if (qname == null) 672 { 673 qname = new QName ("", propName); 674 } 675 676 if (propertyDescriptor[i].isReadable() && 677 !propertyDescriptor[i].isIndexed()) 678 { 679 Object propValue = propertyDescriptor[i].get(value); 681 if (propValue != null) 686 { 687 setAttributeProperty(propValue, qname, attrs, context); 688 } 689 } 690 } 691 } 692 catch (Exception e) 693 { 694 return attrs; 696 } 697 698 return attrs; 699 } 700 701 private void setAttributeProperty(Object propValue, 702 QName qname, 703 AttributesImpl attrs, 704 SerializationContext context) throws Exception 705 { 706 String propString = context.getValueAsString(propValue, null); 707 String namespace = qname.getNamespaceURI(); 708 String localName = qname.getLocalPart(); 709 710 SOAPConstants soapConstants = context.getMessageContext().getSOAPConstants(); 713 QName parentElementQName = (QName )context.getElementStack().peek(); 714 QName headerQName = soapConstants.getHeaderQName(); 715 if (headerQName.equals(parentElementQName)) 716 { 717 if (localName.equals("mustUnderstand") || localName.equals("role")) 718 { 719 namespace = soapConstants.getEnvelopeURI(); 720 } 721 } 722 723 attrs.addAttribute(namespace, 724 localName, 725 context.attributeQName2String(qname), 726 "CDATA", 727 propString); 728 } 729 } 730 | Popular Tags |