Search in sources :

Example 1 with EXistXQueryService

use of org.exist.xmldb.EXistXQueryService in project exist by eXist-db.

the class XMLDBXQueryTask method execute.

@Override
public void execute() throws BuildException {
    if (uri == null) {
        throw new BuildException("you have to specify an XMLDB collection URI");
    }
    if (text != null) {
        final PropertyHelper helper = PropertyHelper.getPropertyHelper(getProject());
        query = helper.replaceProperties(null, text, null);
    }
    if (queryFile == null && query == null && queryUri == null) {
        throw new BuildException("you have to specify a query either as attribute, text, URI or in a file");
    }
    registerDatabase();
    try {
        log("Get base collection: " + uri, Project.MSG_DEBUG);
        final Collection base = DatabaseManager.getCollection(uri, user, password);
        if (base == null) {
            final String msg = "Collection " + uri + " could not be found.";
            if (failonerror) {
                throw new BuildException(msg);
            } else {
                log(msg, Project.MSG_ERR);
            }
        } else {
            final EXistXQueryService service = (EXistXQueryService) base.getService("XQueryService", "1.0");
            // set pretty-printing on
            service.setProperty(OutputKeys.INDENT, "yes");
            service.setProperty(OutputKeys.ENCODING, "UTF-8");
            for (final Variable var : variables) {
                System.out.println("Name: " + var.name);
                System.out.println("Value: " + var.value);
                service.declareVariable(var.name, var.value);
            }
            final Source source;
            if (queryUri != null) {
                log("XQuery url " + queryUri, Project.MSG_DEBUG);
                if (queryUri.startsWith(XmldbURI.XMLDB_URI_PREFIX)) {
                    final Resource resource = base.getResource(queryUri);
                    source = new BinarySource((byte[]) resource.getContent(), true);
                } else {
                    source = new URLSource(new URL(queryUri));
                }
            } else if (queryFile != null) {
                log("XQuery file " + queryFile.getAbsolutePath(), Project.MSG_DEBUG);
                source = new FileSource(queryFile.toPath(), true);
            } else {
                log("XQuery string: " + query, Project.MSG_DEBUG);
                source = new StringSource(query);
            }
            final ResourceSet results = service.execute(source);
            log("Found " + results.getSize() + " results", Project.MSG_INFO);
            if ((destDir != null) && (results != null)) {
                log("write results to directory " + destDir.getAbsolutePath(), Project.MSG_INFO);
                final ResourceIterator iter = results.getIterator();
                log("Writing results to directory " + destDir.getAbsolutePath(), Project.MSG_DEBUG);
                while (iter.hasMoreResources()) {
                    final XMLResource res = (XMLResource) iter.nextResource();
                    log("Writing resource " + res.getId(), Project.MSG_DEBUG);
                    writeResource(res, destDir);
                }
            } else if (outputproperty != null) {
                final ResourceIterator iter = results.getIterator();
                String result = null;
                while (iter.hasMoreResources()) {
                    final XMLResource res = (XMLResource) iter.nextResource();
                    result = res.getContent().toString();
                }
                getProject().setNewProperty(outputproperty, result);
            }
        }
    } catch (final XMLDBException e) {
        final String msg = "XMLDB exception caught while executing query: " + e.getMessage();
        if (failonerror) {
            throw new BuildException(msg, e);
        } else {
            log(msg, e, Project.MSG_ERR);
        }
    } catch (final IOException e) {
        final String msg = "XMLDB exception caught while writing destination file: " + e.getMessage();
        if (failonerror) {
            throw new BuildException(msg, e);
        } else {
            log(msg, e, Project.MSG_ERR);
        }
    }
}
Also used : XMLResource(org.xmldb.api.modules.XMLResource) PropertyHelper(org.apache.tools.ant.PropertyHelper) IOException(java.io.IOException) URL(java.net.URL) XMLResource(org.xmldb.api.modules.XMLResource) EXistXQueryService(org.exist.xmldb.EXistXQueryService) BuildException(org.apache.tools.ant.BuildException)

Example 2 with EXistXQueryService

use of org.exist.xmldb.EXistXQueryService in project exist by eXist-db.

the class InternalModuleTest method reusedModuleVariables.

/**
 * Similar to {@link #moduleVariables()} but
 * re-executes the query to ensure on subsequent
 * invocations, reusing the cached query (and query
 * context) do not cause problems.
 */
@Test
public void reusedModuleVariables() throws XMLDBException {
    final Source querySource = new StringSource(getModuleVariableQuery("org.exist.xquery.InternalModuleTest$TestModuleWithVariables"));
    final EXistXQueryService queryService = (EXistXQueryService) existServer.getRoot().getService("XQueryService", "1.0");
    moduleVariablesQuery(queryService, querySource, COUNTER.get());
    moduleVariablesQuery(queryService, querySource, COUNTER.get());
    moduleVariablesQuery(queryService, querySource, COUNTER.get());
}
Also used : EXistXQueryService(org.exist.xmldb.EXistXQueryService) StringSource(org.exist.source.StringSource) StringSource(org.exist.source.StringSource) Source(org.exist.source.Source) Test(org.junit.Test)

Example 3 with EXistXQueryService

use of org.exist.xmldb.EXistXQueryService in project exist by eXist-db.

the class InternalModuleTest method requestResponseSessionVariables_4_x_x_Api.

/**
 * The $request:request, $session:session, and
 * $response:response variables were removed in eXist-db 5.0.0.
 */
@Test
public void requestResponseSessionVariables_4_x_x_Api() throws XMLDBException {
    final Source querySource = new StringSource("document{" + EOL + "  <vars>" + EOL + "    <request>{empty($request:request)}</request>" + EOL + "    <session>{empty($session:session)}</session>" + EOL + "    <response>{empty($response:response)}</response>" + EOL + "  </vars>" + EOL + "}");
    final EXistXQueryService queryService = (EXistXQueryService) existServer.getRoot().getService("XQueryService", "1.0");
    try {
        requestResponseSessionVariablesQuery_4_x_X_Api(queryService, querySource);
        fail("Expected XQuery error XPST0008");
    } catch (final XMLDBException e) {
        assertTrue(e.getCause() instanceof XPathException);
        final XPathException xpe = (XPathException) e.getCause();
        // TODO(AR) should be XPST0008, eXist-db has a bug with the error code, see: https://github.com/eXist-db/exist/issues/2060
        assertEquals(ErrorCodes.XPDY0002, xpe.getErrorCode());
    }
}
Also used : EXistXQueryService(org.exist.xmldb.EXistXQueryService) XMLDBException(org.xmldb.api.base.XMLDBException) StringSource(org.exist.source.StringSource) StringSource(org.exist.source.StringSource) Source(org.exist.source.Source) Test(org.junit.Test)

Example 4 with EXistXQueryService

use of org.exist.xmldb.EXistXQueryService in project exist by eXist-db.

the class XPathQueryTest method atomization.

@Test
public void atomization() throws XMLDBException, IOException, SAXException {
    final String query = "declare namespace ex = \"http://example.org\";\n" + "declare function ex:elementName() as xs:QName {\n" + "   QName(\"http://test.org\", \"test:name\")\n" + "};\n" + "<test>{\n" + "   element {QName(\"http://test.org\", \"test:name\") }{},\n" + "   element {ex:elementName()} {}\n" + "}</test>";
    final EXistXQueryService service = (EXistXQueryService) getQueryService();
    service.setProperty(OutputKeys.INDENT, "no");
    final ResourceSet result = service.query(query);
    assertEquals(1, result.getSize());
    assertXMLEqual("<test><test:name xmlns:test=\"http://test.org\"/><test:name xmlns:test=\"http://test.org\"/></test>", result.getResource(0).getContent().toString());
}
Also used : EXistXQueryService(org.exist.xmldb.EXistXQueryService)

Example 5 with EXistXQueryService

use of org.exist.xmldb.EXistXQueryService in project exist by eXist-db.

the class XQDocTask method execute.

@Override
public void execute() throws BuildException {
    registerDatabase();
    try {
        int p = uri.indexOf(XmldbURI.ROOT_COLLECTION);
        if (p == Constants.STRING_NOT_FOUND)
            throw new BuildException("invalid uri: '" + uri + "'");
        String baseURI = uri.substring(0, p);
        String path;
        if (p == uri.length() - 3)
            path = "";
        else
            path = uri.substring(p + 3);
        Collection root = null;
        if (createCollection) {
            root = DatabaseManager.getCollection(baseURI + XmldbURI.ROOT_COLLECTION, user, password);
            root = mkcol(root, baseURI, XmldbURI.ROOT_COLLECTION, path);
        } else
            root = DatabaseManager.getCollection(uri, user, password);
        EXistXQueryService service = (EXistXQueryService) root.getService("XQueryService", "1.0");
        Source source = new StringSource(XQUERY);
        service.declareVariable("collection", root.getName());
        service.declareVariable("uri", "");
        if (moduleURI != null) {
            service.declareVariable("uri", moduleURI);
            service.declareVariable("data", "");
            service.execute(source);
        } else {
            for (FileSet fileSet : fileSets) {
                DirectoryScanner scanner = fileSet.getDirectoryScanner(getProject());
                scanner.scan();
                String[] files = scanner.getIncludedFiles();
                log("Found " + files.length + " files to upload.\n");
                Path baseDir = scanner.getBasedir().toPath();
                for (String s : files) {
                    Path file = baseDir.resolve(s);
                    log("Storing " + s + " ...\n");
                    byte[] data = read(file);
                    try {
                        service.declareVariable("name", FileUtils.fileName(file));
                        service.declareVariable("data", data);
                        service.execute(source);
                    } catch (XMLDBException e) {
                        String msg = "XMLDB exception caught: " + e.getMessage();
                        if (failonerror)
                            throw new BuildException(msg, e);
                        else
                            log(msg, e, Project.MSG_ERR);
                    }
                }
            }
        }
    } catch (XMLDBException e) {
        String msg = "XMLDB exception caught: " + e.getMessage();
        if (failonerror)
            throw new BuildException(msg, e);
        else
            log(msg, e, Project.MSG_ERR);
    }
}
Also used : Path(java.nio.file.Path) FileSet(org.apache.tools.ant.types.FileSet) XMLDBException(org.xmldb.api.base.XMLDBException) StringSource(org.exist.source.StringSource) Source(org.exist.source.Source) EXistXQueryService(org.exist.xmldb.EXistXQueryService) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) Collection(org.xmldb.api.base.Collection) BuildException(org.apache.tools.ant.BuildException) StringSource(org.exist.source.StringSource)

Aggregations

EXistXQueryService (org.exist.xmldb.EXistXQueryService)14 StringSource (org.exist.source.StringSource)6 Source (org.exist.source.Source)5 Test (org.junit.Test)4 BuildException (org.apache.tools.ant.BuildException)2 QName (org.exist.dom.QName)2 ResourceSet (org.xmldb.api.base.ResourceSet)2 XMLDBException (org.xmldb.api.base.XMLDBException)2 XMLResource (org.xmldb.api.modules.XMLResource)2 IOException (java.io.IOException)1 URL (java.net.URL)1 Path (java.nio.file.Path)1 DirectoryScanner (org.apache.tools.ant.DirectoryScanner)1 PropertyHelper (org.apache.tools.ant.PropertyHelper)1 FileSet (org.apache.tools.ant.types.FileSet)1 Sequence (org.exist.xquery.value.Sequence)1 Collection (org.xmldb.api.base.Collection)1 Resource (org.xmldb.api.base.Resource)1 XUpdateQueryService (org.xmldb.api.modules.XUpdateQueryService)1