}
writeSingleValue(attributeValue, object, (XMLRecord) row, session);
}
public void writeSingleValue(Object attributeValue, Object parent, XMLRecord record, AbstractSession session) {
XMLMarshaller marshaller = record.getMarshaller();
if (getConverter() != null) {
Converter converter = getConverter();
if (converter instanceof XMLConverter) {
attributeValue = ((XMLConverter) converter).convertObjectValueToDataValue(attributeValue, session, record.getMarshaller());
} else {
attributeValue = converter.convertObjectValueToDataValue(attributeValue, session);
}
}
XMLField field = (XMLField) getField();
if (field.getLastXPathFragment().isAttribute()) {
if (isSwaRef() && (marshaller.getAttachmentMarshaller() != null)) {
//should be a DataHandler here
try {
String value = null;
if (getAttributeClassification() == XMLBinaryDataHelper.getXMLBinaryDataHelper().DATA_HANDLER) {
value = marshaller.getAttachmentMarshaller().addSwaRefAttachment((DataHandler) attributeValue);
} else {
XMLBinaryDataHelper.EncodedData data = XMLBinaryDataHelper.getXMLBinaryDataHelper().getBytesForBinaryValue(//
attributeValue, marshaller, getMimeType(parent));
byte[] bytes = data.getData();
value = marshaller.getAttachmentMarshaller().addSwaRefAttachment(bytes, 0, bytes.length);
}
record.put(field, value);
} catch (ClassCastException cce) {
throw XMLMarshalException.invalidSwaRefAttribute(getAttributeClassification().getName());
}
} else {
//inline case
XMLBinaryDataHelper.EncodedData data = XMLBinaryDataHelper.getXMLBinaryDataHelper().getBytesForBinaryValue(attributeValue, record.getMarshaller(), getMimeType(parent));
String base64Value = ((XMLConversionManager) session.getDatasourcePlatform().getConversionManager()).buildBase64StringFromBytes(data.getData());
record.put(field, base64Value);
}
return;
}
if (record.isXOPPackage() && !isSwaRef() && !shouldInlineBinaryData()) {
//write as attachment
String c_id = XMLConstants.EMPTY_STRING;
byte[] bytes = null;
String elementName = field.getLastXPathFragment().getLocalName();
String namespaceUri = field.getLastXPathFragment().getNamespaceURI();
if(field.getLastXPathFragment().isSelfFragment()) {
//If it's a self mapping, get the element from the DOM record
DOMRecord domRecord = (DOMRecord)record;
if(domRecord.getDOM().getNodeType() == Node.ELEMENT_NODE) {
elementName = domRecord.getDOM().getLocalName();
namespaceUri = domRecord.getDOM().getNamespaceURI();
}
}
if ((getAttributeClassification() == ClassConstants.ABYTE) || (getAttributeClassification() == ClassConstants.APBYTE)) {
if (getAttributeClassification() == ClassConstants.ABYTE) {
attributeValue = ((XMLConversionManager) session.getDatasourcePlatform().getConversionManager()).convertObject(attributeValue, ClassConstants.APBYTE);
}
bytes = (byte[])attributeValue;
c_id = marshaller.getAttachmentMarshaller().addMtomAttachment(//
bytes, 0,//
bytes.length,//
this.getMimeType(parent),//
elementName,//
namespaceUri);//
} else if (getAttributeClassification() == XMLBinaryDataHelper.getXMLBinaryDataHelper().DATA_HANDLER) {
c_id = marshaller.getAttachmentMarshaller().addMtomAttachment(//
(DataHandler) attributeValue, elementName, namespaceUri);
if(c_id == null) {
//get the bytes so we can write it out inline
XMLBinaryDataHelper.EncodedData data = XMLBinaryDataHelper.getXMLBinaryDataHelper().getBytesForBinaryValue(//
attributeValue, marshaller, getMimeType(parent));
bytes = data.getData();
}
} else {
XMLBinaryDataHelper.EncodedData data = XMLBinaryDataHelper.getXMLBinaryDataHelper().getBytesForBinaryValue(//
attributeValue, marshaller, getMimeType(parent));
bytes = data.getData();
c_id = marshaller.getAttachmentMarshaller().addMtomAttachment(bytes, 0,//
bytes.length,//
data.getMimeType(),//
elementName,//
namespaceUri);
}
if(c_id == null) {
XMLField textField = null;
if(field.isSelfField()){
textField = new XMLField(XMLConstants.TEXT);
}else{
textField = new XMLField(field.getXPath() + '/' + XMLConstants.TEXT);
}
textField.setNamespaceResolver(field.getNamespaceResolver());
textField.setSchemaType(field.getSchemaType());
record.put(textField, bytes);
//write out bytes inline
} else {
String xpath = this.getXPath();
String prefix = null;
boolean prefixAlreadyDefined = false;
// If the field's resolver is non-null and has an entry for XOP,
// use it - otherwise, create a new resolver, set the XOP entry,
// on it, and use it instead.
// We do this to avoid setting the XOP namespace declaration on
// a given field or descriptor's resolver, as it is only required
// on the current element
NamespaceResolver resolver = field.getNamespaceResolver();
if (resolver != null) {
prefix = resolver.resolveNamespaceURI(XMLConstants.XOP_URL);
}
if (prefix == null) {
prefix = XMLConstants.XOP_PREFIX;
resolver = new NamespaceResolver();
resolver.put(prefix, XMLConstants.XOP_URL);
} else {
prefixAlreadyDefined = true;
}
String incxpath = null;
if(field.isSelfField()){
incxpath = prefix + ":Include";
xpath = (prefix + include);
}else{
incxpath = xpath + '/' + prefix + ":Include";
xpath += ('/' + prefix + include);
}
XMLField xpathField = new XMLField(xpath);
xpathField.setNamespaceResolver(resolver);
record.put(xpathField, c_id);
// Need to call setAttributeNS on the record, unless the xop prefix
// is defined on the descriptor's resolver already
XMLField incField = new XMLField(incxpath);
incField.setNamespaceResolver(resolver);
Object obj = record.getIndicatingNoEntry(incField);
if (!prefixAlreadyDefined && obj != null && obj instanceof DOMRecord) {
if (((DOMRecord) obj).getDOM().getNodeType() == Node.ELEMENT_NODE) {
((Element) ((DOMRecord) obj).getDOM()).setAttributeNS(XMLConstants.XMLNS_URL, XMLConstants.XMLNS + XMLConstants.COLON + prefix, XMLConstants.XOP_URL);
}
}
}
} else if (isSwaRef() && (marshaller.getAttachmentMarshaller() != null)) {
//AttributeValue should be a data-handler
try {
String c_id = null;
if (getAttributeClassification() == XMLBinaryDataHelper.getXMLBinaryDataHelper().DATA_HANDLER) {
c_id = marshaller.getAttachmentMarshaller().addSwaRefAttachment((DataHandler) attributeValue);
} else {
XMLBinaryDataHelper.EncodedData data = XMLBinaryDataHelper.getXMLBinaryDataHelper().getBytesForBinaryValue(//
attributeValue, marshaller, getMimeType(parent));
byte[] bytes = data.getData();
c_id = marshaller.getAttachmentMarshaller().addSwaRefAttachment(bytes, 0, bytes.length);
}
XMLField textField = new XMLField(field.getXPath() + '/' + XMLConstants.TEXT);
textField.setNamespaceResolver(field.getNamespaceResolver());
textField.setSchemaType(field.getSchemaType());
record.put(textField, c_id);