use of org.exist.xmldb.EXistCollectionManagementService in project exist by eXist-db.
the class MoveResourceRecoveryTest method xmldbStore.
private void xmldbStore() throws XMLDBException, URISyntaxException, IOException {
final org.xmldb.api.base.Collection root = DatabaseManager.getCollection(XmldbURI.LOCAL_DB, "admin", "");
final EXistCollectionManagementService mgr = (EXistCollectionManagementService) root.getService("CollectionManagementService", "1.0");
org.xmldb.api.base.Collection test = root.getChildCollection("test");
if (test == null) {
test = mgr.createCollection("test");
}
org.xmldb.api.base.Collection test2 = test.getChildCollection("test2");
if (test2 == null) {
test2 = mgr.createCollection("test2");
}
final String sample;
try (final InputStream is = SAMPLES.getRomeoAndJulietSample()) {
sample = InputStreamUtil.readString(is, UTF_8);
}
final Resource res = test2.createResource("test3.xml", "XMLResource");
res.setContent(sample);
test2.storeResource(res);
mgr.moveResource(XmldbURI.create(XmldbURI.ROOT_COLLECTION + "/test2/test3.xml"), TestConstants.TEST_COLLECTION_URI, XmldbURI.create("new_test3.xml"));
}
use of org.exist.xmldb.EXistCollectionManagementService in project exist by eXist-db.
the class XMLDBRename method evalWithCollection.
/* (non-Javadoc)
* @see org.exist.xquery.BasicFunction#eval(org.exist.xquery.value.Sequence[], org.exist.xquery.value.Sequence)
*/
public Sequence evalWithCollection(Collection collection, Sequence[] args, Sequence contextSequence) throws XPathException {
if (getSignature().getArgumentCount() == 3) {
final XmldbURI doc = new AnyURIValue(args[1].itemAt(0).getStringValue()).toXmldbURI();
try {
final Resource resource = collection.getResource(doc.toString());
if (resource == null) {
logger.error("Resource {} not found", doc);
throw new XPathException(this, "Resource " + doc + " not found");
}
final String newName = args[2].itemAt(0).getStringValue();
final EXistCollectionManagementService service = (EXistCollectionManagementService) collection.getService("CollectionManagementService", "1.0");
service.moveResource(doc, (XmldbURI) null, XmldbURI.xmldbUriFor(newName));
} catch (final XMLDBException e) {
logger.error(e.getMessage());
throw new XPathException(this, "XMLDB exception caught: " + e.getMessage(), e);
} catch (final URISyntaxException e) {
logger.error(e.getMessage());
throw new XPathException(this, "URI exception: " + e.getMessage(), e);
}
} else {
try {
final String newName = args[1].itemAt(0).getStringValue();
final EXistCollectionManagementService service = (EXistCollectionManagementService) collection.getService("CollectionManagementService", "1.0");
service.move(XmldbURI.xmldbUriFor(collection.getName()), null, XmldbURI.xmldbUriFor(newName));
} catch (final XMLDBException e) {
logger.error("Cannot rename collection: {}", e.getMessage());
throw new XPathException(this, "Cannot rename collection: " + e.getMessage(), e);
} catch (final URISyntaxException e) {
logger.error(e.getMessage());
throw new XPathException(this, "URI exception: " + e.getMessage(), e);
}
}
return Sequence.EMPTY_SEQUENCE;
}
Aggregations