Program: 3254
Project: 3254
Buggy source:
/** 
 * Take a node list of communities and build the structure from them, delegating to the relevant methods in this class for sub-communities and collections
 * @param context the context of the request
 * @param communities a nodelist of communities to create along with their subjstructures
 * @param parent the parent community of the nodelist of communities to create
 * @return an element array containing additional information regarding the created communities (e.g. the handles they have been assigned)
 */
private static Element[] handleCommunities(Context context,NodeList communities,Community parent) throws TransformerException, SQLException, Exception {
  Element[] elements=new Element[communities.getLength()];
  for (int i=0; i < communities.getLength(); i++) {
    Community community;
    Element element=new Element("String_Node_Str");
    if (parent != null) {
      community=parent.createSubcommunity();
    }
 else {
      community=Community.create(null,context);
    }
    community.setMetadata("String_Node_Str","String_Node_Str");
    Node tn=communities.item(i);
    Enumeration keys=communityMap.keys();
    while (keys.hasMoreElements()) {
      Node node=null;
      String key=(String)keys.nextElement();
      NodeList nl=XPathAPI.selectNodeList(tn,key);
      if (nl.getLength() == 1) {
        node=nl.item(0);
        community.setMetadata((String)communityMap.get(key),getStringValue(node));
      }
    }
    community.update();
    element.setAttribute("String_Node_Str",community.getHandle());
    Element nameElement=new Element("String_Node_Str");
    nameElement.setText(community.getMetadata("String_Node_Str"));
    element.addContent(nameElement);
    if (community.getMetadata("String_Node_Str") != null) {
      Element descriptionElement=new Element("String_Node_Str");
      descriptionElement.setText(community.getMetadata("String_Node_Str"));
      element.addContent(descriptionElement);
    }
    if (community.getMetadata("String_Node_Str") != null) {
      Element introElement=new Element("String_Node_Str");
      introElement.setText(community.getMetadata("String_Node_Str"));
      element.addContent(introElement);
    }
    if (community.getMetadata("String_Node_Str") != null) {
      Element copyrightElement=new Element("String_Node_Str");
      copyrightElement.setText(community.getMetadata("String_Node_Str"));
      element.addContent(copyrightElement);
    }
    if (community.getMetadata("String_Node_Str") != null) {
      Element sidebarElement=new Element("String_Node_Str");
      sidebarElement.setText(community.getMetadata("String_Node_Str"));
      element.addContent(sidebarElement);
    }
    NodeList subCommunities=XPathAPI.selectNodeList(tn,"String_Node_Str");
    Element[] subCommunityElements=handleCommunities(context,subCommunities,community);
    NodeList collections=XPathAPI.selectNodeList(tn,"String_Node_Str");
    Element[] collectionElements=handleCollections(context,collections,community);
    int j;
    for (j=0; j < subCommunityElements.length; j++) {
      element.addContent(subCommunityElements[j]);
    }
    for (j=0; j < collectionElements.length; j++) {
      element.addContent(collectionElements[j]);
    }
    elements[i]=element;
  }
  return elements;
}

Fixed version (ground truth):
/** 
 * Take a node list of communities and build the structure from them, delegating to the relevant methods in this class for sub-communities and collections
 * @param context the context of the request
 * @param communities a nodelist of communities to create along with their sub-structures
 * @param parent the parent community of the nodelist of communities to create
 * @return an element array containing additional information regarding the created communities (e.g. the handles they have been assigned)
 */
private static Element[] handleCommunities(Context context,NodeList communities,Community parent) throws TransformerException, SQLException, Exception {
  Element[] elements=new Element[communities.getLength()];
  for (int i=0; i < communities.getLength(); i++) {
    Community community;
    Element element=new Element("String_Node_Str");
    if (parent != null) {
      community=parent.createSubcommunity();
    }
 else {
      community=Community.create(null,context);
    }
    community.setMetadata("String_Node_Str","String_Node_Str");
    Node tn=communities.item(i);
    Enumeration keys=communityMap.keys();
    while (keys.hasMoreElements()) {
      Node node=null;
      String key=(String)keys.nextElement();
      NodeList nl=XPathAPI.selectNodeList(tn,key);
      if (nl.getLength() == 1) {
        node=nl.item(0);
        community.setMetadata((String)communityMap.get(key),getStringValue(node));
      }
    }
    community.update();
    element.setAttribute("String_Node_Str",community.getHandle());
    Element nameElement=new Element("String_Node_Str");
    nameElement.setText(community.getMetadata("String_Node_Str"));
    element.addContent(nameElement);
    if (community.getMetadata("String_Node_Str") != null) {
      Element descriptionElement=new Element("String_Node_Str");
      descriptionElement.setText(community.getMetadata("String_Node_Str"));
      element.addContent(descriptionElement);
    }
    if (community.getMetadata("String_Node_Str") != null) {
      Element introElement=new Element("String_Node_Str");
      introElement.setText(community.getMetadata("String_Node_Str"));
      element.addContent(introElement);
    }
    if (community.getMetadata("String_Node_Str") != null) {
      Element copyrightElement=new Element("String_Node_Str");
      copyrightElement.setText(community.getMetadata("String_Node_Str"));
      element.addContent(copyrightElement);
    }
    if (community.getMetadata("String_Node_Str") != null) {
      Element sidebarElement=new Element("String_Node_Str");
      sidebarElement.setText(community.getMetadata("String_Node_Str"));
      element.addContent(sidebarElement);
    }
    NodeList subCommunities=XPathAPI.selectNodeList(tn,"String_Node_Str");
    Element[] subCommunityElements=handleCommunities(context,subCommunities,community);
    NodeList collections=XPathAPI.selectNodeList(tn,"String_Node_Str");
    Element[] collectionElements=handleCollections(context,collections,community);
    int j;
    for (j=0; j < subCommunityElements.length; j++) {
      element.addContent(subCommunityElements[j]);
    }
    for (j=0; j < collectionElements.length; j++) {
      element.addContent(collectionElements[j]);
    }
    elements[i]=element;
  }
  return elements;
}