1 16 17 package org.apache.axis.encoding; 18 19 import org.apache.axis.Constants; 20 import org.apache.axis.Part; 21 import org.apache.axis.components.logger.LogFactory; 22 import org.apache.axis.message.EnvelopeHandler; 23 import org.apache.axis.message.MessageElement; 24 import org.apache.axis.message.SAX2EventRecorder; 25 import org.apache.axis.message.SAXOutputter; 26 import org.apache.axis.message.SOAPHandler; 27 import org.apache.axis.utils.Messages; 28 import org.apache.axis.soap.SOAPConstants; 29 import org.apache.commons.logging.Log; 30 import org.xml.sax.Attributes ; 31 import org.xml.sax.SAXException ; 32 import org.xml.sax.helpers.DefaultHandler ; 33 34 import javax.xml.namespace.QName ; 35 import java.io.StringWriter ; 36 import java.util.HashSet ; 37 import java.util.Vector ; 38 39 45 46 public class DeserializerImpl extends SOAPHandler 47 implements javax.xml.rpc.encoding.Deserializer , Deserializer, Callback 48 { 49 protected static Log log = 50 LogFactory.getLog(DeserializerImpl.class.getName()); 51 52 protected Object value = null; 53 54 private final boolean debugEnabled = log.isDebugEnabled(); 58 59 protected boolean isEnded = false; 61 62 protected Vector targets = null; 63 64 protected QName defaultType = null; 65 66 protected boolean componentsReadyFlag = false; 67 68 72 private HashSet activeDeserializers = new HashSet (); 73 74 protected boolean isHref = false; 75 protected boolean isNil = false; protected String id = null; 78 public DeserializerImpl() { 79 } 80 81 84 public String getMechanismType() { 85 return Constants.AXIS_SAX; 86 } 87 88 92 public Object getValue() 93 { 94 return value; 95 } 96 100 public void setValue(Object value) 101 { 102 this.value = value; 103 } 104 105 111 public Object getValue(Object hint) 112 { 113 return null; 114 } 115 116 122 public void setChildValue(Object value, Object hint) throws SAXException 123 { 124 } 125 126 public void setValue(Object value, Object hint) throws SAXException { 127 if (hint instanceof Deserializer) { 128 activeDeserializers.remove(hint); 130 131 if (componentsReady()) { 134 valueComplete(); 136 } 137 } 138 } 139 140 149 public void setDefaultType(QName qName) { 150 defaultType = qName; 151 } 152 public QName getDefaultType() { 153 return defaultType; 154 } 155 156 166 public void registerValueTarget(Target target) 167 { 168 if (targets == null) { 169 targets = new Vector (); 170 } 171 172 targets.addElement(target); 173 } 174 175 179 public Vector getValueTargets() { 180 return targets; 181 } 182 183 186 public void removeValueTargets() { 187 if (targets != null) { 188 targets = null; 189 } 190 } 191 192 201 public void moveValueTargets(Deserializer other) 202 { 203 if ((other == null) || (other.getValueTargets() == null)) { 204 return; 205 } 206 207 if (targets == null) { 208 targets = new Vector (); 209 } 210 211 targets.addAll(other.getValueTargets()); 212 other.removeValueTargets(); 213 } 214 215 227 public boolean componentsReady() { 228 return (componentsReadyFlag || 229 (!isHref && isEnded && activeDeserializers.isEmpty())); 230 } 231 232 243 public void valueComplete() throws SAXException 244 { 245 if (componentsReady()) { 246 if (targets != null) { 247 for (int i = 0; i < targets.size(); i++) { 248 Target target = (Target) targets.get(i); 249 target.set(value); 250 if (debugEnabled) { 251 log.debug(Messages.getMessage("setValueInTarget00", 252 "" + value, "" + target)); 253 } 254 } 255 removeValueTargets(); 257 } 258 } 259 } 260 261 public void addChildDeserializer(Deserializer dSer) { 262 if (activeDeserializers != null) { 266 activeDeserializers.add(dSer); 267 } 268 dSer.registerValueTarget(new CallbackTarget(this, dSer)); 271 } 272 273 274 277 278 314 public void startElement(String namespace, String localName, 315 String prefix, Attributes attributes, 316 DeserializationContext context) 317 throws SAXException 318 { 319 super.startElement(namespace, localName, prefix, attributes, context); 320 321 if (context.isNil(attributes)) { 324 value = null; 325 isNil = true; 326 return; 327 } 328 329 SOAPConstants soapConstants = context.getSOAPConstants(); 330 331 id = attributes.getValue("id"); 339 if (id != null) { 340 context.addObjectById(id, value); 341 if (debugEnabled) { 342 log.debug(Messages.getMessage("deserInitPutValueDebug00", "" + value, id)); 343 } 344 context.registerFixup("#" + id, this); 345 } 346 347 String href = attributes.getValue(soapConstants.getAttrHref()); 348 if (href != null) { 349 isHref = true; 350 351 Object ref = context.getObjectByRef(href); 352 if (debugEnabled) { 353 log.debug(Messages.getMessage( 354 "gotForID00", 355 new String [] {"" + ref, href, (ref == null ? "*null*" : ref.getClass().toString())})); 356 } 357 358 if (ref == null) { 359 context.registerFixup(href, this); 361 return; 362 } 363 364 if (ref instanceof MessageElement) { 365 context.replaceElementHandler(new EnvelopeHandler(this)); 366 367 SAX2EventRecorder r = context.getRecorder(); 368 context.setRecorder(null); 369 ((MessageElement)ref).publishToHandler((DefaultHandler ) context); 370 context.setRecorder(r); 371 } else { 372 373 if( !href.startsWith("#") && defaultType != null && ref instanceof Part ){ 374 Deserializer dser = context.getDeserializerForType(defaultType ); 376 if(null != dser){ 377 dser.startElement(namespace, localName, 378 prefix, attributes, 379 context); 380 ref = dser.getValue(); 381 } 382 } 383 384 value = ref; 387 componentsReadyFlag = true; 388 valueComplete(); 389 } 390 391 } else { 392 isHref = false; 393 onStartElement(namespace, localName, prefix, attributes, 394 context); 395 } 396 } 397 398 409 public void onStartElement(String namespace, String localName, 410 String prefix, Attributes attributes, 411 DeserializationContext context) 412 throws SAXException 413 { 414 if (this.getClass().equals(DeserializerImpl.class)) { 417 QName type = context.getTypeFromAttributes(namespace, 418 localName, 419 attributes); 420 421 if (type == null) { 424 type = defaultType; 425 if (type == null) { 426 type = Constants.XSD_STRING; 427 } 428 } 429 430 if (debugEnabled) { 431 log.debug(Messages.getMessage("gotType00", "Deser", "" + type)); 432 } 433 434 if (type != null) { 438 Deserializer dser = context.getDeserializerForType(type); 439 if (dser == null) { 440 dser = context.getDeserializerForClass(null); 441 } 442 if (dser != null) { 443 dser.moveValueTargets(this); 445 context.replaceElementHandler((SOAPHandler) dser); 446 boolean isRef = context.isProcessingRef(); 448 context.setProcessingRef(true); 449 dser.startElement(namespace, localName, prefix, 450 attributes, context); 451 context.setProcessingRef(isRef); 452 } else { 453 throw new SAXException ( 454 Messages.getMessage("noDeser00", "" + type)); 455 } 456 } 457 } 458 } 459 460 475 public SOAPHandler onStartChild(String namespace, String localName, 476 String prefix, Attributes attributes, 477 DeserializationContext context) 478 throws SAXException 479 { 480 return null; 481 } 482 483 484 485 494 public final void endElement(String namespace, String localName, 495 DeserializationContext context) 496 throws SAXException 497 { 498 super.endElement(namespace, localName, context); 499 500 isEnded = true; 501 if (!isHref) { 502 onEndElement(namespace, localName, context); 503 } 504 505 if (componentsReady()) { 509 valueComplete(); 510 } 511 512 if (id != null) { 516 context.addObjectById(id, value); 517 if (debugEnabled) { 518 log.debug(Messages.getMessage("deserPutValueDebug00", "" + value, id)); 519 } 520 } 521 } 522 523 530 public void onEndElement(String namespace, String localName, 531 DeserializationContext context) 532 throws SAXException 533 { 534 539 if (this.getClass().equals(DeserializerImpl.class) && 540 targets != null && 541 !targets.isEmpty()) { 542 StringWriter writer = new StringWriter (); 543 SerializationContext serContext = 544 new SerializationContext(writer, 545 context.getMessageContext()); 546 serContext.setSendDecl(false); 547 548 SAXOutputter so = null; 549 so = new SAXOutputter(serContext); 550 context.getCurElement().publishContents(so); 551 if (!isNil) { 552 value = writer.getBuffer().toString(); 553 } 554 } 555 } 556 557 } 558 | Popular Tags |