Program: 66715
Project: 66715
Buggy source:
public Object getValueToWrite(Object value,Object parent,XMLRecord record,XMLField field,XMLField includeField,AbstractSession session){
  XMLMarshaller marshaller=record.getMarshaller();
  Object element=value;
  boolean isAttribute=((XMLField)getField()).getLastXPathFragment().isAttribute();
  if (getValueConverter() != null) {
    Converter converter=getValueConverter();
    if (converter instanceof XMLConverter) {
      element=((XMLConverter)converter).convertObjectValueToDataValue(element,session,record.getMarshaller());
    }
 else {
      element=converter.convertObjectValueToDataValue(element,session);
    }
  }
  if (isAttribute) {
    if (isSwaRef() && (marshaller.getAttachmentMarshaller() != null)) {
      try {
        String id=marshaller.getAttachmentMarshaller().addSwaRefAttachment((DataHandler)element);
        element=id;
      }
 catch (      ClassCastException cce) {
        throw XMLMarshalException.invalidSwaRefAttribute(getAttributeClassification().getName());
      }
    }
 else {
      XMLBinaryDataHelper.EncodedData data=XMLBinaryDataHelper.getXMLBinaryDataHelper().getBytesForBinaryValue(element,record.getMarshaller(),mimeTypePolicy.getMimeType(parent));
      String base64Value=((XMLConversionManager)session.getDatasourcePlatform().getConversionManager()).buildBase64StringFromBytes(data.getData());
      element=base64Value;
    }
  }
 else {
    if (record.isXOPPackage() && !isSwaRef() && !shouldInlineBinaryData()) {
      String c_id=XMLConstants.EMPTY_STRING;
      byte[] bytes=null;
      if ((getCollectionContentType() == ClassConstants.ABYTE) || (getCollectionContentType() == ClassConstants.APBYTE)) {
        if (getCollectionContentType() == ClassConstants.ABYTE) {
          element=session.getDatasourcePlatform().getConversionManager().convertObject(element,ClassConstants.APBYTE);
        }
        bytes=(byte[])element;
        c_id=marshaller.getAttachmentMarshaller().addMtomAttachment(bytes,0,bytes.length,this.mimeTypePolicy.getMimeType(parent),field.getLastXPathFragment().getLocalName(),field.getLastXPathFragment().getNamespaceURI());
      }
 else       if (getCollectionContentType() == XMLBinaryDataHelper.getXMLBinaryDataHelper().DATA_HANDLER) {
        c_id=marshaller.getAttachmentMarshaller().addMtomAttachment((DataHandler)element,field.getLastXPathFragment().getLocalName(),field.getLastXPathFragment().getNamespaceURI());
        if (c_id == null) {
          XMLBinaryDataHelper.EncodedData data=XMLBinaryDataHelper.getXMLBinaryDataHelper().getBytesForBinaryValue(element,marshaller,this.mimeTypePolicy.getMimeType(parent));
          bytes=data.getData();
        }
      }
 else {
        XMLBinaryDataHelper.EncodedData data=XMLBinaryDataHelper.getXMLBinaryDataHelper().getBytesForBinaryValue(element,marshaller,this.mimeTypePolicy.getMimeType(parent));
        bytes=data.getData();
        c_id=marshaller.getAttachmentMarshaller().addMtomAttachment(bytes,0,bytes.length,data.getMimeType(),field.getLastXPathFragment().getLocalName(),field.getLastXPathFragment().getNamespaceURI());
      }
      if (c_id == null) {
        element=bytes;
      }
 else {
        DOMRecord include=new DOMRecord(field.getLastXPathFragment().getLocalName());
        include.setSession(session);
        include.put(includeField,c_id);
        element=include;
        NamespaceResolver resolver=((XMLField)getField()).getNamespaceResolver();
        if (resolver == null || resolver.resolveNamespaceURI(XMLConstants.XOP_URL) == null) {
          resolver=new NamespaceResolver();
          resolver.put(XMLConstants.XOP_PREFIX,XMLConstants.XOP_URL);
          String xpath=XMLConstants.XOP_PREFIX + XMLConstants.COLON + INCLUDE;
          XMLField incField=new XMLField(xpath);
          incField.setNamespaceResolver(resolver);
          Object obj=include.getIndicatingNoEntry(incField);
          if (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 + XMLConstants.XOP_PREFIX,XMLConstants.XOP_URL);
            }
          }
        }
      }
    }
 else     if (isSwaRef() && (marshaller.getAttachmentMarshaller() != null)) {
      try {
        String c_id=marshaller.getAttachmentMarshaller().addSwaRefAttachment((DataHandler)element);
        element=c_id;
      }
 catch (      Exception ex) {
      }
    }
 else {
      if (!((getCollectionContentType() == ClassConstants.ABYTE) || (getCollectionContentType() == ClassConstants.APBYTE))) {
        element=XMLBinaryDataHelper.getXMLBinaryDataHelper().getBytesForBinaryValue(element,marshaller,this.mimeTypePolicy.getMimeType(parent)).getData();
      }
    }
  }
  return element;
}

Fixed version (ground truth):
public Object getValueToWrite(Object value,Object parent,XMLRecord record,XMLField field,XMLField includeField,AbstractSession session){
  XMLMarshaller marshaller=record.getMarshaller();
  Object element=value;
  boolean isAttribute=((XMLField)getField()).getLastXPathFragment().isAttribute();
  if (getValueConverter() != null) {
    Converter converter=getValueConverter();
    if (converter instanceof XMLConverter) {
      element=((XMLConverter)converter).convertObjectValueToDataValue(element,session,record.getMarshaller());
    }
 else {
      element=converter.convertObjectValueToDataValue(element,session);
    }
  }
  if (isAttribute) {
    if (isSwaRef() && (marshaller.getAttachmentMarshaller() != null)) {
      try {
        String id=marshaller.getAttachmentMarshaller().addSwaRefAttachment((DataHandler)element);
        element=id;
      }
 catch (      ClassCastException cce) {
        throw XMLMarshalException.invalidSwaRefAttribute(getAttributeClassification().getName());
      }
    }
 else {
      XMLBinaryDataHelper.EncodedData data=XMLBinaryDataHelper.getXMLBinaryDataHelper().getBytesForBinaryValue(element,record.getMarshaller(),mimeTypePolicy.getMimeType(parent));
      String base64Value=((XMLConversionManager)session.getDatasourcePlatform().getConversionManager()).buildBase64StringFromBytes(data.getData());
      element=base64Value;
    }
  }
 else {
    if (record.isXOPPackage() && !isSwaRef() && !shouldInlineBinaryData()) {
      String c_id=XMLConstants.EMPTY_STRING;
      byte[] bytes=null;
      if ((getAttributeElementClass() == ClassConstants.ABYTE) || (getAttributeElementClass() == ClassConstants.APBYTE)) {
        if (getAttributeElementClass() == ClassConstants.ABYTE) {
          element=session.getDatasourcePlatform().getConversionManager().convertObject(element,ClassConstants.APBYTE);
        }
        bytes=(byte[])element;
        c_id=marshaller.getAttachmentMarshaller().addMtomAttachment(bytes,0,bytes.length,this.mimeTypePolicy.getMimeType(parent),field.getLastXPathFragment().getLocalName(),field.getLastXPathFragment().getNamespaceURI());
      }
 else       if (getAttributeElementClass() == XMLBinaryDataHelper.getXMLBinaryDataHelper().DATA_HANDLER) {
        c_id=marshaller.getAttachmentMarshaller().addMtomAttachment((DataHandler)element,field.getLastXPathFragment().getLocalName(),field.getLastXPathFragment().getNamespaceURI());
        if (c_id == null) {
          XMLBinaryDataHelper.EncodedData data=XMLBinaryDataHelper.getXMLBinaryDataHelper().getBytesForBinaryValue(element,marshaller,this.mimeTypePolicy.getMimeType(parent));
          bytes=data.getData();
        }
      }
 else {
        XMLBinaryDataHelper.EncodedData data=XMLBinaryDataHelper.getXMLBinaryDataHelper().getBytesForBinaryValue(element,marshaller,this.mimeTypePolicy.getMimeType(parent));
        bytes=data.getData();
        c_id=marshaller.getAttachmentMarshaller().addMtomAttachment(bytes,0,bytes.length,data.getMimeType(),field.getLastXPathFragment().getLocalName(),field.getLastXPathFragment().getNamespaceURI());
      }
      if (c_id == null) {
        element=bytes;
      }
 else {
        DOMRecord include=new DOMRecord(field.getLastXPathFragment().getLocalName());
        include.setSession(session);
        include.put(includeField,c_id);
        element=include;
        NamespaceResolver resolver=((XMLField)getField()).getNamespaceResolver();
        if (resolver == null || resolver.resolveNamespaceURI(XMLConstants.XOP_URL) == null) {
          resolver=new NamespaceResolver();
          resolver.put(XMLConstants.XOP_PREFIX,XMLConstants.XOP_URL);
          String xpath=XMLConstants.XOP_PREFIX + XMLConstants.COLON + INCLUDE;
          XMLField incField=new XMLField(xpath);
          incField.setNamespaceResolver(resolver);
          Object obj=include.getIndicatingNoEntry(incField);
          if (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 + XMLConstants.XOP_PREFIX,XMLConstants.XOP_URL);
            }
          }
        }
      }
    }
 else     if (isSwaRef() && (marshaller.getAttachmentMarshaller() != null)) {
      try {
        String c_id=marshaller.getAttachmentMarshaller().addSwaRefAttachment((DataHandler)element);
        element=c_id;
      }
 catch (      Exception ex) {
      }
    }
 else {
      if (!((getAttributeElementClass() == ClassConstants.ABYTE) || (getAttributeElementClass() == ClassConstants.APBYTE))) {
        element=XMLBinaryDataHelper.getXMLBinaryDataHelper().getBytesForBinaryValue(element,marshaller,this.mimeTypePolicy.getMimeType(parent)).getData();
      }
    }
  }
  return element;
}