Search in sources :

Example 31 with NotDirectoryException

use of java.nio.file.NotDirectoryException in project sldeditor by robward-scisys.

the class FileTreeNode method populateDirectories.

/**
 * Populate directories. For display purposes, we return our own name public String toString() {
 * return name; } If we are a directory, scan our contents and populate with children. In
 * addition, populate those children if the "descend" flag is true. We only descend once, to
 * avoid recursing the whole subtree.
 *
 * @param descend the descend
 * @return true, if successful
 */
public boolean populateDirectories(boolean descend) {
    boolean addedNodes = false;
    if (!isRoot || (isRoot && descend)) {
        // Do this only once
        if (!populated) {
            if (interim) {
                // We have had a quick look here before:
                // remove the dummy node that we added last time
                removeAllChildren();
                interim = false;
            }
            // Get list of contents
            List<Path> names = new ArrayList<>();
            DirectoryStream<Path> stream = null;
            try {
                Path pathPath = Paths.get(path);
                stream = Files.newDirectoryStream(pathPath);
                if (stream != null) {
                    for (Path localPath : stream) {
                        names.add(localPath.getFileName());
                    }
                }
            } catch (AccessDeniedException e) {
            // Access was denied
            } catch (NotDirectoryException e) {
            // Ignore
            } catch (IOException e) {
                ConsoleManager.getInstance().exception(this, e);
            } finally {
                try {
                    if (stream != null) {
                        stream.close();
                    }
                } catch (IOException e) {
                    ConsoleManager.getInstance().exception(this, e);
                }
            }
            // Process the directories
            for (Path filename : names) {
                Path d = Paths.get(path, filename.toString());
                try {
                    if (d.toFile().isDirectory()) {
                        addFolder(descend, filename.toString());
                        addedNodes = true;
                        if (!descend) {
                            // Only add one node if not descending
                            break;
                        }
                    } else if (d.toFile().isFile()) {
                        if (validFile(filename.toString())) {
                            addFile(filename.toString());
                        }
                    }
                } catch (Exception e) {
                // Ignore phantoms or access problems
                }
            }
            // so that we look again in the future if we need to
            if (descend || !addedNodes) {
                populated = true;
                if (isDir() && !fileWatcherSet) {
                    Path pathPath = Paths.get(path);
                    FileSystemWatcher.getInstance().addWatch(this, pathPath);
                    fileWatcherSet = true;
                }
            } else {
                // Just set interim state
                interim = true;
            }
        }
    }
    return addedNodes;
}
Also used : Path(java.nio.file.Path) AccessDeniedException(java.nio.file.AccessDeniedException) NotDirectoryException(java.nio.file.NotDirectoryException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) NotDirectoryException(java.nio.file.NotDirectoryException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) AccessDeniedException(java.nio.file.AccessDeniedException)

Aggregations

NotDirectoryException (java.nio.file.NotDirectoryException)31 Path (java.nio.file.Path)17 NoSuchFileException (java.nio.file.NoSuchFileException)16 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)7 Test (org.junit.Test)7 IOException (java.io.IOException)6 AccessDeniedException (java.nio.file.AccessDeniedException)6 DirectoryNotEmptyException (java.nio.file.DirectoryNotEmptyException)6 FileNotFoundException (java.io.FileNotFoundException)5 FileSystemException (java.nio.file.FileSystemException)5 MCRFilesystemNode (org.mycore.datamodel.ifs.MCRFilesystemNode)5 AtomicMoveNotSupportedException (java.nio.file.AtomicMoveNotSupportedException)4 FileSystemLoopException (java.nio.file.FileSystemLoopException)4 MCRDirectory (org.mycore.datamodel.ifs.MCRDirectory)4 MCRPath (org.mycore.datamodel.niofs.MCRPath)4 EOFException (java.io.EOFException)3 HashSet (java.util.HashSet)3 CorruptIndexException (org.apache.lucene.index.CorruptIndexException)3 IndexFormatTooNewException (org.apache.lucene.index.IndexFormatTooNewException)3 IndexFormatTooOldException (org.apache.lucene.index.IndexFormatTooOldException)3