1 16 17 package org.apache.axis.message; 18 19 import javax.xml.namespace.QName ; 20 import org.apache.axis.AxisFault; 21 import org.apache.axis.Constants; 22 import org.apache.axis.SOAPPart; 23 import org.apache.axis.utils.Mapping; 24 import org.apache.axis.utils.XMLUtils; 25 import org.w3c.dom.Attr ; 26 import org.w3c.dom.CDATASection ; 27 import org.w3c.dom.Comment ; 28 import org.w3c.dom.DOMException ; 29 import org.w3c.dom.DOMImplementation ; 30 import org.w3c.dom.Document ; 31 import org.w3c.dom.DocumentFragment ; 32 import org.w3c.dom.DocumentType ; 33 import org.w3c.dom.Element ; 34 import org.w3c.dom.EntityReference ; 35 import org.w3c.dom.NamedNodeMap ; 36 import org.w3c.dom.Node ; 37 import org.w3c.dom.NodeList ; 38 import org.w3c.dom.ProcessingInstruction ; 39 40 import javax.xml.parsers.ParserConfigurationException ; 41 import javax.xml.soap.SOAPException ; 42 43 55 56 public class SOAPDocumentImpl 57 implements org.w3c.dom.Document , java.io.Serializable { 58 59 protected Document delegate = null; 61 protected SOAPPart soapPart = null; 62 63 68 public SOAPDocumentImpl(SOAPPart sp) { 69 try { 70 delegate = XMLUtils.newDocument(); 71 } catch (ParserConfigurationException e) { 72 } 74 soapPart = sp; 75 } 76 77 82 public DocumentType getDoctype() { 83 return delegate.getDoctype(); 84 } 85 86 public DOMImplementation getImplementation() { 87 return delegate.getImplementation(); 88 } 89 90 95 public Element getDocumentElement() { 96 return soapPart.getDocumentElement(); 97 } 98 99 109 110 public org.w3c.dom.Element createElement(String tagName) 111 throws DOMException { 112 int index = tagName.indexOf(":"); 113 String prefix, localname; 114 if (index < 0) { 115 prefix = ""; 116 localname = tagName; 117 } else { 118 prefix = tagName.substring(0, index); 119 localname = tagName.substring(index + 1); 120 } 121 122 try { 123 SOAPEnvelope soapenv = 124 (org.apache.axis.message.SOAPEnvelope) soapPart.getEnvelope(); 125 if (soapenv != null) { 126 if (tagName.equalsIgnoreCase(Constants.ELEM_ENVELOPE)) 127 new SOAPEnvelope(); 128 if (tagName.equalsIgnoreCase(Constants.ELEM_HEADER)) 129 return new SOAPHeader(soapenv, soapenv.getSOAPConstants()); 130 if (tagName.equalsIgnoreCase(Constants.ELEM_BODY)) 131 return new SOAPBody(soapenv, soapenv.getSOAPConstants()); 132 if (tagName.equalsIgnoreCase(Constants.ELEM_FAULT)) 133 return new SOAPEnvelope(); 134 if (tagName.equalsIgnoreCase(Constants.ELEM_FAULT_DETAIL)) 135 return new SOAPFault(new AxisFault(tagName)); 136 else { 137 return new MessageElement("", prefix, localname); 138 } 139 } else { 140 return new MessageElement("", prefix, localname); 141 } 142 143 } catch (SOAPException se) { 144 throw new DOMException (DOMException.INVALID_STATE_ERR, ""); 145 } 146 } 147 148 155 public DocumentFragment createDocumentFragment() { 156 return delegate.createDocumentFragment(); 157 } 158 165 public org.w3c.dom.Text createTextNode(String data) { 166 org.apache.axis.message.Text me = 167 new org.apache.axis.message.Text(delegate.createTextNode(data)); 168 me.setOwnerDocument(soapPart); 169 return me; 170 171 } 172 173 180 public Comment createComment(String data) { 181 return new org.apache.axis.message.CommentImpl(data); 182 } 183 184 195 public CDATASection createCDATASection(String data) throws DOMException { 196 return new CDATAImpl(data); 197 } 198 199 213 public ProcessingInstruction createProcessingInstruction( 214 String target, 215 String data) 216 throws DOMException { 217 throw new java.lang.UnsupportedOperationException ( 218 "createProcessingInstruction"); 219 } 220 221 224 public Attr createAttribute(String name) throws DOMException { 225 return delegate.createAttribute(name); 226 } 227 228 233 public EntityReference createEntityReference(String name) 234 throws DOMException { 235 throw new java.lang.UnsupportedOperationException ( 236 "createEntityReference"); 237 } 238 239 public Node importNode(Node importedNode, boolean deep) throws DOMException { 241 Node targetNode = null; 242 243 int type = importedNode.getNodeType(); 244 switch (type) { 245 case ELEMENT_NODE : 246 Element el = (Element ) importedNode; 247 if (deep) { 248 targetNode = new SOAPBodyElement(el); 249 break; 250 } 251 252 SOAPBodyElement target = new SOAPBodyElement(); 253 org.w3c.dom.NamedNodeMap attrs = el.getAttributes(); 254 for (int i = 0; i < attrs.getLength(); i++) { 255 org.w3c.dom.Node att = attrs.item(i); 256 if (att.getNamespaceURI() != null && 257 att.getPrefix() != null && 258 att.getNamespaceURI().equals(Constants.NS_URI_XMLNS) && 259 att.getPrefix().equals("xmlns")) { 260 Mapping map = new Mapping(att.getNodeValue(), att.getLocalName()); 261 target.addMapping(map); 262 } 263 if (att.getLocalName() != null) { 264 target.addAttribute(att.getPrefix(), 265 att.getNamespaceURI(), 266 att.getLocalName(), 267 att.getNodeValue()); 268 } else if (att.getNodeName() != null) { 269 target.addAttribute(att.getPrefix(), 270 att.getNamespaceURI(), 271 att.getNodeName(), 272 att.getNodeValue()); 273 } 274 } 275 276 if (el.getLocalName() == null) { 277 target.setName(el.getNodeName()); 278 } else { 279 target.setQName(new QName (el.getNamespaceURI(), el.getLocalName())); 280 } 281 targetNode = target; 282 break; 283 284 case ATTRIBUTE_NODE : 285 if (importedNode.getLocalName() == null) { 286 targetNode = createAttribute(importedNode.getNodeName()); 287 } else { 288 targetNode = createAttributeNS(importedNode.getNamespaceURI(), 289 importedNode.getLocalName()); 290 } 291 break; 292 293 case TEXT_NODE : 294 targetNode = createTextNode(importedNode.getNodeValue()); 295 break; 296 297 case CDATA_SECTION_NODE : 298 targetNode = createCDATASection(importedNode.getNodeValue()); 299 break; 300 301 case COMMENT_NODE : 302 targetNode = createComment(importedNode.getNodeValue()); 303 break; 304 305 case DOCUMENT_FRAGMENT_NODE : 306 targetNode = createDocumentFragment(); 307 if (deep) { 308 org.w3c.dom.NodeList children = importedNode.getChildNodes(); 309 for (int i = 0; i < children.getLength(); i++){ 310 targetNode.appendChild(importNode(children.item(i), true)); 311 } 312 } 313 break; 314 315 case ENTITY_REFERENCE_NODE : 316 targetNode = createEntityReference(importedNode.getNodeName()); 317 break; 318 319 case PROCESSING_INSTRUCTION_NODE : 320 ProcessingInstruction pi = (ProcessingInstruction ) importedNode; 321 targetNode = createProcessingInstruction(pi.getTarget(), pi.getData()); 322 break; 323 324 case ENTITY_NODE : 325 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, "Entity nodes are not supported."); 327 328 case NOTATION_NODE : 329 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, "Notation nodes are not supported."); 331 332 case DOCUMENT_TYPE_NODE : 333 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, "DocumentType nodes cannot be imported."); 334 335 case DOCUMENT_NODE : 336 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, "Document nodes cannot be imported."); 337 338 default : 339 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, "Node type (" + type + ") cannot be imported."); 340 } 341 342 return targetNode; 343 } 344 345 353 public Element createElementNS(String namespaceURI, String qualifiedName) 354 throws DOMException { 355 org.apache.axis.soap.SOAPConstants soapConstants = null; 356 if (Constants.URI_SOAP11_ENV.equals(namespaceURI)) { 357 soapConstants = org.apache.axis.soap.SOAPConstants.SOAP11_CONSTANTS; 358 } else if (Constants.URI_SOAP12_ENV.equals(namespaceURI)) { 359 soapConstants = org.apache.axis.soap.SOAPConstants.SOAP12_CONSTANTS; 360 } 361 362 MessageElement me = null; 364 if (soapConstants != null) { 365 if (qualifiedName.equals(Constants.ELEM_ENVELOPE)) { 366 me = new SOAPEnvelope(soapConstants); 368 } else if (qualifiedName.equals(Constants.ELEM_HEADER)) { 369 me = new SOAPHeader(null, soapConstants); 370 } else if (qualifiedName.equals(Constants.ELEM_BODY)) { 372 me = new SOAPBody(null, soapConstants); 373 } else if (qualifiedName.equals(Constants.ELEM_FAULT)) { 374 me = null; 375 } else if (qualifiedName.equals(Constants.ELEM_FAULT_DETAIL)) { 376 me = null; 378 } else { 379 throw new DOMException ( 380 DOMException.INVALID_STATE_ERR, 381 "No such Localname for SOAP URI"); 382 } 383 return null; 385 } else { 387 me = new MessageElement(namespaceURI, qualifiedName); 388 } 389 390 if (me != null) 391 me.setOwnerDocument(soapPart); 392 393 return me; 394 395 } 396 397 401 public Attr createAttributeNS(String namespaceURI, String qualifiedName) 402 throws DOMException { 403 return delegate.createAttributeNS(namespaceURI, qualifiedName); 404 } 405 406 411 public NodeList getElementsByTagNameNS( 412 String namespaceURI, 413 String localName) { 414 try { 415 NodeListImpl list = new NodeListImpl(); 416 if (soapPart != null) { 417 SOAPEnvelope soapEnv = 418 (org.apache.axis.message.SOAPEnvelope) soapPart 419 .getEnvelope(); 420 SOAPHeader header = 421 (org.apache.axis.message.SOAPHeader) soapEnv.getHeader(); 422 if (header != null) { 423 list.addNodeList(header.getElementsByTagNameNS( 424 namespaceURI, 425 localName)); 426 } 427 SOAPBody body = 428 (org.apache.axis.message.SOAPBody) soapEnv.getBody(); 429 if (body != null) { 430 list.addNodeList(body.getElementsByTagNameNS( 431 namespaceURI, 432 localName)); 433 } 434 } 435 return list; 436 } catch (SOAPException se) { 437 throw new DOMException (DOMException.INVALID_STATE_ERR, ""); 438 } 439 } 440 441 446 public NodeList getElementsByTagName(String localName) { 447 448 try { 449 NodeListImpl list = new NodeListImpl(); 450 if (soapPart != null) { 451 SOAPEnvelope soapEnv = 452 (org.apache.axis.message.SOAPEnvelope) soapPart 453 .getEnvelope(); 454 SOAPHeader header = 455 (org.apache.axis.message.SOAPHeader) soapEnv.getHeader(); 456 if (header != null) { 457 list.addNodeList(header.getElementsByTagName(localName)); 458 } 459 SOAPBody body = 460 (org.apache.axis.message.SOAPBody) soapEnv.getBody(); 461 if (body != null) { 462 list.addNodeList(body.getElementsByTagName(localName)); 463 } 464 } 465 return list; 466 } catch (SOAPException se) { 467 throw new DOMException (DOMException.INVALID_STATE_ERR, ""); 468 } 469 } 470 484 public Element getElementById(String elementId) { 485 return delegate.getElementById(elementId); 486 } 487 488 492 493 public String getNodeName() { 494 return null; 495 } 496 497 public String getNodeValue() throws DOMException { 498 throw new DOMException ( 499 DOMException.NO_DATA_ALLOWED_ERR, 500 "Cannot use TextNode.get in " + this); 501 } 502 503 public void setNodeValue(String nodeValue) throws DOMException { 504 throw new DOMException ( 505 DOMException.NO_DATA_ALLOWED_ERR, 506 "Cannot use TextNode.set in " + this); 507 } 508 509 514 public short getNodeType() { 515 return Node.DOCUMENT_NODE; 516 } 517 518 public Node getParentNode() { 519 return null; 520 } 521 522 public NodeList getChildNodes() { 523 try { 524 if (soapPart != null) { 525 NodeListImpl children = new NodeListImpl(); 526 children.addNode(soapPart.getEnvelope()); 527 return children; 528 } else { 529 return NodeListImpl.EMPTY_NODELIST; 530 } 531 } catch (SOAPException se) { 532 throw new DOMException (DOMException.INVALID_STATE_ERR, ""); 533 } 534 535 } 536 537 542 public Node getFirstChild() { 543 try { 544 if (soapPart != null) 545 return (org.apache.axis.message.SOAPEnvelope) soapPart 546 .getEnvelope(); 547 else 548 return null; 549 } catch (SOAPException se) { 550 throw new DOMException (DOMException.INVALID_STATE_ERR, ""); 551 } 552 553 } 554 555 558 public Node getLastChild() { 559 try { 560 if (soapPart != null) 561 return (org.apache.axis.message.SOAPEnvelope) soapPart 562 .getEnvelope(); 563 else 564 return null; 565 } catch (SOAPException se) { 566 throw new DOMException (DOMException.INVALID_STATE_ERR, ""); 567 } 568 569 } 570 571 public Node getPreviousSibling() { 572 return null; 573 } 574 public Node getNextSibling() { 575 576 return null; 577 } 578 579 public NamedNodeMap getAttributes() { 580 return null; 581 } 582 583 587 public Document getOwnerDocument() { 588 return null; 589 } 590 591 593 public Node insertBefore(Node newChild, Node refChild) 594 throws DOMException { 595 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, ""); 596 } 597 598 public Node replaceChild(Node newChild, Node oldChild) 599 throws DOMException { 600 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, ""); 601 } 602 603 public Node removeChild(Node oldChild) throws DOMException { 604 try { 605 Node envNode; 606 if (soapPart != null) { 607 envNode = soapPart.getEnvelope(); 608 if (envNode.equals(oldChild)) { 609 return envNode; 610 } 611 } 612 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, ""); 613 } catch (SOAPException se) { 614 throw new DOMException (DOMException.INVALID_STATE_ERR, ""); 615 } 616 } 617 618 public Node appendChild(Node newChild) throws DOMException { 619 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, ""); 620 } 621 622 public boolean hasChildNodes() { 623 try { 624 if (soapPart != null) { 625 if (soapPart.getEnvelope() != null) { 626 return true; 627 } 628 } 629 return false; 630 } catch (SOAPException se) { 631 throw new DOMException (DOMException.INVALID_STATE_ERR, ""); 632 } 633 634 } 635 636 640 public Node cloneNode(boolean deep) { 641 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, ""); 642 } 643 644 648 public void normalize() { 649 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, ""); 650 } 651 652 private String [] features = { "foo", "bar" }; 654 private String version = "version 2.0"; 655 656 public boolean isSupported(String feature, String version) { 657 if (!version.equalsIgnoreCase(version)) 658 return false; 659 else 660 return true; 661 } 662 663 public String getPrefix() { 664 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, ""); 665 } 666 public void setPrefix(String prefix) { 667 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, ""); 668 } 669 670 public String getNamespaceURI() { 671 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, ""); 672 } 673 public void setNamespaceURI(String nsURI) { 674 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, ""); 675 } 676 677 public String getLocalName() { 678 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, ""); 679 } 680 681 public boolean hasAttributes() { 682 throw new DOMException (DOMException.NOT_SUPPORTED_ERR, ""); 683 } 684 } 685 | Popular Tags |