Program: 104410
Project: 104410
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=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);
      }
    }
  }
}

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;
  SipStatusEnum personStatus=null;
  NodeList personList=presence.getElementsByTagName(PERSON_ELEMENT);
  if (personList.getLength() > 1) {
    logger.error("String_Node_Str");
    return;
  }
  if (personList.getLength() > 0) {
    Node personNode=personList.item(0);
    if (personNode.getNodeType() != Node.ELEMENT_NODE) {
      logger.error("String_Node_Str");
      return;
    }
    Element person=(Element)personNode;
    NodeList activityList=person.getElementsByTagName(ACTIVITY_ELEMENT);
    if (activityList.getLength() > 0) {
      Element activity=null;
      for (int i=0; i < activityList.getLength(); i++) {
        Node activityNode=activityList.item(i);
        if (activityNode.getNodeType() != Node.ELEMENT_NODE) {
          continue;
        }
        if (activityNode.getNodeName().equals(AWAY_ELEMENT) || activityNode.getNodeName().equals(BUSY_ELEMENT)) {
          activity=(Element)activityNode;
          break;
        }
      }
      if (activity != null) {
        if (activity.getNodeName().equals(AWAY_ELEMENT)) {
          personStatus=SipStatusEnum.AWAY;
        }
 else         if (activity.getNodeName().equals(BUSY_ELEMENT)) {
          personStatus=SipStatusEnum.DO_NOT_DISTURB;
        }
      }
    }
  }
  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;
      }
      if (personStatus != null) {
        changePresenceStatusForContact(sipcontact,personStatus);
        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;
      NodeList noteList=tuple.getElementsByTagName(NOTE_ELEMENT);
      boolean changed=false;
      for (int k=0; k < noteList.getLength() && !changed; 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);
        }
 else         if (state.equalsIgnoreCase(SipStatusEnum.DO_NOT_DISTURB.getStatusName())) {
          changed=true;
          changePresenceStatusForContact(sipcontact,SipStatusEnum.DO_NOT_DISTURB);
        }
      }
      if (changed == false) {
        if (getTextContent(basic).equalsIgnoreCase(ONLINE_STATUS)) {
          changePresenceStatusForContact(sipcontact,SipStatusEnum.ONLINE);
        }
 else         if (getTextContent(basic).equalsIgnoreCase(OFFLINE_STATUS)) {
          changePresenceStatusForContact(sipcontact,SipStatusEnum.OFFLINE);
        }
      }
    }
  }
}