document.mark(4);
boolean isFastInfosetDocument = Decoder.isFastInfosetDocument(document);
document.reset();
if (isFastInfosetDocument) {
SAXDocumentParser parser = new SAXDocumentParser();
SAXEventSerializer ses = new SAXEventSerializer(events);
parser.setContentHandler(ses);
parser.setProperty(Properties.LEXICAL_HANDLER_PROPERTY, ses);
parser.parse(document);
} else {
SAXParserFactory parserFactory = SAXParserFactory.newInstance();
parserFactory.setNamespaceAware(true);
SAXParser parser = parserFactory.newSAXParser();
SAXEventSerializer ses = new SAXEventSerializer(events);
XMLReader reader = parser.getXMLReader();
reader.setProperty("http://xml.org/sax/properties/lexical-handler", ses);
reader.setContentHandler(ses);
if (workingDirectory != null) {
reader.setEntityResolver(createRelativePathResolver(workingDirectory));
}