Search in sources :

Example 21 with XMLDBException

use of org.xmldb.api.base.XMLDBException 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 22 with XMLDBException

use of org.xmldb.api.base.XMLDBException 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 23 with XMLDBException

use of org.xmldb.api.base.XMLDBException 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 24 with XMLDBException

use of org.xmldb.api.base.XMLDBException 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 25 with XMLDBException

use of org.xmldb.api.base.XMLDBException 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);
        }
    }
}
Also used : XPathQueryService(org.xmldb.api.modules.XPathQueryService) Collection(org.xmldb.api.base.Collection) XMLDBException(org.xmldb.api.base.XMLDBException) PropertyHelper(org.apache.tools.ant.PropertyHelper) BuildException(org.apache.tools.ant.BuildException) ResourceSet(org.xmldb.api.base.ResourceSet) IOException(java.io.IOException) ResourceIterator(org.xmldb.api.base.ResourceIterator) XMLResource(org.xmldb.api.modules.XMLResource)

Aggregations

XMLDBException (org.xmldb.api.base.XMLDBException)174 Collection (org.xmldb.api.base.Collection)66 Resource (org.xmldb.api.base.Resource)34 URISyntaxException (java.net.URISyntaxException)30 XMLResource (org.xmldb.api.modules.XMLResource)30 ResourceSet (org.xmldb.api.base.ResourceSet)23 BuildException (org.apache.tools.ant.BuildException)21 IOException (java.io.IOException)19 XPathException (org.exist.xquery.XPathException)18 PermissionDeniedException (org.exist.security.PermissionDeniedException)16 SAXException (org.xml.sax.SAXException)16 EXistException (org.exist.EXistException)15 UserManagementService (org.exist.xmldb.UserManagementService)15 XPathQueryService (org.xmldb.api.modules.XPathQueryService)13 BinaryResource (org.xmldb.api.modules.BinaryResource)12 ArrayList (java.util.ArrayList)11 Account (org.exist.security.Account)11 EXistResource (org.exist.xmldb.EXistResource)10 EXistXPathQueryService (org.exist.xmldb.EXistXPathQueryService)10 Properties (java.util.Properties)9