Search in sources :

Example 1 with Resource

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

the class ClientFrame method exportAction.

private void exportAction(final ActionEvent ev) {
    if (fileman.getSelectedRowCount() == 0) {
        return;
    }
    final int[] rows = fileman.getSelectedRows();
    for (final int row : rows) {
        final ResourceDescriptor desc = resources.getRow(fileman.convertRowIndexToModel(row));
        if (desc.isCollection()) {
            continue;
        }
        final JFileChooser chooser = new JFileChooser(preferences.get("directory.last", System.getProperty("user.dir")));
        chooser.setMultiSelectionEnabled(false);
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        chooser.setSelectedFile(Paths.get(desc.getName().getCollectionPath()).toFile());
        if (chooser.showDialog(this, "Select file for export") == JFileChooser.APPROVE_OPTION) {
            preferences.put("directory.last", chooser.getCurrentDirectory().getAbsolutePath());
            final Path file = chooser.getSelectedFile().toPath();
            if (Files.exists(file) && JOptionPane.showConfirmDialog(this, "File exists. Overwrite?", "Overwrite?", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
                return;
            }
            final Resource resource;
            final SAXSerializer contentSerializer;
            try {
                final Collection collection = client.getCollection();
                resource = collection.getResource(desc.getName().toString());
                if (resource instanceof ExtendedResource) {
                    try (final OutputStream os = new BufferedOutputStream(Files.newOutputStream(file))) {
                        ((ExtendedResource) resource).getContentIntoAStream(os);
                    }
                } else {
                    contentSerializer = (SAXSerializer) SerializerPool.getInstance().borrowObject(SAXSerializer.class);
                    try (final Writer writer = Files.newBufferedWriter(file, UTF_8)) {
                        // write resource to contentSerializer
                        contentSerializer.setOutput(writer, properties);
                        ((EXistResource) resource).setLexicalHandler(contentSerializer);
                        ((XMLResource) resource).getContentAsSAX(contentSerializer);
                    } finally {
                        SerializerPool.getInstance().returnObject(contentSerializer);
                    }
                }
            // TODO finally close os
            } catch (final Exception e) {
                System.err.println("An exception occurred" + e.getMessage());
                e.printStackTrace();
            }
        }
    }
}
Also used : Path(java.nio.file.Path) XMLResource(org.xmldb.api.modules.XMLResource) Resource(org.xmldb.api.base.Resource) XMLResource(org.xmldb.api.modules.XMLResource) URISyntaxException(java.net.URISyntaxException) XMLDBException(org.xmldb.api.base.XMLDBException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) BadLocationException(javax.swing.text.BadLocationException) SAXException(org.xml.sax.SAXException) Collection(org.xmldb.api.base.Collection) SAXSerializer(org.exist.util.serializer.SAXSerializer)

Example 2 with Resource

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

the class ChownTask method execute.

/* (non-Javadoc)
     * @see org.apache.tools.ant.Task#execute()
     */
public void execute() throws BuildException {
    super.execute();
    if ((name == null) || (group == null)) {
        throw (new BuildException("Must specify user and group"));
    }
    try {
        final Account usr = service.getAccount(name);
        if (resource != null) {
            final Resource res = base.getResource(resource);
            service.chown(res, usr, group);
        } else {
            service.chown(usr, group);
        }
    } 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 : Account(org.exist.security.Account) Resource(org.xmldb.api.base.Resource) XMLDBException(org.xmldb.api.base.XMLDBException) BuildException(org.apache.tools.ant.BuildException)

Example 3 with Resource

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

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

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

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