Search in sources :

Example 6 with MappedXMLStreamWriter

use of org.codehaus.jettison.mapped.MappedXMLStreamWriter in project GeoGig by boundlessgeo.

the class JettisonRepresentation method createWriter.

private XMLStreamWriter createWriter(Writer writer) {
    final MediaType mediaType = getMediaType();
    XMLStreamWriter xml;
    if (mediaType.getSubType().equalsIgnoreCase("xml")) {
        try {
            xml = XMLOutputFactory.newFactory().createXMLStreamWriter(writer);
        } catch (XMLStreamException ex) {
            throw new RuntimeException(ex);
        }
    } else if (mediaType == MediaType.APPLICATION_JSON) {
        xml = new MappedXMLStreamWriter(new MappedNamespaceConvention(), writer);
    } else {
        throw new RuntimeException("mediatype not handled " + mediaType);
    }
    return xml;
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) MappedXMLStreamWriter(org.codehaus.jettison.mapped.MappedXMLStreamWriter) MediaType(org.restlet.data.MediaType) MappedXMLStreamWriter(org.codehaus.jettison.mapped.MappedXMLStreamWriter) MappedNamespaceConvention(org.codehaus.jettison.mapped.MappedNamespaceConvention)

Example 7 with MappedXMLStreamWriter

use of org.codehaus.jettison.mapped.MappedXMLStreamWriter in project opencast by opencast.

the class Event method toJSON.

/**
 * Serializes the recording event.
 *
 * @return the serialized recording event
 */
@Override
public String toJSON() {
    try {
        if (context == null) {
            createJAXBContext();
        }
        Marshaller marshaller = Event.context.createMarshaller();
        Configuration config = new Configuration();
        config.setSupressAtAttributes(true);
        MappedNamespaceConvention con = new MappedNamespaceConvention(config);
        StringWriter writer = new StringWriter();
        XMLStreamWriter xmlStreamWriter = new MappedXMLStreamWriter(con, writer) {

            @Override
            public void writeStartElement(String prefix, String local, String uri) throws XMLStreamException {
                super.writeStartElement("", local, "");
            }

            @Override
            public void writeStartElement(String uri, String local) throws XMLStreamException {
                super.writeStartElement("", local, "");
            }

            @Override
            public void setPrefix(String pfx, String uri) throws XMLStreamException {
            }

            @Override
            public void setDefaultNamespace(String uri) throws XMLStreamException {
            }
        };
        marshaller.marshal(this, xmlStreamWriter);
        return writer.toString();
    } catch (JAXBException e) {
        throw new IllegalStateException(e.getLinkedException() != null ? e.getLinkedException() : e);
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) Configuration(org.codehaus.jettison.mapped.Configuration) StringWriter(java.io.StringWriter) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) MappedXMLStreamWriter(org.codehaus.jettison.mapped.MappedXMLStreamWriter) JAXBException(javax.xml.bind.JAXBException) MappedNamespaceConvention(org.codehaus.jettison.mapped.MappedNamespaceConvention) MappedXMLStreamWriter(org.codehaus.jettison.mapped.MappedXMLStreamWriter)

Example 8 with MappedXMLStreamWriter

use of org.codehaus.jettison.mapped.MappedXMLStreamWriter in project cxf by apache.

the class JSONUtils method createStreamWriter.

// CHECKSTYLE:OFF
public static XMLStreamWriter createStreamWriter(OutputStream os, QName qname, boolean writeXsiType, Configuration config, boolean serializeAsArray, List<String> arrayKeys, boolean dropRootElement, String enc) throws Exception {
    // CHECKSTYLE:ON
    MappedNamespaceConvention convention = new MappedNamespaceConvention(config);
    AbstractXMLStreamWriter xsw = new MappedXMLStreamWriter(convention, new OutputStreamWriter(os, enc));
    if (serializeAsArray) {
        if (arrayKeys != null) {
            for (String key : arrayKeys) {
                xsw.serializeAsArray(key);
            }
        } else if (qname != null) {
            String key = getKey(convention, qname);
            xsw.serializeAsArray(key);
        }
    }
    return !writeXsiType || dropRootElement ? new IgnoreContentJettisonWriter(xsw, writeXsiType, dropRootElement) : xsw;
}
Also used : AbstractXMLStreamWriter(org.codehaus.jettison.AbstractXMLStreamWriter) MappedNamespaceConvention(org.codehaus.jettison.mapped.MappedNamespaceConvention) MappedXMLStreamWriter(org.codehaus.jettison.mapped.MappedXMLStreamWriter) OutputStreamWriter(java.io.OutputStreamWriter)

Aggregations

MappedNamespaceConvention (org.codehaus.jettison.mapped.MappedNamespaceConvention)8 MappedXMLStreamWriter (org.codehaus.jettison.mapped.MappedXMLStreamWriter)8 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)6 Configuration (org.codehaus.jettison.mapped.Configuration)6 StringWriter (java.io.StringWriter)5 JAXBException (javax.xml.bind.JAXBException)5 Marshaller (javax.xml.bind.Marshaller)5 OutputStreamWriter (java.io.OutputStreamWriter)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 AbstractXMLStreamWriter (org.codehaus.jettison.AbstractXMLStreamWriter)1 BadgerFishXMLStreamWriter (org.codehaus.jettison.badgerfish.BadgerFishXMLStreamWriter)1 MediaType (org.restlet.data.MediaType)1