use of org.xmldb.api.base.Resource in project exist by eXist-db.
the class XMLDBXUpdateTask method execute.
@Override
public void execute() throws BuildException {
if (uri == null) {
throw (new BuildException("You have to specify an XMLDB collection URI"));
}
log("XUpdate command is: " + commands, 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 XUpdateQueryService service = (XUpdateQueryService) base.getService("XUpdateQueryService", "1.0");
if (resource != null) {
log("Updating resource: " + resource, Project.MSG_INFO);
final Resource res = base.getResource(resource);
if (res == null) {
final String msg = "Resource " + resource + " not found.";
if (failonerror) {
throw (new BuildException(msg));
} else {
log(msg, Project.MSG_ERR);
}
} else {
service.updateResource(resource, commands);
}
} else {
log("Updating collection: " + base.getName(), Project.MSG_INFO);
service.update(commands);
}
}
} catch (final XMLDBException e) {
final String msg = "XMLDB exception during XUpdate: " + e.getMessage();
if (failonerror) {
throw (new BuildException(msg, e));
} else {
log(msg, e, Project.MSG_ERR);
}
}
}
use of org.xmldb.api.base.Resource in project exist by eXist-db.
the class XMLDBExtractTask method extractResources.
/**
* Create directory 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 extractResources(final Collection base, final String path) throws XMLDBException, IOException {
final String[] resources = base.listResources();
if (resources != null) {
Path dir = destDir;
log("Extracting to directory " + destDir.toAbsolutePath().toString(), Project.MSG_DEBUG);
if (path != null) {
dir = destDir.resolve(path);
}
for (final String resource : resources) {
final Resource res = base.getResource(resource);
log("Extracting resource: " + res.getId(), Project.MSG_DEBUG);
if (Files.notExists(dir) && createdirectories) {
Files.createDirectories(dir);
}
if (Files.exists(dir)) {
writeResource(res, dir);
}
}
}
}
use of org.xmldb.api.base.Resource in project exist by eXist-db.
the class ChmodTask method execute.
/* (non-Javadoc)
* @see org.apache.tools.ant.Task#execute()
*/
public void execute() throws BuildException {
Resource res = null;
super.execute();
if (permissions == null) {
if (mode == null) {
throw (new BuildException("you have to specify permissions"));
} else {
permissions = mode;
}
}
try {
if (resource != null) {
res = base.getResource(resource);
}
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.Resource 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.Resource in project exist by eXist-db.
the class XMLDBCopy method evalWithCollection.
@Override
public Sequence evalWithCollection(final Collection collection, final Sequence[] args, final Sequence contextSequence) throws XPathException {
if (isCalledAs(FS_COPY_RESOURCE_NAME)) {
final XmldbURI destination = new AnyURIValue(args[2].itemAt(0).getStringValue()).toXmldbURI();
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 EXistCollectionManagementService service = (EXistCollectionManagementService) collection.getService("CollectionManagementService", "1.0");
final DBBroker.PreserveType preserve;
if (getArgumentCount() == 5) {
final boolean preserveArg = args[4].itemAt(0).toJavaObject(boolean.class);
if (preserveArg) {
preserve = DBBroker.PreserveType.PRESERVE;
} else {
preserve = DBBroker.PreserveType.DEFAULT;
}
} else {
preserve = DBBroker.PreserveType.DEFAULT;
}
final XmldbURI newName;
if (getArgumentCount() >= 4) {
if (!args[3].isEmpty()) {
newName = XmldbURI.create(args[3].itemAt(0).getStringValue());
} else {
newName = doc.lastSegment();
}
} else {
newName = null;
}
service.copyResource(doc, destination, newName, preserve.name());
if (isCalledAs(FS_COPY_RESOURCE_NAME)) {
return new StringValue(destination.append(newName).getRawCollectionPath());
} else {
return Sequence.EMPTY_SEQUENCE;
}
} catch (final XMLDBException e) {
logger.error("XMLDB exception caught: ", e);
throw new XPathException(this, "XMLDB exception caught: " + e.getMessage(), e);
}
} else {
final XmldbURI destination = new AnyURIValue(args[1].itemAt(0).getStringValue()).toXmldbURI();
try {
final EXistCollectionManagementService service = (EXistCollectionManagementService) collection.getService("CollectionManagementService", "1.0");
final DBBroker.PreserveType preserve;
if (getArgumentCount() == 3) {
final boolean preserveArg = args[2].itemAt(0).toJavaObject(boolean.class);
if (preserveArg) {
preserve = DBBroker.PreserveType.PRESERVE;
} else {
preserve = DBBroker.PreserveType.DEFAULT;
}
} else {
preserve = DBBroker.PreserveType.DEFAULT;
}
service.copy(XmldbURI.xmldbUriFor(collection.getName()), destination, null, preserve.name());
if (isCalledAs(FS_COPY_COLLECTION_NAME)) {
final XmldbURI targetName = XmldbURI.xmldbUriFor(collection.getName()).lastSegment();
return new StringValue(destination.append(targetName).getRawCollectionPath());
} else {
return Sequence.EMPTY_SEQUENCE;
}
} catch (final XMLDBException e) {
logger.error("Cannot copy collection: ", e);
throw new XPathException(this, "Cannot copy collection: " + e.getMessage(), e);
} catch (final URISyntaxException e) {
logger.error("URI exception: ", e);
throw new XPathException(this, "URI exception: " + e.getMessage(), e);
}
}
}
Aggregations