use of org.exist.dom.memtree.SAXAdapter in project exist by eXist-db.
the class DocUtils method parse.
/**
* Utility function to parse an input stream into an in-memory DOM document.
*
* @param pool The broker pool
* @param context The XQuery context
* @param is The input stream to parse from
* @return document The document that was parsed
* @throws XPathException in case of dynamic error
*/
public static org.exist.dom.memtree.DocumentImpl parse(final BrokerPool pool, final XQueryContext context, final InputStream is) throws XPathException {
// we use eXist's in-memory DOM implementation
final XMLReaderPool parserPool = pool.getParserPool();
XMLReader reader = null;
try {
reader = pool.getParserPool().borrowXMLReader();
final InputSource src = new InputSource(is);
final SAXAdapter adapter = new SAXAdapter(context);
reader.setContentHandler(adapter);
try {
reader.setProperty(Namespaces.SAX_LEXICAL_HANDLER, adapter);
reader.parse(src);
} catch (final SAXNotRecognizedException | SAXNotSupportedException e) {
throw new XPathException("Error creating XML parser: " + e.getMessage(), e);
} catch (final IOException | SAXException e) {
throw new XPathException("Error while parsing XML: " + e.getMessage(), e);
}
return adapter.getDocument();
} finally {
if (reader != null) {
parserPool.returnXMLReader(reader);
}
}
}
use of org.exist.dom.memtree.SAXAdapter in project exist by eXist-db.
the class Utils method nodeFromString.
public static NodeImpl nodeFromString(XQueryContext context, String source) throws IOException {
SAXAdapter adapter = new SAXAdapter(context);
final XMLReaderPool parserPool = context.getBroker().getBrokerPool().getParserPool();
XMLReader xr = null;
try {
try {
xr = parserPool.borrowXMLReader();
xr.setContentHandler(adapter);
xr.setProperty(Namespaces.SAX_LEXICAL_HANDLER, adapter);
} catch (Exception e) {
throw new IOException(e);
}
try {
InputSource src = new InputSource(new StringReader(source));
xr.parse(src);
return (NodeImpl) adapter.getDocument();
} catch (SAXException e) {
throw new IOException(e);
}
} finally {
if (xr != null) {
parserPool.returnXMLReader(xr);
}
}
}
Aggregations