Search in sources :

Example 86 with XMLStreamWriter

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();
}
Also used : StringWriter(java.io.StringWriter) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter)

Example 87 with XMLStreamWriter

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();
}
Also used : XMLStreamWriter(javax.xml.stream.XMLStreamWriter)

Example 88 with XMLStreamWriter

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);
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) Fault(org.apache.cxf.interceptor.Fault) Document(org.w3c.dom.Document)

Example 89 with XMLStreamWriter

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>"));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) DelegatingXMLStreamWriter(org.apache.cxf.staxutils.DelegatingXMLStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 90 with XMLStreamWriter

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());
}
Also used : TagVO2(org.apache.cxf.jaxrs.resources.TagVO2) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) DelegatingXMLStreamWriter(org.apache.cxf.staxutils.DelegatingXMLStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Aggregations

XMLStreamWriter (javax.xml.stream.XMLStreamWriter)209 XMLStreamException (javax.xml.stream.XMLStreamException)84 StringWriter (java.io.StringWriter)47 Test (org.junit.Test)47 ByteArrayOutputStream (java.io.ByteArrayOutputStream)40 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)40 XMLStreamReader (javax.xml.stream.XMLStreamReader)32 QName (javax.xml.namespace.QName)26 Document (org.w3c.dom.Document)26 Fault (org.apache.cxf.interceptor.Fault)25 IOException (java.io.IOException)23 OutputStream (java.io.OutputStream)21 ByteArrayInputStream (java.io.ByteArrayInputStream)17 StreamSource (javax.xml.transform.stream.StreamSource)17 StringReader (java.io.StringReader)16 JAXBException (javax.xml.bind.JAXBException)14 DOMSource (javax.xml.transform.dom.DOMSource)14 MessagePartInfo (org.apache.cxf.service.model.MessagePartInfo)14 Message (org.apache.cxf.message.Message)13 Element (org.w3c.dom.Element)11