Program: 4907
Project: 4907
Buggy source:
/** 
 * finds the structure of the document by looking at the names of  the keys in the NameValue List and creates an XML string. <p> The name value list must have the correct ordering.  <p> The values are converted into the correect encoding as per the  XML specifications. So the caller of this method need not  pre-encode the special XML characters into the values. 
 */
public static String constructXML(List namValList,String uri){
  uri=LimeXMLUtils.encodeXML(uri);
  int size=namValList.size();
  String first="String_Node_Str";
  String last="String_Node_Str";
  String prevString="String_Node_Str";
  ArrayList tagsToClose=new ArrayList();
  boolean prevAtt=false;
  boolean rootAtts;
  if (namValList.size() == 0)   return "String_Node_Str";
  NameValue nv=(NameValue)namValList.get(0);
  String n=nv.getName();
  boolean end=n.endsWith(XMLStringUtils.DELIMITER);
  StringTokenizer tok=new StringTokenizer(n,XMLStringUtils.DELIMITER);
  int c=tok.countTokens();
  if (end && c == 2)   rootAtts=true;
 else   rootAtts=false;
  for (int i=0; i < size; i++) {
    NameValue namevalue=(NameValue)namValList.get(i);
    String currString=namevalue.getName();
    String value=LimeXMLUtils.encodeXML((String)namevalue.getValue());
    List currFields=XMLStringUtils.split(currString);
    int commonCount=0;
    List prevFields=null;
    boolean attribute=false;
    if (currString.endsWith(XMLStringUtils.DELIMITER))     attribute=true;
    if (prevAtt && !attribute)     first=first + "String_Node_Str";
    if (i > 0) {
      prevFields=XMLStringUtils.split(prevString);
      commonCount=getCommonCount(currFields,prevFields);
    }
    int z=currFields.size();
    int numPending=tagsToClose.size();
    if (commonCount < numPending) {
      int closeCount=numPending - commonCount;
      int currClose=numPending - 1;
      for (int k=0; k < closeCount; k++) {
        String closeStr=(String)tagsToClose.remove(currClose);
        currClose--;
        last=last + "String_Node_Str" + closeStr+ "String_Node_Str";
      }
    }
    if (!last.equals("String_Node_Str")) {
      first=first + last;
      last="String_Node_Str";
    }
    for (int j=commonCount; j < z - 1; j++) {
      String str=(String)currFields.get(j);
      first=first + "String_Node_Str" + str;
      if (i == 0 && j == 0 && !rootAtts) {
        first=first + "String_Node_Str" + uri+ "String_Node_Str";
      }
 else       if (i == 0 && j == 0 && rootAtts) {
        first=first + "String_Node_Str" + uri+ "String_Node_Str";
      }
      if ((!attribute) && (j > 0 || i > 0))       first=first + "String_Node_Str";
      tagsToClose.add(str);
    }
    String curr=(String)currFields.get(z - 1);
    if (!attribute)     first=first + "String_Node_Str" + curr+ "String_Node_Str"+ value+ "String_Node_Str"+ curr+ "String_Node_Str";
 else {
      first=first + "String_Node_Str" + curr+ "String_Node_Str"+ value+ "String_Node_Str";
      if (i == size - 1)       first=first + "String_Node_Str";
    }
    prevString=currString;
    prevAtt=attribute;
  }
  int stillPending=tagsToClose.size();
  for (int l=stillPending - 1; l >= 0; l--) {
    String tag=(String)tagsToClose.remove(l);
    first=first + "String_Node_Str" + tag+ "String_Node_Str";
  }
  first="String_Node_Str" + first;
  return first;
}

Fixed version (ground truth):
/** 
 * finds the structure of the document by looking at the names of  the keys in the NameValue List and creates an XML string. <p> The name value list must have the correct ordering.  <p> The values are converted into the correect encoding as per the  XML specifications. So the caller of this method need not  pre-encode the special XML characters into the values. 
 */
public static String constructXML(List namValList,String uri){
  uri=LimeXMLUtils.encodeXML(uri);
  int size=namValList.size();
  String first="String_Node_Str";
  String last="String_Node_Str";
  String prevString="String_Node_Str";
  ArrayList tagsToClose=new ArrayList();
  boolean prevAtt=false;
  boolean rootAtts;
  if (namValList.size() == 0)   return "String_Node_Str";
  NameValue nv=(NameValue)namValList.get(0);
  String n=nv.getName();
  boolean end=n.endsWith(XMLStringUtils.DELIMITER);
  StringTokenizer tok=new StringTokenizer(n,XMLStringUtils.DELIMITER);
  int c=tok.countTokens();
  if (end && c == 2)   rootAtts=true;
 else   rootAtts=false;
  for (int i=0; i < size; i++) {
    NameValue namevalue=(NameValue)namValList.get(i);
    String currString=namevalue.getName();
    String value=LimeXMLUtils.encodeXML((String)namevalue.getValue());
    List currFields=XMLStringUtils.split(currString);
    int commonCount=0;
    List prevFields=null;
    boolean attribute=false;
    if (currString.endsWith(XMLStringUtils.DELIMITER))     attribute=true;
    if (prevAtt && !attribute)     first=first + "String_Node_Str";
    if (i > 0) {
      prevFields=XMLStringUtils.split(prevString);
      commonCount=getCommonCount(currFields,prevFields);
    }
    int z=currFields.size();
    int numPending=tagsToClose.size();
    if (commonCount < numPending) {
      int closeCount=numPending - commonCount;
      int currClose=numPending - 1;
      for (int k=0; k < closeCount; k++) {
        String closeStr=(String)tagsToClose.remove(currClose);
        currClose--;
        last=last + "String_Node_Str" + closeStr+ "String_Node_Str";
      }
    }
    if (!last.equals("String_Node_Str")) {
      first=first + last;
      last="String_Node_Str";
    }
    for (int j=commonCount; j < z - 1; j++) {
      String str=(String)currFields.get(j);
      first=first + "String_Node_Str" + str;
      if (i == 0 && j == 0 && !rootAtts) {
        first=first + "String_Node_Str" + uri+ "String_Node_Str";
      }
 else       if (i == 0 && j == 0 && rootAtts) {
        first=first + "String_Node_Str" + uri+ "String_Node_Str";
      }
      if ((!attribute) && (j > 0 || i > 0))       first=first + "String_Node_Str";
      tagsToClose.add(str);
    }
    String curr=(String)currFields.get(z - 1);
    if (!attribute)     first=first + "String_Node_Str" + curr+ "String_Node_Str"+ value+ "String_Node_Str"+ curr+ "String_Node_Str";
 else {
      first=first + "String_Node_Str" + curr+ "String_Node_Str"+ value+ "String_Node_Str";
      if (i == size - 1)       first=first + "String_Node_Str";
    }
    prevString=currString;
    prevAtt=attribute;
  }
  int stillPending=tagsToClose.size();
  for (int l=stillPending - 1; l >= 0; l--) {
    String tag=(String)tagsToClose.remove(l);
    first=first + "String_Node_Str" + tag+ "String_Node_Str";
  }
  first=XML_HEADER + first;
  return first;
}