Program: 68657
Project: 68657
Buggy source:
public void writeFromObjectIntoRow(Object object,AbstractRecord row,AbstractSession session) throws DescriptorException {
  if (this.isReadOnly()) {
    return;
  }
  DOMRecord record=(DOMRecord)row;
  Object attributeValue=this.getAttributeValueFromObject(object);
  Node root=record.getDOM();
  org.w3c.dom.Document doc=record.getDocument();
  XMLField xmlRootField=null;
  if (attributeValue == null) {
    return;
  }
  if (field != null) {
    root=XPathEngine.getInstance().create((XMLField)getField(),record.getDOM());
    DOMRecord rootRecord=new DOMRecord(root);
    rootRecord.setDocPresPolicy(record.getDocPresPolicy());
    rootRecord.setNamespaceResolver(record.getNamespaceResolver());
    rootRecord.setMarshaller(record.getMarshaller());
    record=rootRecord;
  }
  ContainerPolicy cp=this.getContainerPolicy();
  ArrayList childNodes=getUnmappedChildNodes(record.getDOM().getChildNodes());
  Object iter=cp.iteratorFor(attributeValue);
  int childNodeCount=0;
  boolean wasXMLRoot=false;
  while (cp.hasNext(iter) && (childNodeCount < childNodes.size())) {
    Object element=cp.next(iter,session);
    Object originalObject=element;
    Node nextChild=null;
    while (childNodeCount < childNodes.size()) {
      Node nextPossible=(Node)childNodes.get(childNodeCount);
      if ((nextPossible.getNodeType() == Node.ELEMENT_NODE) || (nextPossible.getNodeType() == Node.TEXT_NODE) || (nextPossible.getNodeType() == Node.CDATA_SECTION_NODE)) {
        nextChild=nextPossible;
        childNodeCount++;
        break;
      }
      childNodeCount++;
    }
    if (nextChild == null) {
      break;
    }
    if (usesXMLRoot() && (element instanceof XMLRoot)) {
      xmlRootField=new XMLField();
      wasXMLRoot=true;
      XPathFragment frag=new XPathFragment();
      if ((((XMLRoot)element)).getRootFragment().getNamespaceURI() != null) {
        frag.setNamespaceURI(((XMLRoot)element).getNamespaceURI());
      }
 else {
        frag.setXPath(((XMLRoot)element).getLocalName());
      }
      xmlRootField.setXPathFragment(frag);
      xmlRootField.setNamespaceResolver(record.getNamespaceResolver());
      element=((XMLRoot)element).getObject();
    }
    if (element instanceof String) {
      if (wasXMLRoot) {
        if (((XMLRoot)originalObject).getRootFragment().getNamespaceURI() != null) {
          String prefix=record.getNamespaceResolver().resolveNamespaceURI(((XMLRoot)originalObject).getRootFragment().getNamespaceURI());
          if ((prefix == null) || prefix.equals("String_Node_Str")) {
            xmlRootField.getXPathFragment().setGeneratedPrefix(true);
            prefix=record.getNamespaceResolver().generatePrefix();
          }
          xmlRootField.getXPathFragment().setXPath(prefix + "String_Node_Str" + ((XMLRoot)originalObject).getLocalName());
        }
      }
      if (xmlRootField != null) {
        XPathEngine.getInstance().create(xmlRootField,root,element);
      }
 else {
        Text textNode=doc.createTextNode((String)element);
        root.replaceChild(textNode,nextChild);
      }
    }
 else     if (element instanceof org.w3c.dom.Node) {
      Node importedCopy=doc.importNode((Node)element,true);
      root.replaceChild(importedCopy,nextChild);
    }
 else {
      XMLDescriptor referenceDescriptor=(XMLDescriptor)session.getDescriptor(element.getClass());
      if (wasXMLRoot) {
        if (((XMLRoot)originalObject).getRootFragment().getNamespaceURI() != null) {
          String prefix=referenceDescriptor.getNonNullNamespaceResolver().resolveNamespaceURI(((XMLRoot)originalObject).getNamespaceURI());
          if ((prefix == null) || prefix.equals("String_Node_Str")) {
            prefix=record.getNamespaceResolver().resolveNamespaceURI(((XMLRoot)originalObject).getRootFragment().getNamespaceURI());
          }
          if ((prefix == null) || prefix.equals("String_Node_Str")) {
            xmlRootField.getXPathFragment().setGeneratedPrefix(true);
            prefix=record.getNamespaceResolver().generatePrefix();
          }
          xmlRootField.getXPathFragment().setXPath(prefix + "String_Node_Str" + ((XMLRoot)originalObject).getLocalName());
        }
      }
      DOMRecord nestedRecord=(DOMRecord)buildCompositeRow(element,session,referenceDescriptor,row,xmlRootField,element,wasXMLRoot);
      if (nestedRecord != null) {
        if (nestedRecord.getDOM() != nextChild) {
          root.replaceChild(nestedRecord.getDOM(),nextChild);
        }
      }
    }
  }
  if (childNodeCount < childNodes.size()) {
    for (int i=childNodeCount; i < childNodes.size(); i++) {
      Node child=(Node)childNodes.get(i);
      if ((child.getNodeType() == Node.ELEMENT_NODE) || (child.getNodeType() == Node.TEXT_NODE) || (child.getNodeType() == Node.CDATA_SECTION_NODE)) {
        root.removeChild(child);
      }
    }
  }
  if (cp.hasNext(iter)) {
    while (cp.hasNext(iter)) {
      Object element=cp.next(iter,session);
      writeSingleValue(element,object,record,session);
    }
  }
}

Fixed version (ground truth):
public void writeFromObjectIntoRow(Object object,AbstractRecord row,AbstractSession session) throws DescriptorException {
  if (this.isReadOnly()) {
    return;
  }
  DOMRecord record=(DOMRecord)row;
  Object attributeValue=this.getAttributeValueFromObject(object);
  Node root=record.getDOM();
  org.w3c.dom.Document doc=record.getDocument();
  XMLField xmlRootField=null;
  if (attributeValue == null) {
    return;
  }
  if (field != null) {
    root=XPathEngine.getInstance().create((XMLField)getField(),record.getDOM());
    DOMRecord rootRecord=new DOMRecord(root);
    rootRecord.setDocPresPolicy(record.getDocPresPolicy());
    rootRecord.setNamespaceResolver(record.getNamespaceResolver());
    rootRecord.setMarshaller(record.getMarshaller());
    record=rootRecord;
  }
  ContainerPolicy cp=this.getContainerPolicy();
  ArrayList childNodes=getUnmappedChildNodes(record.getDOM().getChildNodes());
  Object iter=cp.iteratorFor(attributeValue);
  int childNodeCount=0;
  boolean wasXMLRoot=false;
  while (cp.hasNext(iter) && (childNodeCount < childNodes.size())) {
    Object element=cp.next(iter,session);
    if (this.getConverter() != null) {
      element=getConverter().convertObjectValueToDataValue(element,session,record.getMarshaller());
    }
    Object originalObject=element;
    Node nextChild=null;
    while (childNodeCount < childNodes.size()) {
      Node nextPossible=(Node)childNodes.get(childNodeCount);
      if ((nextPossible.getNodeType() == Node.ELEMENT_NODE) || (nextPossible.getNodeType() == Node.TEXT_NODE) || (nextPossible.getNodeType() == Node.CDATA_SECTION_NODE)) {
        nextChild=nextPossible;
        childNodeCount++;
        break;
      }
      childNodeCount++;
    }
    if (nextChild == null) {
      break;
    }
    if (usesXMLRoot() && (element instanceof XMLRoot)) {
      xmlRootField=new XMLField();
      wasXMLRoot=true;
      XPathFragment frag=new XPathFragment();
      if ((((XMLRoot)element)).getRootFragment().getNamespaceURI() != null) {
        frag.setNamespaceURI(((XMLRoot)element).getNamespaceURI());
      }
 else {
        frag.setXPath(((XMLRoot)element).getLocalName());
      }
      xmlRootField.setXPathFragment(frag);
      xmlRootField.setNamespaceResolver(record.getNamespaceResolver());
      element=((XMLRoot)element).getObject();
    }
    if (element instanceof String) {
      if (wasXMLRoot) {
        if (((XMLRoot)originalObject).getRootFragment().getNamespaceURI() != null) {
          String prefix=record.getNamespaceResolver().resolveNamespaceURI(((XMLRoot)originalObject).getRootFragment().getNamespaceURI());
          if ((prefix == null) || prefix.equals("String_Node_Str")) {
            xmlRootField.getXPathFragment().setGeneratedPrefix(true);
            prefix=record.getNamespaceResolver().generatePrefix();
          }
          xmlRootField.getXPathFragment().setXPath(prefix + "String_Node_Str" + ((XMLRoot)originalObject).getLocalName());
        }
      }
      if (xmlRootField != null) {
        XPathEngine.getInstance().create(xmlRootField,root,element);
      }
 else {
        Text textNode=doc.createTextNode((String)element);
        root.replaceChild(textNode,nextChild);
      }
    }
 else     if (element instanceof org.w3c.dom.Node) {
      Node importedCopy=doc.importNode((Node)element,true);
      root.replaceChild(importedCopy,nextChild);
    }
 else {
      XMLDescriptor referenceDescriptor=(XMLDescriptor)session.getDescriptor(element.getClass());
      if (wasXMLRoot) {
        if (((XMLRoot)originalObject).getRootFragment().getNamespaceURI() != null) {
          String prefix=referenceDescriptor.getNonNullNamespaceResolver().resolveNamespaceURI(((XMLRoot)originalObject).getNamespaceURI());
          if ((prefix == null) || prefix.equals("String_Node_Str")) {
            prefix=record.getNamespaceResolver().resolveNamespaceURI(((XMLRoot)originalObject).getRootFragment().getNamespaceURI());
          }
          if ((prefix == null) || prefix.equals("String_Node_Str")) {
            xmlRootField.getXPathFragment().setGeneratedPrefix(true);
            prefix=record.getNamespaceResolver().generatePrefix();
          }
          xmlRootField.getXPathFragment().setXPath(prefix + "String_Node_Str" + ((XMLRoot)originalObject).getLocalName());
        }
      }
      DOMRecord nestedRecord=(DOMRecord)buildCompositeRow(element,session,referenceDescriptor,row,xmlRootField,element,wasXMLRoot);
      if (nestedRecord != null) {
        if (nestedRecord.getDOM() != nextChild) {
          root.replaceChild(nestedRecord.getDOM(),nextChild);
        }
      }
    }
  }
  if (childNodeCount < childNodes.size()) {
    for (int i=childNodeCount; i < childNodes.size(); i++) {
      Node child=(Node)childNodes.get(i);
      if ((child.getNodeType() == Node.ELEMENT_NODE) || (child.getNodeType() == Node.TEXT_NODE) || (child.getNodeType() == Node.CDATA_SECTION_NODE)) {
        root.removeChild(child);
      }
    }
  }
  if (cp.hasNext(iter)) {
    while (cp.hasNext(iter)) {
      Object element=cp.next(iter,session);
      writeSingleValue(element,object,record,session);
    }
  }
}