Search in sources :

Example 21 with Collection

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

the class XMLDBExtractTask method extractSubCollections.

/**
 * Extract multiple resources from a collection.
 *
 * @param base the collection
 * @param path the path
 *
 * @throws XMLDBException if a database error occurs
 * @throws IOException if an I/O error occurs
 */
private void extractSubCollections(final Collection base, final String path) throws XMLDBException, IOException {
    final String[] childCols = base.listChildCollections();
    if (childCols != null) {
        for (final String childCol : childCols) {
            final Collection col = base.getChildCollection(childCol);
            if (col != null) {
                log("Extracting collection: " + col.getName(), Project.MSG_DEBUG);
                Path dir = destDir;
                final String subdir;
                if (path != null) {
                    dir = destDir.resolve(path).resolve(childCol);
                    subdir = path + File.separator + childCol;
                } else {
                    subdir = childCol;
                }
                if (Files.notExists(dir) && createdirectories) {
                    Files.createDirectories(dir);
                }
                extractResources(col, subdir);
                if (subcollections) {
                    extractSubCollections(col, subdir);
                }
            }
        }
    }
}
Also used : Path(java.nio.file.Path) Collection(org.xmldb.api.base.Collection)

Example 22 with Collection

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

the class XMLDBXPathTask 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 (query == null) {
        throw new BuildException("you have to specify a query");
    }
    log("XPath is: " + query, org.apache.tools.ant.Project.MSG_DEBUG);
    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 XPathQueryService service = (XPathQueryService) base.getService("XPathQueryService", "1.0");
            // set pretty-printing on
            service.setProperty(OutputKeys.INDENT, "yes");
            service.setProperty(OutputKeys.ENCODING, "UTF-8");
            if (namespace != null) {
                log("Using namespace: " + namespace, Project.MSG_DEBUG);
                service.setNamespace("ns", namespace);
            }
            final ResourceSet results;
            if (resource != null) {
                log("Query resource: " + resource, Project.MSG_DEBUG);
                results = service.queryResource(resource, query);
            } else {
                log("Query collection", Project.MSG_DEBUG);
                results = service.query(query);
            }
            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) {
                if (count) {
                    getProject().setNewProperty(outputproperty, String.valueOf(results.getSize()));
                } else {
                    final ResourceIterator iter = results.getIterator();
                    final StringBuilder result = new StringBuilder();
                    while (iter.hasMoreResources()) {
                        final XMLResource res = (XMLResource) iter.nextResource();
                        result.append(res.getContent().toString());
                        result.append("\n");
                    }
                    getProject().setNewProperty(outputproperty, result.toString());
                }
            }
        }
    } 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 : XPathQueryService(org.xmldb.api.modules.XPathQueryService) Collection(org.xmldb.api.base.Collection) XMLDBException(org.xmldb.api.base.XMLDBException) PropertyHelper(org.apache.tools.ant.PropertyHelper) BuildException(org.apache.tools.ant.BuildException) ResourceSet(org.xmldb.api.base.ResourceSet) IOException(java.io.IOException) ResourceIterator(org.xmldb.api.base.ResourceIterator) XMLResource(org.xmldb.api.modules.XMLResource)

Example 23 with Collection

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

the class XmldbTaskTest method fileSetup.

@Before
public void fileSetup() throws XMLDBException {
    final Collection col = existEmbeddedServer.createCollection(existEmbeddedServer.getRoot(), TEST_COLLECTION_NAME);
    final Resource res = col.createResource(TEST_RESOURCE_NAME, XMLResource.RESOURCE_TYPE);
    res.setContent("<test>hello <subject>world</subject></test>");
    col.storeResource(res);
    final Resource binResource = col.createResource(BIN_TEST_RESOURCE_NAME, BinaryResource.RESOURCE_TYPE);
    binResource.setContent("blah blah");
    col.storeResource(binResource);
    final CollectionManagementService service = (CollectionManagementService) col.getService("CollectionManagementService", "1.0");
    final Collection otherCol = service.createCollection(OTHER_TEST_COLLECTION_NAME);
    final Resource otherRes = otherCol.createResource(OTHER_TEST_RESOURCE_NAME, XMLResource.RESOURCE_TYPE);
    otherRes.setContent("<test>other</test>");
    otherCol.storeResource(otherRes);
    otherCol.close();
    col.close();
}
Also used : CollectionManagementService(org.xmldb.api.modules.CollectionManagementService) BinaryResource(org.xmldb.api.modules.BinaryResource) XMLResource(org.xmldb.api.modules.XMLResource) Resource(org.xmldb.api.base.Resource) Collection(org.xmldb.api.base.Collection) Before(org.junit.Before)

Example 24 with Collection

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

the class AbstractXMLDBTask method setPermissions.

protected final void setPermissions(final Resource res) throws BuildException {
    Collection base = null;
    UserManagementService service = null;
    if (uri == null) {
        throw (new BuildException("you have to specify an XMLDB collection URI"));
    }
    try {
        log("Get base collection: " + uri, Project.MSG_DEBUG);
        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 {
            service = (UserManagementService) base.getService("UserManagementService", "1.0");
            setPermissions(res, service);
        }
    } catch (final XMLDBException e) {
        final String msg = "XMLDB exception caught: " + e.getMessage();
        if (failonerror) {
            throw (new BuildException(msg, e));
        } else {
            log(msg, e, Project.MSG_ERR);
        }
    }
}
Also used : Collection(org.xmldb.api.base.Collection) XMLDBException(org.xmldb.api.base.XMLDBException) UserManagementService(org.exist.xmldb.UserManagementService) BuildException(org.apache.tools.ant.BuildException)

Example 25 with Collection

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

the class FileTaskTest method chmod.

@Test
public void chmod() throws XMLDBException {
    final Project project = buildFileRule.getProject();
    project.setProperty(PROP_ANT_TEST_DATA_TEST_COLLECTION, TEST_COLLECTION_NAME);
    project.setProperty(PROP_ANT_TEST_DATA_TEST_RESOURCE, TEST_RESOURCE_NAME);
    buildFileRule.executeTarget("chmod");
    final String result = project.getProperty(PROP_ANT_TEST_DATA_RESULT);
    assertThat(result, containsString(TEST_RESOURCE_NAME));
    final Collection col = existEmbeddedServer.getRoot().getChildCollection(TEST_COLLECTION_NAME);
    final EXistResource res = (EXistResource) col.getResource(TEST_RESOURCE_NAME);
    assertEquals("---rwxrwx", res.getPermissions().toString());
}
Also used : EXistResource(org.exist.xmldb.EXistResource) Project(org.apache.tools.ant.Project) Collection(org.xmldb.api.base.Collection) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Aggregations

Collection (org.xmldb.api.base.Collection)345 XMLResource (org.xmldb.api.modules.XMLResource)140 Test (org.junit.Test)115 Resource (org.xmldb.api.base.Resource)111 UserManagementService (org.exist.xmldb.UserManagementService)91 CollectionManagementService (org.xmldb.api.modules.CollectionManagementService)85 BinaryResource (org.xmldb.api.modules.BinaryResource)80 XMLDBException (org.xmldb.api.base.XMLDBException)68 ResourceSet (org.xmldb.api.base.ResourceSet)55 EXistCollectionManagementService (org.exist.xmldb.EXistCollectionManagementService)48 XPathQueryService (org.xmldb.api.modules.XPathQueryService)31 EXistResource (org.exist.xmldb.EXistResource)25 EXistXPathQueryService (org.exist.xmldb.EXistXPathQueryService)20 Before (org.junit.Before)20 URISyntaxException (java.net.URISyntaxException)18 Path (java.nio.file.Path)18 InputStream (java.io.InputStream)17 BuildException (org.apache.tools.ant.BuildException)14 XmldbURI (org.exist.xmldb.XmldbURI)13 Account (org.exist.security.Account)10