use of org.metatype.sxc.util.XoXMLStreamWriterImpl in project tomee by apache.
the class Sxc method marshal.
public static void marshal(final JAXBObject objectType, final Object object, final Result result) throws JAXBException {
if (result == null)
throw new IllegalArgumentException("result is null");
if (!(result instanceof StreamResult))
throw new IllegalArgumentException("result is null");
if (object == null)
throw new IllegalArgumentException("object is null");
if (objectType == null)
throw new IllegalArgumentException("jaxbObject is null");
final StreamResult streamResult = (StreamResult) result;
XMLStreamWriter writer = null;
try {
final XMLOutputFactory xof = getXmOutputFactory();
writer = xof.createXMLStreamWriter(streamResult.getOutputStream(), "UTF-8");
writer = new PrettyPrintXMLStreamWriter(writer);
final XoXMLStreamWriter w = new XoXMLStreamWriterImpl(writer);
try {
w.writeStartDocument("UTF-8", null);
// write xsi:type if there is no default root element for this type
final RuntimeContext context = new RuntimeContext((ExtendedMarshaller) null);
try {
final QName name = objectType.getXmlRootElement();
// open element
w.writeStartElementWithAutoPrefix(name.getNamespaceURI(), name.getLocalPart());
objectType.write(w, object, context);
w.writeEndElement();
} catch (Exception e) {
if (e instanceof JAXBException) {
// assume event handler has already been notified
throw (JAXBException) e;
}
if (e instanceof RuntimeXMLStreamException) {
// simply unwrap and handle below
e = ((RuntimeXMLStreamException) e).getCause();
}
if (e instanceof XMLStreamException) {
final Throwable cause = e.getCause();
if (cause instanceof JAXBException) {
throw (JAXBException) e;
}
throw new MarshalException(cause == null ? e : cause);
}
throw new MarshalException(e);
}
w.writeEndDocument();
} catch (final Exception e) {
throw new MarshalException(e);
}
} catch (final XMLStreamException e) {
throw new JAXBException("Could not close XMLStreamWriter.", e);
} finally {
if (writer != null) {
try {
writer.close();
} catch (final XMLStreamException ignored) {
}
}
}
}
Aggregations