use of javax.xml.stream.XMLStreamWriter in project cxf by apache.
the class StaxUtils method toString.
public static String toString(Source src) {
StringWriter sw = new StringWriter(1024);
XMLStreamWriter writer = null;
try {
writer = createXMLStreamWriter(sw);
copy(src, writer);
writer.flush();
} catch (XMLStreamException e) {
throw new RuntimeException(e);
} finally {
StaxUtils.close(writer);
}
return sw.toString();
}
use of javax.xml.stream.XMLStreamWriter in project cxf by apache.
the class StaxUtils method copy.
public static void copy(XMLStreamReader reader, OutputStream os) throws XMLStreamException {
XMLStreamWriter xsw = StaxUtils.createXMLStreamWriter(os);
StaxUtils.copy(reader, xsw);
xsw.close();
}
use of javax.xml.stream.XMLStreamWriter in project cxf by apache.
the class WSDLGetOutInterceptor method handleMessage.
public void handleMessage(Message message) throws Fault {
Document doc = (Document) message.get(WSDLGetInterceptor.DOCUMENT_HOLDER);
if (doc == null) {
return;
}
message.remove(WSDLGetInterceptor.DOCUMENT_HOLDER);
XMLStreamWriter writer = message.getContent(XMLStreamWriter.class);
if (writer == null) {
return;
}
message.put(Message.CONTENT_TYPE, "text/xml");
try {
StaxUtils.writeDocument(doc, writer, !MessageUtils.getContextualBoolean(message, StaxOutInterceptor.FORCE_START_DOCUMENT, false), true);
} catch (XMLStreamException e) {
throw new Fault(e);
}
}
use of javax.xml.stream.XMLStreamWriter in project cxf by apache.
the class JSONProviderTest method testCopyReaderToDocument.
@Test
public void testCopyReaderToDocument() throws Exception {
String s = "{\"tagVO\":{\"group\":\"b\",\"name\":\"a\"}}";
ByteArrayInputStream is = new ByteArrayInputStream(s.getBytes());
Document doc = new JSONProvider<Document>().readFrom(Document.class, Document.class, new Annotation[] {}, MediaType.APPLICATION_JSON_TYPE, new MetadataMap<String, String>(), is);
ByteArrayOutputStream os = new ByteArrayOutputStream();
XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(os);
StaxUtils.copy(doc, writer);
writer.writeEndDocument();
String s2 = os.toString();
assertTrue(s2.contains("<group>b</group><name>a</name>"));
}
use of javax.xml.stream.XMLStreamWriter in project cxf by apache.
the class JSONProviderTest method testAttributesAsElementsWithTransform.
@Test
public void testAttributesAsElementsWithTransform() throws Exception {
JSONProvider<TagVO2Holder> provider = new JSONProvider<TagVO2Holder>() {
protected XMLStreamWriter createTransformWriterIfNeeded(XMLStreamWriter writer, OutputStream os, boolean dropAtXmlLevel) {
return TransformUtils.createTransformWriterIfNeeded(writer, os, Collections.emptyMap(), null, Collections.emptyMap(), true, null);
}
};
provider.setIgnoreNamespaces(true);
TagVO2 tag = new TagVO2("A", "B");
tag.setAttrInt(123);
TagVO2Holder holder = new TagVO2Holder();
holder.setTag(tag);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
provider.writeTo(holder, TagVO2Holder.class, TagVO2Holder.class, new Annotation[0], MediaType.TEXT_XML_TYPE, new MetadataMap<String, Object>(), bos);
String expected = "{\"tagholder\":{\"attr\":\"attribute\",\"thetag\":{\"attrInt\":123,\"group\":\"B\",\"name\":\"A\"}}}";
assertEquals(expected, bos.toString());
}
Aggregations