Search in sources :

Example 86 with XQuery

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

the class MatchListenerTest method indexOnInnerElement.

@Test
public void indexOnInnerElement() throws PermissionDeniedException, IOException, LockException, CollectionConfigurationException, SAXException, EXistException, XPathException {
    configureAndStore(CONF2, XML);
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        final XQuery xquery = pool.getXQueryService();
        assertNotNull(xquery);
        Sequence seq = xquery.execute(broker, "//para[ngram:contains(note, 'nested inner')]", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        String result = queryResult2String(broker, seq, 0);
        XMLAssert.assertEquals("<para>another paragraph with <note><hi>" + MATCH_START + "nested" + MATCH_END + "</hi>" + MATCH_START + " inner" + MATCH_END + "</note> elements.</para>", result);
        seq = xquery.execute(broker, "//note[ngram:contains(., 'nested inner')]/parent::para", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        result = queryResult2String(broker, seq, 0);
        XMLAssert.assertEquals("<para>another paragraph with <note><hi>" + MATCH_START + "nested" + MATCH_END + "</hi>" + MATCH_START + " inner" + MATCH_END + "</note> elements.</para>", result);
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) XQuery(org.exist.xquery.XQuery) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool) Test(org.junit.Test)

Example 87 with XQuery

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

the class MatchListenerTest method wildcardMatch.

@Test
public void wildcardMatch() throws PermissionDeniedException, IOException, LockException, CollectionConfigurationException, SAXException, EXistException, XPathException, XpathException {
    configureAndStore(CONF1, XML);
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        final XQuery xquery = pool.getXQueryService();
        assertNotNull(xquery);
        Sequence seq = xquery.execute(broker, "//para[ngram:wildcard-contains(., 'double.*match')]", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        String result = queryResult2String(broker, seq, 0);
        XMLAssert.assertEquals("<para>" + MATCH_START + "double match double match" + MATCH_END + "</para>", result);
        seq = xquery.execute(broker, "//para[ngram:wildcard-contains(., 'paragraph.*content\\.')]", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        result = queryResult2String(broker, seq, 0);
        XMLAssert.assertEquals("<para>some " + MATCH_START + "paragraph with " + MATCH_END + "<hi>" + MATCH_START + "mixed" + MATCH_END + "</hi>" + MATCH_START + " content." + MATCH_END + "</para>", result);
        String wildcardQuery = "...with.*[tn].*ele.ent[sc].*";
        seq = xquery.execute(broker, "//para[ngram:wildcard-contains(., '" + wildcardQuery + "')]", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        result = queryResult2String(broker, seq, 0);
        XMLAssert.assertEquals("<para>another paragra" + MATCH_START + "ph with " + MATCH_END + "<note><hi>" + MATCH_START + "nested" + MATCH_END + "</hi>" + MATCH_START + " inner" + MATCH_END + "</note>" + MATCH_START + " elements." + MATCH_END + "</para>", result);
        final XpathEngine xpe = XMLUnit.newXpathEngine();
        final NodeList matches = xpe.getMatchingNodes("//exist:match", XMLUnit.buildControlDocument(result));
        final StringBuilder m = new StringBuilder();
        for (int i = 0; i < matches.getLength(); i++) {
            m.append(matches.item(i).getTextContent());
        }
        String match = m.toString();
        assertMatches(wildcardQuery, match);
        wildcardQuery = "\\*.*\\?";
        seq = xquery.execute(broker, "//para[ngram:wildcard-contains(., '" + wildcardQuery + "')]", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        result = queryResult2String(broker, seq, 0);
        XMLAssert.assertEquals("<para>Where did all the " + MATCH_START + "*s go?" + MATCH_END + "</para>", result);
        match = xpe.evaluate("//exist:match", XMLUnit.buildControlDocument(result));
        assertMatches(wildcardQuery, match);
        wildcardQuery = ".est[][?]tes.";
        seq = xquery.execute(broker, "//para[ngram:wildcard-contains(., '" + wildcardQuery + "')]", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        result = queryResult2String(broker, seq, 0);
        XMLAssert.assertEquals("<para>" + MATCH_START + "test]test" + MATCH_END + " " + MATCH_START + "test[test" + MATCH_END + " " + MATCH_START + "test?test" + MATCH_END + "</para>", result);
        match = xpe.evaluate("//exist:match", XMLUnit.buildControlDocument(result));
        seq = xquery.execute(broker, "//para[ngram:wildcard-contains(., '^" + wildcardQuery + "')]", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        result = queryResult2String(broker, seq, 0);
        XMLAssert.assertEquals("<para>" + MATCH_START + "test]test" + MATCH_END + " test[test test?test</para>", result);
        match = xpe.evaluate("//exist:match", XMLUnit.buildControlDocument(result));
        seq = xquery.execute(broker, "//para[ngram:wildcard-contains(., '" + wildcardQuery + "$')]", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        result = queryResult2String(broker, seq, 0);
        XMLAssert.assertEquals("<para>test]test test[test " + MATCH_START + "test?test" + MATCH_END + "</para>", result);
        match = xpe.evaluate("//exist:match", XMLUnit.buildControlDocument(result));
        wildcardQuery = "^aaa.aaa$";
        seq = xquery.execute(broker, "//para[ngram:wildcard-contains(., '" + wildcardQuery + "')]", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        result = queryResult2String(broker, seq, 0);
        XMLAssert.assertEquals("<para>" + MATCH_START + "aaacaaa" + MATCH_END + "</para>", result);
        match = xpe.evaluate("//exist:match", XMLUnit.buildControlDocument(result));
        assertMatches(wildcardQuery, match);
        wildcardQuery = ".+simple";
        seq = xquery.execute(broker, "//para[ngram:wildcard-contains(., '" + wildcardQuery + "')]", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        result = queryResult2String(broker, seq, 0);
        XMLAssert.assertEquals("<para>" + MATCH_START + "a simple" + MATCH_END + " paragraph</para>", result);
        match = xpe.evaluate("//exist:match", XMLUnit.buildControlDocument(result));
        assertMatches(wildcardQuery, match);
        wildcardQuery = "a s.?i.?m.?p.?l.?e.?";
        seq = xquery.execute(broker, "//para[ngram:wildcard-contains(., '" + wildcardQuery + "')]", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        result = queryResult2String(broker, seq, 0);
        XMLAssert.assertEquals("<para>" + MATCH_START + "a simple " + MATCH_END + "paragraph</para>", result);
        match = xpe.evaluate("//exist:match", XMLUnit.buildControlDocument(result));
        assertMatches(wildcardQuery, match);
        wildcardQuery = "a s.?i.?m.?p.?l.?e.?";
        seq = xquery.execute(broker, "//para[ngram:wildcard-contains(., '" + wildcardQuery + "')]", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        result = queryResult2String(broker, seq, 0);
        XMLAssert.assertEquals("<para>" + MATCH_START + "a simple " + MATCH_END + "paragraph</para>", result);
        match = xpe.evaluate("//exist:match", XMLUnit.buildControlDocument(result));
        assertMatches(wildcardQuery, match);
        wildcardQuery = "b.{3,6}c";
        seq = xquery.execute(broker, "//para[ngram:wildcard-contains(., '" + wildcardQuery + "')]", null);
        assertNotNull(seq);
        assertEquals(2, seq.getItemCount());
        for (int i = 0; i < 2; i++) {
            result = queryResult2String(broker, seq, i);
            match = xpe.evaluate("//exist:match", XMLUnit.buildControlDocument(result));
            assertMatches(wildcardQuery, match);
        }
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) XpathEngine(org.custommonkey.xmlunit.XpathEngine) XQuery(org.exist.xquery.XQuery) NodeList(org.w3c.dom.NodeList) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool) Test(org.junit.Test)

Example 88 with XQuery

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

the class MatchListenerTest method matchInParent.

@Test
public void matchInParent() throws PermissionDeniedException, IOException, LockException, CollectionConfigurationException, SAXException, EXistException, XPathException {
    configureAndStore(CONF1, XML);
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        final XQuery xquery = pool.getXQueryService();
        assertNotNull(xquery);
        final Sequence seq = xquery.execute(broker, "//para[ngram:contains(., 'mixed')]/hi", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        final String result = queryResult2String(broker, seq, 0);
        XMLAssert.assertEquals("<hi>" + MATCH_START + "mixed" + MATCH_END + "</hi>", result);
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) XQuery(org.exist.xquery.XQuery) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool) Test(org.junit.Test)

Example 89 with XQuery

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

the class MatchListenerTest method doubleMatch.

@Test
public void doubleMatch() throws PermissionDeniedException, XPathException, SAXException, EXistException, CollectionConfigurationException, LockException, IOException {
    configureAndStore(CONF1, XML);
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        final XQuery xquery = pool.getXQueryService();
        assertNotNull(xquery);
        Sequence seq = xquery.execute(broker, "//para[ngram:contains(., 'double match')]", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        String result = queryResult2String(broker, seq, 0);
        XMLAssert.assertEquals("<para>" + MATCH_START + "double match" + MATCH_END + " " + MATCH_START + "double match" + MATCH_END + "</para>", result);
        seq = xquery.execute(broker, "//para[ngram:contains(., 'aaa aaa')]", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        result = queryResult2String(broker, seq, 0);
        XMLAssert.assertEquals("<para>" + MATCH_START + "aaa aaa" + MATCH_END + " aaa</para>", result);
        seq = xquery.execute(broker, "//para[ngram:ends-with(., 'aaa aaa')]", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        result = queryResult2String(broker, seq, 0);
        XMLAssert.assertEquals("<para>aaa " + MATCH_START + "aaa aaa" + MATCH_END + "</para>", result);
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) XQuery(org.exist.xquery.XQuery) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool) Test(org.junit.Test)

Example 90 with XQuery

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

the class MatchListenerTest method matchInAncestor.

@Test
public void matchInAncestor() throws PermissionDeniedException, IOException, LockException, CollectionConfigurationException, SAXException, EXistException, XPathException {
    configureAndStore(CONF1, XML);
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        final XQuery xquery = pool.getXQueryService();
        assertNotNull(xquery);
        Sequence seq = xquery.execute(broker, "//para[ngram:contains(., 'nested')]/note", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        String result = queryResult2String(broker, seq, 0);
        XMLAssert.assertEquals("<note><hi>" + MATCH_START + "nested" + MATCH_END + "</hi> inner</note>", result);
        seq = xquery.execute(broker, "//para[ngram:contains(., 'nested')]//hi", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        result = queryResult2String(broker, seq, 0);
        XMLAssert.assertEquals("<hi>" + MATCH_START + "nested" + MATCH_END + "</hi>", result);
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) XQuery(org.exist.xquery.XQuery) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool) Test(org.junit.Test)

Aggregations

XQuery (org.exist.xquery.XQuery)135 Sequence (org.exist.xquery.value.Sequence)108 DBBroker (org.exist.storage.DBBroker)107 BrokerPool (org.exist.storage.BrokerPool)105 CompiledXQuery (org.exist.xquery.CompiledXQuery)59 Test (org.junit.Test)36 XQueryContext (org.exist.xquery.XQueryContext)33 Txn (org.exist.storage.txn.Txn)32 XPathException (org.exist.xquery.XPathException)21 TransactionManager (org.exist.storage.txn.TransactionManager)17 Item (org.exist.xquery.value.Item)16 InputSource (org.xml.sax.InputSource)16 XQueryPool (org.exist.storage.XQueryPool)15 DefaultDocumentSet (org.exist.dom.persistent.DefaultDocumentSet)12 DocumentSet (org.exist.dom.persistent.DocumentSet)12 StringReader (java.io.StringReader)11 Properties (java.util.Properties)11 MutableDocumentSet (org.exist.dom.persistent.MutableDocumentSet)11 Modification (org.exist.xupdate.Modification)11 XUpdateProcessor (org.exist.xupdate.XUpdateProcessor)11