}
XMLDescriptor xmlDescriptor = getDescriptor(object, isXMLRoot);
AbstractSession session = xmlContext.getSession(xmlDescriptor);
WriterRecord writerRecord;
if (isFormattedOutput()) {
writerRecord = new FormattedWriterRecord();
} else {
writerRecord = new WriterRecord();
}
writerRecord.setMarshaller(this);
writerRecord.setWriter(writer);
//if this is a simple xml root, the session and descriptor will be null
if (session == null || !xmlContext.getDocumentPreservationPolicy(session).shouldPreserveDocument()) {
marshal(object, writerRecord, xmlDescriptor, isXMLRoot);
try {
writer.flush();
} catch (IOException e) {
throw XMLMarshalException.marshalException(e);
}
return;
}
try {
Node xmlDocument = objectToXMLNode(object, xmlDescriptor, isXMLRoot);
writerRecord.setSession(session);
if (isFragment()) {
writerRecord.node(xmlDocument, xmlDescriptor.getNamespaceResolver());
} else {
writerRecord.startDocument(encoding, version);
writerRecord.node(xmlDocument, writerRecord.getNamespaceResolver());
writerRecord.endDocument();
}
} catch (XMLPlatformException e) {
throw XMLMarshalException.marshalException(e);
}
}