--- old/src/java.xml.ws/share/classes/com/sun/xml/internal/messaging/saaj/soap/impl/ElementImpl.java 2017-05-03 18:05:33.275095500 +0300 +++ new/src/java.xml.ws/share/classes/com/sun/xml/internal/messaging/saaj/soap/impl/ElementImpl.java 2017-05-03 18:05:33.167095503 +0300 @@ -27,21 +27,37 @@ import java.net.URI; import java.net.URISyntaxException; -import java.util.*; import java.util.logging.Level; import java.util.logging.Logger; import javax.xml.namespace.QName; -import javax.xml.soap.*; -import org.w3c.dom.*; import org.w3c.dom.Node; import com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl; import com.sun.xml.internal.messaging.saaj.soap.SOAPDocument; import com.sun.xml.internal.messaging.saaj.soap.SOAPDocumentImpl; import com.sun.xml.internal.messaging.saaj.soap.name.NameImpl; -import com.sun.xml.internal.messaging.saaj.util.*; +import com.sun.xml.internal.messaging.saaj.util.LogDomainConstants; +import com.sun.xml.internal.messaging.saaj.util.NamespaceContextIterator; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.NoSuchElementException; +import javax.xml.soap.Name; +import javax.xml.soap.SOAPBodyElement; +import javax.xml.soap.SOAPConstants; +import javax.xml.soap.SOAPElement; +import javax.xml.soap.SOAPException; +import org.w3c.dom.Attr; +import org.w3c.dom.CharacterData; +import org.w3c.dom.DOMException; +import org.w3c.dom.Document; +import org.w3c.dom.DocumentFragment; +import org.w3c.dom.Element; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.NodeList; +import org.w3c.dom.TypeInfo; +import org.w3c.dom.UserDataHandler; public class ElementImpl implements SOAPElement, SOAPBodyElement { @@ -108,7 +124,7 @@ @Override public NodeList getElementsByTagName(String name) { - return new NodeListImpl(getSoapDocument(), element.getElementsByTagName(name)); + return new NodeListImpl(soapDocument, element.getElementsByTagName(name)); } @Override @@ -139,21 +155,21 @@ this.soapDocument = ownerDoc; this.element = ownerDoc.getDomDocument().createElementNS(name.getURI(), name.getQualifiedName()); elementQName = NameImpl.convertToQName(name); - getSoapDocument().register(this); + soapDocument.register(this); } public ElementImpl(SOAPDocumentImpl ownerDoc, QName name) { this.soapDocument = ownerDoc; this.element = ownerDoc.getDomDocument().createElementNS(name.getNamespaceURI(), getQualifiedName(name)); elementQName = name; - getSoapDocument().register(this); + soapDocument.register(this); } public ElementImpl(SOAPDocumentImpl ownerDoc, Element domElement) { this.element = domElement; this.soapDocument = ownerDoc; this.elementQName = new QName(domElement.getNamespaceURI(), domElement.getLocalName()); - getSoapDocument().register(this); + soapDocument.register(this); } public ElementImpl( @@ -165,7 +181,7 @@ this.element = ownerDoc.getDomDocument().createElementNS(uri, qualifiedName); elementQName = new QName(uri, getLocalPart(qualifiedName), getPrefix(qualifiedName)); - getSoapDocument().register(this); + soapDocument.register(this); } public void ensureNamespaceIsDeclared(String prefix, String uri) { @@ -178,28 +194,29 @@ } } + @Override public Document getOwnerDocument() { return soapDocument; } @Override public Node insertBefore(Node newChild, Node refChild) throws DOMException { - return element.insertBefore(getSoapDocument().getDomNode(newChild), getSoapDocument().getDomNode(refChild)); + return soapDocument.findIfPresent(element.insertBefore(soapDocument.getDomNode(newChild), soapDocument.getDomNode(refChild))); } @Override public Node replaceChild(Node newChild, Node oldChild) throws DOMException { - return element.replaceChild(getSoapDocument().getDomNode(newChild), getSoapDocument().getDomNode(oldChild)); + return soapDocument.findIfPresent(element.replaceChild(soapDocument.getDomNode(newChild), soapDocument.getDomNode(oldChild))); } @Override public Node removeChild(Node oldChild) throws DOMException { - return element.removeChild(getSoapDocument().getDomNode(oldChild)); + return soapDocument.findIfPresent(element.removeChild(soapDocument.getDomNode(oldChild))); } @Override public Node appendChild(Node newChild) throws DOMException { - return element.appendChild(getSoapDocument().getDomNode(newChild)); + return soapDocument.findIfPresent(element.appendChild(soapDocument.getDomNode(newChild))); } @Override @@ -307,14 +324,17 @@ return element.getUserData(key); } + @Override public SOAPElement addChildElement(Name name) throws SOAPException { return addElement(name); } + @Override public SOAPElement addChildElement(QName qname) throws SOAPException { return addElement(qname); } + @Override public SOAPElement addChildElement(String localName) throws SOAPException { String nsUri = getNamespaceURI(""); Name name = (nsUri == null || nsUri.isEmpty()) @@ -323,6 +343,7 @@ return addChildElement(name); } + @Override public SOAPElement addChildElement(String localName, String prefix) throws SOAPException { String uri = getNamespaceURI(prefix); @@ -337,6 +358,7 @@ return addChildElement(localName, prefix, uri); } + @Override public String getNamespaceURI(String prefix) { if ("xmlns".equals(prefix)) { @@ -415,12 +437,14 @@ return null; } + @Override public SOAPElement setElementQName(QName newName) throws SOAPException { ElementImpl copy = new ElementImpl((SOAPDocumentImpl) getOwnerDocument(), newName); return replaceElementWithSOAPElement(this,copy); } + @Override public QName createQName(String localName, String prefix) throws SOAPException { String uri = getNamespaceURI(prefix); @@ -485,6 +509,7 @@ return new NamespaceContextIterator(this, traverseStack); } + @Override public SOAPElement addChildElement( String localName, String prefix, @@ -496,6 +521,7 @@ return convertToSoapElement(newElement); } + @Override public SOAPElement addChildElement(SOAPElement element) throws SOAPException { @@ -531,7 +557,7 @@ log.severe("SAAJ0158.impl.version.mismatch.fault"); throw new SOAPExceptionImpl("SOAP Version mismatch encountered when trying to add SOAPFault to SOAPBody"); } - Iterator it = this.getChildElements(); + Iterator it = this.getChildElements(); if (it.hasNext()) { log.severe("SAAJ0156.impl.adding.fault.error"); throw new SOAPExceptionImpl("Cannot add SOAPFault as a child of a non-Empty SOAPBody"); @@ -603,7 +629,7 @@ } protected void addNode(org.w3c.dom.Node newElement) throws SOAPException { - insertBefore(getSoapDocument().getDomNode(newElement), null); + insertBefore(soapDocument.getDomNode(newElement), null); if (getOwnerDocument() instanceof DocumentFragment) return; @@ -623,7 +649,7 @@ Node child = getFirstChild(); while (child != null) { if (child instanceof Element) { - return (Element) getSoapDocument().find(child); + return (Element) soapDocument.find(child); } child = child.getNextSibling(); } @@ -634,7 +660,7 @@ Node eachChild = getFirstChild(); while (eachChild != null) { if (eachChild instanceof Element) { - SOAPElement eachChildSoap = (SOAPElement) getSoapDocument().find(eachChild); + SOAPElement eachChildSoap = (SOAPElement) soapDocument.find(eachChild); if (eachChildSoap != null) { if (eachChildSoap.getElementName().equals(name)) { return eachChildSoap; @@ -658,6 +684,7 @@ return null; } + @Override public SOAPElement addTextNode(String text) throws SOAPException { if (text.startsWith(CDATAImpl.cdataUC) || text.startsWith(CDATAImpl.cdataLC)) @@ -680,6 +707,7 @@ return this; } + @Override public SOAPElement addAttribute(Name name, String value) throws SOAPException { addAttributeBare(name, value); @@ -689,6 +717,7 @@ return this; } + @Override public SOAPElement addAttribute(QName qname, String value) throws SOAPException { addAttributeBare(qname, value); @@ -731,6 +760,7 @@ } } + @Override public SOAPElement addNamespaceDeclaration(String prefix, String uri) throws SOAPException { if (prefix.length() > 0) { @@ -743,10 +773,12 @@ return this; } + @Override public String getAttributeValue(Name name) { return getAttributeValueFrom(this, name); } + @Override public String getAttributeValue(QName qname) { return getAttributeValueFrom( this, @@ -756,9 +788,10 @@ getQualifiedName(qname)); } - public Iterator getAllAttributes() { + @Override + public Iterator getAllAttributes() { Iterator i = getAllAttributesFrom(this); - ArrayList list = new ArrayList(); + ArrayList list = new ArrayList<>(); while (i.hasNext()) { Name name = i.next(); if (!"xmlns".equalsIgnoreCase(name.getPrefix())) @@ -767,9 +800,10 @@ return list.iterator(); } - public Iterator getAllAttributesAsQNames() { + @Override + public Iterator getAllAttributesAsQNames() { Iterator i = getAllAttributesFrom(this); - ArrayList list = new ArrayList(); + ArrayList list = new ArrayList<>(); while (i.hasNext()) { Name name = i.next(); if (!"xmlns".equalsIgnoreCase(name.getPrefix())) { @@ -780,11 +814,13 @@ } - public Iterator getNamespacePrefixes() { + @Override + public Iterator getNamespacePrefixes() { return doGetNamespacePrefixes(false); } - public Iterator getVisibleNamespacePrefixes() { + @Override + public Iterator getVisibleNamespacePrefixes() { return doGetNamespacePrefixes(true); } @@ -805,11 +841,13 @@ } } + @Override public boolean hasNext() { findNext(); return next != null; } + @Override public String next() { findNext(); if (next == null) { @@ -821,6 +859,7 @@ return last; } + @Override public void remove() { if (last == null) { throw new IllegalStateException(); @@ -832,18 +871,22 @@ }; } + @Override public Name getElementName() { return NameImpl.convertToName(elementQName); } + @Override public QName getElementQName() { return elementQName; } + @Override public boolean removeAttribute(Name name) { return removeAttribute(name.getURI(), name.getLocalName()); } + @Override public boolean removeAttribute(QName name) { return removeAttribute(name.getNamespaceURI(), name.getLocalPart()); } @@ -860,6 +903,7 @@ return true; } + @Override public boolean removeNamespaceDeclaration(String prefix) { org.w3c.dom.Attr declaration = getNamespaceAttr(prefix); if (declaration == null) { @@ -873,12 +917,13 @@ return true; } - public Iterator getChildElements() { + @Override + public Iterator getChildElements() { return getChildElementsFrom(this); } protected SOAPElement convertToSoapElement(Element element) { - final Node soapNode = getSoapDocument().findIfPresent(element); + final Node soapNode = soapDocument.findIfPresent(element); if (soapNode instanceof SOAPElement) { return (SOAPElement) soapNode; } else { @@ -888,6 +933,36 @@ } } + protected TextImpl convertToSoapText(CharacterData characterData) { + final Node soapNode = getSoapDocument().findIfPresent(characterData); + if (soapNode instanceof TextImpl) { + return (TextImpl) soapNode; + } else { + TextImpl t = null; + switch (characterData.getNodeType()) { + case CDATA_SECTION_NODE: + t = new CDATAImpl(getSoapDocument(), characterData.getData()); + break; + case COMMENT_NODE: + t = new SOAPCommentImpl(getSoapDocument(), characterData.getData()); + break; + case TEXT_NODE: + t = new SOAPTextImpl(getSoapDocument(), characterData.getData()); + break; + } + Node parent = getSoapDocument().find(characterData.getParentNode()); + if (parent != null) { + parent.replaceChild(t, characterData); + } // XXX else throw an exception? + + return t; + +// return replaceElementWithSOAPElement( +// element, +// (ElementImpl) createElement(NameImpl.copyElementName(element))); + } + } + protected SOAPElement replaceElementWithSOAPElement( Element element, ElementImpl copy) { @@ -898,13 +973,13 @@ copy.addAttributeBare(name, getAttributeValueFrom(element, name)); } - Iterator eachChild = getChildElementsFrom(element); + Iterator eachChild = getChildElementsFromDOM(element); while (eachChild.hasNext()) { Node nextChild = eachChild.next(); copy.insertBefore(nextChild, null); } - Node parent = getSoapDocument().find(element.getParentNode()); + Node parent = soapDocument.find(element.getParentNode()); if (parent != null) { parent.replaceChild(copy, element); } // XXX else throw an exception? @@ -912,18 +987,71 @@ return copy; } + private Iterator getChildElementsFromDOM(final Element el) { + return new Iterator() { + Node next = el.getFirstChild(); + Node nextNext = null; + Node last = null; + Node soapElement = getSoapDocument().findIfPresent(el); + + @Override + public boolean hasNext() { + if (next != null) { + return true; + } + if (nextNext != null) { + next = nextNext; + } + + return next != null; + } + + public Node next() { + if (hasNext()) { + last = next; + next = null; + + if ((soapElement instanceof ElementImpl) + && (last instanceof Element)) { + last = + ((ElementImpl) soapElement).convertToSoapElement( + (Element) last); + } else if ((soapElement instanceof ElementImpl) && (last instanceof CharacterData)) { + last = ((ElementImpl) soapElement).convertToSoapText( + (CharacterData) last); + } + + nextNext = last.getNextSibling(); + return last; + } + throw new NoSuchElementException(); + } + + @Override + public void remove() { + if (last == null) { + throw new IllegalStateException(); + } + Node target = last; + last = null; + el.removeChild(target); + } + }; + } + protected Iterator getChildElementNodes() { return new Iterator() { - Iterator eachNode = getChildElements(); + Iterator eachNode = getChildElements(); Node next = null; Node last = null; + @Override public boolean hasNext() { if (next == null) { while (eachNode.hasNext()) { Node node = eachNode.next(); if (node instanceof Element) { - next = getSoapDocument().findIfPresent(node); + next = soapDocument.findIfPresent(node); break; } } @@ -931,15 +1059,17 @@ return next != null; } - public Node next() { + @Override + public javax.xml.soap.Node next() { if (hasNext()) { last = next; next = null; - return last; + return (javax.xml.soap.Node) last; } throw new NoSuchElementException(); } + @Override public void remove() { if (last == null) { throw new IllegalStateException(); @@ -951,20 +1081,23 @@ }; } - public Iterator getChildElements(final Name name) { + @Override + public Iterator getChildElements(final Name name) { return getChildElements(name.getURI(), name.getLocalName()); } - public Iterator getChildElements(final QName qname) { + @Override + public Iterator getChildElements(final QName qname) { return getChildElements(qname.getNamespaceURI(), qname.getLocalPart()); } - private Iterator getChildElements(final String nameUri, final String nameLocal) { - return new Iterator() { + private Iterator getChildElements(final String nameUri, final String nameLocal) { + return new Iterator() { Iterator eachElement = getChildElementNodes(); Node next = null; Node last = null; + @Override public boolean hasNext() { if (next == null) { while (eachElement.hasNext()) { @@ -982,15 +1115,17 @@ return next != null; } - public Node next() { + @Override + public javax.xml.soap.Node next() { if (!hasNext()) { throw new NoSuchElementException(); } last = next; next = null; - return last; + return (javax.xml.soap.Node) last; } + @Override public void remove() { if (last == null) { throw new IllegalStateException(); @@ -1002,6 +1137,7 @@ }; } + @Override public void removeContents() { Node currentChild = getFirstChild(); @@ -1020,6 +1156,7 @@ } } + @Override public void setEncodingStyle(String encodingStyle) throws SOAPException { if (!"".equals(encodingStyle)) { try { @@ -1037,6 +1174,7 @@ tryToFindEncodingStyleAttributeName(); } + @Override public String getEncodingStyle() { String encodingStyle = encodingStyleAttribute.getValue(); if (encodingStyle != null) @@ -1058,11 +1196,13 @@ } // Node methods + @Override public String getValue() { javax.xml.soap.Node valueNode = getValueNode(); return valueNode == null ? null : valueNode.getValue(); } + @Override public void setValue(String value) { Node valueNode = getValueNodeStrict(); if (valueNode != null) { @@ -1092,7 +1232,7 @@ } protected javax.xml.soap.Node getValueNode() { - Iterator i = getChildElements(); + Iterator i = getChildElements(); while (i.hasNext()) { Node n = i.next(); if (n.getNodeType() == org.w3c.dom.Node.TEXT_NODE || @@ -1101,12 +1241,13 @@ normalize(); // Should remove the normalization step when this gets fixed in // DOM/Xerces. - return getSoapDocument().find(n); + return soapDocument.find(n); } } return null; } + @Override public void setParentElement(SOAPElement element) throws SOAPException { if (element == null) { log.severe("SAAJ0106.impl.no.null.to.parent.elem"); @@ -1138,12 +1279,13 @@ encodingStyleAttribute.setName(encodingStyleAttributeName); } + @Override public SOAPElement getParentElement() { Node parentNode = getParentNode(); if (parentNode instanceof SOAPDocument) { return null; } - return (SOAPElement) getSoapDocument().find(parentNode); + return (SOAPElement) soapDocument.find(parentNode); } protected String getSOAPNamespace() { @@ -1167,6 +1309,7 @@ return soapNamespace; } + @Override public void detachNode() { Node parent = getParentNode(); if (parent != null) { @@ -1184,6 +1327,7 @@ } } + @Override public void recycleNode() { detachNode(); // TBD @@ -1260,10 +1404,12 @@ int attributeIndex = 0; String currentName; + @Override public boolean hasNext() { return attributeIndex < attributesLength; } + @Override public Name next() { if (!hasNext()) { throw new NoSuchElementException(); @@ -1283,6 +1429,7 @@ } } + @Override public void remove() { if (currentName == null) { throw new IllegalStateException(); @@ -1331,13 +1478,14 @@ return attribute == null ? null : attribute.getValue(); } - protected Iterator getChildElementsFrom(final Element element) { - return new Iterator() { + protected Iterator getChildElementsFrom(final Element element) { + return new Iterator() { Node next = element.getFirstChild(); Node nextNext = null; Node last = null; - Node soapElement = getSoapDocument().findIfPresent(element); + Node soapElement = soapDocument.findIfPresent(element); + @Override public boolean hasNext() { if (next != null) { return true; @@ -1349,7 +1497,8 @@ return next != null; } - public Node next() { + @Override + public javax.xml.soap.Node next() { if (hasNext()) { last = next; next = null; @@ -1362,11 +1511,12 @@ } nextNext = last.getNextSibling(); - return getSoapDocument().findIfPresent(last); + return (javax.xml.soap.Node) soapDocument.findIfPresent(last); } throw new NoSuchElementException(); } + @Override public void remove() { if (last == null) { throw new IllegalStateException(); @@ -1428,6 +1578,7 @@ //TODO: This is a temporary SAAJ workaround for optimizing XWS // should be removed once the corresponding JAXP bug is fixed // It appears the bug will be fixed in JAXP 1.4 (not by Appserver 9 timeframe) + @Override public void setAttributeNS( String namespaceURI,String qualifiedName, String value) { int index = qualifiedName.indexOf(':'); @@ -1483,7 +1634,7 @@ @Override public NodeList getElementsByTagNameNS(String namespaceURI, String localName) throws DOMException { - return new NodeListImpl(getSoapDocument(), element.getElementsByTagNameNS(namespaceURI, localName)); + return new NodeListImpl(soapDocument, element.getElementsByTagNameNS(namespaceURI, localName)); } @Override @@ -1538,37 +1689,37 @@ @Override public Node getParentNode() { - return getSoapDocument().find(element.getParentNode()); + return soapDocument.find(element.getParentNode()); } @Override public NodeList getChildNodes() { - return new NodeListImpl(getSoapDocument(), element.getChildNodes()); + return new NodeListImpl(soapDocument, element.getChildNodes()); } @Override public Node getFirstChild() { - return getSoapDocument().findIfPresent(element.getFirstChild()); + return soapDocument.findIfPresent(element.getFirstChild()); } @Override public Node getLastChild() { - return getSoapDocument().findIfPresent(element.getLastChild()); + return soapDocument.findIfPresent(element.getLastChild()); } @Override public Node getPreviousSibling() { - return getSoapDocument().findIfPresent(element.getPreviousSibling()); + return soapDocument.findIfPresent(element.getPreviousSibling()); } @Override public Node getNextSibling() { - return getSoapDocument().findIfPresent(element.getNextSibling()); + return soapDocument.findIfPresent(element.getNextSibling()); } @Override public NamedNodeMap getAttributes() { - return element.getAttributes(); + return new NamedNodeMapImpl(element.getAttributes(), soapDocument); } public Element getDomElement() {