Search in sources :

Example 1 with CompiledExpression

use of org.xmldb.api.base.CompiledExpression in project exist by eXist-db.

the class StoredModuleTest method testQuery.

@Test
public void testQuery() throws Exception {
    Collection c = createCollection("test");
    writeModule(c, "test.xqm", MODULE);
    String query = "import module namespace itg-modules = \"http://localhost:80/itg/xquery\" at " + "\"xmldb:exist://" + XmldbURI.ROOT_COLLECTION + "/test/test.xqm\"; itg-modules:check-coll()";
    String[] cols = { "one", "two", "three" };
    final XQueryService xqService = (XQueryService) existEmbeddedServer.getRoot().getService("XQueryService", "1.0");
    xqService.setNamespace("itg-modules", "http://localhost:80/itg/xquery");
    CompiledExpression compiledQuery = xqService.compile(query);
    for (int i = 0; i < cols.length; i++) {
        xqService.declareVariable("itg-modules:coll", cols[i]);
        ResourceSet result = xqService.execute(compiledQuery);
        result.getResource(0).getContent();
    }
}
Also used : XQueryService(org.xmldb.api.modules.XQueryService) Collection(org.xmldb.api.base.Collection) ResourceSet(org.xmldb.api.base.ResourceSet) CompiledExpression(org.xmldb.api.base.CompiledExpression)

Example 2 with CompiledExpression

use of org.xmldb.api.base.CompiledExpression in project exist by eXist-db.

the class TestDataGenerator method generate.

public Path[] generate(final org.xmldb.api.base.Collection collection, final String xqueryContent) throws SAXException {
    final String query = IMPORT + xqueryContent;
    try {
        final XQueryService service = (XQueryService) collection.getService("XQueryService", "1.0");
        service.declareVariable("filename", "");
        service.declareVariable("count", "0");
        final CompiledExpression compiled = service.compile(query);
        for (int i = 0; i < count; i++) {
            generatedFiles[i] = Files.createTempFile(prefix, ".xml");
            service.declareVariable("filename", generatedFiles[i].getFileName().toString());
            service.declareVariable("count", new Integer(i));
            final ResourceSet result = service.execute(compiled);
            try (final Writer out = Files.newBufferedWriter(generatedFiles[i], StandardCharsets.UTF_8)) {
                final SAXSerializer sax = new SAXSerializer(out, outputProps);
                for (ResourceIterator iter = result.getIterator(); iter.hasMoreResources(); ) {
                    XMLResource r = (XMLResource) iter.nextResource();
                    r.getContentAsSAX(sax);
                }
            }
        }
    } catch (final XMLDBException | IOException e) {
        LOG.error(e.getMessage(), e);
        throw new SAXException(e.getMessage(), e);
    }
    return generatedFiles;
}
Also used : XQueryService(org.xmldb.api.modules.XQueryService) XMLDBException(org.xmldb.api.base.XMLDBException) ResourceSet(org.xmldb.api.base.ResourceSet) CompiledExpression(org.xmldb.api.base.CompiledExpression) XMLResource(org.xmldb.api.modules.XMLResource) SAXException(org.xml.sax.SAXException) SAXSerializer(org.exist.util.serializer.SAXSerializer) ResourceIterator(org.xmldb.api.base.ResourceIterator)

Example 3 with CompiledExpression

use of org.xmldb.api.base.CompiledExpression in project exist by eXist-db.

the class RemoteQueryTest method resourceSet.

@Test
public void resourceSet() throws XMLDBException {
    String query = "//SPEECH[SPEAKER = 'HAMLET']";
    XQueryService service = (XQueryService) testCollection.getService("XQueryService", "1.0");
    service.setProperty("highlight-matches", "none");
    CompiledExpression compiled = service.compile(query);
    ResourceSet result = service.execute(compiled);
    assertEquals(result.getSize(), 359);
    for (int i = 0; i < result.getSize(); i++) {
        XMLResource r = (XMLResource) result.getResource(i);
        Node node = r.getContentAsDOM().getFirstChild();
    }
}
Also used : XQueryService(org.xmldb.api.modules.XQueryService) Node(org.w3c.dom.Node) ResourceSet(org.xmldb.api.base.ResourceSet) CompiledExpression(org.xmldb.api.base.CompiledExpression) XMLResource(org.xmldb.api.modules.XMLResource) XmlRpcTest(org.exist.xmlrpc.XmlRpcTest) Test(org.junit.Test)

Example 4 with CompiledExpression

use of org.xmldb.api.base.CompiledExpression in project exist by eXist-db.

the class RemoteQueryTest method externalVar.

@Test
public void externalVar() throws XMLDBException {
    String query = XmlRpcTest.QUERY_MODULE_DATA;
    XQueryService service = (XQueryService) testCollection.getService("XQueryService", "1.0");
    service.setProperty("highlight-matches", "none");
    service.setNamespace("tm", "http://exist-db.org/test/module");
    service.setNamespace("tm-query", "http://exist-db.org/test/module/query");
    service.declareVariable("tm:imported-external-string", "imported-string-value");
    service.declareVariable("tm-query:local-external-string", "local-string-value");
    CompiledExpression compiled = service.compile(query);
    ResourceSet result = service.execute(compiled);
    assertEquals(result.getSize(), 2);
    for (int i = 0; i < result.getSize(); i++) {
        XMLResource r = (XMLResource) result.getResource(i);
    }
}
Also used : XQueryService(org.xmldb.api.modules.XQueryService) ResourceSet(org.xmldb.api.base.ResourceSet) CompiledExpression(org.xmldb.api.base.CompiledExpression) XMLResource(org.xmldb.api.modules.XMLResource) XmlRpcTest(org.exist.xmlrpc.XmlRpcTest) Test(org.junit.Test)

Aggregations

CompiledExpression (org.xmldb.api.base.CompiledExpression)4 ResourceSet (org.xmldb.api.base.ResourceSet)4 XQueryService (org.xmldb.api.modules.XQueryService)4 XMLResource (org.xmldb.api.modules.XMLResource)3 XmlRpcTest (org.exist.xmlrpc.XmlRpcTest)2 Test (org.junit.Test)2 SAXSerializer (org.exist.util.serializer.SAXSerializer)1 Node (org.w3c.dom.Node)1 SAXException (org.xml.sax.SAXException)1 Collection (org.xmldb.api.base.Collection)1 ResourceIterator (org.xmldb.api.base.ResourceIterator)1 XMLDBException (org.xmldb.api.base.XMLDBException)1