if (getAttachmentMarshaller() != null) {
marshalRecord.setXOPPackage(getAttachmentMarshaller().isXOPPackage());
}
addDescriptorNamespacesToXMLRecord(descriptor, marshalRecord);
NamespaceResolver nr = marshalRecord.getNamespaceResolver();
XMLRoot root = null;
if(isXMLRoot) {
root = (XMLRoot)object;
}
marshalRecord.beforeContainmentMarshal(object);
if (!isFragment()) {
String encoding = getEncoding();
String version = DEFAULT_XML_VERSION;
if (!isXMLRoot) {
marshalRecord.setLeafElementType(descriptor.getDefaultRootElementType());
} else {
if (root.getEncoding() != null) {
encoding = root.getEncoding();
}
if (root.getXMLVersion() != null) {
version = root.getXMLVersion();
}
}
marshalRecord.startDocument(encoding, version);
}
if(isXMLRoot) {
if(root.getObject() instanceof Node) {
marshalRecord.node((Node)root.getObject(), new NamespaceResolver());
marshalRecord.endDocument();
return;
}
}
XPathFragment rootFragment = buildRootFragment(object, descriptor, isXMLRoot, marshalRecord);
String schemaLocation = getSchemaLocation();
String noNsSchemaLocation = getNoNamespaceSchemaLocation();
boolean isNil = false;
if (isXMLRoot) {
object = root.getObject();
if (root.getSchemaLocation() != null) {
schemaLocation = root.getSchemaLocation();
}
if (root.getNoNamespaceSchemaLocation() != null) {
noNsSchemaLocation = root.getNoNamespaceSchemaLocation();
}
marshalRecord.setLeafElementType(root.getSchemaType());
isNil = root.isNil();
}
String xsiPrefix = null;
if ((null != getSchemaLocation()) || (null != getNoNamespaceSchemaLocation()) || (isNil)) {
xsiPrefix = nr.resolveNamespaceURI(XMLConstants.SCHEMA_INSTANCE_URL);
if (null == xsiPrefix) {
xsiPrefix = XMLConstants.SCHEMA_INSTANCE_PREFIX;
nr.put(XMLConstants.SCHEMA_INSTANCE_PREFIX, XMLConstants.SCHEMA_INSTANCE_URL);
}
}
TreeObjectBuilder treeObjectBuilder = null;
if (descriptor != null) {
treeObjectBuilder = (TreeObjectBuilder) descriptor.getObjectBuilder();
}
if(session == null){
session = (AbstractSession) xmlContext.getSession(0);
}
marshalRecord.setSession(session);
if (null != rootFragment) {
marshalRecord.startPrefixMappings(nr);
if (!isXMLRoot && descriptor.getNamespaceResolver() == null && rootFragment.hasNamespace()) {
// throw an exception if the name has a : in it but the namespaceresolver is null
throw XMLMarshalException.namespaceResolverNotSpecified(rootFragment.getShortName());
}
marshalRecord.openStartElement(rootFragment, nr);
if (null != schemaLocation) {
marshalRecord.attribute(XMLConstants.SCHEMA_INSTANCE_URL, XMLConstants.SCHEMA_LOCATION, xsiPrefix + XMLConstants.COLON + XMLConstants.SCHEMA_LOCATION, schemaLocation);
}
if (null != noNsSchemaLocation) {
marshalRecord.attribute(XMLConstants.SCHEMA_INSTANCE_URL, XMLConstants.NO_NS_SCHEMA_LOCATION, xsiPrefix + XMLConstants.COLON + XMLConstants.NO_NS_SCHEMA_LOCATION, noNsSchemaLocation);
}
if (isNil) {
marshalRecord.attribute(XMLConstants.SCHEMA_INSTANCE_URL, XMLConstants.SCHEMA_NIL_ATTRIBUTE, xsiPrefix + XMLConstants.COLON + XMLConstants.SCHEMA_NIL_ATTRIBUTE, "true");
}
marshalRecord.namespaceDeclarations(nr);
if (descriptor != null && !isNil) {
treeObjectBuilder.addXsiTypeAndClassIndicatorIfRequired(marshalRecord, descriptor, null, descriptor.getDefaultRootElementField(), root, object, isXMLRoot, true);
treeObjectBuilder.marshalAttributes(marshalRecord, object, session);
}
marshalRecord.closeStartElement();
}
if (treeObjectBuilder != null && !isNil) {
treeObjectBuilder.buildRow(marshalRecord, object, session, this, rootFragment, WriteType.UNDEFINED);
} else if (isXMLRoot) {
//if(null == object) {
//marshalRecord.attribute(XMLConstants.XMLNS_URL, XMLConstants.SCHEMA_INSTANCE_PREFIX, XMLConstants.XMLNS + XMLConstants.COLON + XMLConstants.SCHEMA_INSTANCE_PREFIX, XMLConstants.SCHEMA_INSTANCE_URL);
//marshalRecord.attribute(XMLConstants.SCHEMA_INSTANCE_URL, XMLConstants.SCHEMA_NIL_ATTRIBUTE, XMLConstants.SCHEMA_INSTANCE_PREFIX + XMLConstants.COLON + XMLConstants.SCHEMA_NIL_ATTRIBUTE, "true");
if(object != null && !isNil) {
if(root.getDeclaredType() != null && root.getObject() != null && root.getDeclaredType() != root.getObject().getClass()) {
QName type = (QName)XMLConversionManager.getDefaultJavaTypes().get(object.getClass());
if(type != null) {
xsiPrefix = nr.resolveNamespaceURI(XMLConstants.SCHEMA_INSTANCE_URL);
if (null == xsiPrefix) {
xsiPrefix = XMLConstants.SCHEMA_INSTANCE_PREFIX;
marshalRecord.attribute(XMLConstants.XMLNS_URL, xsiPrefix, XMLConstants.XMLNS + ':' + xsiPrefix, XMLConstants.SCHEMA_INSTANCE_URL);
}