1 55 56 package org.jboss.axis.encoding; 57 58 import org.jboss.axis.Constants; 59 import org.jboss.axis.Part; 60 import org.jboss.axis.message.EnvelopeHandler; 61 import org.jboss.axis.message.SAX2EventRecorder; 62 import org.jboss.axis.message.SAXOutputter; 63 import org.jboss.axis.message.SOAPElementAxisImpl; 64 import org.jboss.axis.message.SOAPHandler; 65 import org.jboss.axis.soap.SOAPConstants; 66 import org.jboss.axis.utils.Messages; 67 import org.jboss.logging.Logger; 68 import org.xml.sax.Attributes ; 69 import org.xml.sax.SAXException ; 70 import org.xml.sax.helpers.DefaultHandler ; 71 72 import javax.xml.namespace.QName ; 73 import java.io.StringWriter ; 74 import java.util.Enumeration ; 75 import java.util.HashSet ; 76 import java.util.Vector ; 77 78 85 86 public class DeserializerImpl extends SOAPHandler 87 implements javax.xml.rpc.encoding.Deserializer , Deserializer, Callback 88 { 89 private static Logger log = Logger.getLogger(DeserializerImpl.class.getName()); 90 91 protected Object value = null; 92 93 protected boolean isEnded = false; 95 96 protected Vector targets = null; 97 98 protected QName defaultType = null; 99 100 boolean componentsReadyFlag = false; 101 102 106 private HashSet activeDeserializers = new HashSet (); 107 108 public DeserializerImpl() 109 { 110 } 111 112 115 public String getMechanismType() 116 { 117 return Constants.AXIS_SAX; 118 } 119 120 125 public Object getValue() 126 { 127 return value; 128 } 129 130 135 public void setValue(Object value) 136 { 137 this.value = value; 138 } 139 140 147 public Object getValue(Object hint) 148 { 149 return null; 150 } 151 152 159 public void setChildValue(Object value, Object hint) throws SAXException 160 { 161 } 162 163 public void setValue(Object value, Object hint) throws SAXException 164 { 165 if (hint instanceof Deserializer) 166 { 167 activeDeserializers.remove(hint); 169 170 if (componentsReady()) 173 { 174 valueComplete(); 176 } 177 } 178 } 179 180 189 public void setDefaultType(QName qName) 190 { 191 defaultType = qName; 192 } 193 194 public QName getDefaultType() 195 { 196 return defaultType; 197 } 198 199 210 public void registerValueTarget(Target target) 211 { 212 if (targets == null) 213 targets = new Vector (); 214 215 targets.addElement(target); 216 } 217 218 223 public Vector getValueTargets() 224 { 225 return targets; 226 } 227 228 231 public void removeValueTargets() 232 { 233 if (targets != null) 234 { 235 targets.clear(); 236 targets = null; 237 } 238 } 239 240 250 public void moveValueTargets(Deserializer other) 251 { 252 if ((other == null) || (other.getValueTargets() == null)) 253 return; 254 255 if (targets == null) 256 targets = new Vector (); 257 258 Enumeration e = other.getValueTargets().elements(); 259 while (e.hasMoreElements()) 260 { 261 targets.addElement(e.nextElement()); 262 } 263 other.removeValueTargets(); 264 } 265 266 278 public boolean componentsReady() 279 { 280 return (componentsReadyFlag || 281 (!isHref && isEnded && activeDeserializers.isEmpty())); 282 } 283 284 295 public void valueComplete() throws SAXException 296 { 297 if (componentsReady()) 298 { 299 if (targets != null) 300 { 301 Enumeration e = targets.elements(); 302 while (e.hasMoreElements()) 303 { 304 Target target = (Target)e.nextElement(); 305 target.set(value); 306 if (log.isDebugEnabled()) 307 { 308 log.debug(Messages.getMessage("setValueInTarget00", 309 "" + value, "" + target)); 310 } 311 } 312 removeValueTargets(); 314 } 315 } 316 } 317 318 public void addChildDeserializer(Deserializer dSer) 319 { 320 activeDeserializers.add(dSer); 324 325 dSer.registerValueTarget(new CallbackTarget(this, dSer)); 328 } 329 330 protected boolean isHref = false; 331 protected boolean isNil = false; protected String id = null; 334 337 338 375 public void startElement(String namespace, String localName, 376 String prefix, Attributes attributes, 377 DeserializationContext context) 378 throws SAXException 379 { 380 super.startElement(namespace, localName, prefix, attributes, context); 381 382 if (context.isNil(attributes)) 385 { 386 value = null; 387 isNil = true; 388 return; 389 } 390 391 SOAPConstants soapConstants = context.getMessageContext().getSOAPConstants(); 392 393 id = attributes.getValue("id"); 401 if (id != null) 402 { 403 context.addObjectById(id, value); 404 if (log.isDebugEnabled()) 405 { 406 log.debug(Messages.getMessage("deserInitPutValueDebug00", "" + value, id)); 407 } 408 context.registerFixup("#" + id, this); 409 } 410 411 String href = attributes.getValue(soapConstants.getAttrHref()); 412 if (href != null) 413 { 414 isHref = true; 415 416 Object ref = context.getObjectByRef(href); 417 if (log.isDebugEnabled()) 418 { 419 log.debug(Messages.getMessage("gotForID00", 420 new String []{"" + ref, href, (ref == null ? "*null*" : ref.getClass().toString())})); 421 } 422 423 if (ref == null) 424 { 425 context.registerFixup(href, this); 427 return; 428 } 429 430 if (ref instanceof SOAPElementAxisImpl) 431 { 432 context.replaceElementHandler(new EnvelopeHandler(this)); 433 434 SAX2EventRecorder r = context.getRecorder(); 435 context.setRecorder(null); 436 ((SOAPElementAxisImpl)ref).publishToHandler((DefaultHandler )context); 437 context.setRecorder(r); 438 } 439 else 440 { 441 442 if (!href.startsWith("#") && defaultType != null && ref instanceof Part) 443 { 444 Deserializer dser = context.getDeserializerForType(defaultType); 446 if (null != dser) 447 { 448 dser.startElement(namespace, localName, 449 prefix, attributes, 450 context); 451 ref = dser.getValue(); 452 } 453 } 454 455 value = ref; 458 componentsReadyFlag = true; 459 valueComplete(); 460 } 461 462 } 463 else 464 { 465 isHref = false; 466 onStartElement(namespace, localName, prefix, attributes, 467 context); 468 } 469 } 470 471 483 public void onStartElement(String namespace, String localName, 484 String prefix, Attributes attributes, 485 DeserializationContext context) 486 throws SAXException 487 { 488 if (this.getClass().equals(DeserializerImpl.class)) 491 { 492 QName type = context.getTypeFromAttributes(namespace, 493 localName, 494 attributes); 495 496 if (type == null) 499 { 500 type = defaultType; 501 if (type == null) 502 { 503 type = Constants.XSD_STRING; 504 } 505 } 506 507 if (log.isDebugEnabled()) 508 { 509 log.debug(Messages.getMessage("gotType00", "Deser", "" + type)); 510 } 511 512 if (type != null) 516 { 517 Deserializer dser = context.getDeserializerForType(type); 518 if (dser != null) 519 { 520 dser.moveValueTargets(this); 522 context.replaceElementHandler((SOAPHandler)dser); 523 boolean isRef = context.isProcessingRef(); 525 context.setProcessingRef(true); 526 dser.startElement(namespace, localName, prefix, 527 attributes, context); 528 context.setProcessingRef(isRef); 529 } 530 else 531 { 532 throw new SAXException (Messages.getMessage("noDeser00", "" + type)); 533 } 534 } 535 } 536 } 537 538 554 public SOAPHandler onStartChild(String namespace, String localName, 555 String prefix, Attributes attributes, 556 DeserializationContext context) 557 throws SAXException 558 { 559 return null; 560 } 561 562 563 573 public final void endElement(String namespace, String localName, 574 DeserializationContext context) 575 throws SAXException 576 { 577 super.endElement(namespace, localName, context); 578 579 isEnded = true; 580 if (!isHref) 581 { 582 onEndElement(namespace, localName, context); 583 } 584 585 if (componentsReady()) 589 { 590 valueComplete(); 591 } 592 593 if (id != null) 597 { 598 context.addObjectById(id, value); 599 if (log.isDebugEnabled()) 600 { 601 log.debug(Messages.getMessage("deserPutValueDebug00", "" + value, id)); 602 } 603 } 604 } 605 606 614 public void onEndElement(String namespace, String localName, 615 DeserializationContext context) 616 throws SAXException 617 { 618 623 if (this.getClass().equals(DeserializerImpl.class) && 624 targets != null && 625 !targets.isEmpty()) 626 { 627 628 StringWriter writer = new StringWriter (); 629 SerializationContextImpl serContext = 630 new SerializationContextImpl(writer, 631 context.getMessageContext()); 632 serContext.setSendDecl(false); 633 634 SAXOutputter so = null; 635 so = new SAXOutputter(serContext); 636 context.getCurElement().publishContents(so); 637 if (!isNil) 638 { 639 value = writer.getBuffer().toString(); 640 } 641 } 642 } 643 644 } 645 | Popular Tags |