1 16 17 package org.enhydra.shark.wfxml.util; 18 19 import org.apache.axis.AxisFault; 20 import org.apache.axis.Constants; 21 import org.apache.axis.components.logger.LogFactory; 22 import org.apache.axis.description.FieldDesc; 23 import org.apache.axis.description.TypeDesc; 24 import org.apache.axis.encoding.SerializationContext; 25 import org.apache.axis.encoding.Serializer; 26 import org.apache.axis.message.MessageElement; 27 import org.apache.axis.utils.BeanPropertyDescriptor; 28 import org.apache.axis.utils.BeanUtils; 29 import org.apache.axis.utils.Messages; 30 import org.apache.axis.utils.FieldPropertyDescriptor; 31 import org.apache.axis.wsdl.fromJava.Types; 32 import org.apache.axis.wsdl.symbolTable.SchemaUtils; 33 import org.apache.commons.logging.Log; 34 import org.w3c.dom.Element ; 35 import org.xml.sax.Attributes ; 36 import org.xml.sax.helpers.AttributesImpl ; 37 import org.apache.axis.encoding.ser.*; 38 39 import javax.xml.namespace.QName ; 40 import java.io.IOException ; 41 import java.io.Serializable ; 42 import java.lang.reflect.InvocationTargetException ; 43 import java.lang.reflect.Modifier ; 44 import java.util.List ; 45 import java.lang.reflect.InvocationTargetException ; 46 import java.lang.reflect.Method ; 47 48 55 public class BeanSerializerShark implements Serializer, Serializable { 56 57 protected static Log log = 58 LogFactory.getLog(BeanSerializerShark.class.getName()); 59 60 QName xmlType; 61 Class javaType; 62 63 protected BeanPropertyDescriptor[] propertyDescriptor = null; 64 protected TypeDesc typeDesc = null; 65 66 67 public BeanSerializerShark(Class javaType, QName xmlType) { 69 this(javaType, xmlType, TypeDesc.getTypeDescForClass(javaType)); 70 } 71 72 public BeanSerializerShark(Class javaType, QName xmlType, TypeDesc typeDesc) { 74 this(javaType, xmlType, typeDesc, null); 75 76 if (typeDesc != null) { 77 propertyDescriptor = typeDesc.getPropertyDescriptors(); 78 } else { 79 propertyDescriptor = BeanUtils.getPd(javaType, null); 80 } 81 } 82 83 public BeanSerializerShark(Class javaType, QName xmlType, TypeDesc typeDesc, 85 BeanPropertyDescriptor[] propertyDescriptor) { 86 this.xmlType = xmlType; 87 this.javaType = javaType; 88 this.typeDesc = typeDesc; 89 this.propertyDescriptor = propertyDescriptor; 90 } 91 private boolean isEmptyObjectType(QName name) { 92 return (name.getLocalPart().equals("GetPropertiesRq")) 93 || (name.getLocalPart().equals("SubscribeRs")) 94 || (name.getLocalPart().equals("UnsubscribeRs")) 95 || (name.getLocalPart().equals("CompletedRs")) 96 || (name.getLocalPart().equals("StateChangedRs")); 97 } 98 105 public void serialize(QName name, Attributes attributes, 106 Object value, SerializationContext context) 107 throws IOException 108 { 109 Attributes beanAttrs = getObjectAttributes(value, attributes, context); 113 114 String encodingStyle = context.getEncodingStyle(); 116 boolean isEncoded = Constants.isSOAP_ENC(encodingStyle); 117 118 boolean suppressElement = !context.isEncoded() && 120 name.getNamespaceURI().equals("") && 121 name.getLocalPart().equals("any"); 122 123 boolean grpElement = name.toString().endsWith("Group"); 124 suppressElement |= grpElement; 125 if (isEmptyObjectType(name)) { 126 context.setWriteXMLType(null); 127 } 128 if (!suppressElement) 129 context.startElement(name, beanAttrs); 130 131 if (!isEmptyObjectType(name)) { 132 try { 133 for (int i=0; i<propertyDescriptor.length; i++) { 135 String propName = propertyDescriptor[i].getName(); 136 if (propName.equals("class")) 137 continue; 138 QName qname = null; 139 QName xmlType = null; 140 boolean isOmittable = false; 141 142 147 if (typeDesc != null) { 148 FieldDesc field = typeDesc.getFieldByName(propName); 149 if (field != null) { 150 if (!field.isElement()) 151 continue; 152 153 if (isEncoded) { 157 qname = new QName ( 158 field.getXmlName().getLocalPart()); 159 } else { 160 qname = field.getXmlName(); 161 } 162 isOmittable = field.isMinOccursZero(); 163 xmlType = field.getXmlType(); 164 } 165 } 166 167 if (qname == null) { 168 qname = new QName (isEncoded ? "" : name.getNamespaceURI(), 169 propName); 170 } 171 172 if (xmlType == null) { 173 xmlType = context.getQNameForClass(propertyDescriptor[i].getType()); 175 } 176 177 if(propertyDescriptor[i].isReadable()) { 179 if (!propertyDescriptor[i].isIndexed()) { 180 Object propValue = 182 propertyDescriptor[i].get(value); 183 if (propValue == null && 187 isOmittable && 188 !isEncoded) 189 continue; 190 191 if (null == propValue && qname.toString().endsWith("Group")) { 192 System.err.println("\telemQName:"+ qname +" contains 'Group' not appending nil"); 193 continue; 194 } 195 context.serialize(qname, 196 null, 197 propValue, 198 xmlType, 199 true, 200 null); 201 } else { 202 int j=0; 204 while(j >= 0) { 205 Object propValue = null; 206 try { 207 propValue = 208 propertyDescriptor[i].get(value, j); 209 j++; 210 } catch (Exception e) { 211 j = -1; 212 } 213 if (j >= 0) { 214 context.serialize(qname, null, 215 propValue, xmlType, 216 true, null); 217 } 218 } 219 } 220 } 221 } 222 223 BeanPropertyDescriptor anyDesc = typeDesc == null ? null : 224 typeDesc.getAnyDesc(); 225 if (anyDesc != null) { 226 Object anyVal = anyDesc.get(value); 229 if (anyVal != null && anyVal instanceof MessageElement[]) { 230 MessageElement [] anyContent = (MessageElement[])anyVal; 231 for (int i = 0; i < anyContent.length; i++) { 232 MessageElement element = anyContent[i]; 233 element.output(context); 234 } 235 } 236 } 237 } catch (InvocationTargetException ite) { 238 Throwable target = ite.getTargetException(); 239 log.error(Messages.getMessage("exception00"), target); 240 throw new IOException (target.toString()); 241 } catch (Exception e) { 242 log.error(Messages.getMessage("exception00"), e); 243 throw new IOException (e.toString()); 244 } 245 } 246 247 if (!suppressElement) 248 context.endElement(); 249 } 250 251 252 253 public String getMechanismType() { return Constants.AXIS_SAX; } 254 255 266 public Element writeSchema(Class javaType, Types types) throws Exception { 267 268 Element complexType = types.createElement("complexType"); 270 271 Element e = null; 273 Class superClass = javaType.getSuperclass(); 274 BeanPropertyDescriptor[] superPd = null; 275 List stopClasses = types.getStopClasses(); 276 if (superClass != null && 277 superClass != java.lang.Object .class && 278 superClass != java.lang.Exception .class && 279 superClass != java.lang.Throwable .class && 280 superClass != java.rmi.RemoteException .class && 281 superClass != org.apache.axis.AxisFault.class && 282 (stopClasses == null || 283 !(stopClasses.contains(superClass.getName()))) ) { 284 String base = types.writeType(superClass); 286 Element complexContent = types.createElement("complexContent"); 287 complexType.appendChild(complexContent); 288 Element extension = types.createElement("extension"); 289 complexContent.appendChild(extension); 290 extension.setAttribute("base", base); 291 e = extension; 292 TypeDesc superTypeDesc = TypeDesc.getTypeDescForClass(superClass); 294 if (superTypeDesc != null) { 295 superPd = superTypeDesc.getPropertyDescriptors(); 296 } else { 297 superPd = BeanUtils.getPd(superClass, null); 298 } 299 } else { 300 e = complexType; 301 } 302 303 Element all = types.createElement("sequence"); 311 e.appendChild(all); 312 313 if (Modifier.isAbstract(javaType.getModifiers())) { 314 complexType.setAttribute("abstract", "true"); 315 } 316 317 for (int i=0; i<propertyDescriptor.length; i++) { 319 String propName = propertyDescriptor[i].getName(); 320 321 boolean writeProperty = true; 323 if (propName.equals("class")) { 324 writeProperty = false; 325 } 326 327 if (superPd != null && writeProperty) { 330 for (int j=0; j<superPd.length && writeProperty; j++) { 331 if (propName.equals(superPd[j].getName())) { 332 writeProperty = false; 333 } 334 } 335 } 336 if (!writeProperty) { 337 continue; 338 } 339 340 345 if (typeDesc != null) { 346 Class fieldType = propertyDescriptor[i].getType(); 347 FieldDesc field = typeDesc.getFieldByName(propName); 348 349 if (field != null) { 350 QName qname = field.getXmlName(); 351 QName fieldXmlType = field.getXmlType(); 352 boolean isAnonymous = fieldXmlType != null && fieldXmlType.getLocalPart().startsWith(">"); 353 354 if (qname != null) { 355 360 propName = qname.getLocalPart(); 362 } 363 if (!field.isElement()) { 364 writeAttribute(types, 365 propName, 366 fieldType, 367 fieldXmlType, 368 complexType); 369 } else { 370 writeField(types, 371 propName, 372 fieldXmlType, 373 fieldType, 374 propertyDescriptor[i].isIndexed(), 375 field.isMinOccursZero(), 376 all, isAnonymous); 377 } 378 } else { 379 writeField(types, 380 propName, 381 null, 382 fieldType, 383 propertyDescriptor[i].isIndexed(), false, all, false); 384 } 385 } else { 386 boolean done = false; 387 if(propertyDescriptor[i] instanceof FieldPropertyDescriptor){ 388 FieldPropertyDescriptor fpd = (FieldPropertyDescriptor) propertyDescriptor[i]; 389 Class clazz = fpd.getField().getType(); 390 if(types.getTypeQName(clazz)!=null) { 391 writeField(types, 392 propName, 393 null, 394 clazz, 395 false, false, all, false); 396 397 done = true; 398 } 399 } 400 if(!done) { 401 writeField(types, 402 propName, 403 null, 404 propertyDescriptor[i].getType(), 405 propertyDescriptor[i].isIndexed(), false, all, false); 406 } 407 408 } 409 } 410 411 return complexType; 413 } 414 415 425 protected void writeField(Types types, 426 String fieldName, 427 QName xmlType, 428 Class fieldType, 429 boolean isUnbounded, 430 boolean isOmittable, 431 Element where, 432 boolean isAnonymous) throws Exception { 433 Element elem; 434 if (isAnonymous) { 435 elem = types.createElementWithAnonymousType(fieldName, 436 fieldType, isOmittable, where.getOwnerDocument()); 437 } else { 438 if (!SchemaUtils.isSimpleSchemaType(xmlType) && Types.isArray(fieldType)) { 439 xmlType = null; 440 } 441 442 String elementType = types.writeType(fieldType, xmlType); 443 444 if (elementType == null) { 445 QName anyQN = Constants.XSD_ANYTYPE; 447 String prefix = types.getNamespaces().getCreatePrefix(anyQN.getNamespaceURI()); 448 elementType = prefix + ":" + anyQN.getLocalPart(); 449 } 450 451 elem = types.createElement(fieldName, 452 elementType, 453 types.isNullable(fieldType), 454 isOmittable, 455 where.getOwnerDocument()); 456 } 457 458 if (isUnbounded) { 459 elem.setAttribute("maxOccurs", "unbounded"); 460 } 461 462 where.appendChild(elem); 463 } 464 465 472 protected void writeAttribute(Types types, 473 String fieldName, 474 Class fieldType, 475 QName fieldXmlType, 476 Element where) throws Exception { 477 478 if (!types.isAcceptableAsAttribute(fieldType)) { 480 throw new AxisFault(Messages.getMessage("AttrNotSimpleType00", 481 fieldName, 482 fieldType.getName())); 483 } 484 Element elem = types.createAttributeElement(fieldName, 485 fieldType, fieldXmlType, 486 false, 487 where.getOwnerDocument()); 488 where.appendChild(elem); 489 } 490 491 499 protected Attributes getObjectAttributes(Object value, 500 Attributes attributes, 501 SerializationContext context) { 502 503 if (typeDesc == null || !typeDesc.hasAttributes()) 504 return attributes; 505 506 AttributesImpl attrs; 507 if (attributes == null) { 508 attrs = new AttributesImpl (); 509 } else if (attributes instanceof AttributesImpl ) { 510 attrs = (AttributesImpl )attributes; 511 } else { 512 attrs = new AttributesImpl (attributes); 513 } 514 515 try { 516 for (int i=0; i<propertyDescriptor.length; i++) { 519 String propName = propertyDescriptor[i].getName(); 520 if (propName.equals("class")) 521 continue; 522 523 FieldDesc field = typeDesc.getFieldByName(propName); 524 if (field == null || field.isElement()) 526 continue; 527 528 QName qname = field.getXmlName(); 529 if (qname == null) { 530 qname = new QName ("", propName); 531 } 532 533 if (propertyDescriptor[i].isReadable() && 534 !propertyDescriptor[i].isIndexed()) { 535 Object propValue = propertyDescriptor[i].get(value); 537 if (qname.equals(new QName (Constants.URI_SOAP11_ENV, Constants.ATTR_MUST_UNDERSTAND))) { 539 if (propValue.equals(Boolean.TRUE)) { 540 propValue = "1"; 541 } else if (propValue.equals(Boolean.FALSE)) { 542 propValue = "0"; 543 } 544 } 545 if (propValue != null) { 550 setAttributeProperty(propValue, 551 qname, 552 field.getXmlType(), 553 attrs, 554 context); 555 } 556 } 557 } 558 } catch (Exception e) { 559 return attrs; 561 } 562 563 return attrs; 564 } 565 566 private void setAttributeProperty(Object propValue, 567 QName qname, 568 QName xmlType, AttributesImpl attrs, 569 SerializationContext context) throws Exception { 570 571 String namespace = qname.getNamespaceURI(); 572 String localName = qname.getLocalPart(); 573 574 if (attrs.getIndex(namespace, localName) != -1) { 580 return; 581 } 582 583 String propString = context.getValueAsString(propValue, xmlType); 584 585 attrs.addAttribute(namespace, 586 localName, 587 context.attributeQName2String(qname), 588 "CDATA", 589 propString); 590 } 591 } 592 | Popular Tags |