1 16 package org.apache.axis2.saaj; 17 18 import org.w3c.dom.Attr ; 19 import org.w3c.dom.DOMException ; 20 import org.w3c.dom.NodeList ; 21 22 import javax.xml.namespace.QName ; 23 import javax.xml.soap.Name ; 24 import javax.xml.soap.SOAPElement ; 25 import javax.xml.soap.SOAPException ; 26 import java.util.ArrayList ; 27 import java.util.Iterator ; 28 34 public class SOAPElementImpl extends NodeImpl implements SOAPElement { 35 40 protected org.apache.axis2.om.OMElement omElement; 41 42 47 public SOAPElementImpl(org.apache.axis2.om.OMElement omElement) 48 { 49 super(omElement); 50 this.omElement = omElement; 51 } 52 53 57 public SOAPElementImpl() { 58 super(); 59 } 60 61 66 public org.apache.axis2.om.OMElement getOMElement() { 67 return this.omElement; 68 } 69 70 77 public SOAPElement addChildElement(Name name) throws SOAPException { 78 82 org.apache.axis2.om.OMElement newOMElement = org.apache.axis2.om.OMAbstractFactory.getOMFactory().createOMElement(new QName (name.getURI(), name.getLocalName(), name.getPrefix()), omElement); 85 omElement.addChild(newOMElement); 86 return new SOAPElementImpl(newOMElement); 87 } 88 89 96 public SOAPElement addChildElement(String localName) throws SOAPException { 97 org.apache.axis2.om.OMElement newOMElement = org.apache.axis2.om.OMAbstractFactory.getOMFactory().createOMElement(new QName (localName), omElement); 101 omElement.addChild(newOMElement); 102 return new SOAPElementImpl(newOMElement); 103 } 104 105 113 public SOAPElement addChildElement(String localName, String prefix) 114 throws SOAPException { 115 org.apache.axis2.om.OMElement newOMElement = org.apache.axis2.om.OMAbstractFactory.getOMFactory().createOMElement(new QName (null,localName,prefix), omElement); 116 omElement.addChild(newOMElement); 117 return new SOAPElementImpl(newOMElement); 118 } 119 120 128 public SOAPElement addChildElement(String localName, String prefix, 129 String uri) throws SOAPException { 130 org.apache.axis2.om.OMElement newOMElement = org.apache.axis2.om.OMAbstractFactory.getOMFactory().createOMElement(new QName (uri,localName,prefix), omElement); 131 omElement.addChild(newOMElement); 132 return new SOAPElementImpl(newOMElement); 133 } 134 135 142 public SOAPElement addChildElement(SOAPElement element) 143 throws SOAPException { 144 150 org.apache.axis2.om.OMElement omElementToAdd = ((SOAPElementImpl)element).getOMElement(); 151 omElementToAdd.setParent(omElement); 152 omElement.addChild(omElementToAdd); 153 return new SOAPElementImpl(omElementToAdd); 154 } 155 156 163 public SOAPElement addTextNode(String text) throws SOAPException { 164 omElement.setText(text); 167 return this; 168 } 169 170 179 public SOAPElement addAttribute(Name name, String value) 180 throws SOAPException { 181 org.apache.axis2.om.OMNamespace omNS = org.apache.axis2.om.OMAbstractFactory.getOMFactory().createOMNamespace(name.getURI(), name.getPrefix()); 182 omElement.addAttribute(name.getLocalName(), value, omNS); 186 return this; 187 } 188 189 197 public SOAPElement addNamespaceDeclaration(String prefix, String uri) 198 throws SOAPException { 199 omElement.declareNamespace(uri, prefix); 200 return this; 201 } 202 203 209 public String getAttributeValue(Name name) { 210 return omElement.getFirstAttribute( new QName (name.getURI(),name.getLocalName(),name.getPrefix())).getValue(); 213 } 214 215 220 public Iterator getAllAttributes() { 221 Iterator attrIter = omElement.getAttributes(); 222 ArrayList arrayList = new ArrayList (); 223 while (attrIter.hasNext()) { 224 Object o = attrIter.next(); 225 if (o instanceof org.apache.axis2.om.OMAttribute) { 226 javax.xml.soap.Node soapNode = new NodeImpl((org.apache.axis2.om.OMAttribute)o); 228 arrayList.add(soapNode); 229 } 230 } 231 return arrayList.iterator(); 232 } 233 234 240 public String getNamespaceURI(String prefix) { 241 Iterator nsIter = omElement.getAllDeclaredNamespaces(); 244 245 while(nsIter.hasNext()) { 247 Object o = nsIter.next(); 248 if (o instanceof org.apache.axis2.om.OMNamespace) { 249 org.apache.axis2.om.OMNamespace ns = (org.apache.axis2.om.OMNamespace)o; 250 if( ns.getPrefix().equalsIgnoreCase(prefix)) 251 return ns.getName(); 252 } 253 } 254 return null; 255 } 256 257 263 public Iterator getNamespacePrefixes() { 264 ArrayList prefixList = new ArrayList (); 266 Iterator nsIter = omElement.getAllDeclaredNamespaces(); 267 while(nsIter.hasNext()) { 268 Object o = nsIter.next(); 269 if (o instanceof org.apache.axis2.om.OMNamespace) { 270 org.apache.axis2.om.OMNamespace ns = (org.apache.axis2.om.OMNamespace)o; 271 prefixList.add(ns.getPrefix()); 272 } 273 } 274 return prefixList.iterator(); 275 } 276 277 282 public Name getElementName() { 283 QName qName = omElement.getQName(); 284 return (Name )(new PrefixedQName(qName.getNamespaceURI(), qName.getLocalPart(), qName.getPrefix())); 285 } 286 287 295 public boolean removeAttribute(Name name) { 296 org.apache.axis2.om.OMAttribute attr = omElement.getFirstAttribute(new QName (name.getURI(),name.getLocalName(),name.getPrefix())); 299 if (attr != null) { 300 omElement.removeAttribute(attr); 301 return true; 302 } 303 return false; 304 } 305 306 312 public boolean removeNamespaceDeclaration(String prefix) { 313 return false; 316 } 317 318 323 public Iterator getChildElements() { 324 Iterator childIter = omElement.getChildren(); 328 ArrayList arrayList = new ArrayList (); 329 while(childIter.hasNext()) { 330 Object o = childIter.next(); 331 if (o instanceof org.apache.axis2.om.OMNode) { 332 if(o instanceof org.apache.axis2.om.OMText){ 333 javax.xml.soap.Text childText = new TextImpl(((org.apache.axis2.om.OMText)o).getText()); 334 arrayList.add(childText); 335 }else{ 336 SOAPElement childElement = new SOAPElementImpl((org.apache.axis2.om.OMElement)o); 337 arrayList.add(childElement); 338 } 339 341 } 342 } 343 return arrayList.iterator(); 344 } 345 346 352 public Iterator getChildElements(Name name) { 353 QName qName = new QName (name.getURI(),name.getLocalName()); 354 Iterator childIter = omElement.getChildrenWithName(qName); 355 ArrayList arrayList = new ArrayList (); 356 while(childIter.hasNext()) { 357 Object o = childIter.next(); 358 if (o instanceof org.apache.axis2.om.OMNode) { 359 SOAPElement childElement = new SOAPElementImpl((org.apache.axis2.om.OMElement)o); 360 arrayList.add(childElement); 361 } 362 } 363 return arrayList.iterator(); 364 } 365 366 371 public void setEncodingStyle(String encodingStyle) throws SOAPException { 372 373 378 } 379 380 385 public String getEncodingStyle() { 386 return null; 389 } 390 391 395 public void removeContents() { 396 Iterator childIter = omElement.getChildren(); 398 399 while(childIter.hasNext()) { 400 Object o = childIter.next(); 401 if(o instanceof org.apache.axis2.om.OMNode) 402 ((org.apache.axis2.om.OMNode)o).detach(); 403 } 404 } 405 406 411 public Iterator getVisibleNamespacePrefixes() { 412 Iterator namespacesIter = omElement.getAllDeclaredNamespaces(); 414 ArrayList returnList = new ArrayList (); 415 while(namespacesIter.hasNext()) { 416 Object o = namespacesIter.next(); 417 if (o instanceof org.apache.axis2.om.OMNamespace) { 418 javax.xml.soap.Node soapNode = new NodeImpl((org.apache.axis2.om.OMNamespace)o); 419 returnList.add(soapNode); 420 } 421 } org.apache.axis2.om.OMContainer parent = omElement.getParent(); 425 if (parent!=null && parent instanceof org.apache.axis2.om.OMElement) { 426 Iterator parentScopeNamespacesIter = ((org.apache.axis2.om.OMElement)parent).getAllDeclaredNamespaces(); 427 while(parentScopeNamespacesIter.hasNext()) { 428 Object o = parentScopeNamespacesIter.next(); 429 if (o instanceof org.apache.axis2.om.OMNamespace) { 430 javax.xml.soap.Node soapNode = new NodeImpl((org.apache.axis2.om.OMNamespace)o); 431 returnList.add(soapNode); 432 } 433 } 434 } 435 return returnList.iterator(); 436 } 437 438 443 public String getTagName() { 444 return this.getLocalName(); 445 } 446 447 452 public void removeAttribute(String localName) throws DOMException { 453 Name elementQualifiedName = this.getElementName(); 455 this.removeAttribute(new PrefixedQName(elementQualifiedName.getURI(), localName, elementQualifiedName.getPrefix())); 457 } 458 459 467 public boolean hasAttribute(String localName) { 468 Iterator attrIter = omElement.getAttributes(); 469 while(attrIter.hasNext()) { 470 org.apache.axis2.om.OMAttribute omAttr = (org.apache.axis2.om.OMAttribute)(attrIter.next()); 471 if (omAttr.getLocalName().equals(localName)) { 472 return true; 473 } 474 } 475 return false; 476 } 477 478 487 public String getAttribute(String name) { 488 Iterator attrIter = omElement.getAttributes(); 489 while(attrIter.hasNext()) { 490 org.apache.axis2.om.OMAttribute omAttr = (org.apache.axis2.om.OMAttribute)(attrIter.next()); 491 if (omAttr.getLocalName().equals(name)) { 492 return omAttr.getValue(); 493 } 494 } 495 return null; 496 } 497 498 501 public void removeAttributeNS(String namespaceURI, String localName) throws DOMException { 502 Name name = new PrefixedQName(namespaceURI, localName, null); 503 this.removeAttribute(name); 504 } 505 506 516 public void setAttribute(String localName, String value) throws DOMException { 517 org.apache.axis2.om.OMAttribute omAttr = org.apache.axis2.om.OMAbstractFactory.getOMFactory().createOMAttribute(localName, omElement.getNamespace(), value); 520 omElement.addAttribute(omAttr); 521 } 522 523 533 public boolean hasAttributeNS(String namespaceURI, String localName) { 534 Iterator attrIter = omElement.getAttributes(); 535 while(attrIter.hasNext()) { 536 org.apache.axis2.om.OMAttribute omAttr = (org.apache.axis2.om.OMAttribute)(attrIter.next()); 537 if (omAttr.getLocalName().equals(localName) && omAttr.getNamespace().getName().equals(namespaceURI)) { 538 return true; 539 } 540 } 541 return false; 542 } 543 544 551 public Attr getAttributeNode(String localName) { 552 Iterator attrIter = omElement.getAttributes(); 553 while(attrIter.hasNext()) { 554 org.apache.axis2.om.OMAttribute omAttr = (org.apache.axis2.om.OMAttribute)(attrIter.next()); 555 if (omAttr.getLocalName().equals(localName)) { 556 return (new org.apache.axis2.saaj.AttrImpl(omAttr, this)); 559 } 560 } 561 return null; 562 } 563 564 573 public Attr removeAttributeNode(Attr oldAttr) throws DOMException { 574 org.apache.axis2.om.OMNamespace oldAttrNS = org.apache.axis2.om.OMAbstractFactory.getOMFactory().createOMNamespace(oldAttr.getNamespaceURI(),oldAttr.getPrefix()); 578 org.apache.axis2.om.OMAttribute omAttr = org.apache.axis2.om.OMAbstractFactory.getOMFactory().createOMAttribute(oldAttr.getName(), oldAttrNS, oldAttr.getValue()); 579 omElement.removeAttribute( omAttr ); 580 return ( new org.apache.axis2.saaj.AttrImpl(omAttr, this)); 581 } 582 583 591 public Attr setAttributeNode(Attr attr) throws DOMException { 592 org.apache.axis2.om.OMAttribute omAttr = org.apache.axis2.om.OMAbstractFactory.getOMFactory().createOMAttribute(attr.getName(), omElement.getNamespace(), attr.getValue()); 595 omElement.addAttribute(omAttr); 596 return (new org.apache.axis2.saaj.AttrImpl(omAttr, this)); 597 } 598 599 607 public Attr setAttributeNodeNS(Attr attr) throws DOMException { 608 org.apache.axis2.om.OMNamespace attrNS = org.apache.axis2.om.OMAbstractFactory.getOMFactory().createOMNamespace(attr.getNamespaceURI(), attr.getPrefix()); 609 org.apache.axis2.om.OMAttribute omAttr = org.apache.axis2.om.OMAbstractFactory.getOMFactory().createOMAttribute(attr.getName(), attrNS, attr.getValue()); 610 omElement.addAttribute(omAttr); 611 return (new org.apache.axis2.saaj.AttrImpl(omAttr, this)); 612 } 613 614 625 public NodeList getElementsByTagName(String localName) { 626 Iterator childIter = this.getChildElements(); 627 NodeListImpl returnList; 628 if (childIter==null) 629 return null; 630 else { 631 returnList = new NodeListImpl(); 632 while(childIter.hasNext()) { 633 NodeList list = getElementsByTagNamePreOrder((SOAPElement )childIter.next(), localName); 634 returnList.addNodeList(list); 636 } 637 } 638 return (NodeList )returnList; 639 } 640 641 private NodeList getElementsByTagNamePreOrder(SOAPElement child, String localName) { 642 NodeListImpl returnList = new NodeListImpl(); 643 if(child.getLocalName().equals(localName)) { 645 returnList.addNode((org.w3c.dom.Node )child); 647 } 648 returnList.addNodeList(child.getElementsByTagName(localName)); 649 return returnList; 650 } 651 652 660 public String getAttributeNS(String namespaceURI, String localName) { 661 Iterator attrIter = omElement.getAttributes(); 662 while(attrIter.hasNext()) { 663 org.apache.axis2.om.OMAttribute omAttr = (org.apache.axis2.om.OMAttribute)(attrIter.next()); 664 if (omAttr.getLocalName().equals(localName) && omAttr.getNamespace().getName().equals(namespaceURI)) { 665 return omAttr.getValue(); 666 } 667 } 668 return null; 669 } 670 671 680 public void setAttributeNS(String namespaceURI, String localName, String value) 681 throws DOMException { 682 org.apache.axis2.om.OMNamespace attrNS = org.apache.axis2.om.OMAbstractFactory.getOMFactory().createOMNamespace(namespaceURI,null); 684 org.apache.axis2.om.OMAttribute omAttr = org.apache.axis2.om.OMAbstractFactory.getOMFactory().createOMAttribute(localName, attrNS, value); 685 omElement.addAttribute( omAttr ); 686 } 687 688 696 public Attr getAttributeNodeNS(String namespaceURI, String localName) { 697 Iterator attrIter = omElement.getAttributes(); 698 while(attrIter.hasNext()) { 699 org.apache.axis2.om.OMAttribute omAttr = (org.apache.axis2.om.OMAttribute)(attrIter.next()); 700 if (omAttr.getLocalName().equals(localName) && omAttr.getNamespace().getName().equals(namespaceURI)) { 701 return (new org.apache.axis2.saaj.AttrImpl(omAttr, this)); 702 } 703 } 704 return null; 705 } 706 707 719 public NodeList getElementsByTagNameNS(String namespaceURI, String localName) { 720 Iterator childIter = this.getChildElements(); 721 NodeListImpl returnList; 722 if (childIter==null) 723 return null; 724 else { 725 returnList = new NodeListImpl(); 726 while(childIter.hasNext()) { 727 NodeList list = getElementsByTagNameNSPreOrder((SOAPElement )childIter.next(), namespaceURI, localName); 728 returnList.addNodeList(list); 730 } 731 } 732 return (NodeList )returnList; 733 } 734 735 private NodeList getElementsByTagNameNSPreOrder(SOAPElement child, String namespaceURI, String localName) { 736 NodeListImpl returnList = new NodeListImpl(); 737 if(child.getNamespaceURI().equals(namespaceURI) && child.getLocalName().equals(localName)) { 739 returnList.addNode((org.w3c.dom.Node )child); 741 } 742 returnList.addNodeList(child.getElementsByTagNameNS(namespaceURI, localName)); 743 return returnList; 744 } 745 746 } 747 | Popular Tags |