Search in sources :

Example 16 with Collection

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

the class XMLDBCreateTask method mkcol.

private Collection mkcol(final Collection root, final String base, /*String path,*/
final String relPath) throws XMLDBException, URISyntaxException {
    CollectionManagementService mgtService;
    Collection current = root;
    Collection c;
    XmldbURI baseUri = XmldbURI.xmldbUriFor(base);
    final XmldbURI collPath = XmldbURI.xmldbUriFor(relPath);
    log("BASEURI=" + baseUri, Project.MSG_DEBUG);
    log("RELPATH=" + relPath, Project.MSG_DEBUG);
    // log("PATH=" + path, Project.MSG_DEBUG);
    final XmldbURI[] segments = collPath.getPathSegments();
    for (final XmldbURI segment : segments) {
        baseUri = baseUri.append(segment);
        log("Get collection " + baseUri, Project.MSG_DEBUG);
        c = DatabaseManager.getCollection(baseUri.toString(), user, password);
        if (c == null) {
            log("Create collection management service for collection " + current.getName(), Project.MSG_DEBUG);
            mgtService = (CollectionManagementService) current.getService("CollectionManagementService", "1.0");
            log("Create child collection " + segment);
            current = mgtService.createCollection(segment.toString());
            log("Created collection " + current.getName() + '.');
        } else {
            current = c;
        }
    }
    return (current);
}
Also used : CollectionManagementService(org.xmldb.api.modules.CollectionManagementService) Collection(org.xmldb.api.base.Collection) XmldbURI(org.exist.xmldb.XmldbURI)

Example 17 with Collection

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

the class XMLDBExistTask method eval.

@Override
public boolean eval() throws BuildException {
    boolean exist = false;
    if (uri == null) {
        throw (new BuildException("You have to specify an XMLDB collection URI"));
    }
    registerDatabase();
    try {
        log("Checking collection: " + uri, Project.MSG_INFO);
        final Collection base = DatabaseManager.getCollection(uri, user, password);
        if (base != null) {
            log("Base collection found", Project.MSG_DEBUG);
            exist = true;
        }
        if ((base != null) && (resource != null)) {
            log("Checking resource: " + resource, Project.MSG_INFO);
            final Resource res = base.getResource(resource);
            if (res == null) {
                log("Resource not found", Project.MSG_DEBUG);
                exist = false;
            }
        }
    } catch (final XMLDBException e) {
        // ignore is false already
        log("Resource or collection cannot be retrieved", Project.MSG_DEBUG);
        exist = false;
    }
    return (exist);
}
Also used : 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 18 with Collection

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

the class XMLDBRemoveTask method execute.

@Override
public void execute() throws BuildException {
    if (uri == null) {
        throw (new BuildException("You have to specify an XMLDB collection URI"));
    }
    if ((resource == null) && (collection == null)) {
        throw (new BuildException("Missing parameter: either resource or collection should be specified"));
    }
    registerDatabase();
    try {
        log("Get base collection: " + uri, Project.MSG_DEBUG);
        final Collection base = DatabaseManager.getCollection(uri, user, password);
        if (base == null) {
            throw (new BuildException("Collection " + uri + " could not be found."));
        }
        if (resource != null) {
            log("Removing 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 {
                base.removeResource(res);
            }
        } else {
            log("Removing collection: " + collection, Project.MSG_INFO);
            final CollectionManagementService service = (CollectionManagementService) base.getService("CollectionManagementService", "1.0");
            service.removeCollection(collection);
        }
    } catch (final XMLDBException e) {
        final String msg = "XMLDB exception during remove: " + e.getMessage();
        if (failonerror) {
            throw (new BuildException(msg, e));
        } else {
            log(msg, e, Project.MSG_ERR);
        }
    }
}
Also used : CollectionManagementService(org.xmldb.api.modules.CollectionManagementService) 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 19 with Collection

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

the class XMLDBStoreTask method execute.

@Override
public void execute() throws BuildException {
    if (uri == null) {
        throw new BuildException("you have to specify an XMLDB collection URI");
    }
    if ((fileSetList == null) && (srcFile == null)) {
        throw new BuildException("no file set specified");
    }
    registerDatabase();
    int p = uri.indexOf(XmldbURI.ROOT_COLLECTION);
    if (p == Constants.STRING_NOT_FOUND) {
        throw new BuildException("invalid uri: '" + uri + "'");
    }
    final String baseURI = uri.substring(0, p);
    final String path;
    if (p == (uri.length() - 3)) {
        path = "";
    } else {
        path = uri.substring(p + 3);
    }
    Collection root = null;
    try {
        if (createCollection) {
            root = DatabaseManager.getCollection(baseURI + XmldbURI.ROOT_COLLECTION, user, password);
            root = mkcol(root, baseURI, XmldbURI.ROOT_COLLECTION, path);
        } else {
            root = DatabaseManager.getCollection(uri, user, password);
        }
    } 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);
            return;
        }
    }
    if (root == null) {
        final String msg = "Collection " + uri + " could not be found.";
        if (failonerror) {
            throw new BuildException(msg);
        } else {
            log(msg, Project.MSG_ERR);
        }
    } else {
        Resource res;
        Collection col = root;
        String relDir;
        String prevDir = null;
        if (srcFile != null) {
            log("Storing " + srcFile.getName());
            MimeType mime = getMimeTable().getContentTypeFor(srcFile.getName());
            final String baseMimeType;
            if (forceMimeType != null) {
                baseMimeType = forceMimeType;
            } else if (mime != null) {
                baseMimeType = mime.getName();
            } else {
                baseMimeType = defaultMimeType;
            }
            if (type != null) {
                if ("xml".equals(type)) {
                    mime = (baseMimeType != null) ? (new MimeType(baseMimeType, MimeType.XML)) : MimeType.XML_TYPE;
                } else if ("binary".equals(type)) {
                    mime = (baseMimeType != null) ? (new MimeType(baseMimeType, MimeType.BINARY)) : MimeType.BINARY_TYPE;
                }
            }
            // single file
            if (mime == null) {
                final String msg = "Cannot guess mime-type kind for " + srcFile.getName() + ". Treating it as a binary.";
                log(msg, Project.MSG_ERR);
                mime = (baseMimeType != null) ? (new MimeType(baseMimeType, MimeType.BINARY)) : MimeType.BINARY_TYPE;
            }
            final String resourceType = mime.isXMLType() ? XMLResource.RESOURCE_TYPE : BinaryResource.RESOURCE_TYPE;
            if (targetFile == null) {
                targetFile = srcFile.getName();
            }
            try {
                log("Creating resource " + targetFile + " in collection " + col.getName() + " of type " + resourceType + " with mime-type: " + mime.getName(), Project.MSG_DEBUG);
                res = col.createResource(targetFile, resourceType);
                if (srcFile.length() == 0) {
                // note: solves bug id 2429889 when this task hits empty files
                } else {
                    res.setContent(srcFile);
                    ((EXistResource) res).setMimeType(mime.getName());
                    col.storeResource(res);
                }
                if (permissions != null) {
                    setPermissions(res);
                }
            } 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);
                }
            }
        } else {
            for (final FileSet fileSet : fileSetList) {
                log("Storing fileset", Project.MSG_DEBUG);
                // using fileset
                final DirectoryScanner scanner = fileSet.getDirectoryScanner(getProject());
                scanner.scan();
                final String[] includedFiles = scanner.getIncludedFiles();
                final String[] includedDirs = scanner.getIncludedDirectories();
                log("Found " + includedDirs.length + " directories and " + includedFiles.length + " files.\n");
                final File baseDir = scanner.getBasedir();
                if (includeEmptyDirs && createSubcollections) {
                    for (final String included : includedDirs) {
                        try {
                            log("Creating " + included + " ...\n");
                            // TODO : use dedicated function in XmldbURI
                            // check whether the relative file path contains file seps
                            p = included.lastIndexOf(File.separatorChar);
                            if (p != Constants.STRING_NOT_FOUND) {
                                relDir = included.substring(0, p);
                                // It's necessary to do this translation on Windows, and possibly MacOS:
                                relDir = relDir.replace(File.separatorChar, '/');
                                if (createSubcollections && ((prevDir == null) || (!relDir.equals(prevDir)))) {
                                    // TODO : use dedicated function in XmldbURI
                                    col = mkcol(root, baseURI, XmldbURI.ROOT_COLLECTION + path, relDir);
                                    prevDir = relDir;
                                }
                            } else {
                                col = mkcol(root, baseURI, XmldbURI.ROOT_COLLECTION + path, included);
                            }
                        } 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);
                            }
                        }
                    }
                }
                for (final String included : includedFiles) {
                    try {
                        final File file = new File(baseDir, included);
                        log("Storing " + included + " ...\n");
                        // TODO : use dedicated function in XmldbURI
                        // check whether the relative file path contains file seps
                        p = included.lastIndexOf(File.separatorChar);
                        if (p != Constants.STRING_NOT_FOUND) {
                            relDir = included.substring(0, p);
                            // It's necessary to do this translation on Windows, and possibly MacOS:
                            relDir = relDir.replace(File.separatorChar, '/');
                            if (createSubcollections && ((prevDir == null) || (!relDir.equals(prevDir)))) {
                                // TODO : use dedicated function in XmldbURI
                                col = mkcol(root, baseURI, XmldbURI.ROOT_COLLECTION + path, relDir);
                                prevDir = relDir;
                            }
                        } else {
                            // No file separator found in resource name, reset col to the root collection
                            col = root;
                        }
                        MimeType currentMime = getMimeTable().getContentTypeFor(file.getName());
                        final String currentBaseMimeType;
                        if (forceMimeType != null) {
                            currentBaseMimeType = forceMimeType;
                        } else if (currentMime != null) {
                            currentBaseMimeType = currentMime.getName();
                        } else {
                            currentBaseMimeType = defaultMimeType;
                        }
                        if (type != null) {
                            if ("xml".equals(type)) {
                                currentMime = (currentBaseMimeType != null) ? (new MimeType(currentBaseMimeType, MimeType.XML)) : MimeType.XML_TYPE;
                            } else if ("binary".equals(type)) {
                                currentMime = (currentBaseMimeType != null) ? (new MimeType(currentBaseMimeType, MimeType.BINARY)) : MimeType.BINARY_TYPE;
                            }
                        }
                        if (currentMime == null) {
                            final String msg = "Cannot find mime-type kind for " + file.getName() + ". Treating it as a binary.";
                            log(msg, Project.MSG_ERR);
                            currentMime = (currentBaseMimeType != null) ? (new MimeType(currentBaseMimeType, MimeType.BINARY)) : MimeType.BINARY_TYPE;
                        }
                        final String resourceType = currentMime.isXMLType() ? XMLResource.RESOURCE_TYPE : BinaryResource.RESOURCE_TYPE;
                        log("Creating resource " + file.getName() + " in collection " + col.getName() + " of type " + resourceType + " with mime-type: " + currentMime.getName(), Project.MSG_DEBUG);
                        res = col.createResource(file.getName(), resourceType);
                        res.setContent(file);
                        ((EXistResource) res).setMimeType(currentMime.getName());
                        col.storeResource(res);
                        if (permissions != null) {
                            setPermissions(res);
                        }
                    } 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 : EXistResource(org.exist.xmldb.EXistResource) FileSet(org.apache.tools.ant.types.FileSet) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) EXistResource(org.exist.xmldb.EXistResource) BinaryResource(org.xmldb.api.modules.BinaryResource) XMLResource(org.xmldb.api.modules.XMLResource) Resource(org.xmldb.api.base.Resource) Collection(org.xmldb.api.base.Collection) XMLDBException(org.xmldb.api.base.XMLDBException) BuildException(org.apache.tools.ant.BuildException) File(java.io.File) MimeType(org.exist.util.MimeType)

Example 20 with Collection

use of org.xmldb.api.base.Collection 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)

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