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);
}
}
}
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();
}
}
}
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);
}
}
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();
}
}
}
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);
}
}
}
Aggregations