Program: 66068
Project: 66068
Buggy source:
private void writeElement(Element elem,XMLEventWriter xew) throws XMLStreamException {
  if (elem.getPrefix() != null && elem.getPrefix().length() > 0) {
    String namespaceURI=xew.getNamespaceContext().getNamespaceURI(elem.getPrefix());
    xew.add(xmlEventFactory.createStartElement(elem.getPrefix(),elem.getNamespaceURI(),elem.getLocalName()));
    if (!(elem.getNamespaceURI().equals(namespaceURI))) {
      xew.add(xmlEventFactory.createNamespace(elem.getPrefix(),elem.getNamespaceURI()));
    }
  }
 else {
    String localName=elem.getLocalName();
    String name=elem.getNodeName();
    if (elem.getNamespaceURI() == null || elem.getNamespaceURI().length() == 0) {
      xew.add(xmlEventFactory.createStartElement("String_Node_Str","String_Node_Str",elem.getNodeName()));
      String defaultNamespace=xew.getNamespaceContext().getNamespaceURI(XMLConstants.EMPTY_STRING);
      if (defaultNamespace != null && defaultNamespace.length() > 0) {
        xew.add(xmlEventFactory.createNamespace(XMLConstants.EMPTY_STRING));
      }
    }
 else {
      xew.add(xmlEventFactory.createStartElement(XMLConstants.EMPTY_STRING,elem.getNamespaceURI(),elem.getLocalName()));
    }
  }
  NodeList childNodes=elem.getChildNodes();
  NamedNodeMap attrs=elem.getAttributes();
  ArrayList<Attr> nonNamespaceDeclAttrs=new ArrayList<Attr>();
  for (int i=0; i < attrs.getLength(); i++) {
    Attr next=(Attr)attrs.item(i);
    if (next.getNodeType() == Node.ATTRIBUTE_NODE) {
      Attr attribute=next;
      if (next.getPrefix() != null && next.getPrefix().equals(XMLConstants.XMLNS)) {
        String currentUri=xew.getNamespaceContext().getNamespaceURI(next.getLocalName());
        if (currentUri == null || !currentUri.equals(next.getValue())) {
          xew.add(xmlEventFactory.createNamespace(next.getLocalName(),next.getValue()));
        }
      }
 else {
        nonNamespaceDeclAttrs.add(attribute);
      }
    }
  }
  for (  Attr next : nonNamespaceDeclAttrs) {
    if (next.getPrefix() == null) {
      xew.add(xmlEventFactory.createAttribute(next.getName(),next.getValue()));
    }
 else {
      xew.add(xmlEventFactory.createAttribute(next.getPrefix(),next.getNamespaceURI(),next.getLocalName(),next.getValue()));
    }
  }
  for (int i=0; i < childNodes.getLength(); i++) {
    Node next=childNodes.item(i);
    if (next.getNodeType() == Node.TEXT_NODE) {
      xew.add(xmlEventFactory.createCharacters(((Text)next).getNodeValue()));
    }
 else     if (next.getNodeType() == Node.CDATA_SECTION_NODE) {
      xew.add(xmlEventFactory.createCData(next.getNodeValue()));
    }
 else     if (next.getNodeType() == Node.COMMENT_NODE) {
      xew.add(xmlEventFactory.createComment(next.getNodeValue()));
    }
 else     if (next.getNodeType() == Node.ELEMENT_NODE) {
      writeElement((Element)next,xew);
    }
  }
  if (elem.getPrefix() != null && elem.getPrefix().length() > 0) {
    xew.add(xmlEventFactory.createEndElement(elem.getPrefix(),elem.getNamespaceURI(),elem.getLocalName()));
  }
 else   if (elem.getNamespaceURI() != null && elem.getNamespaceURI().length() > 0) {
    xew.add(xmlEventFactory.createEndElement("String_Node_Str",elem.getNamespaceURI(),elem.getLocalName()));
  }
 else {
    xew.add(xmlEventFactory.createEndElement("String_Node_Str","String_Node_Str",elem.getNodeName()));
  }
}

Fixed version (ground truth):
private void writeElement(Element elem,XMLEventWriter xew) throws XMLStreamException {
  if (elem.getPrefix() != null && elem.getPrefix().length() > 0) {
    String namespaceURI=xew.getNamespaceContext().getNamespaceURI(elem.getPrefix());
    xew.add(xmlEventFactory.createStartElement(elem.getPrefix(),elem.getNamespaceURI(),elem.getLocalName()));
    if (!(elem.getNamespaceURI().equals(namespaceURI))) {
      xew.add(xmlEventFactory.createNamespace(elem.getPrefix(),elem.getNamespaceURI()));
    }
  }
 else {
    String localName=elem.getLocalName();
    String name=elem.getNodeName();
    if (elem.getNamespaceURI() == null || elem.getNamespaceURI().length() == 0) {
      String defaultNamespace=xew.getNamespaceContext().getNamespaceURI(XMLConstants.EMPTY_STRING);
      xew.add(xmlEventFactory.createStartElement("String_Node_Str","String_Node_Str",elem.getNodeName()));
      if (defaultNamespace != null && defaultNamespace.length() > 0) {
        xew.add(xmlEventFactory.createNamespace(XMLConstants.EMPTY_STRING));
      }
    }
 else {
      xew.add(xmlEventFactory.createStartElement(XMLConstants.EMPTY_STRING,elem.getNamespaceURI(),elem.getLocalName()));
    }
  }
  NodeList childNodes=elem.getChildNodes();
  NamedNodeMap attrs=elem.getAttributes();
  ArrayList<Attr> nonNamespaceDeclAttrs=new ArrayList<Attr>();
  for (int i=0; i < attrs.getLength(); i++) {
    Attr next=(Attr)attrs.item(i);
    if (next.getNodeType() == Node.ATTRIBUTE_NODE) {
      Attr attribute=next;
      if (next.getPrefix() != null && next.getPrefix().equals(XMLConstants.XMLNS)) {
        String currentUri=xew.getNamespaceContext().getNamespaceURI(next.getLocalName());
        if (currentUri == null || !currentUri.equals(next.getValue())) {
          xew.add(xmlEventFactory.createNamespace(next.getLocalName(),next.getValue()));
        }
      }
 else {
        nonNamespaceDeclAttrs.add(attribute);
      }
    }
  }
  for (  Attr next : nonNamespaceDeclAttrs) {
    if (next.getPrefix() == null) {
      xew.add(xmlEventFactory.createAttribute(next.getName(),next.getValue()));
    }
 else {
      xew.add(xmlEventFactory.createAttribute(next.getPrefix(),next.getNamespaceURI(),next.getLocalName(),next.getValue()));
    }
  }
  for (int i=0; i < childNodes.getLength(); i++) {
    Node next=childNodes.item(i);
    if (next.getNodeType() == Node.TEXT_NODE) {
      xew.add(xmlEventFactory.createCharacters(((Text)next).getNodeValue()));
    }
 else     if (next.getNodeType() == Node.CDATA_SECTION_NODE) {
      xew.add(xmlEventFactory.createCData(next.getNodeValue()));
    }
 else     if (next.getNodeType() == Node.COMMENT_NODE) {
      xew.add(xmlEventFactory.createComment(next.getNodeValue()));
    }
 else     if (next.getNodeType() == Node.ELEMENT_NODE) {
      writeElement((Element)next,xew);
    }
  }
  if (elem.getPrefix() != null && elem.getPrefix().length() > 0) {
    xew.add(xmlEventFactory.createEndElement(elem.getPrefix(),elem.getNamespaceURI(),elem.getLocalName()));
  }
 else   if (elem.getNamespaceURI() != null && elem.getNamespaceURI().length() > 0) {
    xew.add(xmlEventFactory.createEndElement("String_Node_Str",elem.getNamespaceURI(),elem.getLocalName()));
  }
 else {
    xew.add(xmlEventFactory.createEndElement("String_Node_Str","String_Node_Str",elem.getNodeName()));
  }
}