1 /*
2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
145 return addChildElement(localName, prefix, uri);
146 }
147
148 public String getNamespaceURI(String prefix) {
149
150 if ("xmlns".equals(prefix)) {
151 return XMLNS_URI;
152 }
153
154 if("xml".equals(prefix)) {
155 return XML_URI;
156 }
157
158 if ("".equals(prefix)) {
159
160 org.w3c.dom.Node currentAncestor = this;
161 while (currentAncestor != null &&
162 !(currentAncestor instanceof Document)) {
163
164 if (currentAncestor instanceof ElementImpl) {
165 QName name = ((ElementImpl) currentAncestor).getElementQName();
166 /*
167 if (prefix.equals(name.getPrefix())) {
168 String uri = name.getNamespaceURI();
169 if ("".equals(uri)) {
170 return null;
171 }
172 else {
173 return uri;
174 }
175 }*/
176 if (((Element) currentAncestor).hasAttributeNS(
177 XMLNS_URI, "xmlns")) {
178
179 String uri =
180 ((Element) currentAncestor).getAttributeNS(
181 XMLNS_URI, "xmlns");
182 if ("".equals(uri))
183 return null;
184 else {
185 return uri;
186 }
322 "Cannot add fragments which contain elements "
323 + "which are in the SOAP namespace");
324 }
325
326 if ("Fault".equalsIgnoreCase(localName) && !"Body".equalsIgnoreCase(this.getLocalName())) {
327 log.severe("SAAJ0154.impl.adding.fault.to.nonbody");
328 throw new SOAPExceptionImpl("Cannot add a SOAPFault as a child of " + this.getLocalName());
329 }
330
331 if ("Detail".equalsIgnoreCase(localName) && !"Fault".equalsIgnoreCase(this.getLocalName())) {
332 log.severe("SAAJ0155.impl.adding.detail.nonfault");
333 throw new SOAPExceptionImpl("Cannot add a Detail as a child of " + this.getLocalName());
334 }
335
336 if ("Fault".equalsIgnoreCase(localName)) {
337 // if body is not empty throw an exception
338 if (!elementURI.equals(this.getElementName().getURI())) {
339 log.severe("SAAJ0158.impl.version.mismatch.fault");
340 throw new SOAPExceptionImpl("SOAP Version mismatch encountered when trying to add SOAPFault to SOAPBody");
341 }
342 Iterator it = this.getChildElements();
343 if (it.hasNext()) {
344 log.severe("SAAJ0156.impl.adding.fault.error");
345 throw new SOAPExceptionImpl("Cannot add SOAPFault as a child of a non-Empty SOAPBody");
346 }
347 }
348 }
349
350 // preserve the encodingStyle attr as it may get lost in the import
351 String encodingStyle = element.getEncodingStyle();
352
353 ElementImpl importedElement = (ElementImpl) importElement(element);
354 addNode(importedElement);
355
356 if (encodingStyle != null)
357 importedElement.setEncodingStyle(encodingStyle);
358
359 return convertToSoapElement(importedElement);
360 }
361
362 protected Element importElement(Element element) {
433 child = child.getNextSibling();
434 }
435 return null;
436 }
437
438 protected SOAPElement findChild(NameImpl name) {
439 Node eachChild = getFirstChild();
440 while (eachChild != null) {
441 if (eachChild instanceof SOAPElement) {
442 SOAPElement eachChildSoap = (SOAPElement) eachChild;
443 if (eachChildSoap.getElementName().equals(name)) {
444 return eachChildSoap;
445 }
446 }
447 eachChild = eachChild.getNextSibling();
448 }
449 return null;
450 }
451
452 protected SOAPElement findAndConvertChildElement(NameImpl name) {
453 Iterator eachChild = getChildElementNodes();
454 while (eachChild.hasNext()) {
455 SOAPElement child = (SOAPElement) eachChild.next();
456 if (child.getElementName().equals(name)) {
457 return child;
458 }
459 }
460
461 return null;
462 }
463
464 public SOAPElement addTextNode(String text) throws SOAPException {
465 if (text.startsWith(CDATAImpl.cdataUC)
466 || text.startsWith(CDATAImpl.cdataLC))
467 return addCDATA(
468 text.substring(CDATAImpl.cdataUC.length(), text.length() - 3));
469 return addText(text);
470 }
471
472 protected SOAPElement addCDATA(String text) throws SOAPException {
473 org.w3c.dom.Text cdata =
543 }
544 //Fix for CR:6474641
545 //tryToFindEncodingStyleAttributeName();
546 return this;
547 }
548
549 public String getAttributeValue(Name name) {
550 return getAttributeValueFrom(this, name);
551 }
552
553 public String getAttributeValue(QName qname) {
554 return getAttributeValueFrom(
555 this,
556 qname.getNamespaceURI(),
557 qname.getLocalPart(),
558 qname.getPrefix(),
559 getQualifiedName(qname));
560 }
561
562 public Iterator getAllAttributes() {
563 Iterator i = getAllAttributesFrom(this);
564 ArrayList list = new ArrayList();
565 while (i.hasNext()) {
566 Name name = (Name) i.next();
567 if (!"xmlns".equalsIgnoreCase(name.getPrefix()))
568 list.add(name);
569 }
570 return list.iterator();
571 }
572
573 public Iterator getAllAttributesAsQNames() {
574 Iterator i = getAllAttributesFrom(this);
575 ArrayList list = new ArrayList();
576 while (i.hasNext()) {
577 Name name = (Name) i.next();
578 if (!"xmlns".equalsIgnoreCase(name.getPrefix())) {
579 list.add(NameImpl.convertToQName(name));
580 }
581 }
582 return list.iterator();
583 }
584
585
586 public Iterator getNamespacePrefixes() {
587 return doGetNamespacePrefixes(false);
588 }
589
590 public Iterator getVisibleNamespacePrefixes() {
591 return doGetNamespacePrefixes(true);
592 }
593
594 protected Iterator doGetNamespacePrefixes(final boolean deep) {
595 return new Iterator() {
596 String next = null;
597 String last = null;
598 NamespaceContextIterator eachNamespace =
599 getNamespaceContextNodes(deep);
600
601 void findNext() {
602 while (next == null && eachNamespace.hasNext()) {
603 String attributeKey =
604 eachNamespace.nextNamespaceAttr().getNodeName();
605 if (attributeKey.startsWith("xmlns:")) {
606 next = attributeKey.substring("xmlns:".length());
607 }
608 }
609 }
610
611 public boolean hasNext() {
612 findNext();
613 return next != null;
614 }
615
616 public Object next() {
617 findNext();
618 if (next == null) {
619 throw new NoSuchElementException();
620 }
621
622 last = next;
623 next = null;
624 return last;
625 }
626
627 public void remove() {
628 if (last == null) {
629 throw new IllegalStateException();
630 }
631 eachNamespace.remove();
632 next = null;
633 last = null;
634 }
635 };
636 }
659 if (attribute == null) {
660 return false;
661 }
662 removeAttributeNode(attribute);
663 return true;
664 }
665
666 public boolean removeNamespaceDeclaration(String prefix) {
667 org.w3c.dom.Attr declaration = getNamespaceAttr(prefix);
668 if (declaration == null) {
669 return false;
670 }
671 try {
672 removeAttributeNode(declaration);
673 } catch (DOMException de) {
674 // ignore
675 }
676 return true;
677 }
678
679 public Iterator getChildElements() {
680 return getChildElementsFrom(this);
681 }
682
683 protected SOAPElement convertToSoapElement(Element element) {
684 if (element instanceof SOAPElement) {
685 return (SOAPElement) element;
686 } else {
687 return replaceElementWithSOAPElement(
688 element,
689 (ElementImpl) createElement(NameImpl.copyElementName(element)));
690 }
691 }
692
693 protected static SOAPElement replaceElementWithSOAPElement(
694 Element element,
695 ElementImpl copy) {
696
697 Iterator eachAttribute = getAllAttributesFrom(element);
698 while (eachAttribute.hasNext()) {
699 Name name = (Name) eachAttribute.next();
700 copy.addAttributeBare(name, getAttributeValueFrom(element, name));
701 }
702
703 Iterator eachChild = getChildElementsFrom(element);
704 while (eachChild.hasNext()) {
705 Node nextChild = (Node) eachChild.next();
706 copy.insertBefore(nextChild, null);
707 }
708
709 Node parent = element.getParentNode();
710 if (parent != null) {
711 parent.replaceChild(copy, element);
712 } // XXX else throw an exception?
713
714 return copy;
715 }
716
717 protected Iterator getChildElementNodes() {
718 return new Iterator() {
719 Iterator eachNode = getChildElements();
720 Node next = null;
721 Node last = null;
722
723 public boolean hasNext() {
724 if (next == null) {
725 while (eachNode.hasNext()) {
726 Node node = (Node) eachNode.next();
727 if (node instanceof SOAPElement) {
728 next = node;
729 break;
730 }
731 }
732 }
733 return next != null;
734 }
735
736 public Object next() {
737 if (hasNext()) {
738 last = next;
739 next = null;
740 return last;
741 }
742 throw new NoSuchElementException();
743 }
744
745 public void remove() {
746 if (last == null) {
747 throw new IllegalStateException();
748 }
749 Node target = last;
750 last = null;
751 removeChild(target);
752 }
753 };
754 }
755
756 public Iterator getChildElements(final Name name) {
757 return getChildElements(name.getURI(), name.getLocalName());
758 }
759
760 public Iterator getChildElements(final QName qname) {
761 return getChildElements(qname.getNamespaceURI(), qname.getLocalPart());
762 }
763
764 private Iterator getChildElements(final String nameUri, final String nameLocal) {
765 return new Iterator() {
766 Iterator eachElement = getChildElementNodes();
767 Node next = null;
768 Node last = null;
769
770 public boolean hasNext() {
771 if (next == null) {
772 while (eachElement.hasNext()) {
773 Node element = (Node) eachElement.next();
774 String elementUri = element.getNamespaceURI();
775 elementUri = elementUri == null ? "" : elementUri;
776 String elementName = element.getLocalName();
777 if (elementUri.equals(nameUri)
778 && elementName.equals(nameLocal)) {
779 next = element;
780 break;
781 }
782 }
783 }
784 return next != null;
785 }
786
787 public Object next() {
788 if (!hasNext()) {
789 throw new NoSuchElementException();
790 }
791 last = next;
792 next = null;
793 return last;
794 }
795
796 public void remove() {
797 if (last == null) {
798 throw new IllegalStateException();
799 }
800 Node target = last;
801 last = null;
802 removeChild(target);
803 }
804 };
805 }
806
807 public void removeContents() {
877 }
878 }
879 }
880
881 protected Node getValueNodeStrict() {
882 Node node = getFirstChild();
883 if (node != null) {
884 if (node.getNextSibling() == null
885 && node.getNodeType() == org.w3c.dom.Node.TEXT_NODE) {
886 return node;
887 } else {
888 log.severe("SAAJ0107.impl.elem.child.not.single.text");
889 throw new IllegalStateException();
890 }
891 }
892
893 return null;
894 }
895
896 protected javax.xml.soap.Node getValueNode() {
897 Iterator i = getChildElements();
898 while (i.hasNext()) {
899 javax.xml.soap.Node n = (javax.xml.soap.Node) i.next();
900 if (n.getNodeType() == org.w3c.dom.Node.TEXT_NODE ||
901 n.getNodeType() == org.w3c.dom.Node.CDATA_SECTION_NODE) {
902 // TODO: Hack to fix text node split into multiple lines.
903 normalize();
904 // Should remove the normalization step when this gets fixed in
905 // DOM/Xerces.
906 return (javax.xml.soap.Node) n;
907 }
908 }
909 return null;
910 }
911
912 public void setParentElement(SOAPElement element) throws SOAPException {
913 if (element == null) {
914 log.severe("SAAJ0106.impl.no.null.to.parent.elem");
915 throw new SOAPException("Cannot pass NULL to setParentElement");
916 }
917 element.addChildElement(this);
1037 }
1038 }
1039 }
1040
1041 protected static org.w3c.dom.Attr getNamespaceAttrFrom(
1042 Element element,
1043 String prefix) {
1044 NamespaceContextIterator eachNamespace =
1045 new NamespaceContextIterator(element);
1046 while (eachNamespace.hasNext()) {
1047 org.w3c.dom.Attr namespaceDecl = eachNamespace.nextNamespaceAttr();
1048 String declaredPrefix =
1049 NameImpl.getLocalNameFromTagName(namespaceDecl.getNodeName());
1050 if (declaredPrefix.equals(prefix)) {
1051 return namespaceDecl;
1052 }
1053 }
1054 return null;
1055 }
1056
1057 protected static Iterator getAllAttributesFrom(final Element element) {
1058 final NamedNodeMap attributes = element.getAttributes();
1059
1060 return new Iterator() {
1061 int attributesLength = attributes.getLength();
1062 int attributeIndex = 0;
1063 String currentName;
1064
1065 public boolean hasNext() {
1066 return attributeIndex < attributesLength;
1067 }
1068
1069 public Object next() {
1070 if (!hasNext()) {
1071 throw new NoSuchElementException();
1072 }
1073 Node current = attributes.item(attributeIndex++);
1074 currentName = current.getNodeName();
1075
1076 String prefix = NameImpl.getPrefixFromTagName(currentName);
1077 if (prefix.length() == 0) {
1078 return NameImpl.createFromUnqualifiedName(currentName);
1079 } else {
1080 Name attributeName =
1081 NameImpl.createFromQualifiedName(
1082 currentName,
1083 current.getNamespaceURI());
1084 return attributeName;
1085 }
1086 }
1087
1088 public void remove() {
1089 if (currentName == null) {
1116 boolean mustUseGetAttributeNodeNS = (nonzeroLengthUri != null);
1117
1118 if (mustUseGetAttributeNodeNS) {
1119
1120 if (!element.hasAttributeNS(uri, localName)) {
1121 return null;
1122 }
1123
1124 String attrValue =
1125 element.getAttributeNS(nonzeroLengthUri, localName);
1126
1127 return attrValue;
1128 }
1129
1130 Attr attribute = null;
1131 attribute = element.getAttributeNode(qualifiedName);
1132
1133 return attribute == null ? null : attribute.getValue();
1134 }
1135
1136 protected static Iterator getChildElementsFrom(final Element element) {
1137 return new Iterator() {
1138 Node next = element.getFirstChild();
1139 Node nextNext = null;
1140 Node last = null;
1141
1142 public boolean hasNext() {
1143 if (next != null) {
1144 return true;
1145 }
1146 if (next == null && nextNext != null) {
1147 next = nextNext;
1148 }
1149
1150 return next != null;
1151 }
1152
1153 public Object next() {
1154 if (hasNext()) {
1155 last = next;
1156 next = null;
1157
1158 if ((element instanceof ElementImpl)
1159 && (last instanceof Element)) {
1160 last =
1161 ((ElementImpl) element).convertToSoapElement(
1162 (Element) last);
1163 }
1164
1165 nextNext = last.getNextSibling();
1166 return last;
1167 }
1168 throw new NoSuchElementException();
1169 }
1170
1171 public void remove() {
1172 if (last == null) {
1173 throw new IllegalStateException();
|
1 /*
2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
145 return addChildElement(localName, prefix, uri);
146 }
147
148 public String getNamespaceURI(String prefix) {
149
150 if ("xmlns".equals(prefix)) {
151 return XMLNS_URI;
152 }
153
154 if("xml".equals(prefix)) {
155 return XML_URI;
156 }
157
158 if ("".equals(prefix)) {
159
160 org.w3c.dom.Node currentAncestor = this;
161 while (currentAncestor != null &&
162 !(currentAncestor instanceof Document)) {
163
164 if (currentAncestor instanceof ElementImpl) {
165 /*
166 QName name = ((ElementImpl) currentAncestor).getElementQName();
167 if (prefix.equals(name.getPrefix())) {
168 String uri = name.getNamespaceURI();
169 if ("".equals(uri)) {
170 return null;
171 }
172 else {
173 return uri;
174 }
175 }*/
176 if (((Element) currentAncestor).hasAttributeNS(
177 XMLNS_URI, "xmlns")) {
178
179 String uri =
180 ((Element) currentAncestor).getAttributeNS(
181 XMLNS_URI, "xmlns");
182 if ("".equals(uri))
183 return null;
184 else {
185 return uri;
186 }
322 "Cannot add fragments which contain elements "
323 + "which are in the SOAP namespace");
324 }
325
326 if ("Fault".equalsIgnoreCase(localName) && !"Body".equalsIgnoreCase(this.getLocalName())) {
327 log.severe("SAAJ0154.impl.adding.fault.to.nonbody");
328 throw new SOAPExceptionImpl("Cannot add a SOAPFault as a child of " + this.getLocalName());
329 }
330
331 if ("Detail".equalsIgnoreCase(localName) && !"Fault".equalsIgnoreCase(this.getLocalName())) {
332 log.severe("SAAJ0155.impl.adding.detail.nonfault");
333 throw new SOAPExceptionImpl("Cannot add a Detail as a child of " + this.getLocalName());
334 }
335
336 if ("Fault".equalsIgnoreCase(localName)) {
337 // if body is not empty throw an exception
338 if (!elementURI.equals(this.getElementName().getURI())) {
339 log.severe("SAAJ0158.impl.version.mismatch.fault");
340 throw new SOAPExceptionImpl("SOAP Version mismatch encountered when trying to add SOAPFault to SOAPBody");
341 }
342 Iterator<Node> it = this.getChildElements();
343 if (it.hasNext()) {
344 log.severe("SAAJ0156.impl.adding.fault.error");
345 throw new SOAPExceptionImpl("Cannot add SOAPFault as a child of a non-Empty SOAPBody");
346 }
347 }
348 }
349
350 // preserve the encodingStyle attr as it may get lost in the import
351 String encodingStyle = element.getEncodingStyle();
352
353 ElementImpl importedElement = (ElementImpl) importElement(element);
354 addNode(importedElement);
355
356 if (encodingStyle != null)
357 importedElement.setEncodingStyle(encodingStyle);
358
359 return convertToSoapElement(importedElement);
360 }
361
362 protected Element importElement(Element element) {
433 child = child.getNextSibling();
434 }
435 return null;
436 }
437
438 protected SOAPElement findChild(NameImpl name) {
439 Node eachChild = getFirstChild();
440 while (eachChild != null) {
441 if (eachChild instanceof SOAPElement) {
442 SOAPElement eachChildSoap = (SOAPElement) eachChild;
443 if (eachChildSoap.getElementName().equals(name)) {
444 return eachChildSoap;
445 }
446 }
447 eachChild = eachChild.getNextSibling();
448 }
449 return null;
450 }
451
452 protected SOAPElement findAndConvertChildElement(NameImpl name) {
453 Iterator<Node> eachChild = getChildElementNodes();
454 while (eachChild.hasNext()) {
455 SOAPElement child = (SOAPElement) eachChild.next();
456 if (child.getElementName().equals(name)) {
457 return child;
458 }
459 }
460
461 return null;
462 }
463
464 public SOAPElement addTextNode(String text) throws SOAPException {
465 if (text.startsWith(CDATAImpl.cdataUC)
466 || text.startsWith(CDATAImpl.cdataLC))
467 return addCDATA(
468 text.substring(CDATAImpl.cdataUC.length(), text.length() - 3));
469 return addText(text);
470 }
471
472 protected SOAPElement addCDATA(String text) throws SOAPException {
473 org.w3c.dom.Text cdata =
543 }
544 //Fix for CR:6474641
545 //tryToFindEncodingStyleAttributeName();
546 return this;
547 }
548
549 public String getAttributeValue(Name name) {
550 return getAttributeValueFrom(this, name);
551 }
552
553 public String getAttributeValue(QName qname) {
554 return getAttributeValueFrom(
555 this,
556 qname.getNamespaceURI(),
557 qname.getLocalPart(),
558 qname.getPrefix(),
559 getQualifiedName(qname));
560 }
561
562 public Iterator getAllAttributes() {
563 Iterator<Name> i = getAllAttributesFrom(this);
564 ArrayList<Name> list = new ArrayList<Name>();
565 while (i.hasNext()) {
566 Name name = i.next();
567 if (!"xmlns".equalsIgnoreCase(name.getPrefix()))
568 list.add(name);
569 }
570 return list.iterator();
571 }
572
573 public Iterator getAllAttributesAsQNames() {
574 Iterator<Name> i = getAllAttributesFrom(this);
575 ArrayList<QName> list = new ArrayList<QName>();
576 while (i.hasNext()) {
577 Name name = i.next();
578 if (!"xmlns".equalsIgnoreCase(name.getPrefix())) {
579 list.add(NameImpl.convertToQName(name));
580 }
581 }
582 return list.iterator();
583 }
584
585
586 public Iterator getNamespacePrefixes() {
587 return doGetNamespacePrefixes(false);
588 }
589
590 public Iterator getVisibleNamespacePrefixes() {
591 return doGetNamespacePrefixes(true);
592 }
593
594 protected Iterator<String> doGetNamespacePrefixes(final boolean deep) {
595 return new Iterator<String>() {
596 String next = null;
597 String last = null;
598 NamespaceContextIterator eachNamespace =
599 getNamespaceContextNodes(deep);
600
601 void findNext() {
602 while (next == null && eachNamespace.hasNext()) {
603 String attributeKey =
604 eachNamespace.nextNamespaceAttr().getNodeName();
605 if (attributeKey.startsWith("xmlns:")) {
606 next = attributeKey.substring("xmlns:".length());
607 }
608 }
609 }
610
611 public boolean hasNext() {
612 findNext();
613 return next != null;
614 }
615
616 public String next() {
617 findNext();
618 if (next == null) {
619 throw new NoSuchElementException();
620 }
621
622 last = next;
623 next = null;
624 return last;
625 }
626
627 public void remove() {
628 if (last == null) {
629 throw new IllegalStateException();
630 }
631 eachNamespace.remove();
632 next = null;
633 last = null;
634 }
635 };
636 }
659 if (attribute == null) {
660 return false;
661 }
662 removeAttributeNode(attribute);
663 return true;
664 }
665
666 public boolean removeNamespaceDeclaration(String prefix) {
667 org.w3c.dom.Attr declaration = getNamespaceAttr(prefix);
668 if (declaration == null) {
669 return false;
670 }
671 try {
672 removeAttributeNode(declaration);
673 } catch (DOMException de) {
674 // ignore
675 }
676 return true;
677 }
678
679 public Iterator<Node> getChildElements() {
680 return getChildElementsFrom(this);
681 }
682
683 protected SOAPElement convertToSoapElement(Element element) {
684 if (element instanceof SOAPElement) {
685 return (SOAPElement) element;
686 } else {
687 return replaceElementWithSOAPElement(
688 element,
689 (ElementImpl) createElement(NameImpl.copyElementName(element)));
690 }
691 }
692
693 protected static SOAPElement replaceElementWithSOAPElement(
694 Element element,
695 ElementImpl copy) {
696
697 Iterator<Name> eachAttribute = getAllAttributesFrom(element);
698 while (eachAttribute.hasNext()) {
699 Name name = eachAttribute.next();
700 copy.addAttributeBare(name, getAttributeValueFrom(element, name));
701 }
702
703 Iterator<Node> eachChild = getChildElementsFrom(element);
704 while (eachChild.hasNext()) {
705 Node nextChild = eachChild.next();
706 copy.insertBefore(nextChild, null);
707 }
708
709 Node parent = element.getParentNode();
710 if (parent != null) {
711 parent.replaceChild(copy, element);
712 } // XXX else throw an exception?
713
714 return copy;
715 }
716
717 protected Iterator<Node> getChildElementNodes() {
718 return new Iterator<Node>() {
719 Iterator<Node> eachNode = getChildElements();
720 Node next = null;
721 Node last = null;
722
723 public boolean hasNext() {
724 if (next == null) {
725 while (eachNode.hasNext()) {
726 Node node = eachNode.next();
727 if (node instanceof SOAPElement) {
728 next = node;
729 break;
730 }
731 }
732 }
733 return next != null;
734 }
735
736 public Node next() {
737 if (hasNext()) {
738 last = next;
739 next = null;
740 return last;
741 }
742 throw new NoSuchElementException();
743 }
744
745 public void remove() {
746 if (last == null) {
747 throw new IllegalStateException();
748 }
749 Node target = last;
750 last = null;
751 removeChild(target);
752 }
753 };
754 }
755
756 public Iterator getChildElements(final Name name) {
757 return getChildElements(name.getURI(), name.getLocalName());
758 }
759
760 public Iterator getChildElements(final QName qname) {
761 return getChildElements(qname.getNamespaceURI(), qname.getLocalPart());
762 }
763
764 private Iterator<Node> getChildElements(final String nameUri, final String nameLocal) {
765 return new Iterator<Node>() {
766 Iterator<Node> eachElement = getChildElementNodes();
767 Node next = null;
768 Node last = null;
769
770 public boolean hasNext() {
771 if (next == null) {
772 while (eachElement.hasNext()) {
773 Node element = eachElement.next();
774 String elementUri = element.getNamespaceURI();
775 elementUri = elementUri == null ? "" : elementUri;
776 String elementName = element.getLocalName();
777 if (elementUri.equals(nameUri)
778 && elementName.equals(nameLocal)) {
779 next = element;
780 break;
781 }
782 }
783 }
784 return next != null;
785 }
786
787 public Node next() {
788 if (!hasNext()) {
789 throw new NoSuchElementException();
790 }
791 last = next;
792 next = null;
793 return last;
794 }
795
796 public void remove() {
797 if (last == null) {
798 throw new IllegalStateException();
799 }
800 Node target = last;
801 last = null;
802 removeChild(target);
803 }
804 };
805 }
806
807 public void removeContents() {
877 }
878 }
879 }
880
881 protected Node getValueNodeStrict() {
882 Node node = getFirstChild();
883 if (node != null) {
884 if (node.getNextSibling() == null
885 && node.getNodeType() == org.w3c.dom.Node.TEXT_NODE) {
886 return node;
887 } else {
888 log.severe("SAAJ0107.impl.elem.child.not.single.text");
889 throw new IllegalStateException();
890 }
891 }
892
893 return null;
894 }
895
896 protected javax.xml.soap.Node getValueNode() {
897 Iterator<Node> i = getChildElements();
898 while (i.hasNext()) {
899 javax.xml.soap.Node n = (javax.xml.soap.Node) i.next();
900 if (n.getNodeType() == org.w3c.dom.Node.TEXT_NODE ||
901 n.getNodeType() == org.w3c.dom.Node.CDATA_SECTION_NODE) {
902 // TODO: Hack to fix text node split into multiple lines.
903 normalize();
904 // Should remove the normalization step when this gets fixed in
905 // DOM/Xerces.
906 return (javax.xml.soap.Node) n;
907 }
908 }
909 return null;
910 }
911
912 public void setParentElement(SOAPElement element) throws SOAPException {
913 if (element == null) {
914 log.severe("SAAJ0106.impl.no.null.to.parent.elem");
915 throw new SOAPException("Cannot pass NULL to setParentElement");
916 }
917 element.addChildElement(this);
1037 }
1038 }
1039 }
1040
1041 protected static org.w3c.dom.Attr getNamespaceAttrFrom(
1042 Element element,
1043 String prefix) {
1044 NamespaceContextIterator eachNamespace =
1045 new NamespaceContextIterator(element);
1046 while (eachNamespace.hasNext()) {
1047 org.w3c.dom.Attr namespaceDecl = eachNamespace.nextNamespaceAttr();
1048 String declaredPrefix =
1049 NameImpl.getLocalNameFromTagName(namespaceDecl.getNodeName());
1050 if (declaredPrefix.equals(prefix)) {
1051 return namespaceDecl;
1052 }
1053 }
1054 return null;
1055 }
1056
1057 protected static Iterator<Name> getAllAttributesFrom(final Element element) {
1058 final NamedNodeMap attributes = element.getAttributes();
1059
1060 return new Iterator<Name>() {
1061 int attributesLength = attributes.getLength();
1062 int attributeIndex = 0;
1063 String currentName;
1064
1065 public boolean hasNext() {
1066 return attributeIndex < attributesLength;
1067 }
1068
1069 public Name next() {
1070 if (!hasNext()) {
1071 throw new NoSuchElementException();
1072 }
1073 Node current = attributes.item(attributeIndex++);
1074 currentName = current.getNodeName();
1075
1076 String prefix = NameImpl.getPrefixFromTagName(currentName);
1077 if (prefix.length() == 0) {
1078 return NameImpl.createFromUnqualifiedName(currentName);
1079 } else {
1080 Name attributeName =
1081 NameImpl.createFromQualifiedName(
1082 currentName,
1083 current.getNamespaceURI());
1084 return attributeName;
1085 }
1086 }
1087
1088 public void remove() {
1089 if (currentName == null) {
1116 boolean mustUseGetAttributeNodeNS = (nonzeroLengthUri != null);
1117
1118 if (mustUseGetAttributeNodeNS) {
1119
1120 if (!element.hasAttributeNS(uri, localName)) {
1121 return null;
1122 }
1123
1124 String attrValue =
1125 element.getAttributeNS(nonzeroLengthUri, localName);
1126
1127 return attrValue;
1128 }
1129
1130 Attr attribute = null;
1131 attribute = element.getAttributeNode(qualifiedName);
1132
1133 return attribute == null ? null : attribute.getValue();
1134 }
1135
1136 protected static Iterator<Node> getChildElementsFrom(final Element element) {
1137 return new Iterator<Node>() {
1138 Node next = element.getFirstChild();
1139 Node nextNext = null;
1140 Node last = null;
1141
1142 public boolean hasNext() {
1143 if (next != null) {
1144 return true;
1145 }
1146 if (next == null && nextNext != null) {
1147 next = nextNext;
1148 }
1149
1150 return next != null;
1151 }
1152
1153 public Node next() {
1154 if (hasNext()) {
1155 last = next;
1156 next = null;
1157
1158 if ((element instanceof ElementImpl)
1159 && (last instanceof Element)) {
1160 last =
1161 ((ElementImpl) element).convertToSoapElement(
1162 (Element) last);
1163 }
1164
1165 nextNext = last.getNextSibling();
1166 return last;
1167 }
1168 throw new NoSuchElementException();
1169 }
1170
1171 public void remove() {
1172 if (last == null) {
1173 throw new IllegalStateException();
|