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);
}
}
Aggregations