changeset 1383:f7e7fd169aa4

Merge
author asaha
date Thu, 08 Jun 2017 13:44:31 -0700
parents 32a01bf45f0f f87d9aaee64f
children c59814f445e8
files .hgtags
diffstat 5 files changed, 319 insertions(+), 95 deletions(-) [+]
line wrap: on
line diff
--- a/.hgtags	Tue Dec 13 12:52:09 2016 -0800
+++ b/.hgtags	Thu Jun 08 13:44:31 2017 -0700
@@ -659,11 +659,44 @@
 14fa3ed278a279dc85c150bf54b2ec4fa29cf347 jdk8u112-b14
 5f84e87f91d5bc36ed026b88d183821e5109d730 jdk8u112-b15
 d82dd7a24a496e26987caa328d1fb4fc794a4770 jdk8u112-b16
+021da5d50285a523d4622a727ea1a7019f2b52e4 jdk8u112-b31
 452662a83e5bc6dc7e9425ddd10f6c8fc98d50d8 jdk8u121-b00
 9cd16be39ca6f2c8f7cc99ad07a77bb9d0696c75 jdk8u121-b01
 f092b9a890ceeca4a2f4d55cf7d6f3f113cdb462 jdk8u121-b02
 c1b0f76c9fe9657d3f5cdd1e23bfd1d499018431 jdk8u121-b03
 44674172423a0d361466e34eedcaec18a8810b13 jdk8u121-b04
+18da635b5919a0b7cdde8573a0d502efdbf3673e jdk8u121-b05
+5b76a2126855f8949ab8fbadfa3ee2f29da9c21c jdk8u121-b06
+f10aa5b29848eab891bdd173540d91fd31f9ff20 jdk8u121-b07
+105d3bbf5e3b2b24da665b332d2dbf44980c87f2 jdk8u121-b08
+9f2588382771f855c4450f59b470d069a1cb1d01 jdk8u121-b09
+c317f0eacd602a8765d25b0fcd382f76af3697a5 jdk8u121-b10
+89aa912be940d6c30f59b80c826f212541912a56 jdk8u121-b11
+52b3f9fb54ee4304a9c34a2fe07f0c9a49472185 jdk8u121-b12
+5b8834cc3bb9e24153319c766e04e194945a61b9 jdk8u121-b13
 2359a73f36ca99ba30aef88a38936f6f4e18e65c jdk8u131-b00
+bc5500cde753aed78c92e7301548fa1450c9b104 jdk8u131-b01
+c146d8a61d677fd4d07778d0295b4e88e16a7dd3 jdk8u131-b02
+e0d1730c483c2d63fa78b74df4dd7fec8682220c jdk8u131-b03
+d2694f83db12a0aff88260c3a09a5b2474ee4afa jdk8u131-b04
+d7763a127acc2b87793b59b388062b7fbecd7755 jdk8u131-b05
+0e3bd0a2751161863c1d0d5e9ce7a22b9a440c12 jdk8u131-b06
+62b6998733d3f94a0092c2863b81159330fe92e0 jdk8u131-b07
+4e86f5b1caaf3083befd44c7369885eacdd3fe95 jdk8u131-b08
+548546d23a924ff0962df885468ce49816ae48a9 jdk8u131-b09
+bc74b4850d97ff1986bf551c752ce212f97f4b0e jdk8u131-b10
+7817f0d2519573e42405ef96a7c7ff1d768f92ec jdk8u131-b11
 74aa403ef03d56469e9364cc45c2ec18c6e50e33 jdk8u141-b00
+b1138396b99b1f8ebdb7d28c7143c96eb5b4f991 jdk8u141-b01
+a7b9281956f9616eab0498386b9605853db7b408 jdk8u141-b02
+3d27876c2ce476b16195c4519b266613f13e3e1b jdk8u141-b03
+a08a6118b9df52f2963f482dbe6801d56b9086f7 jdk8u141-b04
+dcbac02db203e4734921852aeed19416c7820f15 jdk8u141-b05
+f552ee60279fa16d60477fc59c21fee8d298840a jdk8u141-b06
+86a22a62ecd5f74faf535de048fe40563e99065f jdk8u141-b07
+1d5f442d50dff2e2c8efd58a1b95bcefe5253a1b jdk8u141-b08
+34f55abdda466e3aab4900368127f09183ef859d jdk8u141-b09
+e965788c8d73328fce1eeb96af953815dc13b927 jdk8u141-b10
+7f1844127578d6726da60f6813bfa9206b57dda9 jdk8u141-b11
+d8134565e6c8cab3ccd0e356c787e0aa75ef68ee jdk8u141-b12
 eb09a34966f43c62cb286c78c10dc722fd12d884 jdk8u151-b00
--- a/THIRD_PARTY_README	Tue Dec 13 12:52:09 2016 -0800
+++ b/THIRD_PARTY_README	Thu Jun 08 13:44:31 2017 -0700
@@ -3318,12 +3318,12 @@
 
 -------------------------------------------------------------------------------
 
-%% This notice is provided with respect to zlib v1.2.8, which may be included 
+%% This notice is provided with respect to zlib v1.2.11, which may be included 
 with JRE 8, JDK 8, and OpenJDK 8.
 
 --- begin of LICENSE ---
 
-  version 1.2.8, April 28th, 2013
+  version 1.2.11, January 15th, 2017
 
   Copyright (C) 1995-2013 Jean-loup Gailly and Mark Adler
 
--- a/src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/soap/impl/DetailImpl.java	Tue Dec 13 12:52:09 2016 -0800
+++ b/src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/soap/impl/DetailImpl.java	Thu Jun 08 13:44:31 2017 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -47,13 +47,13 @@
     public DetailEntry addDetailEntry(Name name) throws SOAPException {
         DetailEntry entry = createDetailEntry(name);
         addNode(entry);
-        return (DetailEntry) circumventBug5034339(entry);
+        return entry;
     }
 
     public DetailEntry addDetailEntry(QName qname) throws SOAPException {
         DetailEntry entry = createDetailEntry(qname);
         addNode(entry);
-        return (DetailEntry) circumventBug5034339(entry);
+        return entry;
     }
 
     protected SOAPElement addElement(Name name) throws SOAPException {
@@ -119,28 +119,4 @@
        return true;
    }
 
-    //overriding this method since the only two uses of this method
-    // are in ElementImpl and DetailImpl
-    //whereas the original base impl does the correct job for calls to it inside ElementImpl
-    // But it would not work for DetailImpl.
-    protected SOAPElement circumventBug5034339(SOAPElement element) {
-
-        Name elementName = element.getElementName();
-        if (!isNamespaceQualified(elementName)) {
-            String prefix = elementName.getPrefix();
-            String defaultNamespace = getNamespaceURI(prefix);
-            if (defaultNamespace != null) {
-                Name newElementName =
-                    NameImpl.create(
-                        elementName.getLocalName(),
-                        elementName.getPrefix(),
-                        defaultNamespace);
-                SOAPElement newElement = createDetailEntry(newElementName);
-                replaceChild(newElement, element);
-                return newElement;
-            }
-        }
-        return element;
-    }
-
 }
--- a/src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/soap/impl/ElementImpl.java	Tue Dec 13 12:52:09 2016 -0800
+++ b/src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/soap/impl/ElementImpl.java	Thu Jun 08 13:44:31 2017 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -127,8 +127,11 @@
     }
 
     public SOAPElement addChildElement(String localName) throws SOAPException {
-        return (SOAPElement) addChildElement(
-            NameImpl.createFromUnqualifiedName(localName));
+        String nsUri = getNamespaceURI("");
+        Name name = (nsUri == null || nsUri.isEmpty())
+                ?  NameImpl.createFromUnqualifiedName(localName)
+                :  NameImpl.createFromQualifiedName(localName, nsUri);
+        return addChildElement(name);
     }
 
     public SOAPElement addChildElement(String localName, String prefix)
@@ -372,13 +375,13 @@
     protected SOAPElement addElement(Name name) throws SOAPException {
         SOAPElement newElement = createElement(name);
         addNode(newElement);
-        return circumventBug5034339(newElement);
+        return newElement;
     }
 
     protected SOAPElement addElement(QName name) throws SOAPException {
         SOAPElement newElement = createElement(name);
         addNode(newElement);
-        return circumventBug5034339(newElement);
+        return newElement;
     }
 
     protected SOAPElement createElement(Name name) {
@@ -1201,26 +1204,6 @@
         return !"".equals(name.getNamespaceURI());
     }
 
-    protected SOAPElement circumventBug5034339(SOAPElement element) {
-
-        Name elementName = element.getElementName();
-        if (!isNamespaceQualified(elementName)) {
-            String prefix = elementName.getPrefix();
-            String defaultNamespace = getNamespaceURI(prefix);
-            if (defaultNamespace != null) {
-                Name newElementName =
-                    NameImpl.create(
-                        elementName.getLocalName(),
-                        elementName.getPrefix(),
-                        defaultNamespace);
-                SOAPElement newElement = createElement(newElementName);
-                replaceChild(newElement, element);
-                return newElement;
-            }
-        }
-        return element;
-    }
-
     //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)
--- a/src/share/jaxws_classes/com/sun/xml/internal/ws/api/message/saaj/SaajStaxWriter.java	Tue Dec 13 12:52:09 2016 -0800
+++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/api/message/saaj/SaajStaxWriter.java	Thu Jun 08 13:44:31 2017 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -25,8 +25,10 @@
 
 package com.sun.xml.internal.ws.api.message.saaj;
 
+import java.util.Iterator;
 import java.util.Arrays;
-import java.util.Iterator;
+import java.util.List;
+import java.util.LinkedList;
 
 import javax.xml.namespace.NamespaceContext;
 import javax.xml.namespace.QName;
@@ -42,6 +44,17 @@
 /**
  * SaajStaxWriter builds a SAAJ SOAPMessage by using XMLStreamWriter interface.
  *
+ * <p>
+ * Defers creation of SOAPElement until all the aspects of the name of the element are known.
+ * In some cases, the namespace uri is indicated only by the {@link #writeNamespace(String, String)} call.
+ * After opening an element ({@code writeStartElement}, {@code writeEmptyElement} methods), all attributes
+ * and namespace assignments are retained within {@link DeferredElement} object ({@code deferredElement} field).
+ * As soon as any other method than {@code writeAttribute}, {@code writeNamespace}, {@code writeDefaultNamespace}
+ * or {@code setNamespace} is called, the contents of {@code deferredElement} is transformed into new SOAPElement
+ * (which is appropriately inserted into the SOAPMessage under construction).
+ * This mechanism is necessary to fix JDK-8159058 issue.
+ * </p>
+ *
  * @author shih-chang.chen@oracle.com
  */
 public class SaajStaxWriter implements XMLStreamWriter {
@@ -49,6 +62,7 @@
     protected SOAPMessage soap;
     protected String envURI;
     protected SOAPElement currentElement;
+    protected DeferredElement deferredElement;
 
     static final protected String Envelope = "Envelope";
     static final protected String Header = "Header";
@@ -59,6 +73,7 @@
         soap = msg;
         currentElement = soap.getSOAPPart().getEnvelope();
         envURI = currentElement.getNamespaceURI();
+        this.deferredElement = new DeferredElement();
     }
 
     public SOAPMessage getSOAPMessage() {
@@ -67,11 +82,8 @@
 
     @Override
     public void writeStartElement(final String localName) throws XMLStreamException {
-        try {
-            currentElement = currentElement.addChildElement(localName);
-        } catch (SOAPException e) {
-            throw new XMLStreamException(e);
-        }
+        currentElement = deferredElement.flushTo(currentElement);
+        deferredElement.setLocalName(localName);
     }
 
     @Override
@@ -81,8 +93,10 @@
 
     @Override
     public void writeStartElement(final String prefix, final String ln, final String ns) throws XMLStreamException {
-        try {
-            if (envURI.equals(ns)) {
+        currentElement = deferredElement.flushTo(currentElement);
+
+        if (envURI.equals(ns)) {
+            try {
                 if (Envelope.equals(ln)) {
                     currentElement = soap.getSOAPPart().getEnvelope();
                     fixPrefix(prefix);
@@ -96,13 +110,16 @@
                     fixPrefix(prefix);
                     return;
                 }
+            } catch (SOAPException e) {
+                throw new XMLStreamException(e);
             }
-            currentElement = (prefix == null) ?
-                    currentElement.addChildElement(new QName(ns, ln)) :
-                    currentElement.addChildElement(ln, prefix, ns);
-        } catch (SOAPException e) {
-            throw new XMLStreamException(e);
+
         }
+
+        deferredElement.setLocalName(ln);
+        deferredElement.setNamespaceUri(ns);
+        deferredElement.setPrefix(prefix);
+
     }
 
     private void fixPrefix(final String prfx) throws XMLStreamException {
@@ -129,11 +146,13 @@
 
     @Override
     public void writeEndElement() throws XMLStreamException {
+        currentElement = deferredElement.flushTo(currentElement);
         if (currentElement != null) currentElement = currentElement.getParentElement();
     }
 
     @Override
     public void writeEndDocument() throws XMLStreamException {
+        currentElement = deferredElement.flushTo(currentElement);
     }
 
     @Override
@@ -151,19 +170,14 @@
 
     @Override
     public void writeAttribute(final String prefix, final String ns, final String ln, final String value) throws XMLStreamException {
-        try {
-            if (ns == null) {
-                if (prefix == null && xmlns.equals(ln)) {
-                    currentElement.addNamespaceDeclaration("", value);
-                } else {
-                    currentElement.setAttributeNS("", ln, value);
-                }
+        if (ns == null && prefix == null && xmlns.equals(ln)) {
+            writeNamespace("", value);
+        } else {
+            if (deferredElement.isInitialized()) {
+                deferredElement.addAttribute(prefix, ns, ln, value);
             } else {
-                QName name = (prefix == null) ? new QName(ns, ln) : new QName(ns, ln, prefix);
-                currentElement.addAttribute(name, value);
+                addAttibuteToElement(currentElement, prefix, ns, ln, value);
             }
-        } catch (SOAPException e) {
-            throw new XMLStreamException(e);
         }
     }
 
@@ -174,16 +188,16 @@
 
     @Override
     public void writeNamespace(String prefix, final String uri) throws XMLStreamException {
-
         // make prefix default if null or "xmlns" (according to javadoc)
-        if (prefix == null || "xmlns".equals(prefix)) {
-            prefix = "";
-        }
-
-        try {
-            currentElement.addNamespaceDeclaration(prefix, uri);
-        } catch (SOAPException e) {
-            throw new XMLStreamException(e);
+        String thePrefix = prefix == null || "xmlns".equals(prefix) ? "" : prefix;
+        if (deferredElement.isInitialized()) {
+            deferredElement.addNamespaceDeclaration(thePrefix, uri);
+        } else {
+            try {
+                currentElement.addNamespaceDeclaration(thePrefix, uri);
+            } catch (SOAPException e) {
+                throw new XMLStreamException(e);
+            }
         }
     }
 
@@ -194,35 +208,40 @@
 
     @Override
     public void writeComment(final String data) throws XMLStreamException {
+        currentElement = deferredElement.flushTo(currentElement);
         Comment c = soap.getSOAPPart().createComment(data);
         currentElement.appendChild(c);
     }
 
     @Override
     public void writeProcessingInstruction(final String target) throws XMLStreamException {
+        currentElement = deferredElement.flushTo(currentElement);
         Node n = soap.getSOAPPart().createProcessingInstruction(target, "");
         currentElement.appendChild(n);
     }
 
     @Override
     public void writeProcessingInstruction(final String target, final String data) throws XMLStreamException {
+        currentElement = deferredElement.flushTo(currentElement);
         Node n = soap.getSOAPPart().createProcessingInstruction(target, data);
         currentElement.appendChild(n);
     }
 
     @Override
     public void writeCData(final String data) throws XMLStreamException {
+        currentElement = deferredElement.flushTo(currentElement);
         Node n = soap.getSOAPPart().createCDATASection(data);
         currentElement.appendChild(n);
     }
 
     @Override
     public void writeDTD(final String dtd) throws XMLStreamException {
-        //TODO ... Don't do anything here
+        currentElement = deferredElement.flushTo(currentElement);
     }
 
     @Override
     public void writeEntityRef(final String name) throws XMLStreamException {
+        currentElement = deferredElement.flushTo(currentElement);
         Node n = soap.getSOAPPart().createEntityReference(name);
         currentElement.appendChild(n);
     }
@@ -250,6 +269,7 @@
 
     @Override
     public void writeCharacters(final String text) throws XMLStreamException {
+        currentElement = deferredElement.flushTo(currentElement);
         try {
             currentElement.addTextNode(text);
         } catch (SOAPException e) {
@@ -259,6 +279,7 @@
 
     @Override
     public void writeCharacters(final char[] text, final int start, final int len) throws XMLStreamException {
+        currentElement = deferredElement.flushTo(currentElement);
         char[] chr = (start == 0 && len == text.length) ? text : Arrays.copyOfRange(text, start, start + len);
         try {
             currentElement.addTextNode(new String(chr));
@@ -274,10 +295,16 @@
 
     @Override
     public void setPrefix(final String prefix, final String uri) throws XMLStreamException {
-        try {
-            this.currentElement.addNamespaceDeclaration(prefix, uri);
-        } catch (SOAPException e) {
-            throw new XMLStreamException(e);
+        // TODO: this in fact is not what would be expected from XMLStreamWriter
+        //       (e.g. XMLStreamWriter for writing to output stream does not write anything as result of
+        //        this method, it just rememebers that given prefix is associated with the given uri
+        //        for the scope; to actually declare the prefix assignment in the resulting XML, one
+        //        needs to call writeNamespace(...) method
+        // Kept for backwards compatibility reasons - this might be worth of further investigation.
+        if (deferredElement.isInitialized()) {
+            deferredElement.addNamespaceDeclaration(prefix, uri);
+        } else {
+            throw new XMLStreamException("Namespace not associated with any element");
         }
     }
 
@@ -308,12 +335,12 @@
                 return currentElement.lookupPrefix(namespaceURI);
             }
             public Iterator getPrefixes(final String namespaceURI) {
-                return new Iterator() {
+                return new Iterator<String>() {
                     String prefix = getPrefix(namespaceURI);
                     public boolean hasNext() {
                         return (prefix != null);
                     }
-                    public Object next() {
+                    public String next() {
                         if (!hasNext()) throw new java.util.NoSuchElementException();
                         String next = prefix;
                         prefix = null;
@@ -324,4 +351,209 @@
             }
         };
     }
+
+    static void addAttibuteToElement(SOAPElement element, String prefix, String ns, String ln, String value)
+            throws XMLStreamException {
+        try {
+            if (ns == null) {
+                element.setAttributeNS("", ln, value);
+            } else {
+                QName name = prefix == null ? new QName(ns, ln) : new QName(ns, ln, prefix);
+                element.addAttribute(name, value);
+            }
+        } catch (SOAPException e) {
+            throw new XMLStreamException(e);
+        }
+    }
+
+    /**
+     * Holds details of element that needs to be deferred in order to manage namespace assignments correctly.
+     *
+     * <p>
+     * An instance of can be set with all the aspects of the element name (local name, prefix, namespace uri).
+     * Attributes and namespace declarations (special case of attribute) can be added.
+     * Namespace declarations are handled so that the element namespace is updated if it is implied by the namespace
+     * declaration and the namespace was not set to non-{@code null} value previously.
+     * </p>
+     *
+     * <p>
+     * The state of this object can be {@link #flushTo(SOAPElement) flushed} to SOAPElement - new SOAPElement will
+     * be added a child element; the new element will have exactly the shape as represented by the state of this
+     * object. Note that the {@link #flushTo(SOAPElement)} method does nothing
+     * (and returns the argument immediately) if the state of this object is not initialized
+     * (i.e. local name is null).
+     * </p>
+     *
+     * @author ondrej.cerny@oracle.com
+     */
+    static class DeferredElement {
+        private String prefix;
+        private String localName;
+        private String namespaceUri;
+        private final List<NamespaceDeclaration> namespaceDeclarations;
+        private final List<AttributeDeclaration> attributeDeclarations;
+
+        DeferredElement() {
+            this.namespaceDeclarations = new LinkedList<NamespaceDeclaration>();
+            this.attributeDeclarations = new LinkedList<AttributeDeclaration>();
+            reset();
+        }
+
+
+        /**
+         * Set prefix of the element.
+         * @param prefix namespace prefix
+         */
+        public void setPrefix(final String prefix) {
+            this.prefix = prefix;
+        }
+
+        /**
+         * Set local name of the element.
+         *
+         * <p>
+         *     This method initializes the element.
+         * </p>
+         *
+         * @param localName local name {@code not null}
+         */
+        public void setLocalName(final String localName) {
+            if (localName == null) {
+                throw new IllegalArgumentException("localName can not be null");
+            }
+            this.localName = localName;
+        }
+
+        /**
+         * Set namespace uri.
+         *
+         * @param namespaceUri namespace uri
+         */
+        public void setNamespaceUri(final String namespaceUri) {
+            this.namespaceUri = namespaceUri;
+        }
+
+        /**
+         * Adds namespace prefix assignment to the element.
+         *
+         * @param prefix prefix (not {@code null})
+         * @param namespaceUri namespace uri
+         */
+        public void addNamespaceDeclaration(final String prefix, final String namespaceUri) {
+            if (null == this.namespaceUri && null != namespaceUri && prefix.equals(emptyIfNull(this.prefix))) {
+                this.namespaceUri = namespaceUri;
+            }
+            this.namespaceDeclarations.add(new NamespaceDeclaration(prefix, namespaceUri));
+        }
+
+        /**
+         * Adds attribute to the element.
+         * @param prefix prefix
+         * @param ns namespace
+         * @param ln local name
+         * @param value value
+         */
+        public void addAttribute(final String prefix, final String ns, final String ln, final String value) {
+            if (ns == null && prefix == null && xmlns.equals(ln)) {
+                this.addNamespaceDeclaration(prefix, value);
+            } else {
+                this.attributeDeclarations.add(new AttributeDeclaration(prefix, ns, ln, value));
+            }
+        }
+
+        /**
+         * Flushes state of this element to the {@code target} element.
+         *
+         * <p>
+         * If this element is initialized then it is added with all the namespace declarations and attributes
+         * to the {@code target} element as a child. The state of this element is reset to uninitialized.
+         * The newly added element object is returned.
+         * </p>
+         * <p>
+         * If this element is not initialized then the {@code target} is returned immediately, nothing else is done.
+         * </p>
+         *
+         * @param target target element
+         * @return {@code target} or new element
+         * @throws XMLStreamException on error
+         */
+        public SOAPElement flushTo(final SOAPElement target) throws XMLStreamException {
+            try {
+                if (this.localName != null) {
+                    // add the element appropriately (based on namespace declaration)
+                    final SOAPElement newElement;
+                    if (this.namespaceUri == null) {
+                        // add element with inherited scope
+                        newElement = target.addChildElement(this.localName);
+                    } else if (prefix == null) {
+                        newElement = target.addChildElement(new QName(this.namespaceUri, this.localName));
+                    } else {
+                        newElement = target.addChildElement(this.localName, this.prefix, this.namespaceUri);
+                    }
+                    // add namespace declarations
+                    for (NamespaceDeclaration namespace : this.namespaceDeclarations) {
+                        target.addNamespaceDeclaration(namespace.prefix, namespace.namespaceUri);
+                    }
+                    // add attribute declarations
+                    for (AttributeDeclaration attribute : this.attributeDeclarations) {
+                        addAttibuteToElement(newElement,
+                                attribute.prefix, attribute.namespaceUri, attribute.localName, attribute.value);
+                    }
+                    // reset state
+                    this.reset();
+
+                    return newElement;
+                } else {
+                    return target;
+                }
+                // else after reset state -> not initialized
+            } catch (SOAPException e) {
+                throw new XMLStreamException(e);
+            }
+        }
+
+        /**
+         * Is the element initialized?
+         * @return boolean indicating whether it was initialized after last flush
+         */
+        public boolean isInitialized() {
+            return this.localName != null;
+        }
+
+        private void reset() {
+            this.localName = null;
+            this.prefix = null;
+            this.namespaceUri = null;
+            this.namespaceDeclarations.clear();
+            this.attributeDeclarations.clear();
+        }
+
+        private static String emptyIfNull(String s) {
+            return s == null ? "" : s;
+        }
+    }
+
+    static class NamespaceDeclaration {
+        final String prefix;
+        final String namespaceUri;
+
+        NamespaceDeclaration(String prefix, String namespaceUri) {
+            this.prefix = prefix;
+            this.namespaceUri = namespaceUri;
+        }
+    }
+
+    static class AttributeDeclaration {
+        final String prefix;
+        final String namespaceUri;
+        final String localName;
+        final String value;
+
+        AttributeDeclaration(String prefix, String namespaceUri, String localName, String value) {
+            this.prefix = prefix;
+            this.namespaceUri = namespaceUri;
+            this.localName = localName;
+            this.value = value;
+        }
+    }
 }