Program: 104384
Project: 104384
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;
  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;
        }
        activity=(Element)activityNode;
        if (activity.getNodeName().equals(AWAY_ELEMENT)) {
          personStatus=SipStatusEnum.AWAY;
          break;
        }
 else         if (activity.getNodeName().equals(BUSY_ELEMENT)) {
          personStatus=SipStatusEnum.BUSY;
          break;
        }
 else         if (activity.getNodeName().equals(OTP_ELEMENT)) {
          personStatus=SipStatusEnum.ON_THE_PHONE;
          break;
        }
      }
    }
  }
  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);
        Iterator states=SipStatusEnum.supportedStatusSet();
        while (states.hasNext()) {
          SipStatusEnum current=(SipStatusEnum)states.next();
          if (current.getStatusName().equalsIgnoreCase(state)) {
            changed=true;
            changePresenceStatusForContact(sipcontact,current);
            break;
          }
        }
      }
      if (changed == false) {
        if (getTextContent(basic).equalsIgnoreCase(ONLINE_STATUS)) {
          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.getElementsByTagNameNS(NS_VALUE,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.getElementsByTagNameNS(ANY_NS,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.getElementsByTagNameNS(ANY_NS,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;
        }
        activity=(Element)activityNode;
        NodeList statusList=activity.getChildNodes();
        for (int j=0; j < statusList.getLength(); j++) {
          Node statusNode=statusList.item(j);
          if (statusNode.getNodeType() == Node.ELEMENT_NODE) {
            String statusname=statusNode.getLocalName();
            if (statusname.equals(AWAY_ELEMENT)) {
              personStatus=SipStatusEnum.AWAY;
              break;
            }
 else             if (statusname.equals(BUSY_ELEMENT)) {
              personStatus=SipStatusEnum.BUSY;
              break;
            }
 else             if (statusname.equals(OTP_ELEMENT)) {
              personStatus=SipStatusEnum.ON_THE_PHONE;
              break;
            }
          }
        }
        if (personStatus != null)         break;
      }
    }
  }
  NodeList tupleList=presence.getElementsByTagNameNS(NS_VALUE,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.getElementsByTagNameNS(NS_VALUE,CONTACT_ELEMENT);
    Vector sipcontact=new Vector(1,3);
    String contactID=null;
    if (contactList.getLength() == 0) {
      contactID=XMLUtils.getAttribute(presNode,ENTITY_ATTRIBUTE);
      if (contactID.startsWith("String_Node_Str"))       contactID=contactID.substring("String_Node_Str".length());
      Contact tmpContact=resolveContactID(contactID);
      if (tmpContact != null) {
        sipcontact.add(tmpContact);
      }
    }
 else {
      for (int j=0; j < contactList.getLength(); j++) {
        Node contactNode=contactList.item(j);
        if (contactNode.getNodeType() != Node.ELEMENT_NODE) {
          continue;
        }
        Element contact=(Element)contactNode;
        contactID=getTextContent(contact);
        Contact tmpContact=resolveContactID(contactID);
        if (tmpContact != null) {
          sipcontact.add(tmpContact);
        }
      }
    }
    if (sipcontact.isEmpty()) {
      logger.debug("String_Node_Str" + contactID);
      continue;
    }
    if (personStatus != null) {
      for (int j=0; j < sipcontact.size(); j++) {
        changePresenceStatusForContact((ContactSipImpl)sipcontact.get(j),personStatus);
      }
      continue;
    }
    NodeList statusList=tuple.getElementsByTagNameNS(NS_VALUE,STATUS_ELEMENT);
    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);
    Element basic=null;
    if (statusNode == null) {
      logger.debug("String_Node_Str");
    }
 else {
      Element status=(Element)statusNode;
      NodeList basicList=status.getElementsByTagNameNS(NS_VALUE,BASIC_ELEMENT);
      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");
      }
 else {
        basic=(Element)basicNode;
      }
    }
    NodeList noteList=tuple.getElementsByTagNameNS(NS_VALUE,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);
      Iterator states=SipStatusEnum.supportedStatusSet();
      while (states.hasNext()) {
        SipStatusEnum current=(SipStatusEnum)states.next();
        if (current.getStatusName().equalsIgnoreCase(state)) {
          changed=true;
          for (int j=0; j < sipcontact.size(); j++) {
            changePresenceStatusForContact((ContactSipImpl)sipcontact.get(j),current);
          }
          break;
        }
      }
    }
    if (changed == false && basic != null) {
      if (getTextContent(basic).equalsIgnoreCase(ONLINE_STATUS)) {
        for (int j=0; j < sipcontact.size(); j++) {
          changePresenceStatusForContact((ContactSipImpl)sipcontact.get(j),SipStatusEnum.ONLINE);
        }
      }
 else       if (getTextContent(basic).equalsIgnoreCase(OFFLINE_STATUS)) {
        for (int j=0; j < sipcontact.size(); j++) {
          changePresenceStatusForContact((ContactSipImpl)sipcontact.get(j),SipStatusEnum.OFFLINE);
        }
      }
    }
 else {
      if (changed == false) {
        logger.debug("String_Node_Str" + "String_Node_Str");
      }
    }
  }
}