Program: 115039
Project: 115039
Buggy source:
private String getPopulatedInstance(CrfVersion crfVersion,EventCrf eventCrf) throws Exception {
  boolean isXform=false;
  if (crfVersion.getXform() != null && !crfVersion.getXform().equals("String_Node_Str"))   isXform=true;
  DocumentBuilderFactory docFactory=DocumentBuilderFactory.newInstance();
  docFactory.setNamespaceAware(true);
  DocumentBuilder build=docFactory.newDocumentBuilder();
  Document doc=build.newDocument();
  Element crfElement=null;
  if (isXform) {
    crfElement=doc.createElement(crfVersion.getXformName());
    crfElement.setAttribute("String_Node_Str","String_Node_Str");
  }
 else {
    crfElement=doc.createElement(crfVersion.getOcOid());
  }
  doc.appendChild(crfElement);
  ArrayList<ItemGroup> itemGroups=itemGroupDao.findByCrfVersionId(crfVersion.getCrfVersionId());
  for (  ItemGroup itemGroup : itemGroups) {
    ItemGroupMetadata itemGroupMetadata=itemGroup.getItemGroupMetadatas().get(0);
    ArrayList<Item> items=(ArrayList<Item>)itemDao.findByItemGroupCrfVersionOrdered(itemGroup.getItemGroupId(),crfVersion.getCrfVersionId());
    Item firstItem=items.get(0);
    int maxGroupRepeat=itemDataDao.getMaxGroupRepeat(eventCrf.getEventCrfId(),firstItem.getItemId());
    Boolean isrepeating=itemGroupMetadata.isRepeatingGroup();
    for (int i=0; i < maxGroupRepeat; i++) {
      Element groupElement=null;
      if (isXform) {
        groupElement=doc.createElement(itemGroup.getName());
      }
 else {
        groupElement=doc.createElement(itemGroup.getOcOid());
      }
      Element repeatOrdinal=null;
      if (isrepeating) {
        repeatOrdinal=doc.createElement("String_Node_Str");
        repeatOrdinal.setTextContent(String.valueOf(i + 1));
        groupElement.appendChild(repeatOrdinal);
        groupElement.setAttribute(ENKETO_ORDINAL,String.valueOf(i + 1));
        groupElement.setAttribute(ENKETO_LAST_USED_ORDINAL,String.valueOf(maxGroupRepeat));
      }
      boolean hasItemData=false;
      for (      Item item : items) {
        ItemData itemData=itemDataDao.findByItemEventCrfOrdinal(item.getItemId(),eventCrf.getEventCrfId(),i + 1);
        ItemFormMetadata itemMetadata=item.getItemFormMetadatas().iterator().next();
        Element question=null;
        if (crfVersion.getXform() != null && !crfVersion.getXform().equals("String_Node_Str"))         question=doc.createElement(item.getName());
 else         question=doc.createElement(item.getOcOid());
        if (itemData != null && itemData.getValue() != null && !itemData.getValue().equals("String_Node_Str")) {
          ResponseType responseType=responseTypeDao.findByItemFormMetaDataId(itemMetadata.getItemFormMetadataId());
          String itemValue=itemData.getValue();
          if (responseType.getResponseTypeId() == 3 || responseType.getResponseTypeId() == 7) {
            itemValue=itemValue.replaceAll("String_Node_Str","String_Node_Str");
          }
          question.setTextContent(itemValue);
        }
        if (itemData == null || !itemData.isDeleted()) {
          hasItemData=true;
          groupElement.appendChild(question);
        }
      }
      if (hasItemData) {
        crfElement.appendChild(groupElement);
      }
    }
  }
  TransformerFactory transformFactory=TransformerFactory.newInstance();
  Transformer transformer=transformFactory.newTransformer();
  transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,"String_Node_Str");
  transformer.setOutputProperty(OutputKeys.INDENT,"String_Node_Str");
  StringWriter writer=new StringWriter();
  StreamResult result=new StreamResult(writer);
  DOMSource source=new DOMSource(doc);
  transformer.transform(source,result);
  String instance=writer.toString();
  return instance;
}

Fixed version (ground truth):
private String getPopulatedInstance(CrfVersion crfVersion,EventCrf eventCrf) throws Exception {
  boolean isXform=false;
  if (crfVersion.getXform() != null && !crfVersion.getXform().equals("String_Node_Str"))   isXform=true;
  DocumentBuilderFactory docFactory=DocumentBuilderFactory.newInstance();
  docFactory.setNamespaceAware(true);
  DocumentBuilder build=docFactory.newDocumentBuilder();
  Document doc=build.newDocument();
  Element crfElement=null;
  if (isXform) {
    crfElement=doc.createElement(crfVersion.getXformName());
    crfElement.setAttribute("String_Node_Str","String_Node_Str");
  }
 else {
    crfElement=doc.createElement(crfVersion.getOcOid());
  }
  doc.appendChild(crfElement);
  ArrayList<ItemGroup> itemGroups=itemGroupDao.findByCrfVersionId(crfVersion.getCrfVersionId());
  for (  ItemGroup itemGroup : itemGroups) {
    ItemGroupMetadata itemGroupMetadata=itemGroupMetadataDao.findByItemGroupCrfVersion(itemGroup.getItemGroupId(),crfVersion.getCrfVersionId()).get(0);
    ArrayList<Item> items=(ArrayList<Item>)itemDao.findByItemGroupCrfVersionOrdered(itemGroup.getItemGroupId(),crfVersion.getCrfVersionId());
    int maxGroupRepeat=itemDataDao.getMaxGroupRepeat(eventCrf.getEventCrfId(),items.get(0).getItemId());
    String repeatGroupMin=itemGroupMetadata.getRepeatNumber().toString();
    Boolean isrepeating=itemGroupMetadata.isRepeatingGroup();
    for (int i=0; i < maxGroupRepeat; i++) {
      Element groupElement=null;
      if (isXform)       groupElement=doc.createElement(itemGroup.getName());
 else       groupElement=doc.createElement(itemGroup.getOcOid());
      Element repeatOrdinal=null;
      if (isrepeating) {
        groupElement.setAttribute(ENKETO_ORDINAL,String.valueOf(i + 1));
        groupElement.setAttribute(ENKETO_LAST_USED_ORDINAL,String.valueOf(maxGroupRepeat));
        repeatOrdinal=doc.createElement("String_Node_Str");
        repeatOrdinal.setTextContent(String.valueOf(i + 1));
        groupElement.appendChild(repeatOrdinal);
      }
      boolean hasItemData=false;
      for (      Item item : items) {
        ItemFormMetadata itemMetadata=itemFormMetadataDao.findByItemCrfVersion(item.getItemId(),crfVersion.getCrfVersionId());
        ItemData itemData=itemDataDao.findByItemEventCrfOrdinal(item.getItemId(),eventCrf.getEventCrfId(),i + 1);
        Element question=null;
        if (crfVersion.getXform() != null && !crfVersion.getXform().equals("String_Node_Str"))         question=doc.createElement(item.getName());
 else         question=doc.createElement(item.getOcOid());
        if (itemData != null && itemData.getValue() != null && !itemData.getValue().equals("String_Node_Str")) {
          ResponseType responseType=responseTypeDao.findByItemFormMetaDataId(itemMetadata.getItemFormMetadataId());
          String itemValue=itemData.getValue();
          if (responseType.getResponseTypeId() == 3 || responseType.getResponseTypeId() == 7) {
            itemValue=itemValue.replaceAll("String_Node_Str","String_Node_Str");
          }
          question.setTextContent(itemValue);
        }
        if ((itemData != null) && (itemData.isDeleted() != true)) {
          hasItemData=true;
          groupElement.appendChild(question);
        }
      }
      if (hasItemData) {
        crfElement.appendChild(groupElement);
      }
    }
  }
  TransformerFactory transformFactory=TransformerFactory.newInstance();
  Transformer transformer=transformFactory.newTransformer();
  transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,"String_Node_Str");
  transformer.setOutputProperty(OutputKeys.INDENT,"String_Node_Str");
  StringWriter writer=new StringWriter();
  StreamResult result=new StreamResult(writer);
  DOMSource source=new DOMSource(doc);
  transformer.transform(source,result);
  String instance=writer.toString();
  return instance;
}