Program: 64661
Project: 64661
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) {
      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()));
  }
}

Fixed version (ground truth):
private void writeElement(Element elem,String newNamespace,String newName,XMLEventWriter xew) throws XMLStreamException {
  String prefix=null;
  String namespace=null;
  String localName=null;
  String nodeName=null;
  if (newName != null) {
    namespace=newNamespace;
    localName=newName;
    nodeName=newName;
    if (newNamespace != null && newNamespace.length() > 0) {
      NamespaceResolver tempNR=new NamespaceResolver();
      tempNR.setDOM(elem);
      prefix=tempNR.resolveNamespaceURI(namespace);
      if (prefix == null || prefix.length() == 0) {
        String defaultNamespace=elem.getAttributeNS(XMLConstants.XMLNS_URL,XMLConstants.XMLNS);
        if (defaultNamespace == null) {
          prefix=tempNR.generatePrefix();
        }
 else         if (defaultNamespace != namespace) {
          prefix=tempNR.generatePrefix();
        }
 else {
          prefix=XMLConstants.EMPTY_STRING;
        }
      }
    }
  }
 else {
    prefix=elem.getPrefix();
    namespace=elem.getNamespaceURI();
    localName=elem.getLocalName();
    nodeName=elem.getNodeName();
  }
  if (prefix != null && prefix.length() > 0) {
    String namespaceURI=xew.getNamespaceContext().getNamespaceURI(prefix);
    xew.add(xmlEventFactory.createStartElement(prefix,namespace,localName));
    if (!(namespace.equals(namespaceURI))) {
      xew.add(xmlEventFactory.createNamespace(prefix,namespace));
    }
  }
 else {
    if (namespace == null || namespace.length() == 0) {
      String defaultNamespace=xew.getNamespaceContext().getNamespaceURI(XMLConstants.EMPTY_STRING);
      xew.add(xmlEventFactory.createStartElement("String_Node_Str","String_Node_Str",nodeName));
      if (defaultNamespace != null && defaultNamespace.length() > 0) {
        xew.add(xmlEventFactory.createNamespace(XMLConstants.EMPTY_STRING));
      }
    }
 else {
      xew.add(xmlEventFactory.createStartElement(XMLConstants.EMPTY_STRING,namespace,localName));
    }
  }
  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,null,null,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()));
  }
}