Search in sources :

Example 36 with XQueryContext

use of org.exist.xquery.XQueryContext in project exist by eXist-db.

the class CollectionTest method doc_dynamicallyAvailableCollection_absoluteUri.

@Test
public void doc_dynamicallyAvailableCollection_absoluteUri() throws XPathException, EXistException, PermissionDeniedException {
    final BrokerPool pool = BrokerPool.getInstance();
    final String doc = "<timestamp>" + System.currentTimeMillis() + "</timestamp>";
    final String collectionUri = "http://from-dynamic-context/collection1";
    final String query = "fn:collection('" + collectionUri + "')";
    try (final DBBroker broker = pool.getBroker()) {
        final XQueryContext context = new XQueryContext(pool);
        context.addDynamicallyAvailableCollection(collectionUri, (broker2, transaction, uri) -> asInMemoryDocument(doc));
        final XQuery xqueryService = pool.getXQueryService();
        final CompiledXQuery compiled = xqueryService.compile(context, query);
        final Sequence result = xqueryService.execute(broker, compiled, null);
        assertFalse(result.isEmpty());
        assertEquals(1, result.getItemCount());
        assertTrue(result.itemAt(0) instanceof Node);
        final Source expectedSource = Input.fromString(doc).build();
        final Source actualSource = Input.fromNode((Node) result.itemAt(0)).build();
        final Diff diff = DiffBuilder.compare(expectedSource).withTest(actualSource).checkForIdentical().checkForSimilar().build();
        assertFalse(diff.toString(), diff.hasDifferences());
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) Diff(org.xmlunit.diff.Diff) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) Node(org.w3c.dom.Node) XQueryContext(org.exist.xquery.XQueryContext) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) Test(org.junit.Test)

Example 37 with XQueryContext

use of org.exist.xquery.XQueryContext in project exist by eXist-db.

the class CollectionTest method doc_dynamicallyAvailableCollection_relativeUri.

@Test
public void doc_dynamicallyAvailableCollection_relativeUri() throws XPathException, EXistException, PermissionDeniedException, URISyntaxException {
    final BrokerPool pool = BrokerPool.getInstance();
    final String doc = "<timestamp>" + System.currentTimeMillis() + "</timestamp>";
    final String baseUri = "http://from-dynamic-context/";
    final String collectionRelativeUri = "collection1";
    final String query = "fn:collection('" + collectionRelativeUri + "')";
    try (final DBBroker broker = pool.getBroker()) {
        final XQueryContext context = new XQueryContext(pool);
        context.setBaseURI(new AnyURIValue(new URI(baseUri)));
        context.addDynamicallyAvailableCollection(baseUri + collectionRelativeUri, (broker2, transaction, uri) -> asInMemoryDocument(doc));
        final XQuery xqueryService = pool.getXQueryService();
        final CompiledXQuery compiled = xqueryService.compile(context, query);
        final Sequence result = xqueryService.execute(broker, compiled, null);
        assertFalse(result.isEmpty());
        assertEquals(1, result.getItemCount());
        assertTrue(result.itemAt(0) instanceof Node);
        final Source expectedSource = Input.fromString(doc).build();
        final Source actualSource = Input.fromNode((Node) result.itemAt(0)).build();
        final Diff diff = DiffBuilder.compare(expectedSource).withTest(actualSource).checkForIdentical().checkForSimilar().build();
        assertFalse(diff.toString(), diff.hasDifferences());
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) Diff(org.xmlunit.diff.Diff) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) AnyURIValue(org.exist.xquery.value.AnyURIValue) Node(org.w3c.dom.Node) XQueryContext(org.exist.xquery.XQueryContext) Sequence(org.exist.xquery.value.Sequence) URI(java.net.URI) BrokerPool(org.exist.storage.BrokerPool) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) Test(org.junit.Test)

Example 38 with XQueryContext

use of org.exist.xquery.XQueryContext in project exist by eXist-db.

the class CustomIndexTest method checkIndex.

// TODO : could be replaced by an XQuery call to index-keys(). See above
private void checkIndex(DBBroker broker, DocumentSet docs, String term, int count) {
    NGramIndexWorker index = (NGramIndexWorker) broker.getIndexController().getWorkerByIndexId(NGramIndex.ID);
    XQueryContext context = new XQueryContext(broker.getBrokerPool());
    Occurrences[] occurrences = index.scanIndex(context, docs, null, null);
    int found = 0;
    for (int i = 0; i < occurrences.length; i++) {
        Occurrences occurrence = occurrences[i];
        if (occurrence.getTerm().compareTo(term) == 0)
            found++;
    }
    assertEquals(count, found);
}
Also used : XQueryContext(org.exist.xquery.XQueryContext)

Example 39 with XQueryContext

use of org.exist.xquery.XQueryContext in project exist by eXist-db.

the class LuceneIndexTest method checkIndex.

/**
 * It really depends on the Analyzer used with the index,
 *  but probably you would like to have the 'term' argument all lower cased.
 *  @see <a href='https://sourceforge.net/p/exist/mailman/message/36436727/'>Help needed with a test case</a>
 */
private Occurrences[] checkIndex(final DocumentSet docs, final DBBroker broker, final QName[] qn, final String term, final int expected) {
    final LuceneIndexWorker index = (LuceneIndexWorker) broker.getIndexController().getWorkerByIndexId(LuceneIndex.ID);
    final Map<String, Object> hints = new HashMap<>();
    if (term != null) {
        hints.put(OrderedValuesIndex.START_VALUE, term);
    }
    if (qn != null && qn.length > 0) {
        final List<QName> qnlist = new ArrayList<>(qn.length);
        qnlist.addAll(Arrays.asList(qn));
        hints.put(QNamedKeysIndex.QNAMES_KEY, qnlist);
    }
    final XQueryContext context = new XQueryContext(broker.getBrokerPool());
    final Occurrences[] occur = index.scanIndex(context, docs, null, hints);
    assertEquals(expected, occur.length);
    return occur;
}
Also used : QName(org.exist.dom.QName) XQueryContext(org.exist.xquery.XQueryContext)

Example 40 with XQueryContext

use of org.exist.xquery.XQueryContext in project exist by eXist-db.

the class Modification method select.

/**
 * Evaluate the select expression.
 *
 * @param docs the documents to evaludate the expression over
 *
 * @return The selected nodes.
 *
 * @throws PermissionDeniedException if the caller has insufficient priviledges
 * @throws EXistException if the database raises an error
 * @throws XPathException if the XPath raises an error
 */
protected NodeList select(DocumentSet docs) throws PermissionDeniedException, EXistException, XPathException {
    final XQuery xquery = broker.getBrokerPool().getXQueryService();
    final XQueryPool pool = broker.getBrokerPool().getXQueryPool();
    final Source source = new StringSource(selectStmt);
    CompiledXQuery compiled = pool.borrowCompiledXQuery(broker, source);
    XQueryContext context;
    if (compiled == null) {
        context = new XQueryContext(broker.getBrokerPool());
    } else {
        context = compiled.getContext();
        context.prepareForReuse();
    }
    context.setStaticallyKnownDocuments(docs);
    declareNamespaces(context);
    declareVariables(context);
    if (compiled == null)
        try {
            compiled = xquery.compile(context, source);
        } catch (final IOException e) {
            throw new EXistException("An exception occurred while compiling the query: " + e.getMessage());
        }
    Sequence resultSeq = null;
    try {
        resultSeq = xquery.execute(broker, compiled, null);
    } finally {
        context.runCleanupTasks();
        pool.returnCompiledXQuery(source, compiled);
    }
    if (!(resultSeq.isEmpty() || Type.subTypeOf(resultSeq.getItemType(), Type.NODE))) {
        throw new EXistException("select expression should evaluate to a node-set; got " + Type.getTypeName(resultSeq.getItemType()));
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("found {} for select: {}", resultSeq.getItemCount(), selectStmt);
    }
    return resultSeq.toNodeSet();
}
Also used : XQueryPool(org.exist.storage.XQueryPool) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQueryContext(org.exist.xquery.XQueryContext) StringSource(org.exist.source.StringSource) IOException(java.io.IOException) EXistException(org.exist.EXistException) Sequence(org.exist.xquery.value.Sequence) StringSource(org.exist.source.StringSource) Source(org.exist.source.Source)

Aggregations

XQueryContext (org.exist.xquery.XQueryContext)70 CompiledXQuery (org.exist.xquery.CompiledXQuery)37 Sequence (org.exist.xquery.value.Sequence)34 XQuery (org.exist.xquery.XQuery)33 DBBroker (org.exist.storage.DBBroker)24 Test (org.junit.Test)23 XPathException (org.exist.xquery.XPathException)18 BrokerPool (org.exist.storage.BrokerPool)17 IOException (java.io.IOException)11 Source (org.exist.source.Source)10 XQueryPool (org.exist.storage.XQueryPool)10 Node (org.w3c.dom.Node)10 MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)8 PermissionDeniedException (org.exist.security.PermissionDeniedException)8 AnyURIValue (org.exist.xquery.value.AnyURIValue)8 URI (java.net.URI)7 StringValue (org.exist.xquery.value.StringValue)7 InputSource (org.xml.sax.InputSource)7 EXistException (org.exist.EXistException)6 QName (org.exist.dom.QName)6