XPathFragment xmlRootFrag = null;
if(objectValue instanceof XMLRoot) {
xmlRootFrag = ((XMLRoot)objectValue).getRootFragment();
}
XMLMarshaller marshaller = marshalRecord.getMarshaller();
if (xmlBinaryDataMapping.getConverter() != null) {
Converter converter = xmlBinaryDataMapping.getConverter();
if (converter instanceof XMLConverter) {
objectValue = ((XMLConverter) converter).convertObjectValueToDataValue(objectValue, session, marshaller);
} else {
objectValue = converter.convertObjectValueToDataValue(objectValue, session);
}
}
XPathFragment groupingFragment = marshalRecord.openStartGroupingElements(namespaceResolver);
if (objectValue == null) {
marshalRecord.closeStartGroupingElements(groupingFragment);
return true;
}
// figure out CID or bytes
String c_id = null;
byte[] bytes = null;
if (xmlBinaryDataMapping.isSwaRef() && (marshaller.getAttachmentMarshaller() != null)) {
//object value should be a DataHandler
if (xmlBinaryDataMapping.getAttributeClassification() == XMLBinaryDataHelper.getXMLBinaryDataHelper().DATA_HANDLER) {
c_id = marshaller.getAttachmentMarshaller().addSwaRefAttachment((DataHandler) objectValue);
if(c_id == null) {
bytes = XMLBinaryDataHelper.getXMLBinaryDataHelper().getBytesForBinaryValue(//
objectValue, marshaller, xmlBinaryDataMapping.getMimeType(object)).getData();
}
} else {
XMLBinaryDataHelper.EncodedData data = XMLBinaryDataHelper.getXMLBinaryDataHelper().getBytesForBinaryValue(//
objectValue, marshaller, xmlBinaryDataMapping.getMimeType(object));
bytes = data.getData();
c_id = marshaller.getAttachmentMarshaller().addSwaRefAttachment(bytes, 0, bytes.length);
}
} else if (marshalRecord.isXOPPackage() && !xmlBinaryDataMapping.shouldInlineBinaryData()) {
XPathFragment lastFrag = ((XMLField) xmlBinaryDataMapping.getField()).getLastXPathFragment();
if(xmlRootFrag != null) {
lastFrag = xmlRootFrag;
}
String localName = null;
String namespaceUri = null;
if(rootFragment != null) {
localName = rootFragment.getLocalName();
namespaceUri = rootFragment.getNamespaceURI();
}
if(!lastFrag.isSelfFragment) {
localName = lastFrag.getLocalName();
namespaceUri = lastFrag.getNamespaceURI();
}
if (objectValue.getClass() == ClassConstants.APBYTE) {
bytes = (byte[]) objectValue;
c_id = marshaller.getAttachmentMarshaller().addMtomAttachment(bytes, 0, bytes.length, this.xmlBinaryDataMapping.getMimeType(object), localName, namespaceUri);
} else if (xmlBinaryDataMapping.getAttributeClassification() == XMLBinaryDataHelper.getXMLBinaryDataHelper().DATA_HANDLER) {
c_id = marshaller.getAttachmentMarshaller().addMtomAttachment((DataHandler) objectValue, localName, namespaceUri);
if(c_id == null) {
bytes = XMLBinaryDataHelper.getXMLBinaryDataHelper().getBytesForBinaryValue(//
objectValue, marshaller, xmlBinaryDataMapping.getMimeType(object)).getData();
}
} else {
XMLBinaryDataHelper.EncodedData data = XMLBinaryDataHelper.getXMLBinaryDataHelper().getBytesForBinaryValue(//
objectValue, marshaller, xmlBinaryDataMapping.getMimeType(object));
bytes = data.getData();
c_id = marshaller.getAttachmentMarshaller().addMtomAttachment(bytes, 0, bytes.length, data.getMimeType(), localName, namespaceUri);
}
}
// handle attributes
if (xPathFragment.isAttribute()) {
// if the CID is null there's nothing to write out
if (c_id != null) {
marshalRecord.attribute(xPathFragment, namespaceResolver, c_id);
}else {
String value = getValueToWrite(((XMLField) xmlBinaryDataMapping.getField()).getSchemaType(), objectValue, session);
marshalRecord.attribute(xPathFragment, namespaceResolver, value);
}
marshalRecord.closeStartGroupingElements(groupingFragment);
return true;
}
marshalRecord.closeStartGroupingElements(groupingFragment);
if(xmlRootFrag != null) {
xPathFragment = xmlRootFrag;
}
if(!xPathFragment.isSelfFragment){
marshalRecord.openStartElement(xPathFragment, namespaceResolver);
marshalRecord.closeStartElement();
}
if (xmlBinaryDataMapping.isSwaRef() && (marshaller.getAttachmentMarshaller() != null)) {
if(c_id != null) {
marshalRecord.characters(c_id);
} else {
String value = getValueToWrite(((XMLField) xmlBinaryDataMapping.getField()).getSchemaType(), bytes, session);
marshalRecord.characters(value);