Search in sources :

Example 21 with SAXAdapter

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);
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) SAXNotSupportedException(org.xml.sax.SAXNotSupportedException) XPathException(org.exist.xquery.XPathException) SAXAdapter(org.exist.dom.memtree.SAXAdapter) SAXNotRecognizedException(org.xml.sax.SAXNotRecognizedException) IOException(java.io.IOException) XMLReaderPool(org.exist.util.XMLReaderPool) XMLReader(org.xml.sax.XMLReader) SAXException(org.xml.sax.SAXException)

Example 22 with SAXAdapter

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);
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) NodeImpl(org.exist.dom.memtree.NodeImpl) StringReader(java.io.StringReader) SAXAdapter(org.exist.dom.memtree.SAXAdapter) IOException(java.io.IOException) XMLReaderPool(org.exist.util.XMLReaderPool) XMLReader(org.xml.sax.XMLReader) SAXException(org.xml.sax.SAXException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Aggregations

SAXAdapter (org.exist.dom.memtree.SAXAdapter)22 InputSource (org.xml.sax.InputSource)18 XMLReader (org.xml.sax.XMLReader)17 IOException (java.io.IOException)13 SAXException (org.xml.sax.SAXException)12 StringReader (java.io.StringReader)10 SAXParser (javax.xml.parsers.SAXParser)10 SAXParserFactory (javax.xml.parsers.SAXParserFactory)7 NodeImpl (org.exist.dom.memtree.NodeImpl)6 ExistSAXParserFactory (org.exist.util.ExistSAXParserFactory)6 XMLReaderPool (org.exist.util.XMLReaderPool)6 XPathException (org.exist.xquery.XPathException)6 InputStream (java.io.InputStream)4 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)4 Document (org.w3c.dom.Document)4 MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)3 Either (com.evolvedbinary.j8fu.Either)2 InputStreamReader (java.io.InputStreamReader)2 Reader (java.io.Reader)2 DocumentImpl (org.exist.dom.memtree.DocumentImpl)2