private static Document convert(XMLStreamReader streamReader) throws XMLStreamException {
NodeFactory nodeFactory = new NodeFactory();
Document document = null;
Element element = null;
ParentNode parent = null;
boolean documentFinished = false;
while (streamReader.hasNext()) {
int event = streamReader.next();
switch (event) {
case XMLStreamConstants.START_DOCUMENT:
document = nodeFactory.startMakingDocument();
parent = document;
break;
case XMLStreamConstants.END_DOCUMENT:
nodeFactory.finishMakingDocument(document);
documentFinished = true;
break;
case XMLStreamConstants.START_ELEMENT:
if (document == null) {
document = nodeFactory.startMakingDocument();
parent = document;
}
String name = QNameUtils.toQualifiedName(streamReader.getName());
if (element == null) {
element = nodeFactory.makeRootElement(name, streamReader.getNamespaceURI());
document.setRootElement(element);
}
else {
element = nodeFactory.startMakingElement(name, streamReader.getNamespaceURI());
parent.appendChild(element);
}
convertNamespaces(streamReader, element);
convertAttributes(streamReader, nodeFactory);
parent = element;
break;
case XMLStreamConstants.END_ELEMENT:
nodeFactory.finishMakingElement(element);
parent = parent.getParent();
break;
case XMLStreamConstants.ATTRIBUTE:
convertAttributes(streamReader, nodeFactory);
break;
case XMLStreamConstants.CHARACTERS: