Search in sources :

Example 36 with XQuery

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

the class LuceneIndexTest method configuration.

@Test
public void configuration() throws EXistException, CollectionConfigurationException, PermissionDeniedException, SAXException, LockException, IOException, XPathException, QName.IllegalQNameException {
    final DocumentSet docs = configureAndStore(COLLECTION_CONFIG4, XML4, "test.xml");
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        checkIndex(docs, broker, new QName[] { new QName("a") }, "x", 1);
        checkIndex(docs, broker, new QName[] { new QName("c") }, "x", 1);
        final XQuery xquery = pool.getXQueryService();
        assertNotNull(xquery);
        Sequence seq = xquery.execute(broker, "/test[ft:query(a, 'x')]", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        seq = xquery.execute(broker, "/test[ft:query(.//c, 'x')]", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        seq = xquery.execute(broker, "/test[ft:query(b, 'x')]", null);
        assertNotNull(seq);
        assertEquals(0, seq.getItemCount());
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) QName(org.exist.dom.QName) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) DefaultDocumentSet(org.exist.dom.persistent.DefaultDocumentSet) DocumentSet(org.exist.dom.persistent.DocumentSet) MutableDocumentSet(org.exist.dom.persistent.MutableDocumentSet) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool)

Example 37 with XQuery

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

the class LuceneIndexTest method attributeMatch.

@Test
public void attributeMatch() throws EXistException, CollectionConfigurationException, PermissionDeniedException, SAXException, TriggerException, LockException, IOException, XPathException, ParserConfigurationException {
    final DocumentSet docs = configureAndStore(COLLECTION_CONFIG7, XML8, "test.xml");
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    final TransactionManager transact = pool.getTransactionManager();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
        final Txn transaction = transact.beginTransaction()) {
        final XQuery xquery = pool.getXQueryService();
        assertNotNull(xquery);
        Sequence seq = xquery.execute(broker, "for $a in ft:query((//b|//c), 'AAA') order by ft:score($a) descending return xs:string($a)", null);
        assertNotNull(seq);
        assertEquals(5, seq.getItemCount());
        assertEquals("AAA on b2", seq.itemAt(0).getStringValue());
        assertEquals("AAA on c1", seq.itemAt(1).getStringValue());
        assertEquals("AAA on b3", seq.itemAt(2).getStringValue());
        assertEquals("AAA on b1", seq.itemAt(3).getStringValue());
        assertEquals("AAA on c2", seq.itemAt(4).getStringValue());
        // path: /a/b
        seq = xquery.execute(broker, "for $a in ft:query(/a/b, 'AAA') order by ft:score($a) descending return xs:string($a)", null);
        assertNotNull(seq);
        assertEquals(2, seq.getItemCount());
        assertEquals("AAA on b2", seq.itemAt(0).getStringValue());
        assertEquals("AAA on b1", seq.itemAt(1).getStringValue());
        seq = xquery.execute(broker, "for $a in ft:query(//@att, 'att') order by ft:score($a) descending return xs:string($a)", null);
        assertNotNull(seq);
        assertEquals(4, seq.getItemCount());
        assertEquals("att on b2", seq.itemAt(0).getStringValue());
        assertEquals("att on b3", seq.itemAt(1).getStringValue());
        assertEquals("att on b1", seq.itemAt(2).getStringValue());
        assertEquals("att on c1", seq.itemAt(3).getStringValue());
        // modify with xupdate and check if boosts are updated accordingly
        final XUpdateProcessor proc = new XUpdateProcessor(broker, docs);
        assertNotNull(proc);
        proc.setBroker(broker);
        proc.setDocumentSet(docs);
        // remove 'att' attribute from first c element: it gets no boost
        // also append an 'att' attribute on second c element which will
        // make the two switch order in the result sequence.
        String xupdate = XUPDATE_START + "   <xu:remove select=\"//c[1]/@att\"/>" + "   <xu:append select=\"//c[2]\"><xu:attribute name=\"att\">att on c2</xu:attribute></xu:append>" + XUPDATE_END;
        final Modification[] modifications = proc.parse(new InputSource(new StringReader(xupdate)));
        assertNotNull(modifications);
        modifications[0].process(transaction);
        modifications[1].process(transaction);
        proc.reset();
        transact.commit(transaction);
        seq = xquery.execute(broker, "for $a in ft:query((//b|//c), 'AAA') order by ft:score($a) descending return xs:string($a)", null);
        assertNotNull(seq);
        assertEquals(5, seq.getItemCount());
        assertEquals("AAA on b2", seq.itemAt(0).getStringValue());
        assertEquals("AAA on c2", seq.itemAt(1).getStringValue());
        assertEquals("AAA on b3", seq.itemAt(2).getStringValue());
        assertEquals("AAA on b1", seq.itemAt(3).getStringValue());
        assertEquals("AAA on c1", seq.itemAt(4).getStringValue());
    }
}
Also used : XUpdateProcessor(org.exist.xupdate.XUpdateProcessor) Modification(org.exist.xupdate.Modification) InputSource(org.xml.sax.InputSource) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) Txn(org.exist.storage.txn.Txn) Sequence(org.exist.xquery.value.Sequence) DBBroker(org.exist.storage.DBBroker) TransactionManager(org.exist.storage.txn.TransactionManager) StringReader(java.io.StringReader) DefaultDocumentSet(org.exist.dom.persistent.DefaultDocumentSet) DocumentSet(org.exist.dom.persistent.DocumentSet) MutableDocumentSet(org.exist.dom.persistent.MutableDocumentSet) BrokerPool(org.exist.storage.BrokerPool)

Example 38 with XQuery

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

the class LuceneMatchListenerTest method matchInDescendant.

@Test
public void matchInDescendant() throws EXistException, PermissionDeniedException, XPathException, SAXException, IOException, XpathException, LockException, CollectionConfigurationException {
    configureAndStore(CONF3, XML);
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        XQuery xquery = pool.getXQueryService();
        assertNotNull(xquery);
        Sequence seq = xquery.execute(broker, "//hi[ft:query(., 'mixed')]/ancestor::para", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        String result = queryResult2String(broker, seq);
        XMLAssert.assertXpathEvaluatesTo("1", "count(//exist:match)", result);
        seq = xquery.execute(broker, "//hi[ft:query(., 'nested')]/parent::note", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        result = queryResult2String(broker, seq);
        XMLAssert.assertXpathEvaluatesTo("1", "count(//hi/exist:match)", 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 39 with XQuery

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

the class ApacheFopTest method simplePdf.

@Test
public void simplePdf() throws EXistException, PermissionDeniedException, XPathException {
    final String fopConfig = "<fop version=\"1.0\">\n" + "    <strict-configuration>true</strict-configuration>\n" + "    <strict-validation>false</strict-validation>\n" + "    <base>./</base>\n" + "    <renderers>\n" + "        <renderer mime=\"application/pdf\"></renderer>\n" + "    </renderers>\n" + "</fop>";
    final String fo = "<fo:root xmlns:fo=\"http://www.w3.org/1999/XSL/Format\">\n" + "    <fo:layout-master-set>\n" + "        <fo:simple-page-master master-name=\"page-left\" page-height=\"297mm\" page-width=\"210mm\" margin-bottom=\"10mm\" margin-top=\"10mm\" margin-left=\"36mm\" margin-right=\"18mm\">\n" + "            <fo:region-body margin-bottom=\"10mm\" margin-top=\"16mm\"/>\n" + "            <fo:region-before region-name=\"head-left\" extent=\"10mm\"/>\n" + "        </fo:simple-page-master>\n" + "        <fo:simple-page-master master-name=\"page-right\" page-height=\"297mm\" page-width=\"210mm\" margin-bottom=\"10mm\" margin-top=\"10mm\" margin-left=\"18mm\" margin-right=\"36mm\">\n" + "            <fo:region-body margin-bottom=\"10mm\" margin-top=\"16mm\"/>\n" + "            <fo:region-before region-name=\"head-right\" extent=\"10mm\"/>\n" + "        </fo:simple-page-master>\n" + "        <fo:page-sequence-master master-name=\"page-content\">\n" + "            <fo:repeatable-page-master-alternatives>\n" + "                <fo:conditional-page-master-reference master-reference=\"page-right\" odd-or-even=\"odd\"/>\n" + "                <fo:conditional-page-master-reference master-reference=\"page-left\" odd-or-even=\"even\"/>\n" + "            </fo:repeatable-page-master-alternatives>\n" + "        </fo:page-sequence-master>\n" + "    </fo:layout-master-set>\n" + "    <fo:page-sequence master-reference=\"page-content\">\n" + "         <fo:flow flow-name=\"xsl-region-body\" hyphenate=\"true\" language=\"en\" xml:lang=\"en\">\n" + "                <fo:block id=\"A97060-t\" line-height=\"16pt\" font-size=\"11pt\">\n" + "                    <fo:block id=\"A97060-e0\" page-break-after=\"right\">\n" + "                        <fo:block id=\"A97060-e100\" text-align=\"justify\" space-before=\".5em\" text-indent=\"1.5em\" space-after=\".5em\">\n" + "                            Hello World!\n" + "                        </fo:block>\n" + "                    </fo:block>\n" + "                </fo:block>\n" + "        </fo:flow>\n" + "    </fo:page-sequence>\n" + "</fo:root>";
    final String xquery = "xquery version \"3.1\";\n" + "\n" + "import module namespace xslfo=\"http://exist-db.org/xquery/xslfo\";\n" + "\n" + "let $config := " + fopConfig + "\n" + "let $fo := " + fo + "\n" + "\n" + "let $pdf := xslfo:render($fo, \"application/pdf\", (), $config)\n" + "return $pdf";
    final BrokerPool pool = server.getBrokerPool();
    final XQuery xqueryService = pool.getXQueryService();
    try (final DBBroker broker = pool.getBroker()) {
        final Sequence result = xqueryService.execute(broker, xquery, null);
        assertNotNull(result);
        assertEquals(1, result.getItemCount());
        final Item pdf = result.itemAt(0);
        assertEquals(Type.BASE64_BINARY, pdf.getType());
    }
}
Also used : Item(org.exist.xquery.value.Item) 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 40 with XQuery

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

the class IPRangeRealm method authenticate.

@Override
public Subject authenticate(final String ipAddress, final Object credentials) throws AuthenticationException {
    // Elevaste to system privileges
    try (final DBBroker broker = getSecurityManager().database().get(Optional.of(getSecurityManager().getSystemSubject()))) {
        // Convert IP address
        final long ipToTest = ipToLong(InetAddress.getByName(ipAddress));
        // Get xquery service
        final XQuery queryService = broker.getBrokerPool().getXQueryService();
        if (queryService == null) {
            LOG.error("IPRange broker unable to retrieve XQueryService");
            return null;
        }
        // Construct XQuery
        final String query = "collection('/db/system/security/iprange/accounts')/account/" + "iprange[" + ipToTest + " ge number(start) and " + ipToTest + " le number(end)]/../name";
        final XQueryContext context = new XQueryContext(broker.getBrokerPool());
        final CompiledXQuery compiled = queryService.compile(context, query);
        final Properties outputProperties = new Properties();
        // Execute xQuery
        final Sequence result = queryService.execute(broker, compiled, null, outputProperties);
        final SequenceIterator i = result.iterate();
        // Get FIRST username when present
        final String username = i.hasNext() ? i.nextItem().getStringValue() : "";
        if (i.hasNext()) {
            LOG.warn("IP address {} matched multiple ipranges. Using first result only.", ipAddress);
        }
        if (!username.isEmpty()) {
            final Account account = getSecurityManager().getAccount(username);
            if (account != null) {
                LOG.info("IPRangeRealm trying {}", account.getName());
                return new SubjectAccreditedImpl((AbstractAccount) account, ipAddress);
            } else {
                LOG.info("IPRangeRealm couldn't resolve account for {}", username);
            }
        } else {
            LOG.info("IPRangeRealm xquery found no matches");
        }
        return null;
    } catch (final EXistException | UnknownHostException | XPathException | PermissionDeniedException e) {
        throw new AuthenticationException(AuthenticationException.UNNOWN_EXCEPTION, e.getMessage());
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) XPathException(org.exist.xquery.XPathException) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQueryContext(org.exist.xquery.XQueryContext) Sequence(org.exist.xquery.value.Sequence) EXistException(org.exist.EXistException) Properties(java.util.Properties) DBBroker(org.exist.storage.DBBroker) SequenceIterator(org.exist.xquery.value.SequenceIterator) SubjectAccreditedImpl(org.exist.security.internal.SubjectAccreditedImpl)

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