Program: 104412
Project: 104412
Buggy source:
/** 
 * Sets the contact's presence status using the PIDF document provided. In case of conflict (more than one status per contact) the last valid status in the document is used. This implementation is very tolerant to be more compatible with bad implementations of SIMPLE. The limit of the tolerance is defined by the CPU cost: as far as the tolerance costs nothing more in well structured documents, we do it.
 * @param presenceDoc the pidf document to use
 */
public void setPidfPresenceStatus(String presenceDoc){
  Document doc=convertDocument(presenceDoc);
  if (doc == null) {
    return;
  }
  logger.debug("String_Node_Str" + presenceDoc);
  NodeList presList=doc.getElementsByTagName(PRESENCE_ELEMENT);
  if (presList.getLength() == 0) {
    logger.error("String_Node_Str");
    return;
  }
  if (presList.getLength() > 1) {
    logger.warn("String_Node_Str");
  }
  Node presNode=presList.item(0);
  if (presNode.getNodeType() != Node.ELEMENT_NODE) {
    logger.error("String_Node_Str");
    return;
  }
  Element presence=(Element)presNode;
  NodeList tupleList=presence.getElementsByTagName(TUPLE_ELEMENT);
  for (int i=0; i < tupleList.getLength(); i++) {
    Node tupleNode=tupleList.item(i);
    if (tupleNode.getNodeType() != Node.ELEMENT_NODE) {
      continue;
    }
    Element tuple=(Element)tupleNode;
    NodeList contactList=tuple.getElementsByTagName(CONTACT_ELEMENT);
    for (int j=0; j < contactList.getLength(); j++) {
      Node contactNode=contactList.item(j);
      if (contactNode.getNodeType() != Node.ELEMENT_NODE) {
        continue;
      }
      Element contact=(Element)contactNode;
      ContactSipImpl sipcontact=(ContactSipImpl)resolveContactID(getTextContent(contact));
      if (sipcontact == null) {
        logger.debug("String_Node_Str" + getTextContent(contact));
        continue;
      }
      NodeList statusList=tuple.getElementsByTagName(STATUS_ELEMENT);
      if (statusList.getLength() == 0) {
        logger.debug("String_Node_Str");
        continue;
      }
      int index=statusList.getLength() - 1;
      Node statusNode=null;
      do {
        Node temp=statusList.item(index);
        if (temp.getNodeType() == Node.ELEMENT_NODE) {
          statusNode=temp;
          break;
        }
        index--;
      }
 while (index >= 0);
      if (statusNode == null) {
        logger.debug("String_Node_Str");
        break;
      }
      Element status=(Element)statusNode;
      NodeList basicList=status.getElementsByTagName(BASIC_ELEMENT);
      if (basicList.getLength() == 0) {
        logger.debug("String_Node_Str");
        continue;
      }
      index=basicList.getLength() - 1;
      Node basicNode=null;
      do {
        Node temp=statusList.item(index);
        if (temp.getNodeType() == Node.ELEMENT_NODE) {
          basicNode=temp;
          break;
        }
        index--;
      }
 while (index >= 0);
      if (basicNode == null) {
        logger.debug("String_Node_Str");
        break;
      }
      Element basic=(Element)basicNode;
      if (getTextContent(basic).equalsIgnoreCase(ONLINE_STATUS)) {
        NodeList noteList=tuple.getElementsByTagName(NOTE_ELEMENT);
        boolean changed=false;
        for (int k=0; k < noteList.getLength(); k++) {
          Node noteNode=noteList.item(k);
          if (noteNode.getNodeType() != Node.ELEMENT_NODE) {
            continue;
          }
          Element note=(Element)noteNode;
          String state=getTextContent(note);
          if (state.equalsIgnoreCase(SipStatusEnum.AWAY.getStatusName())) {
            changed=true;
            changePresenceStatusForContact(sipcontact,SipStatusEnum.AWAY);
          }
        }
        if (changed == false) {
          changePresenceStatusForContact(sipcontact,SipStatusEnum.ONLINE);
        }
      }
 else       if (getTextContent(basic).equalsIgnoreCase(OFFLINE_STATUS)) {
        changePresenceStatusForContact(sipcontact,SipStatusEnum.OFFLINE);
      }
    }
  }
}

Fixed version (ground truth):
/** 
 * Sets the contact's presence status using the PIDF document provided. In case of conflict (more than one status per contact) the last valid status in the document is used. This implementation is very tolerant to be more compatible with bad implementations of SIMPLE. The limit of the tolerance is defined by the CPU cost: as far as the tolerance costs nothing more in well structured documents, we do it.
 * @param presenceDoc the pidf document to use
 */
public void setPidfPresenceStatus(String presenceDoc){
  Document doc=convertDocument(presenceDoc);
  if (doc == null) {
    return;
  }
  logger.debug("String_Node_Str" + presenceDoc);
  NodeList presList=doc.getElementsByTagName(PRESENCE_ELEMENT);
  if (presList.getLength() == 0) {
    logger.error("String_Node_Str");
    return;
  }
  if (presList.getLength() > 1) {
    logger.warn("String_Node_Str");
  }
  Node presNode=presList.item(0);
  if (presNode.getNodeType() != Node.ELEMENT_NODE) {
    logger.error("String_Node_Str");
    return;
  }
  Element presence=(Element)presNode;
  NodeList tupleList=presence.getElementsByTagName(TUPLE_ELEMENT);
  for (int i=0; i < tupleList.getLength(); i++) {
    Node tupleNode=tupleList.item(i);
    if (tupleNode.getNodeType() != Node.ELEMENT_NODE) {
      continue;
    }
    Element tuple=(Element)tupleNode;
    NodeList contactList=tuple.getElementsByTagName(CONTACT_ELEMENT);
    for (int j=0; j < contactList.getLength(); j++) {
      Node contactNode=contactList.item(j);
      if (contactNode.getNodeType() != Node.ELEMENT_NODE) {
        continue;
      }
      Element contact=(Element)contactNode;
      ContactSipImpl sipcontact=(ContactSipImpl)resolveContactID(getTextContent(contact));
      if (sipcontact == null) {
        logger.debug("String_Node_Str" + getTextContent(contact));
        continue;
      }
      NodeList statusList=tuple.getElementsByTagName(STATUS_ELEMENT);
      if (statusList.getLength() == 0) {
        logger.debug("String_Node_Str");
        continue;
      }
      int index=statusList.getLength() - 1;
      Node statusNode=null;
      do {
        Node temp=statusList.item(index);
        if (temp.getNodeType() == Node.ELEMENT_NODE) {
          statusNode=temp;
          break;
        }
        index--;
      }
 while (index >= 0);
      if (statusNode == null) {
        logger.debug("String_Node_Str");
        break;
      }
      Element status=(Element)statusNode;
      NodeList basicList=status.getElementsByTagName(BASIC_ELEMENT);
      if (basicList.getLength() == 0) {
        logger.debug("String_Node_Str");
        continue;
      }
      index=basicList.getLength() - 1;
      Node basicNode=null;
      do {
        Node temp=basicList.item(index);
        if (temp.getNodeType() == Node.ELEMENT_NODE) {
          basicNode=temp;
          break;
        }
        index--;
      }
 while (index >= 0);
      if (basicNode == null) {
        logger.debug("String_Node_Str");
        break;
      }
      Element basic=(Element)basicNode;
      if (getTextContent(basic).equalsIgnoreCase(ONLINE_STATUS)) {
        NodeList noteList=tuple.getElementsByTagName(NOTE_ELEMENT);
        boolean changed=false;
        for (int k=0; k < noteList.getLength(); k++) {
          Node noteNode=noteList.item(k);
          if (noteNode.getNodeType() != Node.ELEMENT_NODE) {
            continue;
          }
          Element note=(Element)noteNode;
          String state=getTextContent(note);
          if (state.equalsIgnoreCase(SipStatusEnum.AWAY.getStatusName())) {
            changed=true;
            changePresenceStatusForContact(sipcontact,SipStatusEnum.AWAY);
          }
        }
        if (changed == false) {
          changePresenceStatusForContact(sipcontact,SipStatusEnum.ONLINE);
        }
      }
 else       if (getTextContent(basic).equalsIgnoreCase(OFFLINE_STATUS)) {
        changePresenceStatusForContact(sipcontact,SipStatusEnum.OFFLINE);
      }
    }
  }
}