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

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

Introduction

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

Prototype

public FileObject[] getChildren() throws FileSystemException;

Source Link

Document

Lists the children of this file.

Usage

From source file:org.sonatype.gshell.commands.vfs.ListDirectoryCommand.java

private void listChildren(final IO io, final FileObject dir) throws Exception {
    assert io != null;
    assert dir != null;

    FileObject[] files;//from  w  w  w  .  j av  a2  s . c o m

    if (includeHidden) {
        files = dir.getChildren();
    } else {
        FileFilter filter = new FileFilter() {
            public boolean accept(final FileSelectInfo selection) {
                assert selection != null;

                try {
                    return !selection.getFile().isHidden();
                } catch (FileSystemException e) {
                    throw new RuntimeException(e);
                }
            }
        };

        files = dir.findFiles(new FileFilterSelector(filter));
    }

    ConsoleReader reader = new ConsoleReader(io.streams.in, io.out, io.getTerminal());

    reader.setPaginationEnabled(false);

    List<String> names = new ArrayList<String>(files.length);
    List<FileObject> dirs = new LinkedList<FileObject>();

    for (FileObject file : files) {
        String fileName = file.getName().getBaseName();

        if (FileObjects.hasChildren(file)) {
            fileName += FileName.SEPARATOR;

            if (recursive) {
                dirs.add(file);
            }
        }

        names.add(fileName);

        file.close();
    }

    if (longList) {
        for (String name : names) {
            io.out.println(name);
        }
    } else {
        reader.printColumns(names);
    }

    if (!dirs.isEmpty()) {
        for (FileObject subdir : dirs) {
            io.out.println();
            io.out.print(subdir.getName().getBaseName());
            io.out.print(":");
            listChildren(io, subdir);
        }
    }

    dir.close();
}

From source file:org.sonatype.gshell.vfs.FileObjects.java

public static boolean hasChildren(final FileObject file) throws FileSystemException {
    assert file != null;

    if (file.getType().hasChildren()) {
        FileObject[] children = file.getChildren();

        if (children != null && children.length != 0) {
            return true;
        }//from  ww  w. jav  a  2s  .  c  om
    }

    return false;
}

From source file:r.base.Files.java

/**
 * {@code list.files} produce a character vector of the names of files in the named directory.
 *
 * @param paths  a character vector of full path names; the default corresponds to the working
 *  directory getwd(). Missing values will be ignored.
 * @param pattern an optional regular expression. Only file names which match the regular
 * expression will be returned.//from  w ww  .  jav a2s.co  m
 * @param allFiles  If FALSE, only the names of visible files are returned. If TRUE, all
 * file names will be returned.
 * @param fullNames If TRUE, the directory path is prepended to the file names. If FALSE,
 * only the file names are returned.
 * @param recursive Should the listing recurse into directories?
 * @param ignoreCase Should pattern-matching be case-insensitive?
 *
 * If a path does not exist or is not a directory or is unreadable it is skipped, with a warning.
 * The files are sorted in alphabetical order, on the full path if full.names = TRUE. Directories are included only if recursive = FALSE.
 *
 * @return
 */
@Primitive("list.files")
public static StringVector listFiles(@Current final Context context, final StringVector paths,
        final String pattern, final boolean allFiles, final boolean fullNames, boolean recursive,
        final boolean ignoreCase) throws IOException {

    return new Object() {

        private final StringVector.Builder result = new StringVector.Builder();
        private final RE filter = pattern == null ? null : new ExtendedRE(pattern).ignoreCase(ignoreCase);

        public StringVector list() throws IOException {
            for (String path : paths) {
                FileObject folder = context.resolveFile(path);
                if (folder.getType() == FileType.FOLDER) {
                    if (allFiles) {
                        add(folder, ".");
                        add(folder, "..");
                    }
                    for (FileObject child : folder.getChildren()) {
                        if (filter(child)) {
                            add(child);
                        }
                    }
                }
            }
            return result.build();
        }

        void add(FileObject file) {
            if (fullNames) {
                result.add(file.getName().getURI());
            } else {
                result.add(file.getName().getBaseName());
            }
        }

        void add(FileObject folder, String name) throws FileSystemException {
            if (fullNames) {
                result.add(folder.resolveFile(name).getName().getURI());
            } else {
                result.add(name);
            }
        }

        boolean filter(FileObject child) throws FileSystemException {
            if (!allFiles && isHidden(child)) {
                return false;
            }
            if (filter != null && !filter.match(child.getName().getBaseName())) {
                return false;
            }
            return true;
        }

        private boolean isHidden(FileObject file) throws FileSystemException {
            return file.isHidden() || file.getName().getBaseName().startsWith(".");
        }
    }.list();
}

From source file:r.base.Files.java

private static void delete(FileObject file, boolean recursive) throws FileSystemException {
    if (file.exists()) {
        if (file.getType() == FileType.FILE) {
            file.delete();//from  ww w.  ja  v  a2 s.  co  m
        } else if (file.getType() == FileType.FOLDER) {
            if (file.getChildren().length == 0) {
                file.delete();
            } else if (recursive) {
                file.delete();
            }
        }
    }
}

From source file:uk.ac.liv.shaman.vfsme.CommonVFSMediaAdaptor.java

private void processChild(FileObject f, FileSystemManager m, StringBuilder sb)
        throws FileSystemException, UnsupportedEncodingException {
    if (f.getType() == FileType.FOLDER || f.getType() == FileType.IMAGINARY) {
        sb.append("<tr>");
        sb.append("<td><b>").append(f.getName()).append("</b></td>");
        sb.append(//from   w  w w.  j  ava  2  s.  c o  m
                "<td align='right'><span Behavior='ElideSpan'>0</span> --<td align='right'><span Behavior='ElideSpan'>0</span> --");
        FileObject[] children = f.getChildren();

        for (FileObject subfile : children)
            processChild(subfile, m, sb);
    } else {
        sb.append("<tr>");
        FileName fname = f.getName();
        sb.append("<td><a href='").append(fname.getURI().replaceAll(" ", "%20")).append("'>").append(fname)
                .append("</a>");
        DateFormat outdfm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long size = 0;
        Date last = new Date();
        try {
            size = f.getContent().getSize();
            last = new Date(f.getContent().getLastModifiedTime());
        } catch (Exception e) {
            // TODO: handle exception
        }
        sb.append("<td align='right'>").append(Long.toString(size)).append("<td align='right'>")
                .append(outdfm.format(last));

    }
}

From source file:unitTests.dataspaces.VFSApplicationScratchSpaceImplTest.java

private void assertIsExistingEmptyDirectory(final String path) throws FileSystemException {
    final FileObject fPartialDS = fileSystemManager.resolveFile(path);

    assertTrue(fPartialDS.exists());//from  www  .  ja v a2 s  . co  m
    assertEquals(FileType.FOLDER, fPartialDS.getType());
    assertEquals(0, fPartialDS.getChildren().length);
}

From source file:unitTests.dataspaces.VFSNodeScratchSpaceImplTest.java

private void assertIsExistingEmptyDirectory(String path) throws FileSystemException {
    FileObject fPartialDS = fileSystemManager.resolveFile(path);

    assertTrue(fPartialDS.exists());//from   www .  j a v a 2  s .c o m
    assertEquals(FileType.FOLDER, fPartialDS.getType());
    assertEquals(0, fPartialDS.getChildren().length);
}