Search in sources :

Example 61 with XQuery

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

the class InspectModuleTest method xqDoc_onAnnotatedFunction.

@Test
public void xqDoc_onAnnotatedFunction() throws PermissionDeniedException, XPathException, EXistException {
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    final XQuery xqueryService = pool.getXQueryService();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
        final Txn transaction = pool.getTransactionManager().beginTransaction()) {
        final String query = "import module namespace inspect = \"http://exist-db.org/xquery/inspection\";\n" + "inspect:inspect-module(xs:anyURI(\"xmldb:exist://" + TEST_COLLECTION.append(TEST_MODULE).toCollectionPathURI() + "\"))\n" + "/function[@name eq \"x:fun4\"]";
        final Sequence result = xqueryService.execute(broker, query, null);
        assertNotNull(result);
        assertEquals(1, result.getItemCount());
        final Item item1 = result.itemAt(0);
        assertTrue(item1 instanceof ElementImpl);
        final Element function = (Element) item1;
        final NodeList descriptions = function.getElementsByTagName("description");
        assertEquals(1, descriptions.getLength());
        assertEquals("An annotated function.", descriptions.item(0).getFirstChild().getNodeValue());
        final NodeList annotations = function.getElementsByTagName("annotation");
        assertEquals(2, annotations.getLength());
        assertEquals("public", ((Element) annotations.item(0)).getAttribute("name"));
        assertEquals("x:path", ((Element) annotations.item(1)).getAttribute("name"));
        assertEquals("/x/y/z", annotations.item(1).getFirstChild().getFirstChild().getNodeValue());
        final NodeList arguments = function.getElementsByTagName("argument");
        assertEquals(0, arguments.getLength());
        final NodeList returns = function.getElementsByTagName("returns");
        assertEquals(1, returns.getLength());
        assertEquals("another result", returns.item(0).getFirstChild().getNodeValue());
        transaction.commit();
    }
}
Also used : Item(org.exist.xquery.value.Item) DBBroker(org.exist.storage.DBBroker) ElementImpl(org.exist.dom.memtree.ElementImpl) XQuery(org.exist.xquery.XQuery) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Txn(org.exist.storage.txn.Txn) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool)

Example 62 with XQuery

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

the class InspectModuleTest method xqDoc_multilineDesciption.

@Test
public void xqDoc_multilineDesciption() throws PermissionDeniedException, XPathException, EXistException {
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    final XQuery xqueryService = pool.getXQueryService();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
        final Txn transaction = pool.getTransactionManager().beginTransaction()) {
        final String query = "import module namespace inspect = \"http://exist-db.org/xquery/inspection\";\n" + "inspect:inspect-module(xs:anyURI(\"xmldb:exist://" + TEST_COLLECTION.append(TEST_MODULE).toCollectionPathURI() + "\"))\n" + "/function[@name eq \"x:fun3\"]";
        final Sequence result = xqueryService.execute(broker, query, null);
        assertNotNull(result);
        assertEquals(1, result.getItemCount());
        final Item item1 = result.itemAt(0);
        assertTrue(item1 instanceof ElementImpl);
        final Element function = (Element) item1;
        final NodeList descriptions = function.getElementsByTagName("description");
        assertEquals(1, descriptions.getLength());
        assertEquals("This is a multiline description and therefore\n spans multiple\n lines.", descriptions.item(0).getFirstChild().getNodeValue());
        final NodeList arguments = function.getElementsByTagName("argument");
        assertEquals(0, arguments.getLength());
        final NodeList returns = function.getElementsByTagName("returns");
        assertEquals(1, returns.getLength());
        assertEquals("another result", returns.item(0).getFirstChild().getNodeValue());
        transaction.commit();
    }
}
Also used : Item(org.exist.xquery.value.Item) DBBroker(org.exist.storage.DBBroker) ElementImpl(org.exist.dom.memtree.ElementImpl) XQuery(org.exist.xquery.XQuery) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Txn(org.exist.storage.txn.Txn) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool)

Example 63 with XQuery

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

the class FunUnparsedTextTest method unparsedTextAvailable_dynamicallyAvailableDocument_relativeUri.

@Test
public void unparsedTextAvailable_dynamicallyAvailableDocument_relativeUri() throws XPathException, EXistException, PermissionDeniedException, URISyntaxException {
    final BrokerPool pool = BrokerPool.getInstance();
    final String text = "hello, the time is: " + System.currentTimeMillis();
    final String baseUri = "http://from-dynamic-context/";
    final String textRelativeUri = "doc1";
    final String query = "fn:unparsed-text-available('" + textRelativeUri + "')";
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        final XQueryContext context = new XQueryContext(pool);
        context.setBaseURI(new AnyURIValue(new URI(baseUri)));
        context.addDynamicallyAvailableTextResource(baseUri + textRelativeUri, UTF_8, (broker2, transaction, uri, charset) -> new InputStreamReader(new ByteArrayInputStream(text.getBytes(UTF_8)), charset));
        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).toJavaObject(Boolean.class).booleanValue());
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) AnyURIValue(org.exist.xquery.value.AnyURIValue) XQueryContext(org.exist.xquery.XQueryContext) Sequence(org.exist.xquery.value.Sequence) URI(java.net.URI) BrokerPool(org.exist.storage.BrokerPool) Test(org.junit.Test)

Example 64 with XQuery

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

the class FunUnparsedTextTest method unparsedTextLines_noDataStream.

@Test(expected = XPathException.class)
public void unparsedTextLines_noDataStream() throws XPathException, EXistException, PermissionDeniedException {
    final BrokerPool pool = BrokerPool.getInstance();
    final String text = "hello, the time is: " + System.currentTimeMillis();
    final String textUri = "http://from-dynamic-context/doc1";
    final String query = "fn:unparsed-text-lines('" + textUri + "')";
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        final XQueryContext context = new XQueryContext(pool);
        context.addDynamicallyAvailableTextResource(textUri, UTF_8, (broker2, transaction, uri, charset) -> new InputStreamReader(null, charset));
        final XQuery xqueryService = pool.getXQueryService();
        final CompiledXQuery compiled = xqueryService.compile(context, query);
        final Sequence result = xqueryService.execute(broker, compiled, null);
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) InputStreamReader(java.io.InputStreamReader) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQueryContext(org.exist.xquery.XQueryContext) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool) Test(org.junit.Test)

Example 65 with XQuery

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

the class FunUnparsedTextTest method unparsedText_dynamicallyAvailableDocument_relativeUri.

@Test
public void unparsedText_dynamicallyAvailableDocument_relativeUri() throws XPathException, EXistException, PermissionDeniedException, URISyntaxException {
    final BrokerPool pool = BrokerPool.getInstance();
    final String text = "hello, the time is: " + System.currentTimeMillis();
    final String baseUri = "http://from-dynamic-context/";
    final String textRelativeUri = "doc1";
    final String query = "fn:unparsed-text('" + textRelativeUri + "')";
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        final XQueryContext context = new XQueryContext(pool);
        context.setBaseURI(new AnyURIValue(new URI(baseUri)));
        context.addDynamicallyAvailableTextResource(baseUri + textRelativeUri, UTF_8, (broker2, transaction, uri, charset) -> new InputStreamReader(new ByteArrayInputStream(text.getBytes(UTF_8)), charset));
        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());
        assertEquals(Type.STRING, result.itemAt(0).getType());
        assertEquals(text, result.itemAt(0).getStringValue());
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) AnyURIValue(org.exist.xquery.value.AnyURIValue) XQueryContext(org.exist.xquery.XQueryContext) Sequence(org.exist.xquery.value.Sequence) URI(java.net.URI) 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