use of javax.xml.stream.XMLStreamWriter in project cxf by apache.
the class SerializationTest method serializeObject.
private String serializeObject(DataBinding dataBinding, TestBean1 bean) throws XMLStreamException {
DataWriter<XMLStreamWriter> writer = dataBinding.createWriter(XMLStreamWriter.class);
StringWriter stringWriter = new StringWriter();
XMLStreamWriter xmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(stringWriter);
writer.write(bean, xmlStreamWriter);
xmlStreamWriter.flush();
xmlStreamWriter.close();
return stringWriter.toString();
}
use of javax.xml.stream.XMLStreamWriter in project cxf by apache.
the class JSONProvider method marshal.
protected void marshal(Marshaller ms, Object actualObject, Class<?> actualClass, Type genericType, String enc, OutputStream os, boolean isCollection) throws Exception {
OutputStream actualOs = os;
MessageContext mc = getContext();
if (mc != null && PropertyUtils.isTrue(mc.get(Marshaller.JAXB_FORMATTED_OUTPUT))) {
actualOs = new CachedOutputStream();
}
XMLStreamWriter writer = createWriter(actualObject, actualClass, genericType, enc, actualOs, isCollection);
if (namespaceMap.size() > 1 || namespaceMap.size() == 1 && !namespaceMap.containsKey(JSONUtils.XSI_URI)) {
setNamespaceMapper(ms, namespaceMap);
}
org.apache.cxf.common.jaxb.JAXBUtils.setNoEscapeHandler(ms);
ms.marshal(actualObject, writer);
writer.close();
if (os != actualOs) {
StringIndenter formatter = new StringIndenter(IOUtils.newStringFromBytes(((CachedOutputStream) actualOs).getBytes()));
Writer outWriter = new OutputStreamWriter(os, enc);
IOUtils.copy(new StringReader(formatter.result()), outWriter, 2048);
outWriter.close();
}
}
use of javax.xml.stream.XMLStreamWriter 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);
}
}
use of javax.xml.stream.XMLStreamWriter in project cxf by apache.
the class AbstractSecurityTest method getMessageBytes.
protected byte[] getMessageBytes(Document doc) throws Exception {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
XMLStreamWriter byteArrayWriter = StaxUtils.createXMLStreamWriter(outputStream);
StaxUtils.writeDocument(doc, byteArrayWriter, false);
byteArrayWriter.flush();
return outputStream.toByteArray();
}
use of javax.xml.stream.XMLStreamWriter in project cxf by apache.
the class HWSAXSourcePayloadProvider method getSOAPBodyFile.
private File getSOAPBodyFile(Document doc) throws Exception {
File file = FileUtils.createTempFile("cxf-systest", "xml");
try (FileOutputStream out = new FileOutputStream(file)) {
XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(out);
StaxUtils.writeDocument(doc, writer, true);
writer.close();
return file;
}
}
Aggregations