Example usage for org.apache.commons.vfs2 FileObject getChildren

List of usage examples for org.apache.commons.vfs2 FileObject getChildren

Introduction

In this page you can find the example usage for org.apache.commons.vfs2 FileObject getChildren.

Prototype

FileObject[] getChildren() throws FileSystemException;

Source Link

Document

Lists the children of this file.

Usage

From source file:org.mycore.datamodel.ifs2.MCRNode.java

/**
 * Returns the number of child nodes of this node.
 * // w w  w  .j  a v  a 2s. co  m
 * @return the number of child nodes of this node.
 */
public int getNumChildren() throws IOException {
    FileObject father = getFather();
    if (father == null) {
        return 0;
    } else {
        return father.getChildren().length;
    }
}

From source file:org.mycore.datamodel.ifs2.MCRNode.java

/**
 * Returns the children of this node. Directories and container files like
 * zip or tar may have child nodes.// ww w. jav  a  2s  . co m
 * 
 * @return a List of child nodes, which may be empty, in undefined order
 */
public List<MCRNode> getChildren() throws IOException {
    List<MCRNode> children = new ArrayList<MCRNode>();
    FileObject father = getFather();
    if (father != null) {
        FileObject[] childFos = father.getChildren();
        for (FileObject childFo : childFos) {
            String name = childFo.getName().getBaseName();
            MCRNode child = getChild(name);
            if (child != null) {
                children.add(child);
            }
        }
    }
    return children;
}

From source file:org.mycore.datamodel.ifs2.MCRStore.java

/**
 * Lists all IDs currently used in the store, in ascending or descending
 * order//from w  w  w  .ja  v  a  2  s .  com
 * 
 * @see #ASCENDING
 * @see #DESCENDING
 * 
 * @param order
 *            the order in which IDs should be returned.
 * @return all IDs currently used in the store
 */
public Iterator<Integer> listIDs(final boolean order) {
    return new Iterator<Integer>() {
        /**
         * List of files or directories in store not yet handled
         */
        List<FileObject> files = new ArrayList<FileObject>();

        /**
         * The next ID to return, when 0, all IDs have been returned
         */
        int nextID;

        /**
         * The last ID that was returned
         */
        int lastID;

        /**
         * The order in which the IDs should be returned, ascending or
         * descending
         */
        boolean order;

        @Override
        public boolean hasNext() {
            return nextID > 0;
        }

        @Override
        public Integer next() {
            if (nextID < 1) {
                throw new NoSuchElementException();
            }

            lastID = nextID;
            nextID = findNextID();
            return lastID;
        }

        @Override
        public void remove() {
            if (lastID == 0) {
                throw new IllegalStateException();
            }
            try {
                MCRStore.this.delete(lastID);
            } catch (final Exception ex) {
                throw new MCRException("Could not delete " + MCRStore.this.getID() + " " + lastID, ex);
            }
            lastID = 0;
        }

        /**
         * Initializes the enumeration and searches for the first ID to
         * return
         * 
         * @param order
         *            the return order, ascending or descending
         */
        Iterator<Integer> init(final boolean order) {
            this.order = order;
            try {
                addChildren(baseDirectory);
            } catch (final FileSystemException e) {
                e.printStackTrace();
            }
            nextID = findNextID();
            return this;
        }

        /**
         * Adds children of the given directory to the list of files to
         * handle next. Depending on the return sort order, ascending or
         * descending file name order is used.
         * 
         * @param dir
         *            the directory thats children should be added
         * @throws FileSystemException 
         */
        private void addChildren(final FileObject dir) throws FileSystemException {
            if (dir.getType() == FileType.FOLDER) {
                final FileObject[] children = dir.getChildren();
                Arrays.sort(children, new MCRFileObjectComparator());

                for (int i = 0; i < children.length; i++) {
                    files.add(order ? i : 0, children[i]);
                }
            }
        }

        /**
         * Finds the next ID used in the store.
         * 
         * @return the next ID, or 0 if there is no other ID any more
         */
        private int findNextID() {
            if (files.isEmpty()) {
                return 0;
            }

            final FileObject first = files.remove(0);
            // checks basename length against prefix (projectId_typeId), file suffix (.xml) and configured id length
            // if they match it should be a parseable id
            if (first.getName().getBaseName().length() == idLength + prefix.length() + suffix.length()) {
                return MCRStore.this.slot2id(first.getName().getBaseName());
            }

            try {
                addChildren(first);
            } catch (final FileSystemException e) {
                e.printStackTrace();
            }
            return findNextID();
        }
    }.init(order);
}

From source file:org.mycore.datamodel.ifs2.MCRStore.java

/**
 * Deletes the data stored in the given file object from the store
 * /*from   w  ww  .  j ava  2 s  . co m*/
 * @param fo
 *            the file object to be deleted
 */
void delete(FileObject fo) throws IOException {
    FileObject parent = fo.getParent();
    fo.delete(Selectors.SELECT_ALL);

    while (!parent.equals(baseDirectory)) {
        final FileObject[] children = parent.getChildren();
        if (children.length > 0) {
            break;
        }
        fo = parent;
        parent = fo.getParent();
        fo.delete();
    }
}

From source file:org.mycore.datamodel.ifs2.MCRStore.java

/**
 * Recursively searches for the highest ID, which is the greatest slot file
 * name currently used in the store.//ww  w. j  a  v a  2  s  . com
 * 
 * @param dir
 *            the directory to search
 * @param depth
 *            the subdirectory depth level of the dir
 * @return the highest slot file name / ID currently stored
 */
private String findMaxID(final FileObject dir, final int depth) throws FileSystemException {
    final FileObject[] children = dir.getChildren();

    if (children.length == 0) {
        return null;
    }

    Arrays.sort(children, new MCRFileObjectComparator());

    if (depth == slotLength.length) {
        return children[children.length - 1].getName().getBaseName();
    }

    for (int i = children.length - 1; i >= 0; i--) {
        final FileObject child = children[i];
        if (!child.getType().hasChildren()) {
            continue;
        }
        final String found = findMaxID(child, depth + 1);
        if (found != null) {
            return found;
        }
    }
    return null;
}

From source file:org.obiba.opal.core.service.ProjectsServiceImpl.java

private void deleteFolder(FileObject folder) throws FileSystemException {
    if (!folder.isWriteable())
        return;//from   w w  w .  j a  va  2  s. c  om

    for (FileObject file : folder.getChildren()) {
        if (file.getType() == FileType.FOLDER) {
            deleteFolder(file);
        } else if (file.isWriteable()) {
            file.delete();
        }
    }
    if (folder.getChildren().length == 0) {
        folder.delete();
    }
}

From source file:org.obiba.opal.web.FilesResource.java

private void addChildren(Opal.FileDto.Builder folderBuilder, FileObject parentFolder, int level)
        throws FileSystemException {
    Opal.FileDto.Builder fileBuilder;/* w  ww .ja  va 2  s . c  o m*/

    // Get the children for the current folder (list of files & folders).
    List<FileObject> children = Arrays.asList(parentFolder.getChildren());

    Collections.sort(children, new Comparator<FileObject>() {

        @Override
        public int compare(FileObject arg0, FileObject arg1) {
            return arg0.getName().compareTo(arg1.getName());
        }
    });

    // Loop through all children.
    for (FileObject child : children) {
        // Build a FileDto representing the child.
        fileBuilder = Opal.FileDto.newBuilder();
        fileBuilder.setName(child.getName().getBaseName()).setPath(child.getName().getPath());
        fileBuilder.setType(
                child.getType() == FileType.FILE ? Opal.FileDto.FileType.FILE : Opal.FileDto.FileType.FOLDER);
        fileBuilder.setReadable(child.isReadable()).setWritable(child.isWriteable());

        // Set size on files only, not folders.
        if (child.getType() == FileType.FILE) {
            fileBuilder.setSize(child.getContent().getSize());
        }

        fileBuilder.setLastModifiedTime(child.getContent().getLastModifiedTime());

        if (child.getType().hasChildren() && child.getChildren().length > 0 && level - 1 > 0
                && child.isReadable()) {
            addChildren(fileBuilder, child, level - 1);
        }

        // Add the current child to the parent FileDto (folder).
        folderBuilder.addChildren(fileBuilder.build());
    }
}

From source file:org.obiba.opal.web.FilesResource.java

private void addFolder(String basePath, FileObject folder, ZipOutputStream outputStream,
        Collection<String> children) throws IOException {
    int baseLength = "/".equals(basePath) ? 1 : basePath.length() + 1;

    // Add the folder.
    outputStream.putNextEntry(new ZipEntry(folder.getName().getPath().substring(baseLength) + "/"));

    // Add its children files and subfolders.
    FileObject[] files = folder.getChildren();
    for (FileObject file : files) {
        if (children == null || children.isEmpty() || children.contains(file.getName().getBaseName())) {
            // only add files for which download is authorized
            if (file.isReadable()) {
                if (file.getType() == FileType.FOLDER) {
                    addFolder(basePath, file, outputStream, null);
                } else {
                    outputStream.putNextEntry(new ZipEntry(file.getName().getPath().substring(baseLength)));
                    try (FileInputStream inputStream = new FileInputStream(
                            opalRuntime.getFileSystem().getLocalFile(file))) {
                        StreamUtil.copy(inputStream, outputStream);
                        outputStream.closeEntry();
                    }/*from w w w . ja  v a2s .c  om*/
                }
            }
        }
    }
}

From source file:org.obiba.opal.web.FilesResource.java

/**
 * Delete writable folder and sub-folders.
 *
 * @param folder//w ww . ja v a  2s  .  co m
 * @throws FileSystemException
 */
private void deleteFolder(FileObject folder) throws FileSystemException {
    if (!folder.isWriteable())
        return;

    FileObject[] files = folder.getChildren();
    for (FileObject file : files) {
        if (file.getType() == FileType.FOLDER) {
            deleteFolder(file);
        } else if (file.isWriteable()) {
            file.delete();
        }
    }
    if (folder.getChildren().length == 0) {
        folder.delete();
    }
}

From source file:org.obiba.opal.web.FileSystemResource.java

private void addFiles(Opal.FileDto.Builder parentFolderBuilder, FileObject parentFolder)
        throws FileSystemException {
    Opal.FileDto.Builder fileBuilder;/*  w ww . j a  v  a 2  s  . com*/

    // Get the children for the current folder (list of files & folders).
    List<FileObject> children = Arrays.asList(parentFolder.getChildren());

    Collections.sort(children, new Comparator<FileObject>() {

        @Override
        public int compare(FileObject arg0, FileObject arg1) {
            return arg0.getName().compareTo(arg1.getName());
        }
    });

    // Loop through all children.
    for (FileObject child : children) {

        // Build a FileDto representing the child.
        fileBuilder = Opal.FileDto.newBuilder();
        fileBuilder.setName(child.getName().getBaseName()).setPath(child.getName().getPath());
        fileBuilder.setType(
                child.getType() == FileType.FILE ? Opal.FileDto.FileType.FILE : Opal.FileDto.FileType.FOLDER);
        fileBuilder.setReadable(child.isReadable()).setWritable(child.isWriteable());

        // If the current child is a folder, add its children recursively.
        if (child.getType() == FileType.FOLDER && child.isReadable()) {
            addFiles(fileBuilder, child);
        }

        // Add the current child to the parent FileDto (folder).
        parentFolderBuilder.addChildren(fileBuilder.build());
    }
}