Class objectClass = object.getClass();
session = xmlContext.getSession(objectClass);
xmlDescriptor = getDescriptor(objectClass, session);
}
}
OutputStreamRecord record;
if (isFormattedOutput()) {
record = new FormattedOutputStreamRecord();
} else {
record = new OutputStreamRecord();
}
record.setMarshaller(this);
record.setOutputStream(outputStream);
//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, record, session, xmlDescriptor, isXMLRoot);
} else {
try {
Node xmlDocument = null;
String rootUri = null;
String rootLocalName = null;
if(isXMLRoot && session == null) {
xmlDocument = (Node)((XMLRoot)object).getObject();
rootUri = ((XMLRoot)object).getNamespaceURI();
rootLocalName = ((XMLRoot)object).getLocalName();
} else {
xmlDocument = objectToXMLNode(object, session, xmlDescriptor, isXMLRoot);
}
record.setSession(session);
if (isFragment()) {
if(xmlDescriptor == null){
record.node(xmlDocument, null, rootUri, rootLocalName );
}else{
record.node(xmlDocument, xmlDescriptor.getNamespaceResolver(), rootUri, rootLocalName );
}
} else {
record.startDocument(encoding, version);
record.node(xmlDocument, record.getNamespaceResolver(), rootUri, rootLocalName);
record.endDocument();
}
} catch (XMLPlatformException e) {
throw XMLMarshalException.marshalException(e);
}
}
record.flush();
}else{
OutputStreamWriter writer = new OutputStreamWriter(outputStream, encoding);
marshal(object, writer);
writer.flush();
}