use of org.exist.dom.memtree.AppendingSAXAdapter in project exist by eXist-db.
the class DecodeExiFunction method eval.
@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
if (args[0].isEmpty()) {
return Sequence.EMPTY_SEQUENCE;
}
try {
BinaryValue exiBinary = ((BinaryValue) args[0].itemAt(0));
context.pushDocumentContext();
try {
MemTreeBuilder builder = context.getDocumentBuilder();
// create default factory and EXI grammar for schema
EXIFactory exiFactory = DefaultEXIFactory.newInstance();
if (args.length > 1) {
if (!args[1].isEmpty()) {
Item xsdItem = args[1].itemAt(0);
try (InputStream xsdInputStream = EXIUtils.getInputStream(xsdItem, context)) {
GrammarFactory grammarFactory = GrammarFactory.newInstance();
Grammars grammar = grammarFactory.createGrammars(xsdInputStream);
exiFactory.setGrammars(grammar);
}
}
}
SAXDecoder decoder = new SAXDecoder(exiFactory);
SAXAdapter adapter = new AppendingSAXAdapter(builder);
decoder.setContentHandler(adapter);
try (InputStream inputStream = exiBinary.getInputStream()) {
decoder.parse(new InputSource(inputStream));
}
return (NodeValue) builder.getDocument().getDocumentElement();
} finally {
context.popDocumentContext();
}
} catch (EXIException | SAXException | IOException exie) {
throw new XPathException(this, new JavaErrorCode(exie.getCause()), exie.getMessage());
}
}
Aggregations