Program: 63936
Project: 63936
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();
  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)));
  }
  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));
  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)));
  }
  return nodes;
}