*/
private static final Document process(final JDOMFactory factory,
final XMLEventReader events) throws JDOMException {
try {
final Document document = factory.document(null);
Element current = null;
XMLEvent event = events.peek();
if (XMLStreamConstants.START_DOCUMENT != event.getEventType()) {
throw new JDOMException("JDOM requires that XMLStreamReaders " +
"are at their beginning when being processed.");
}
while (event.getEventType() != XMLStreamConstants.END_DOCUMENT) {
if (event.isStartDocument()) {
document.setBaseURI(event.getLocation().getSystemId());
document.setProperty("ENCODING_SCHEME",
((javax.xml.stream.events.StartDocument)event).getCharacterEncodingScheme());
document.setProperty("STANDALONE", String.valueOf(
((javax.xml.stream.events.StartDocument)event).isStandalone()));
// document.setProperty("ENCODING",
// ((StartDocument)event).getEncoding());
} else if (event instanceof javax.xml.stream.events.DTD) {
//List<?> list = (List<?>)reader.getProperty("javax.xml.stream.entities");
//System.out.println(list);
final DocType dtype = DTDParser.parse(((javax.xml.stream.events.DTD)event).getDocumentTypeDeclaration(), factory);
document.setDocType(dtype);
} else if (event.isStartElement()) {
final Element emt = processElement(factory, event.asStartElement());
if (current == null) {
document.setRootElement(emt);
final DocType dt = document.getDocType();
if (dt != null) {
dt.setElementName(emt.getName());
}
} else {
current.addContent(emt);
}
current = emt;
} else if (event.isCharacters()) {
final Characters chars = event.asCharacters();
if (chars.isCData()) {
current.addContent(factory.cdata(
((Characters)event).getData()));
} else {
current.addContent(factory.text(
((Characters)event).getData()));
}
} else if (event instanceof javax.xml.stream.events.Comment) {
final Comment comment = factory.comment(
((javax.xml.stream.events.Comment)event).getText());
if (current == null) {
document.addContent(comment);
} else {
current.addContent(comment);
}
} else if (event.isEntityReference()) {
current.addContent(factory.entityRef(
((javax.xml.stream.events.EntityReference)event).getName()));
} else if (event.isProcessingInstruction()) {
final ProcessingInstruction pi = factory.processingInstruction(
((javax.xml.stream.events.ProcessingInstruction)event).getTarget(),
((javax.xml.stream.events.ProcessingInstruction)event).getData());
if (current == null) {
document.addContent(pi);
} else {
current.addContent(pi);
}
} else if (event.isEndElement()) {
current = current.getParentElement();