Program: 65820
Project: 65820
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(XMLField xmlField,Node parent,Object value,AbstractSession 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();
            NamespaceResolver nsr=new NamespaceResolver();
            nsr.setDOM(parentElement);
            String schemaInstancePrefix=resolveNamespacePrefixForURI(XMLConstants.SCHEMA_INSTANCE_URL,nsr);
            if (schemaInstancePrefix == null) {
              nsr=getNamespaceResolverForField(xmlField);
              schemaInstancePrefix=nsr.resolveNamespaceURI(XMLConstants.SCHEMA_INSTANCE_URL);
              if (schemaInstancePrefix == null) {
                schemaInstancePrefix=nsr.generatePrefix(XMLConstants.SCHEMA_INSTANCE_PREFIX);
              }
              parentElement.setAttributeNS(XMLConstants.XMLNS_URL,XMLConstants.XMLNS + XMLConstants.COLON + schemaInstancePrefix,XMLConstants.SCHEMA_INSTANCE_URL);
            }
            parentElement.setAttributeNS(XMLConstants.SCHEMA_INSTANCE_URL,XMLConstants.SCHEMA_INSTANCE_PREFIX + "String_Node_Str" + XMLConstants.SCHEMA_NIL_ATTRIBUTE,XMLConstants.BOOLEAN_STRING_TRUE);
            parentElement.removeChild(node);
          }
 else {
            String stringValue=(String)((XMLConversionManager)session.getDatasourcePlatform().getConversionManager()).convertObject(value,ClassConstants.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(XMLConstants.SCHEMA_INSTANCE_URL,XMLConstants.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(XMLConstants.SCHEMA_INSTANCE_URL,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(XMLField xmlField,Node parent,Object value,AbstractSession 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)((XMLConversionManager)session.getDatasourcePlatform().getConversionManager()).convertObject(value,ClassConstants.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(XMLConstants.SCHEMA_INSTANCE_URL,XMLConstants.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(XMLConstants.SCHEMA_INSTANCE_URL,getNamespaceResolverForField(xmlField)));
  }
  return nodes;
}