Search in sources :

Example 56 with XMLOutputFactory

use of javax.xml.stream.XMLOutputFactory in project sulky by huxi.

the class PropertyListEncoder method encode.

public void encode(PropertyList obj, OutputStream into) throws IOException {
    PropertyListWriter propertyListWriter = new PropertyListWriter();
    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    try {
        XMLStreamWriter writer = outputFactory.createXMLStreamWriter(new OutputStreamWriter(into, StandardCharsets.UTF_8));
        propertyListWriter.write(writer, obj, true);
        writer.close();
    } catch (XMLStreamException e) {
        throw new IOException("Exception while writing XML!", e);
    }
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) XMLStreamException(javax.xml.stream.XMLStreamException) PropertyListWriter(de.huxhorn.sulky.plist.impl.PropertyListWriter) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException)

Example 57 with XMLOutputFactory

use of javax.xml.stream.XMLOutputFactory in project cxf by apache.

the class BadgerFishProvider method writeTo.

public void writeTo(Object obj, Class<?> clazz, Type genericType, Annotation[] annotations, MediaType m, MultivaluedMap<String, Object> headers, OutputStream os) {
    try {
        if (!new Locale("badgerFishLanguage").equals(requestHeaders.getLanguage())) {
            throw new RuntimeException();
        }
        JAXBContext context = getJAXBContext(obj.getClass());
        Marshaller marshaller = context.createMarshaller();
        XMLOutputFactory factory = new BadgerFishXMLOutputFactory();
        XMLStreamWriter xsw = factory.createXMLStreamWriter(os);
        marshaller.marshal(obj, xsw);
        xsw.close();
    } catch (JAXBException e) {
        e.printStackTrace();
    } catch (XMLStreamException e) {
        e.printStackTrace();
    }
}
Also used : Locale(java.util.Locale) Marshaller(javax.xml.bind.Marshaller) XMLOutputFactory(javax.xml.stream.XMLOutputFactory) BadgerFishXMLOutputFactory(org.codehaus.jettison.badgerfish.BadgerFishXMLOutputFactory) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) BadgerFishXMLOutputFactory(org.codehaus.jettison.badgerfish.BadgerFishXMLOutputFactory)

Example 58 with XMLOutputFactory

use of javax.xml.stream.XMLOutputFactory in project cxf by apache.

the class StaxUtils method createXMLStreamWriter.

public static XMLStreamWriter createXMLStreamWriter(Result r) {
    if (r instanceof DOMResult) {
        // use our own DOM writer to avoid issues with Sun's
        // version that doesn't support getNamespaceContext
        DOMResult dr = (DOMResult) r;
        Node nd = dr.getNode();
        if (nd instanceof Document) {
            return new W3CDOMStreamWriter((Document) nd);
        } else if (nd instanceof Element) {
            return new W3CDOMStreamWriter((Element) nd);
        } else if (nd instanceof DocumentFragment) {
            return new W3CDOMStreamWriter((DocumentFragment) nd);
        }
    }
    XMLOutputFactory factory = getXMLOutputFactory();
    try {
        return factory.createXMLStreamWriter(r);
    } catch (XMLStreamException e) {
        throw new RuntimeException("Cant' create XMLStreamWriter", e);
    } finally {
        returnXMLOutputFactory(factory);
    }
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) DOMResult(javax.xml.transform.dom.DOMResult) XMLStreamException(javax.xml.stream.XMLStreamException) Node(org.w3c.dom.Node) StartElement(javax.xml.stream.events.StartElement) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) StartDocument(javax.xml.stream.events.StartDocument) DocumentFragment(org.w3c.dom.DocumentFragment)

Example 59 with XMLOutputFactory

use of javax.xml.stream.XMLOutputFactory in project cxf by apache.

the class JAXBElementProvider method getStreamWriter.

protected XMLStreamWriter getStreamWriter(Object obj, OutputStream os, MediaType mt) {
    XMLStreamWriter writer = null;
    MessageContext mc = getContext();
    if (mc != null) {
        writer = mc.getContent(XMLStreamWriter.class);
        if (writer == null) {
            XMLOutputFactory factory = (XMLOutputFactory) mc.get(XMLOutputFactory.class.getName());
            if (factory != null) {
                try {
                    writer = factory.createXMLStreamWriter(os);
                } catch (XMLStreamException e) {
                    throw ExceptionUtils.toInternalServerErrorException(new RuntimeException("Cant' create XMLStreamWriter", e), null);
                }
            }
        }
        if (writer == null && getEnableStreaming()) {
            writer = StaxUtils.createXMLStreamWriter(os);
        }
    }
    if (writer == null && os == null) {
        writer = getStreamHandlerFromCurrentMessage(XMLStreamWriter.class);
    }
    return createTransformWriterIfNeeded(writer, os, true);
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) MessageContext(org.apache.cxf.jaxrs.ext.MessageContext)

Example 60 with XMLOutputFactory

use of javax.xml.stream.XMLOutputFactory in project cxf by apache.

the class JAXBEncoderDecoderTest method testCustomNamespaces.

@Test
public void testCustomNamespaces() throws Exception {
    Map<String, String> mapper = new HashMap<>();
    mapper.put("http://apache.org/hello_world_soap_http/types", "Omnia");
    mapper.put("http://cxf.apache.org/jaxb_form", "Gallia");
    ObjectWithQualifiedElementElement testObject = new ObjectWithQualifiedElementElement();
    testObject.setString1("twine");
    testObject.setString2("cord");
    QName elName = new QName(wrapperAnnotation.targetNamespace(), wrapperAnnotation.localName());
    MessagePartInfo part = new MessagePartInfo(elName, null);
    part.setElement(true);
    part.setElementQName(elName);
    StringWriter stringWriter = new StringWriter();
    XMLOutputFactory opFactory = XMLOutputFactory.newInstance();
    opFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE);
    XMLEventWriter writer = opFactory.createXMLEventWriter(stringWriter);
    Marshaller m = context.createMarshaller();
    JAXBUtils.setNamespaceMapper(mapper, m);
    JAXBEncoderDecoder.marshall(m, testObject, part, writer);
    writer.flush();
    writer.close();
    String xmlResult = stringWriter.toString();
    // the following is a bit of a crock, but, to tell the truth, this test case most exists
    // so that it could be examined inside the debugger to see how JAXB works.
    assertTrue(xmlResult.contains("Gallia:string2"));
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) Marshaller(javax.xml.bind.Marshaller) ObjectWithQualifiedElementElement(org.apache.cxf.jaxb_form.ObjectWithQualifiedElementElement) StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) XMLEventWriter(javax.xml.stream.XMLEventWriter) QName(javax.xml.namespace.QName) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) Test(org.junit.Test)

Aggregations

XMLOutputFactory (javax.xml.stream.XMLOutputFactory)61 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)40 XMLStreamException (javax.xml.stream.XMLStreamException)24 StringWriter (java.io.StringWriter)23 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 Test (org.junit.Test)15 XMLEventWriter (javax.xml.stream.XMLEventWriter)12 IOException (java.io.IOException)9 DOMResult (javax.xml.transform.dom.DOMResult)7 StAXResult (javax.xml.transform.stax.StAXResult)7 StreamResult (javax.xml.transform.stream.StreamResult)6 HashMap (java.util.HashMap)5 QName (javax.xml.namespace.QName)5 Result (javax.xml.transform.Result)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 OutputStream (java.io.OutputStream)4 Marshaller (javax.xml.bind.Marshaller)4 XMLEventReader (javax.xml.stream.XMLEventReader)4 XMLInputFactory (javax.xml.stream.XMLInputFactory)4 MessagePartInfo (org.apache.cxf.service.model.MessagePartInfo)4