1 16 17 package org.enhydra.shark.wfxml.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 "registryPropertiesGroup" 63 }; 64 65 QName xmlType; 66 Class javaType; 67 protected Map propertyMap = null; 68 protected QName prevQName; 69 70 71 protected TypeDesc typeDesc = null; 72 73 protected int collectionIndex = -1; 75 76 protected SimpleDeserializer cacheStringDSer = null; 77 protected QName cacheXMLType = null; 78 79 public BeanDeserializerShark(Class javaType, QName xmlType) { 81 this(javaType, xmlType, TypeDesc.getTypeDescForClass(javaType)); 82 } 83 84 public BeanDeserializerShark(Class javaType, QName xmlType, TypeDesc typeDesc ) { 86 this(javaType, xmlType, typeDesc, 87 AltBeanDeserializerFactory.getProperties(javaType, typeDesc)); 88 } 89 90 public BeanDeserializerShark(Class javaType, QName xmlType, TypeDesc typeDesc, 92 Map propertyMap ) { 93 this.xmlType = xmlType; 94 this.javaType = javaType; 95 this.typeDesc = typeDesc; 96 this.propertyMap = propertyMap; 97 98 try { 100 value=javaType.newInstance(); 101 } catch (Exception e) { 102 } 106 } 107 108 122 public void startElement(String namespace, String localName, 123 String prefix, Attributes attributes, 124 DeserializationContext context) 125 throws SAXException 126 { 127 if (value == null) { 130 try { 131 value=javaType.newInstance(); 132 } catch (Exception e) { 133 throw new SAXException (Messages.getMessage("cantCreateBean00", 135 javaType.getName(), 136 e.toString())); 137 } 138 } 139 super.startElement(namespace, localName, 141 prefix, attributes, context); 142 } 143 144 156 public SOAPHandler onStartChild(String namespace, 157 String localName, 158 String prefix, 159 Attributes attributes, 160 DeserializationContext context) 161 throws SAXException 162 { 163 if (xmlType.toString().endsWith("GetPropertiesRs") 167 ||xmlType.toString().endsWith("SetPropertiesRs")) { 168 int failures = 0; 172 SOAPHandler sHnd = null; 173 for (int n = 0; n < additional.length; ++n) { 174 try { 175 if (alreadyFailed[n]) { 176 ++failures; 177 continue; 178 } 179 sHnd = additional[n].onStartChild(namespace, localName, prefix, attributes, context); 180 } catch (Throwable t) { 181 alreadyFailed[n] = true; 183 ++failures; 184 } 185 } 186 if (additional.length == failures) 187 throw new SAXException (Messages.getMessage("cantCreateBean00", 188 _addLocalNames[0], 189 "")); 190 return sHnd; 191 } 192 193 handleMixedContent(); 194 195 BeanPropertyDescriptor propDesc = null; 196 FieldDesc fieldDesc = null; 197 198 SOAPConstants soapConstants = context.getSOAPConstants(); 199 String encodingStyle = context.getMessageContext().getEncodingStyle(); 200 boolean isEncoded = Constants.isSOAP_ENC(encodingStyle); 201 202 QName elemQName = new QName (namespace, localName); 203 if ((prevQName == null) || (!prevQName.equals(elemQName))) { 205 collectionIndex = -1; 206 } 207 prevQName = elemQName; 208 209 if (typeDesc != null) { 210 String fieldName = typeDesc.getFieldNameForElement(elemQName, 213 isEncoded); 214 propDesc = (BeanPropertyDescriptor)propertyMap.get(fieldName); 215 fieldDesc = typeDesc.getFieldByName(fieldName); 216 } 217 218 if (propDesc == null) { 219 propDesc = (BeanPropertyDescriptor) propertyMap.get(localName); 221 } 222 223 if (propDesc == null) { 226 propDesc = getAnyPropertyDesc(); 229 if (propDesc != null) { 230 try { 231 MessageElement [] curElements = (MessageElement[])propDesc.get(value); 232 int length = 0; 233 if (curElements != null) { 234 length = curElements.length; 235 } 236 MessageElement [] newElements = new MessageElement[length + 1]; 237 if (curElements != null) { 238 System.arraycopy(curElements, 0, 239 newElements, 0, length); 240 } 241 MessageElement thisEl = context.getCurElement(); 242 243 newElements[length] = thisEl; 244 propDesc.set(value, newElements); 245 if (!localName.equals(thisEl.getName())) { 251 return new SOAPHandler(newElements, length); 252 } 253 return new SOAPHandler(); 254 } catch (Exception e) { 255 throw new SAXException (e); 256 } 257 } 258 } 259 260 261 if (propDesc == null) { 262 throw new SAXException ( 264 Messages.getMessage("badElem00", javaType.getName(), 265 localName)); 266 } 267 268 QName childXMLType = context.getTypeFromAttributes(namespace, 270 localName, 271 attributes); 272 273 String href = attributes.getValue(soapConstants.getAttrHref()); 274 275 if (childXMLType == null && fieldDesc != null && href == null) { 277 childXMLType = fieldDesc.getXmlType(); 278 } 279 280 Deserializer dSer = getDeserializer(childXMLType, propDesc.getType(), 282 href, 283 context); 284 285 if (dSer == null) { 290 dSer = context.getDeserializerForClass(propDesc.getType()); 291 } 292 293 if (context.isNil(attributes)) { 295 if (propDesc != null && propDesc.isIndexed()) { 296 if (!((dSer != null) && (dSer instanceof ArrayDeserializer)) || 297 propDesc.getType().isArray()) { 298 collectionIndex++; 299 dSer.registerValueTarget(new BeanPropertyTarget(value, 300 propDesc, collectionIndex)); 301 addChildDeserializer(dSer); 302 return (SOAPHandler)dSer; 303 } 304 } 305 return null; 306 } 307 308 if (dSer == null) { 309 throw new SAXException (Messages.getMessage("noDeser00", 310 childXMLType.toString())); 311 } 312 313 if (propDesc.isWriteable()) { 315 if (propDesc.isIndexed() && ( 325 !(dSer instanceof ArrayDeserializer) || 326 propDesc.getType().isArray())) { 327 collectionIndex++; 328 dSer.registerValueTarget(new BeanPropertyTarget(value, 329 propDesc, collectionIndex)); 330 } else { 331 collectionIndex = -1; 335 dSer.registerValueTarget(new BeanPropertyTarget(value, 336 propDesc)); 337 } 338 } 339 340 addChildDeserializer(dSer); 343 344 return (SOAPHandler)dSer; 345 } 346 347 354 public BeanPropertyDescriptor getAnyPropertyDesc() { 355 if (typeDesc == null) 356 return null; 357 358 return typeDesc.getAnyDesc(); 359 } 360 361 374 public void onStartElement(String namespace, String localName, 375 String prefix, Attributes attributes, 376 DeserializationContext context) 377 throws SAXException { 378 379 if (xmlType.toString().endsWith("GetPropertiesRs") 380 ||xmlType.toString().endsWith("SetPropertiesRs")) { 381 String pp = xmlType.toString().substring(0, xmlType.toString().length() - "GetPropertiesRs".length()); 385 try { 386 additional = new BeanDeserializerShark[4]; 387 additional[0] = new BeanDeserializerShark(Class.forName("org.enhydra.shark.asap.types.ObserverPropertiesGroup"), 388 new QName (pp + _addLocalNames[0])); 389 additional[1] = new BeanDeserializerShark(Class.forName("org.enhydra.shark.asap.types.InstancePropertiesGroup"), 390 new QName (pp + _addLocalNames[1])); 391 additional[2] = new BeanDeserializerShark(Class.forName("org.enhydra.shark.asap.types.FactoryPropertiesGroup"), 392 new QName (pp + _addLocalNames[2])); 393 additional[3] = new BeanDeserializerShark(Class.forName("org.enhydra.shark.wfxml.types.RegistryPropertiesGroup"), 394 new QName (pp + _addLocalNames[3])); 395 } catch (Throwable t) { 396 t.printStackTrace(); 397 throw new SAXException (t.getMessage()); 398 } 399 alreadyFailed = new boolean[additional.length]; 400 int failures = 0; 401 for (int n = 0; n < additional.length; ++n) { 402 try { 403 alreadyFailed[n] = false; 404 additional[n].startElement(namespace, _addLocalNames[n], prefix, attributes, context); 405 } catch (Throwable t) { 406 t.printStackTrace(); 407 alreadyFailed[n] = true; 408 ++failures; 409 } 410 } 411 if (additional.length == failures) 412 throw new SAXException (Messages.getMessage("cantCreateBean00", 413 _addLocalNames[0], 414 "")); 415 } 416 417 if (value == null) { 420 try { 422 value=javaType.newInstance(); 423 } catch (Exception e) { 424 throw new SAXException (Messages.getMessage("cantCreateBean00", 425 javaType.getName(), 426 e.toString())); 427 } 428 } 429 430 if (typeDesc == null) 433 return; 434 435 for (int i=0; i < attributes.getLength(); i++) { 438 QName attrQName = new QName (attributes.getURI(i), 439 attributes.getLocalName(i)); 440 String fieldName = typeDesc.getFieldNameForAttribute(attrQName); 441 if (fieldName == null) 442 continue; 443 444 FieldDesc fieldDesc = typeDesc.getFieldByName(fieldName); 445 446 BeanPropertyDescriptor bpd = 448 (BeanPropertyDescriptor) propertyMap.get(fieldName); 449 if (bpd != null) { 450 if (!bpd.isWriteable() || bpd.isIndexed() ) continue ; 451 452 Deserializer dSer = getDeserializer(fieldDesc.getXmlType(), 454 bpd.getType(), 455 null, 456 context); 457 if (dSer == null) { 458 dSer = context.getDeserializerForClass(bpd.getType()); 459 } 460 if (dSer == null) 461 throw new SAXException ( 462 Messages.getMessage("unregistered00", 463 bpd.getType().toString())); 464 465 if (! (dSer instanceof SimpleDeserializer)) 466 throw new SAXException ( 467 Messages.getMessage("AttrNotSimpleType00", 468 bpd.getName(), 469 bpd.getType().toString())); 470 471 try { 474 dSer.onStartElement(namespace, localName, prefix, 475 attributes, context); 476 Object val = ((SimpleDeserializer)dSer). 477 makeValue(attributes.getValue(i)); 478 bpd.set(value, val); 479 } catch (Exception e) { 480 throw new SAXException (e); 481 } 482 483 } } } 486 487 497 protected Deserializer getDeserializer(QName xmlType, 498 Class javaType, 499 String href, 500 DeserializationContext context) { 501 if (javaType.isArray()) { 502 context.setDestinationClass(javaType); 503 } 504 if (cacheStringDSer != null) { 506 if (String .class.equals(javaType) && 507 href == null && 508 (cacheXMLType == null && xmlType == null || 509 cacheXMLType != null && cacheXMLType.equals(xmlType))) { 510 cacheStringDSer.reset(); 511 return cacheStringDSer; 512 } 513 } 514 515 Deserializer dSer = null; 516 517 if (xmlType != null && href == null) { 518 dSer = context.getDeserializerForType(xmlType); 520 } else { 521 TypeMapping tm = context.getTypeMapping(); 523 QName defaultXMLType = tm.getTypeQName(javaType); 524 if (href == null) { 531 dSer = context.getDeserializer(javaType, defaultXMLType); 532 } else { 533 dSer = new DeserializerImpl(); 534 context.setDestinationClass(javaType); 535 dSer.setDefaultType(defaultXMLType); 536 } 537 } 538 if (javaType.equals(String .class) && 539 dSer instanceof SimpleDeserializer) { 540 cacheStringDSer = (SimpleDeserializer) dSer; 541 cacheXMLType = xmlType; 542 } 543 return dSer; 544 } 545 546 public void characters(char[] chars, int start, int end) throws SAXException { 547 val.write(chars, start, end); 548 } 549 550 public void onEndElement(String namespace, String localName, 551 DeserializationContext context) throws SAXException { 552 553 if (xmlType.toString().endsWith("GetPropertiesRs") 554 ||xmlType.toString().endsWith("SetPropertiesRs")) { 555 for (int n = 0; n < additional.length; ++n) { 556 if (!alreadyFailed[n]) { 557 BeanPropertyDescriptor propDesc = (BeanPropertyDescriptor) propertyMap.get(_addLocalNames[n]); 558 System.err.println("localName:"+_addLocalNames[n]+", propDesc:"+propDesc); 559 try { 560 propDesc.set(value, additional[n].getValue()); 561 } catch (Throwable t) { 562 t.printStackTrace(); 563 throw new SAXException (t.getMessage()); 564 } 565 } 566 } 567 568 } 569 570 handleMixedContent(); 571 } 572 573 protected void handleMixedContent() throws SAXException { 574 BeanPropertyDescriptor propDesc = getAnyPropertyDesc(); 575 if (propDesc == null || val.size() == 0) { 576 return; 577 } 578 String textValue = val.toString().trim(); 579 val.reset(); 580 if (textValue.length() == 0) { 581 return; 582 } 583 try { 584 MessageElement[] curElements = (MessageElement[]) propDesc.get(value); 585 int length = 0; 586 if (curElements != null) { 587 length = curElements.length; 588 } 589 MessageElement[] newElements = new MessageElement[length + 1]; 590 if (curElements != null) { 591 System.arraycopy(curElements, 0, 592 newElements, 0, length); 593 } 594 MessageElement thisEl = new MessageElement(new org.apache.axis.message.Text(textValue)); 595 newElements[length] = thisEl; 596 propDesc.set(value, newElements); 597 } catch (Exception e) { 598 throw new SAXException (e); 599 } 600 } 601 } 602 | Popular Tags |