}else{
session = xmlContext.getSession(object.getClass());
xmlDescriptor = getDescriptor(object.getClass(), session);
}
WriterRecord writerRecord;
if (isFormattedOutput()) {
writerRecord = new FormattedWriterRecord();
} else {
writerRecord = new WriterRecord();
}
writerRecord.setMarshaller(this);
writer = new BufferedWriter(writer);
writerRecord.setWriter(writer);
//if this is a simple xml root, the session and descriptor will be null
if (!(isXMLRoot && ((XMLRoot)object).getObject() instanceof Node) && ((session == null) || !xmlContext.getDocumentPreservationPolicy(session).shouldPreserveDocument())) {
marshal(object, writerRecord, session, xmlDescriptor, isXMLRoot);
} else {
try {
Node xmlDocument = null;
if(isXMLRoot && session == null) {
xmlDocument = (Node)((XMLRoot)object).getObject();
} else {
xmlDocument = objectToXMLNode(object, session, 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);
}
}