Program: 68807
Project: 68807
Buggy source:
public NodeList replaceCollection(XMLField xmlField,Node parent,Collection values) throws XMLMarshalException {
  NodeList nodes=null;
  if (xmlField != null) {
    nodes=unmarshalXPathEngine.selectNodes(parent,xmlField,xmlField.getNamespaceResolver());
  }
 else {
    nodes=parent.getChildNodes();
  }
  if (nodes.getLength() == 0) {
    return nodes;
  }
  Iterator collectionValues=values.iterator();
  int i=0;
  int nodesLength=nodes.getLength();
  Vector newNodes=new Vector();
  boolean performedReplace=true;
  Object value=null;
  while ((i < nodesLength) && collectionValues.hasNext()) {
    Node oldChild=nodes.item(i);
    Element newChild=null;
    if (performedReplace) {
      value=collectionValues.next();
    }
    Node parentNode=oldChild.getParentNode();
    if (oldChild.getNodeType() != Node.ELEMENT_NODE) {
      if (((oldChild.getNodeType() == Node.TEXT_NODE) || (oldChild.getNodeType() == Node.CDATA_SECTION_NODE)) && (value == null)) {
        Node grandParentNode=parentNode.getParentNode();
        grandParentNode.removeChild(parentNode);
      }
 else {
        oldChild.setNodeValue((String)XMLConversionManager.getDefaultXMLManager().convertObject(value,ClassConstants.STRING));
      }
    }
 else {
      Element element=(Element)oldChild;
      String elementName=element.getTagName();
      Object valueToWrite=getValueToWrite(value,xmlField);
      XPathFragment childFragment=new XPathFragment(elementName);
      childFragment.setNamespaceURI(element.getNamespaceURI());
      newChild=(Element)createElement(parentNode,childFragment,xmlField,valueToWrite);
      if (!newNodes.contains(oldChild)) {
        if (newChild != oldChild) {
          parentNode.replaceChild(newChild,oldChild);
        }
        newNodes.addElement(newChild);
        performedReplace=true;
      }
 else {
        performedReplace=false;
      }
    }
    i++;
  }
  while (i < nodesLength) {
    Node toRemove=nodes.item(i);
    Node removedParent=toRemove.getParentNode();
    if (removedParent != null && !newNodes.contains(toRemove)) {
      removedParent.removeChild(toRemove);
    }
    i++;
  }
  if (value != null && !performedReplace) {
    if (xmlField.getXPathFragment().getNextFragment() == null || xmlField.getXPathFragment().getHasText()) {
      create(xmlField,parent,value,xmlField,xmlBinderPolicy);
    }
 else {
      create(xmlField,parent,value);
    }
  }
  while (collectionValues.hasNext()) {
    value=collectionValues.next();
    if (xmlField.getXPathFragment().getNextFragment() == null || xmlField.getXPathFragment().getHasText()) {
      create(xmlField,parent,value,xmlField,xmlBinderPolicy);
    }
 else {
      create(xmlField,parent,value);
    }
  }
  return nodes;
}

Fixed version (ground truth):
public NodeList replaceCollection(XMLField xmlField,Node parent,Collection values) throws XMLMarshalException {
  NodeList nodes=null;
  if (xmlField != null) {
    nodes=unmarshalXPathEngine.selectNodes(parent,xmlField,xmlField.getNamespaceResolver());
  }
 else {
    nodes=parent.getChildNodes();
  }
  if (nodes.getLength() == 0) {
    return nodes;
  }
  Iterator collectionValues=values.iterator();
  int i=0;
  int nodesLength=nodes.getLength();
  Vector newNodes=new Vector();
  boolean performedReplace=true;
  Object value=null;
  while ((i < nodesLength) && collectionValues.hasNext()) {
    Node oldChild=nodes.item(i);
    Element newChild=null;
    if (performedReplace) {
      value=collectionValues.next();
    }
    Node parentNode=oldChild.getParentNode();
    if (oldChild.getNodeType() != Node.ELEMENT_NODE) {
      if (((oldChild.getNodeType() == Node.TEXT_NODE) || (oldChild.getNodeType() == Node.CDATA_SECTION_NODE)) && (value == null)) {
        Node grandParentNode=parentNode.getParentNode();
        grandParentNode.removeChild(parentNode);
      }
 else {
        oldChild.setNodeValue((String)XMLConversionManager.getDefaultXMLManager().convertObject(value,ClassConstants.STRING));
      }
    }
 else {
      Element element=(Element)oldChild;
      String elementName=element.getTagName();
      Object valueToWrite=getValueToWrite(value,xmlField);
      XPathFragment childFragment=new XPathFragment(elementName);
      childFragment.setNamespaceURI(element.getNamespaceURI());
      newChild=(Element)createElement(parentNode,childFragment,xmlField,valueToWrite);
      if (!newNodes.contains(oldChild)) {
        if (newChild != oldChild) {
          parentNode.replaceChild(newChild,oldChild);
        }
        newNodes.addElement(newChild);
        performedReplace=true;
      }
 else {
        performedReplace=false;
      }
    }
    i++;
  }
  while (i < nodesLength) {
    Node toRemove=nodes.item(i);
    Node removedParent=toRemove.getParentNode();
    if ((removedParent != null) && !newNodes.contains(toRemove)) {
      removedParent.removeChild(toRemove);
    }
    i++;
  }
  if ((value != null) && !performedReplace) {
    if ((xmlField.getXPathFragment().getNextFragment() == null) || xmlField.getXPathFragment().getHasText()) {
      create(xmlField,parent,value,xmlField,xmlBinderPolicy);
    }
 else {
      create(xmlField,parent,value);
    }
  }
  while (collectionValues.hasNext()) {
    value=collectionValues.next();
    if ((xmlField.getXPathFragment().getNextFragment() == null) || xmlField.getXPathFragment().getHasText()) {
      create(xmlField,parent,value,xmlField,xmlBinderPolicy);
    }
 else {
      create(xmlField,parent,value);
    }
  }
  return nodes;
}