Search in sources :

Example 1 with SAXAdapter

use of org.exist.dom.memtree.SAXAdapter in project exist by eXist-db.

the class Eval method loadVarFromURI.

private NodeImpl loadVarFromURI(final String uri) throws XPathException {
    XMLReader xr = null;
    final XMLReaderPool parserPool = context.getBroker().getBrokerPool().getParserPool();
    try {
        final URL url = new URL(uri);
        final InputStreamReader isr = new InputStreamReader(url.openStream(), UTF_8);
        final InputSource src = new InputSource(isr);
        xr = parserPool.borrowXMLReader();
        final SAXAdapter adapter = new SAXAdapter(context);
        xr.setContentHandler(adapter);
        xr.setProperty(Namespaces.SAX_LEXICAL_HANDLER, adapter);
        xr.parse(src);
        isr.close();
        return adapter.getDocument();
    } catch (final SAXException | IOException e) {
        throw new XPathException(this, e);
    } finally {
        if (xr != null) {
            parserPool.returnXMLReader(xr);
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) InputStreamReader(java.io.InputStreamReader) SAXAdapter(org.exist.dom.memtree.SAXAdapter) IOException(java.io.IOException) XMLReaderPool(org.exist.util.XMLReaderPool) XMLReader(org.xml.sax.XMLReader) URL(java.net.URL) SAXException(org.xml.sax.SAXException)

Example 2 with SAXAdapter

use of org.exist.dom.memtree.SAXAdapter in project exist by eXist-db.

the class Parse method eval.

@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
    if (args[0].getItemCount() == 0) {
        return Sequence.EMPTY_SEQUENCE;
    }
    final String xmlContent = args[0].itemAt(0).getStringValue();
    if (xmlContent.isEmpty()) {
        return Sequence.EMPTY_SEQUENCE;
    }
    final StringReader reader = new StringReader(xmlContent);
    final ValidationReport report = new ValidationReport();
    final SAXAdapter adapter = new SAXAdapter(context);
    XMLReader xr = null;
    try {
        final InputSource src = new InputSource(reader);
        final Optional<Either<Throwable, XMLReader>> maybeReaderInst = HtmlToXmlParser.getHtmlToXmlParser(context.getBroker().getConfiguration());
        if (maybeReaderInst.isPresent()) {
            final Either<Throwable, XMLReader> readerInst = maybeReaderInst.get();
            if (readerInst.isLeft()) {
                final String msg = "Unable to parse HTML to XML please ensure the parser is configured in conf.xml and is present on the classpath";
                final Throwable t = readerInst.left().get();
                LOG.error(msg, t);
                throw new XPathException(this, ErrorCodes.EXXQDY0002, t);
            } else {
                xr = readerInst.right().get();
            }
        } else {
            throw new XPathException(this, ErrorCodes.EXXQDY0002, "There is no HTML to XML parser configured in conf.xml");
        }
        xr.setErrorHandler(report);
        xr.setContentHandler(adapter);
        xr.setProperty(Namespaces.SAX_LEXICAL_HANDLER, adapter);
        xr.parse(src);
    } catch (final SAXException e) {
        logger.debug("Error while parsing XML: {}", e.getMessage(), e);
    } catch (final IOException e) {
        throw new XPathException(this, ErrorCodes.EXXQDY0002, "Error while parsing XML: " + e.getMessage(), args[0], e);
    } finally {
        if (!isCalledAs("parse-html") && xr != null) {
            context.getBroker().getBrokerPool().getParserPool().returnXMLReader(xr);
        }
    }
    if (report.isValid()) {
        return adapter.getDocument();
    } else {
        context.pushDocumentContext();
        try {
            final MemTreeBuilder builder = context.getDocumentBuilder();
            final NodeImpl result = Shared.writeReport(report, builder);
            throw new XPathException(this, ErrorCodes.EXXQDY0002, report.toString(), result);
        } finally {
            context.popDocumentContext();
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) NodeImpl(org.exist.dom.memtree.NodeImpl) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) ValidationReport(org.exist.validation.ValidationReport) StringReader(java.io.StringReader) SAXAdapter(org.exist.dom.memtree.SAXAdapter) Either(com.evolvedbinary.j8fu.Either) XMLReader(org.xml.sax.XMLReader)

Example 3 with SAXAdapter

use of org.exist.dom.memtree.SAXAdapter in project exist by eXist-db.

the class TestCase method loadVarFromString.

public NodeImpl loadVarFromString(XQueryContext context, String source) throws IOException {
    SAXAdapter adapter = new SAXAdapter(context);
    SAXParserFactory factory = ExistSAXParserFactory.getSAXParserFactory();
    factory.setNamespaceAware(true);
    XMLReader xr;
    try {
        SAXParser parser = factory.newSAXParser();
        xr = parser.getXMLReader();
        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);
    }
}
Also used : InputSource(org.xml.sax.InputSource) NodeImpl(org.exist.dom.memtree.NodeImpl) StringReader(java.io.StringReader) SAXAdapter(org.exist.dom.memtree.SAXAdapter) SAXParser(javax.xml.parsers.SAXParser) IOException(java.io.IOException) XMLReader(org.xml.sax.XMLReader) XMLDBException(org.xmldb.api.base.XMLDBException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) SAXParserFactory(javax.xml.parsers.SAXParserFactory) ExistSAXParserFactory(org.exist.util.ExistSAXParserFactory) SAXException(org.xml.sax.SAXException)

Example 4 with SAXAdapter

use of org.exist.dom.memtree.SAXAdapter in project exist by eXist-db.

the class ParsingFunctions method parse.

private Sequence parse(final String xmlContent, final Sequence[] args) throws XPathException {
    final SAXAdapter adapter = new FragmentSAXAdapter(context, isCalledAs("parse-xml-fragment"));
    final ValidationReport report = validate(xmlContent, adapter);
    if (report.isValid()) {
        return adapter.getDocument();
    } else {
        try {
            context.pushDocumentContext();
            final MemTreeBuilder builder = context.getDocumentBuilder();
            final NodeImpl result = Shared.writeReport(report, builder);
            throw new XPathException(this, ErrorCodes.FODC0006, ErrorCodes.FODC0006.getDescription() + ": " + report.toString(), result);
        } finally {
            context.popDocumentContext();
        }
    }
}
Also used : MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) NodeImpl(org.exist.dom.memtree.NodeImpl) ValidationReport(org.exist.validation.ValidationReport) SAXAdapter(org.exist.dom.memtree.SAXAdapter)

Example 5 with SAXAdapter

use of org.exist.dom.memtree.SAXAdapter in project exist by eXist-db.

the class GMLIndexTest method lowLevelSearch.

@Test
public void lowLevelSearch() throws EXistException, SAXException, ParserConfigurationException, SpatialIndexException, IOException {
    GMLHandlerJTS geometryHandler = new GeometryHandler();
    GMLFilterGeometry geometryFilter = new GMLFilterGeometry(geometryHandler);
    GMLFilterDocument handler = new GMLFilterDocument(geometryFilter);
    final BrokerPool pool = server.getBrokerPool();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        AbstractGMLJDBCIndexWorker indexWorker = (AbstractGMLJDBCIndexWorker) broker.getIndexController().getWorkerByIndexId(AbstractGMLJDBCIndex.ID);
        // Unplugged
        if (indexWorker != null) {
            SAXParserFactory factory = ExistSAXParserFactory.getSAXParserFactory();
            factory.setNamespaceAware(true);
            InputSource src = new InputSource(new StringReader(IN_MEMORY_GML));
            SAXParser parser = factory.newSAXParser();
            XMLReader reader = parser.getXMLReader();
            SAXAdapter adapter = new SAXAdapter();
            reader.setContentHandler(handler);
            reader.setProperty("http://xml.org/sax/properties/lexical-handler", adapter);
            reader.parse(src);
            Geometry EPSG4326_geometry = indexWorker.transformGeometry(currentGeometry, "osgb:BNG", "EPSG:4326");
            assertNotNull(EPSG4326_geometry);
            NodeSet ns = indexWorker.search(broker, null, EPSG4326_geometry, SpatialOperator.EQUALS);
            assertTrue(ns.getLength() > 0);
            ns = indexWorker.search(broker, null, EPSG4326_geometry, SpatialOperator.DISJOINT);
            assertTrue(ns.getLength() > 0);
            ns = indexWorker.search(broker, null, EPSG4326_geometry, SpatialOperator.INTERSECTS);
            assertTrue(ns.getLength() > 0);
            ns = indexWorker.search(broker, null, EPSG4326_geometry, SpatialOperator.TOUCHES);
            // assertTrue(ns.getLength() > 0);
            ns = indexWorker.search(broker, null, EPSG4326_geometry, SpatialOperator.CROSSES);
            // assertTrue(ns.getLength() > 0);
            ns = indexWorker.search(broker, null, EPSG4326_geometry, SpatialOperator.WITHIN);
            assertTrue(ns.getLength() > 0);
            ns = indexWorker.search(broker, null, EPSG4326_geometry, SpatialOperator.CONTAINS);
            assertTrue(ns.getLength() > 0);
        // ns = ((GMLIndexWorker)index.getWorker()).search(broker, EPSG4326_geometry, SpatialOperator.OVERLAPS);
        // assertTrue(ns.getLength() > 0);
        }
    }
}
Also used : NodeSet(org.exist.dom.persistent.NodeSet) FileInputSource(org.exist.util.FileInputSource) GMLHandlerJTS(org.geotools.gml.GMLHandlerJTS) GMLFilterDocument(org.geotools.gml.GMLFilterDocument) Geometry(com.vividsolutions.jts.geom.Geometry) GMLFilterGeometry(org.geotools.gml.GMLFilterGeometry) DBBroker(org.exist.storage.DBBroker) StringReader(java.io.StringReader) SAXParser(javax.xml.parsers.SAXParser) SAXAdapter(org.exist.dom.memtree.SAXAdapter) GMLFilterGeometry(org.geotools.gml.GMLFilterGeometry) BrokerPool(org.exist.storage.BrokerPool) SAXParserFactory(javax.xml.parsers.SAXParserFactory) ExistSAXParserFactory(org.exist.util.ExistSAXParserFactory)

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