Search in sources :

Example 6 with Resource

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);
        }
    }
}
Also used : XUpdateQueryService(org.xmldb.api.modules.XUpdateQueryService) Resource(org.xmldb.api.base.Resource) Collection(org.xmldb.api.base.Collection) XMLDBException(org.xmldb.api.base.XMLDBException) BuildException(org.apache.tools.ant.BuildException)

Example 7 with Resource

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);
            }
        }
    }
}
Also used : Path(java.nio.file.Path) ExtendedResource(org.exist.xmldb.ExtendedResource) XMLResource(org.xmldb.api.modules.XMLResource) Resource(org.xmldb.api.base.Resource)

Example 8 with Resource

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);
        }
    }
}
Also used : Resource(org.xmldb.api.base.Resource) XMLDBException(org.xmldb.api.base.XMLDBException) BuildException(org.apache.tools.ant.BuildException)

Example 9 with Resource

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();
}
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 10 with Resource

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);
        }
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) XPathException(org.exist.xquery.XPathException) EXistCollectionManagementService(org.exist.xmldb.EXistCollectionManagementService) Resource(org.xmldb.api.base.Resource) XMLDBException(org.xmldb.api.base.XMLDBException) URISyntaxException(java.net.URISyntaxException) XmldbURI(org.exist.xmldb.XmldbURI)

Aggregations

Resource (org.xmldb.api.base.Resource)173 XMLResource (org.xmldb.api.modules.XMLResource)126 Collection (org.xmldb.api.base.Collection)111 BinaryResource (org.xmldb.api.modules.BinaryResource)86 Test (org.junit.Test)77 UserManagementService (org.exist.xmldb.UserManagementService)52 ResourceSet (org.xmldb.api.base.ResourceSet)46 XMLDBException (org.xmldb.api.base.XMLDBException)38 EXistCollectionManagementService (org.exist.xmldb.EXistCollectionManagementService)32 EXistResource (org.exist.xmldb.EXistResource)27 CollectionManagementService (org.xmldb.api.modules.CollectionManagementService)25 XPathQueryService (org.xmldb.api.modules.XPathQueryService)18 EXistXPathQueryService (org.exist.xmldb.EXistXPathQueryService)16 Path (java.nio.file.Path)11 Database (org.xmldb.api.base.Database)11 XPathException (org.exist.xquery.XPathException)10 InputStream (java.io.InputStream)9 Source (javax.xml.transform.Source)9 BuildException (org.apache.tools.ant.BuildException)9 Diff (org.xmlunit.diff.Diff)9