boolean done = false;
int i = 0;
while (!done && reader.getEventType() != XMLStreamConstants.END_DOCUMENT) {
Nodes nodes;
// if (DEBUG) System.out.println(toString(reader.getEventType()));
switch (reader.getEventType()) {
case XMLStreamConstants.START_ELEMENT: {
if (hasRootElement) throw new IllegalAddException(
"StAX reader must not return multiple root elements");
if (factory.getClass() == NodeFactory.class) { // fast path
if (nodeBuilder == null) nodeBuilder = new NodeBuilder();
Element root = readStartTag();
addAttributes(root);
addNamespaceDeclarations(root);
readElement(root); // reads entire subtree
nodes = new Nodes(root);
} else { // slow path
Element root = readStartTagF(true);
if (root == null) {
throw new NullPointerException(
"Factory failed to create root element.");
}
doc.setRootElement(root);
addAttributesF(root);
addNamespaceDeclarations(root);
readElementF(root); // read entire subtree
nodes = factory.finishMakingElement(root);
}
reader.require(XMLStreamConstants.END_ELEMENT, null, null);
if (isFragmentMode) done = true;
break;
}
case XMLStreamConstants.END_ELEMENT:
throw new IllegalAddException(
"A document must not have more than one root element");
case XMLStreamConstants.PROCESSING_INSTRUCTION:
nodes = factory.makeProcessingInstruction(
reader.getPITarget(), reader.getPIData());
break;
case XMLStreamConstants.CHARACTERS:
nodes = NONE; // ignore text in prolog/epilog
break;
case XMLStreamConstants.COMMENT:
nodes = factory.makeComment(reader.getText());
break;
case XMLStreamConstants.SPACE:
nodes = NONE; // ignore text in prolog/epilog
break;
case XMLStreamConstants.START_DOCUMENT:
nodes = NONE; // has already been handled previously
break;
case XMLStreamConstants.END_DOCUMENT:
throw new IllegalStateException("unreachable");
case XMLStreamConstants.CDATA:
nodes = NONE; // ignore text in prolog/epilog
break;
case XMLStreamConstants.ATTRIBUTE:
throw new IllegalAddException(
"Illegal attribute in prolog/epilog");
case XMLStreamConstants.NAMESPACE:
throw new IllegalAddException(
"Illegal namespace declaration in prolog/epilog");
case XMLStreamConstants.DTD:
nodes = readDocType(factory); // FIXME
break;
case XMLStreamConstants.ENTITY_DECLARATION:
nodes = NONE; // ignore (missing StAX support)
break;
case XMLStreamConstants.NOTATION_DECLARATION:
nodes = NONE; // ignore (missing StAX support)
break;
case XMLStreamConstants.ENTITY_REFERENCE:
nodes = NONE; // ignore text in prolog/epilog
break;
default:
throw new XMLException("Unrecognized Stax event type: "
+ reader.getEventType());
}
// append nodes:
for (int j=0; j < nodes.size(); j++) {
Node node = nodes.get(j);
if (node instanceof Element) { // replace fake root with real root
if (hasRootElement) {
throw new IllegalAddException(
"Factory returned multiple root elements");
}