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);
}