Search in sources :

Example 1 with OverlayW3CDOMStreamWriter

use of org.apache.cxf.staxutils.OverlayW3CDOMStreamWriter in project cxf by apache.

the class StaxSerializer method deserialize.

private Node deserialize(Node ctx, XMLStreamReader reader, boolean wrapped) throws XMLEncryptionException {
    Document contextDocument = null;
    if (Node.DOCUMENT_NODE == ctx.getNodeType()) {
        contextDocument = (Document) ctx;
    } else {
        contextDocument = ctx.getOwnerDocument();
    }
    XMLStreamWriter writer = null;
    try {
        if (ctx instanceof SOAPElement) {
            SOAPElement el = (SOAPElement) ctx;
            while (el != null && !(el instanceof SOAPEnvelope)) {
                el = el.getParentElement();
            }
            // cannot load into fragment due to a ClassCastException within SAAJ addChildElement
            // which only checks for Document as parent, not DocumentFragment
            Element element = ctx.getOwnerDocument().createElementNS("dummy", "dummy");
            writer = new SAAJStreamWriter((SOAPEnvelope) el, element);
            return appendNewChild(reader, wrapped, contextDocument, writer, element);
        }
        if (DOMUtils.isJava9SAAJ()) {
            // cannot load into fragment due to a ClassCastException within SAAJ addChildElement
            // which only checks for Document as parent, not DocumentFragment
            Element element = ctx.getOwnerDocument().createElementNS("dummy", "dummy");
            writer = new OverlayW3CDOMStreamWriter(ctx.getOwnerDocument(), element);
            return appendNewChild(reader, wrapped, contextDocument, writer, element);
        }
        // Import to a dummy fragment
        DocumentFragment dummyFragment = contextDocument.createDocumentFragment();
        writer = StaxUtils.createXMLStreamWriter(new DOMResult(dummyFragment));
        StaxUtils.copy(reader, writer);
        if (wrapped) {
            DocumentFragment result = contextDocument.createDocumentFragment();
            Node child = dummyFragment.getFirstChild().getFirstChild();
            if (child != null && child.getNextSibling() == null) {
                return child;
            }
            while (child != null) {
                Node nextChild = child.getNextSibling();
                result.appendChild(child);
                child = nextChild;
            }
            dummyFragment = result;
        }
        return dummyFragment;
    } catch (XMLStreamException ex) {
        throw new XMLEncryptionException(ex);
    }
}
Also used : OverlayW3CDOMStreamWriter(org.apache.cxf.staxutils.OverlayW3CDOMStreamWriter) DOMResult(javax.xml.transform.dom.DOMResult) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) SOAPElement(javax.xml.soap.SOAPElement) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) SOAPElement(javax.xml.soap.SOAPElement) SOAPEnvelope(javax.xml.soap.SOAPEnvelope) Document(org.w3c.dom.Document) DocumentFragment(org.w3c.dom.DocumentFragment) SAAJStreamWriter(org.apache.cxf.binding.soap.saaj.SAAJStreamWriter) XMLEncryptionException(org.apache.xml.security.encryption.XMLEncryptionException)

Aggregations

SOAPElement (javax.xml.soap.SOAPElement)1 SOAPEnvelope (javax.xml.soap.SOAPEnvelope)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)1 DOMResult (javax.xml.transform.dom.DOMResult)1 SAAJStreamWriter (org.apache.cxf.binding.soap.saaj.SAAJStreamWriter)1 OverlayW3CDOMStreamWriter (org.apache.cxf.staxutils.OverlayW3CDOMStreamWriter)1 XMLEncryptionException (org.apache.xml.security.encryption.XMLEncryptionException)1 Document (org.w3c.dom.Document)1 DocumentFragment (org.w3c.dom.DocumentFragment)1 Element (org.w3c.dom.Element)1 Node (org.w3c.dom.Node)1