FilePosition endPos = FilePosition.endOf(tokens.lastPosition());
try {
elementStack.finish(endPos);
} catch (IllegalDocumentStateException ex) {
throw new ParseException(ex.getCajaMessage(), ex);
}
DocumentFragment root = elementStack.getRootElement();
Node firstChild = root.getFirstChild();
if (firstChild == null || firstChild.getNodeType() != Node.ELEMENT_NODE) {
throw new ParseException(new Message(
DomParserMessageType.MISSING_DOCUMENT_ELEMENT, endPos));
}
// Check that there isn't any extraneous content after the root element.
for (Node child = firstChild.getNextSibling(); child != null;
child = child.getNextSibling()) {
switch (child.getNodeType()) {
case Node.COMMENT_NODE:
case Node.DOCUMENT_TYPE_NODE:
continue;
case Node.TEXT_NODE:
if ("".equals(child.getNodeValue().trim())) { continue; }
break;
default: break;
}
throw new ParseException(new Message(
DomParserMessageType.MISPLACED_CONTENT,
Nodes.getFilePositionFor(child)));
}
doc.appendChild(firstChild);