Program: 63720
Project: 63720
Buggy source:
/** 
 * Replace the value of the nodes matching <code>xpathString</code> with <code>value</code>. This method handles elements, indexed elements, and attributes.
 * @param xmlField Field containing XPath query string
 * @param parent Parent element
 * @param value New value for the node
 * @return <code>NodeList</code> containing the nodes that were replaced.
 */
public NodeList replaceValue(Field xmlField,Node parent,Object value,CoreAbstractSession session) throws XMLMarshalException {
  NodeList nodes=unmarshalXPathEngine.selectNodes(parent,xmlField,getNamespaceResolverForField(xmlField));
  int numberOfNodes=nodes.getLength();
  if (numberOfNodes == 0 && xmlField.getLastXPathFragment().nameIsText()) {
    nodes=unmarshalXPathEngine.selectNodes(parent,xmlField,getNamespaceResolverForField(xmlField),null,true);
    XMLNodeList textNodes=new XMLNodeList();
    for (int i=0; i < nodes.getLength(); i++) {
      Element nextNode=(Element)nodes.item(i);
      Text text=nextNode.getOwnerDocument().createTextNode("String_Node_Str");
      nextNode.appendChild(text);
      textNodes.add(text);
    }
    numberOfNodes=textNodes.getLength();
  }
  XMLNodeList createdElements=new XMLNodeList();
  for (int i=0; i < numberOfNodes; i++) {
    Node node=nodes.item(i);
    if (node.getNodeType() != Node.ELEMENT_NODE) {
      if (((node.getNodeType() == Node.TEXT_NODE) || (node.getNodeType() == Node.CDATA_SECTION_NODE)) && (value == null)) {
        Node parentNode=node.getParentNode();
        Node grandParentNode=parentNode.getParentNode();
        grandParentNode.removeChild(parentNode);
      }
 else {
        if (value == null) {
          ((Attr)node).getOwnerElement().removeAttributeNode((Attr)node);
        }
 else {
          if (value == XMLRecord.NIL && ((node.getNodeType() == Node.TEXT_NODE) || (node.getNodeType() == Node.CDATA_SECTION_NODE))) {
            Element parentElement=(Element)node.getParentNode();
            addXsiNilToElement(parentElement,xmlField);
            parentElement.removeChild(node);
          }
 else {
            String stringValue=(String)session.getDatasourcePlatform().getConversionManager().convertObject(value,CoreClassConstants.STRING);
            Element parentElement=(Element)node.getParentNode();
            if (parentElement == null && parent.getNodeType() == Node.ELEMENT_NODE) {
              parentElement=(Element)parent;
            }
            if (stringValue.length() == 0 && ((node.getNodeType() == Node.TEXT_NODE) || (node.getNodeType() == Node.CDATA_SECTION_NODE)) && parentElement != null) {
              parentElement.removeChild(node);
            }
 else {
              node.setNodeValue(stringValue);
              if (((node.getNodeType() == Node.TEXT_NODE) || (node.getNodeType() == Node.CDATA_SECTION_NODE)) && parentElement != null) {
                Attr nil=parentElement.getAttributeNodeNS(javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI,Constants.SCHEMA_NIL_ATTRIBUTE);
                if (nil != null) {
                  parentElement.removeAttributeNode(nil);
                }
              }
            }
          }
        }
      }
    }
 else {
      Element element=(Element)node;
      Node parentNode=element.getParentNode();
      if (value == null) {
        parentNode.removeChild(element);
      }
 else {
        String elementName=element.getTagName();
        Element newElement=null;
        Object valueToWrite=getValueToWrite(value,xmlField,session);
        XPathFragment childFrag=new XPathFragment(elementName);
        childFrag.setNamespaceURI(element.getNamespaceURI());
        newElement=(Element)createElement(parentNode,childFrag,xmlField,valueToWrite,session);
        createdElements.add(newElement);
        if (newElement != element) {
          parentNode.replaceChild(newElement,element);
        }
      }
    }
  }
  if (xmlField.isTypedTextField()) {
    addTypeAttributes(createdElements,xmlField,value,resolveNamespacePrefixForURI(javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI,getNamespaceResolverForField(xmlField)),session);
  }
  return nodes;
}

Fixed version (ground truth):
/** 
 * Replace the value of the nodes matching <code>xpathString</code> with <code>value</code>. This method handles elements, indexed elements, and attributes.
 * @param xmlField Field containing XPath query string
 * @param parent Parent element
 * @param value New value for the node
 * @return <code>NodeList</code> containing the nodes that were replaced.
 */
public NodeList replaceValue(Field xmlField,Node parent,Object value,CoreAbstractSession session) throws XMLMarshalException {
  NodeList nodes=unmarshalXPathEngine.selectNodes(parent,xmlField,getNamespaceResolverForField(xmlField),null,false,false);
  int numberOfNodes=nodes.getLength();
  if (numberOfNodes == 0 && xmlField.getLastXPathFragment().nameIsText()) {
    nodes=unmarshalXPathEngine.selectNodes(parent,xmlField,getNamespaceResolverForField(xmlField),null,true);
    XMLNodeList textNodes=new XMLNodeList();
    for (int i=0; i < nodes.getLength(); i++) {
      Element nextNode=(Element)nodes.item(i);
      Text text=nextNode.getOwnerDocument().createTextNode("String_Node_Str");
      nextNode.appendChild(text);
      textNodes.add(text);
    }
    numberOfNodes=textNodes.getLength();
    nodes=textNodes;
  }
  XMLNodeList createdElements=new XMLNodeList();
  for (int i=0; i < numberOfNodes; i++) {
    Node node=nodes.item(i);
    if (node.getNodeType() != Node.ELEMENT_NODE) {
      if (((node.getNodeType() == Node.TEXT_NODE) || (node.getNodeType() == Node.CDATA_SECTION_NODE)) && (value == null)) {
        Node parentNode=node.getParentNode();
        if (parentNode != null) {
          Node grandParentNode=parentNode.getParentNode();
          NodeList childNodes=parentNode.getChildNodes();
          if (childNodes.getLength() == numberOfNodes) {
            grandParentNode.removeChild(parentNode);
          }
 else {
            for (int x=0; x < childNodes.getLength(); x++) {
              Node next=childNodes.item(x);
              if (next.getNodeType() == Node.TEXT_NODE || next.getNodeType() == Node.CDATA_SECTION_NODE) {
                parentNode.removeChild(next);
              }
            }
          }
        }
      }
 else {
        if (value == null) {
          ((Attr)node).getOwnerElement().removeAttributeNode((Attr)node);
        }
 else {
          if (value == XMLRecord.NIL && ((node.getNodeType() == Node.TEXT_NODE) || (node.getNodeType() == Node.CDATA_SECTION_NODE))) {
            Element parentElement=(Element)node.getParentNode();
            addXsiNilToElement(parentElement,xmlField);
            parentElement.removeChild(node);
          }
 else {
            String stringValue=(String)session.getDatasourcePlatform().getConversionManager().convertObject(value,CoreClassConstants.STRING);
            Element parentElement=(Element)node.getParentNode();
            if (parentElement == null && parent.getNodeType() == Node.ELEMENT_NODE) {
              parentElement=(Element)parent;
            }
            if (stringValue.length() == 0 && ((node.getNodeType() == Node.TEXT_NODE) || (node.getNodeType() == Node.CDATA_SECTION_NODE)) && parentElement != null) {
              parentElement.removeChild(node);
            }
 else {
              node.setNodeValue(stringValue);
              if (((node.getNodeType() == Node.TEXT_NODE) || (node.getNodeType() == Node.CDATA_SECTION_NODE)) && parentElement != null) {
                Attr nil=parentElement.getAttributeNodeNS(javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI,Constants.SCHEMA_NIL_ATTRIBUTE);
                if (nil != null) {
                  parentElement.removeAttributeNode(nil);
                }
              }
            }
          }
        }
      }
    }
 else {
      Element element=(Element)node;
      Node parentNode=element.getParentNode();
      if (value == null) {
        parentNode.removeChild(element);
      }
 else {
        String elementName=element.getTagName();
        Element newElement=null;
        Object valueToWrite=getValueToWrite(value,xmlField,session);
        XPathFragment childFrag=new XPathFragment(elementName);
        childFrag.setNamespaceURI(element.getNamespaceURI());
        newElement=(Element)createElement(parentNode,childFrag,xmlField,valueToWrite,session);
        createdElements.add(newElement);
        if (newElement != element) {
          parentNode.replaceChild(newElement,element);
        }
      }
    }
  }
  if (xmlField.isTypedTextField()) {
    addTypeAttributes(createdElements,xmlField,value,resolveNamespacePrefixForURI(javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI,getNamespaceResolverForField(xmlField)),session);
  }
  return nodes;
}