1 16 17 package org.enhydra.shark.asap.util; 18 19 import org.apache.axis.Constants; 20 import org.apache.axis.components.logger.LogFactory; 21 import org.apache.axis.description.FieldDesc; 22 import org.apache.axis.description.TypeDesc; 23 import org.apache.axis.encoding.DeserializationContext; 24 import org.apache.axis.encoding.Deserializer; 25 import org.apache.axis.encoding.DeserializerImpl; 26 import org.apache.axis.encoding.TypeMapping; 27 import org.apache.axis.message.MessageElement; 28 import org.apache.axis.message.SOAPHandler; 29 import org.apache.axis.utils.BeanPropertyDescriptor; 30 import org.apache.axis.utils.Messages; 31 import org.apache.axis.soap.SOAPConstants; 32 import org.apache.commons.logging.Log; 33 import org.xml.sax.Attributes ; 34 import org.xml.sax.SAXException ; 35 import org.apache.axis.encoding.ser.*; 36 37 import javax.xml.namespace.QName ; 38 import java.io.Serializable ; 39 import java.io.CharArrayWriter ; 40 import java.util.Map ; 41 42 49 public class BeanDeserializerShark extends DeserializerImpl implements Serializable 50 { 51 protected static Log log = 52 LogFactory.getLog(BeanDeserializerShark.class.getName()); 53 54 private final CharArrayWriter val = new CharArrayWriter (); 55 56 private BeanDeserializerShark[] additional = null; 57 private boolean alreadyFailed [] = null; 58 private String [] _addLocalNames = { 59 "observerPropertiesGroup", 60 "instancePropertiesGroup", 61 "factoryPropertiesGroup" 62 }; 63 64 QName xmlType; 65 Class javaType; 66 protected Map propertyMap = null; 67 protected QName prevQName; 68 69 70 protected TypeDesc typeDesc = null; 71 72 protected int collectionIndex = -1; 74 75 protected SimpleDeserializer cacheStringDSer = null; 76 protected QName cacheXMLType = null; 77 78 public BeanDeserializerShark(Class javaType, QName xmlType) { 80 this(javaType, xmlType, TypeDesc.getTypeDescForClass(javaType)); 81 } 82 83 public BeanDeserializerShark(Class javaType, QName xmlType, TypeDesc typeDesc ) { 85 this(javaType, xmlType, typeDesc, 86 AltBeanDeserializerFactory.getProperties(javaType, typeDesc)); 87 } 88 89 public BeanDeserializerShark(Class javaType, QName xmlType, TypeDesc typeDesc, 91 Map propertyMap ) { 92 this.xmlType = xmlType; 93 this.javaType = javaType; 94 this.typeDesc = typeDesc; 95 this.propertyMap = propertyMap; 96 97 try { 99 value=javaType.newInstance(); 100 } catch (Exception e) { 101 } 105 } 106 107 121 public void startElement(String namespace, String localName, 122 String prefix, Attributes attributes, 123 DeserializationContext context) 124 throws SAXException 125 { 126 if (value == null) { 129 try { 130 value=javaType.newInstance(); 131 } catch (Exception e) { 132 throw new SAXException (Messages.getMessage("cantCreateBean00", 134 javaType.getName(), 135 e.toString())); 136 } 137 } 138 super.startElement(namespace, localName, 140 prefix, attributes, context); 141 } 142 143 155 public SOAPHandler onStartChild(String namespace, 156 String localName, 157 String prefix, 158 Attributes attributes, 159 DeserializationContext context) 160 throws SAXException 161 { 162 if (xmlType.toString().endsWith("GetPropertiesRs") 166 ||xmlType.toString().endsWith("SetPropertiesRs")) { 167 int failures = 0; 171 SOAPHandler sHnd = null; 172 for (int n = 0; n < 3; ++n) { 173 try { 174 if (alreadyFailed[n]) { 175 ++failures; 176 continue; 177 } 178 sHnd = additional[n].onStartChild(namespace, localName, prefix, attributes, context); 179 } catch (Throwable t) { 180 alreadyFailed[n] = true; 182 ++failures; 183 } 184 } 185 if (3 == failures) 186 throw new SAXException (Messages.getMessage("cantCreateBean00", 187 _addLocalNames[0], 188 "")); 189 return sHnd; 190 } 191 192 handleMixedContent(); 193 194 BeanPropertyDescriptor propDesc = null; 195 FieldDesc fieldDesc = null; 196 197 SOAPConstants soapConstants = context.getSOAPConstants(); 198 String encodingStyle = context.getMessageContext().getEncodingStyle(); 199 boolean isEncoded = Constants.isSOAP_ENC(encodingStyle); 200 201 QName elemQName = new QName (namespace, localName); 202 if ((prevQName == null) || (!prevQName.equals(elemQName))) { 204 collectionIndex = -1; 205 } 206 prevQName = elemQName; 207 208 if (typeDesc != null) { 209 String fieldName = typeDesc.getFieldNameForElement(elemQName, 212 isEncoded); 213 propDesc = (BeanPropertyDescriptor)propertyMap.get(fieldName); 214 fieldDesc = typeDesc.getFieldByName(fieldName); 215 } 216 217 if (propDesc == null) { 218 propDesc = (BeanPropertyDescriptor) propertyMap.get(localName); 220 } 221 222 if (propDesc == null) { 225 propDesc = getAnyPropertyDesc(); 228 if (propDesc != null) { 229 try { 230 MessageElement [] curElements = (MessageElement[])propDesc.get(value); 231 int length = 0; 232 if (curElements != null) { 233 length = curElements.length; 234 } 235 MessageElement [] newElements = new MessageElement[length + 1]; 236 if (curElements != null) { 237 System.arraycopy(curElements, 0, 238 newElements, 0, length); 239 } 240 MessageElement thisEl = context.getCurElement(); 241 242 newElements[length] = thisEl; 243 propDesc.set(value, newElements); 244 if (!localName.equals(thisEl.getName())) { 250 return new SOAPHandler(newElements, length); 251 } 252 return new SOAPHandler(); 253 } catch (Exception e) { 254 throw new SAXException (e); 255 } 256 } 257 } 258 259 260 if (propDesc == null) { 261 throw new SAXException ( 263 Messages.getMessage("badElem00", javaType.getName(), 264 localName)); 265 } 266 267 QName childXMLType = context.getTypeFromAttributes(namespace, 269 localName, 270 attributes); 271 272 String href = attributes.getValue(soapConstants.getAttrHref()); 273 274 if (childXMLType == null && fieldDesc != null && href == null) { 276 childXMLType = fieldDesc.getXmlType(); 277 } 278 279 Deserializer dSer = getDeserializer(childXMLType, propDesc.getType(), 281 href, 282 context); 283 284 if (dSer == null) { 289 dSer = context.getDeserializerForClass(propDesc.getType()); 290 } 291 292 if (context.isNil(attributes)) { 294 if (propDesc != null && propDesc.isIndexed()) { 295 if (!((dSer != null) && (dSer instanceof ArrayDeserializer)) || 296 propDesc.getType().isArray()) { 297 collectionIndex++; 298 dSer.registerValueTarget(new BeanPropertyTarget(value, 299 propDesc, collectionIndex)); 300 addChildDeserializer(dSer); 301 return (SOAPHandler)dSer; 302 } 303 } 304 return null; 305 } 306 307 if (dSer == null) { 308 throw new SAXException (Messages.getMessage("noDeser00", 309 childXMLType.toString())); 310 } 311 312 if (propDesc.isWriteable()) { 314 if (propDesc.isIndexed() && ( 324 !(dSer instanceof ArrayDeserializer) || 325 propDesc.getType().isArray())) { 326 collectionIndex++; 327 dSer.registerValueTarget(new BeanPropertyTarget(value, 328 propDesc, collectionIndex)); 329 } else { 330 collectionIndex = -1; 334 dSer.registerValueTarget(new BeanPropertyTarget(value, 335 propDesc)); 336 } 337 } 338 339 addChildDeserializer(dSer); 342 343 return (SOAPHandler)dSer; 344 } 345 346 353 public BeanPropertyDescriptor getAnyPropertyDesc() { 354 if (typeDesc == null) 355 return null; 356 357 return typeDesc.getAnyDesc(); 358 } 359 360 373 public void onStartElement(String namespace, String localName, 374 String prefix, Attributes attributes, 375 DeserializationContext context) 376 throws SAXException { 377 378 if (xmlType.toString().endsWith("GetPropertiesRs") 379 ||xmlType.toString().endsWith("SetPropertiesRs")) { 380 String pp = xmlType.toString().substring(0, xmlType.toString().length() - "GetPropertiesRs".length()); 384 try { 385 additional = new BeanDeserializerShark[3]; 386 additional[0] = new BeanDeserializerShark(Class.forName("org.enhydra.shark.asap.types.ObserverPropertiesGroup"), 387 new QName (pp + _addLocalNames[0])); 388 additional[1] = new BeanDeserializerShark(Class.forName("org.enhydra.shark.asap.types.InstancePropertiesGroup"), 389 new QName (pp + _addLocalNames[1])); 390 additional[2] = new BeanDeserializerShark(Class.forName("org.enhydra.shark.asap.types.FactoryPropertiesGroup"), 391 new QName (pp + _addLocalNames[2])); 392 } catch (Throwable t) { 393 t.printStackTrace(); 394 throw new SAXException (t.getMessage()); 395 } 396 alreadyFailed = new boolean[3]; 397 int failures = 0; 398 for (int n = 0; n < 3; ++n) { 399 try { 400 alreadyFailed[n] = false; 401 additional[n].startElement(namespace, _addLocalNames[n], prefix, attributes, context); 402 } catch (Throwable t) { 403 t.printStackTrace(); 404 alreadyFailed[n] = true; 405 ++failures; 406 } 407 } 408 if (3 == failures) 409 throw new SAXException (Messages.getMessage("cantCreateBean00", 410 _addLocalNames[0], 411 "")); 412 } 413 414 if (value == null) { 417 try { 419 value=javaType.newInstance(); 420 } catch (Exception e) { 421 throw new SAXException (Messages.getMessage("cantCreateBean00", 422 javaType.getName(), 423 e.toString())); 424 } 425 } 426 427 if (typeDesc == null) 430 return; 431 432 for (int i=0; i < attributes.getLength(); i++) { 435 QName attrQName = new QName (attributes.getURI(i), 436 attributes.getLocalName(i)); 437 String fieldName = typeDesc.getFieldNameForAttribute(attrQName); 438 if (fieldName == null) 439 continue; 440 441 FieldDesc fieldDesc = typeDesc.getFieldByName(fieldName); 442 443 BeanPropertyDescriptor bpd = 445 (BeanPropertyDescriptor) propertyMap.get(fieldName); 446 if (bpd != null) { 447 if (!bpd.isWriteable() || bpd.isIndexed() ) continue ; 448 449 Deserializer dSer = getDeserializer(fieldDesc.getXmlType(), 451 bpd.getType(), 452 null, 453 context); 454 if (dSer == null) { 455 dSer = context.getDeserializerForClass(bpd.getType()); 456 } 457 if (dSer == null) 458 throw new SAXException ( 459 Messages.getMessage("unregistered00", 460 bpd.getType().toString())); 461 462 if (! (dSer instanceof SimpleDeserializer)) 463 throw new SAXException ( 464 Messages.getMessage("AttrNotSimpleType00", 465 bpd.getName(), 466 bpd.getType().toString())); 467 468 try { 471 dSer.onStartElement(namespace, localName, prefix, 472 attributes, context); 473 Object val = ((SimpleDeserializer)dSer). 474 makeValue(attributes.getValue(i)); 475 bpd.set(value, val); 476 } catch (Exception e) { 477 throw new SAXException (e); 478 } 479 480 } } } 483 484 494 protected Deserializer getDeserializer(QName xmlType, 495 Class javaType, 496 String href, 497 DeserializationContext context) { 498 if (javaType.isArray()) { 499 context.setDestinationClass(javaType); 500 } 501 if (cacheStringDSer != null) { 503 if (String .class.equals(javaType) && 504 href == null && 505 (cacheXMLType == null && xmlType == null || 506 cacheXMLType != null && cacheXMLType.equals(xmlType))) { 507 cacheStringDSer.reset(); 508 return cacheStringDSer; 509 } 510 } 511 512 Deserializer dSer = null; 513 514 if (xmlType != null && href == null) { 515 dSer = context.getDeserializerForType(xmlType); 517 } else { 518 TypeMapping tm = context.getTypeMapping(); 520 QName defaultXMLType = tm.getTypeQName(javaType); 521 if (href == null) { 528 dSer = context.getDeserializer(javaType, defaultXMLType); 529 } else { 530 dSer = new DeserializerImpl(); 531 context.setDestinationClass(javaType); 532 dSer.setDefaultType(defaultXMLType); 533 } 534 } 535 if (javaType.equals(String .class) && 536 dSer instanceof SimpleDeserializer) { 537 cacheStringDSer = (SimpleDeserializer) dSer; 538 cacheXMLType = xmlType; 539 } 540 return dSer; 541 } 542 543 public void characters(char[] chars, int start, int end) throws SAXException { 544 val.write(chars, start, end); 545 } 546 547 public void onEndElement(String namespace, String localName, 548 DeserializationContext context) throws SAXException { 549 550 if (xmlType.toString().endsWith("GetPropertiesRs") 551 ||xmlType.toString().endsWith("SetPropertiesRs")) { 552 for (int n = 0; n < 3; ++n) { 553 if (!alreadyFailed[n]) { 554 BeanPropertyDescriptor propDesc = (BeanPropertyDescriptor) propertyMap.get(_addLocalNames[n]); 555 System.err.println("localName:"+_addLocalNames[n]+", propDesc:"+propDesc); 556 try { 557 propDesc.set(value, additional[n].getValue()); 558 } catch (Throwable t) { 559 t.printStackTrace(); 560 throw new SAXException (t.getMessage()); 561 } 562 } 563 } 564 565 } 566 567 handleMixedContent(); 568 } 569 570 protected void handleMixedContent() throws SAXException { 571 BeanPropertyDescriptor propDesc = getAnyPropertyDesc(); 572 if (propDesc == null || val.size() == 0) { 573 return; 574 } 575 String textValue = val.toString().trim(); 576 val.reset(); 577 if (textValue.length() == 0) { 578 return; 579 } 580 try { 581 MessageElement[] curElements = (MessageElement[]) propDesc.get(value); 582 int length = 0; 583 if (curElements != null) { 584 length = curElements.length; 585 } 586 MessageElement[] newElements = new MessageElement[length + 1]; 587 if (curElements != null) { 588 System.arraycopy(curElements, 0, 589 newElements, 0, length); 590 } 591 MessageElement thisEl = new MessageElement(new org.apache.axis.message.Text(textValue)); 592 newElements[length] = thisEl; 593 propDesc.set(value, newElements); 594 } catch (Exception e) { 595 throw new SAXException (e); 596 } 597 } 598 } 599 | Popular Tags |