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);
}
}
}
}
}
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);
}
}
}
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();
}
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);
}
}
}
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());
}
Aggregations