Program: 64664
Project: 64664
Buggy source:
private void writeElement(Element elem,XMLStreamWriter xsw) throws XMLStreamException {
  if (elem.getPrefix() != null && elem.getPrefix().length() > 0) {
    String namespaceURI=xsw.getNamespaceContext().getNamespaceURI(elem.getPrefix());
    xsw.writeStartElement(elem.getPrefix(),elem.getLocalName(),elem.getNamespaceURI());
    if (!(elem.getNamespaceURI().equals(namespaceURI))) {
      xsw.writeNamespace(elem.getPrefix(),elem.getNamespaceURI());
    }
  }
 else {
    String localName=elem.getLocalName();
    String name=elem.getNodeName();
    if (elem.getNamespaceURI() == null || elem.getNamespaceURI().length() == 0) {
      xsw.writeStartElement(elem.getNodeName());
      String defaultNamespace=xsw.getNamespaceContext().getNamespaceURI(XMLConstants.EMPTY_STRING);
      if (defaultNamespace != null && defaultNamespace.length() > 0) {
        xsw.writeDefaultNamespace(XMLConstants.EMPTY_STRING);
      }
    }
 else {
      xsw.writeStartElement(XMLConstants.EMPTY_STRING,elem.getLocalName(),elem.getNamespaceURI());
    }
  }
  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=xsw.getNamespaceContext().getNamespaceURI(next.getLocalName());
        if (currentUri == null || !currentUri.equals(next.getValue())) {
          xsw.writeNamespace(next.getLocalName(),next.getValue());
        }
      }
 else {
        if (!next.getName().equals(XMLConstants.XMLNS) || !((Boolean)xsw.getProperty(javax.xml.stream.XMLOutputFactory.IS_REPAIRING_NAMESPACES)).booleanValue()) {
          nonNamespaceDeclAttrs.add(attribute);
        }
      }
    }
  }
  for (  Attr next : nonNamespaceDeclAttrs) {
    if (next.getPrefix() == null) {
      xsw.writeAttribute(next.getName(),next.getValue());
    }
 else {
      xsw.writeAttribute(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) {
      xsw.writeCharacters(((Text)next).getNodeValue());
    }
 else     if (next.getNodeType() == Node.CDATA_SECTION_NODE) {
      xsw.writeCData(next.getNodeValue());
    }
 else     if (next.getNodeType() == Node.COMMENT_NODE) {
      xsw.writeComment(next.getNodeValue());
    }
 else     if (next.getNodeType() == Node.ELEMENT_NODE) {
      writeElement((Element)next,xsw);
    }
  }
  xsw.writeEndElement();
}

Fixed version (ground truth):
private void writeElement(Element elem,String newNamespace,String newName,XMLStreamWriter xsw) 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=xsw.getNamespaceContext().getNamespaceURI(prefix);
    xsw.writeStartElement(prefix,localName,namespace);
    if (!(namespace.equals(namespaceURI))) {
      xsw.writeNamespace(prefix,namespace);
    }
  }
 else {
    if (namespace == null || namespace.length() == 0) {
      xsw.writeStartElement(nodeName);
      String defaultNamespace=xsw.getNamespaceContext().getNamespaceURI(XMLConstants.EMPTY_STRING);
      if (defaultNamespace != null && defaultNamespace.length() > 0) {
        xsw.writeDefaultNamespace(XMLConstants.EMPTY_STRING);
      }
    }
 else {
      xsw.writeStartElement(XMLConstants.EMPTY_STRING,localName,namespace);
    }
  }
  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=xsw.getNamespaceContext().getNamespaceURI(next.getLocalName());
        if (currentUri == null || !currentUri.equals(next.getValue())) {
          xsw.writeNamespace(next.getLocalName(),next.getValue());
        }
      }
 else {
        if (!next.getName().equals(XMLConstants.XMLNS) || !((Boolean)xsw.getProperty(javax.xml.stream.XMLOutputFactory.IS_REPAIRING_NAMESPACES)).booleanValue()) {
          nonNamespaceDeclAttrs.add(attribute);
        }
      }
    }
  }
  for (  Attr next : nonNamespaceDeclAttrs) {
    if (next.getPrefix() == null) {
      xsw.writeAttribute(next.getName(),next.getValue());
    }
 else {
      xsw.writeAttribute(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) {
      xsw.writeCharacters(((Text)next).getNodeValue());
    }
 else     if (next.getNodeType() == Node.CDATA_SECTION_NODE) {
      xsw.writeCData(next.getNodeValue());
    }
 else     if (next.getNodeType() == Node.COMMENT_NODE) {
      xsw.writeComment(next.getNodeValue());
    }
 else     if (next.getNodeType() == Node.ELEMENT_NODE) {
      writeElement((Element)next,null,null,xsw);
    }
  }
  xsw.writeEndElement();
}