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

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

Introduction

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

Prototype

public FileType getType() throws FileSystemException;

Source Link

Document

Returns this file's type.

Usage

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

protected void ensureFileHasContent(final FileObject file) throws FileSystemException {
    assert file != null;

    if (!file.getType().hasContent()) {
        FileObjects.close(file);/* w  w w.  j  ava2  s .c o  m*/
        throw new ResultNotification("File has no content: " + file.getName(), Result.FAILURE);
    }
}

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   www .  ja  va  2  s.c om
    }

    return false;
}

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

public void setCurrentDirectory(final Variables vars, final FileObject dir) throws FileSystemException {
    assert vars != null;
    assert dir != null;

    log.trace("Setting CWD: {}", dir);

    // Make sure that the given file object exists and is really a directory
    if (!dir.exists()) {
        throw new RuntimeException("Directory not found: " + dir.getName());
    } else if (!dir.getType().hasChildren()) {
        throw new RuntimeException("File can not contain children: " + dir.getName());
    }/*from ww  w . j a  v  a 2  s . co m*/

    vars.parent().set(CWD, dir);
}

From source file:org.vivoweb.harvester.util.FileAide.java

/**
 * Get a set of non-hidden direct children of the given path
 * @param path the path to search under/*  w  w w . j  av a  2s  .c  o m*/
 * @return a set of non-hidden direct children
 * @throws IOException error resolving path
 */
public static Set<String> getNonHiddenChildren(String path) throws IOException {
    Set<String> allFileListing = new HashSet<String>();
    for (FileObject file : getFileObject(path).findFiles(Selectors.SELECT_CHILDREN)) {
        if (!file.isHidden() && (file.getType() == FileType.FILE)) {
            allFileListing.add(file.getName().getBaseName());
        }
    }
    return allFileListing;
}

From source file:pt.webdetails.cpf.repository.vfs.VfsRepositoryAccess.java

public IRepositoryFile[] listRepositoryFiles(IRepositoryFileFilter fileFilter) {
    try {//  w  ww  .  j a  v a2  s.  co m
        FileObject[] files = repo.getChildren();
        List<IRepositoryFile> repoFiles = new ArrayList<IRepositoryFile>();
        for (FileObject file : files) {
            if (file.exists() && file.isReadable() && file.getType().equals(FileType.FILE)) {
                IRepositoryFile repoFile = new VfsRepositoryFile(repo, file);
                if (fileFilter == null || fileFilter.accept(repoFile)) {
                    repoFiles.add(repoFile);
                }
            }
        }
        return repoFiles.toArray(new IRepositoryFile[] {});
    } catch (FileSystemException e) {
        throw new RuntimeException("Cannot list repo files", e);
    }
}

From source file:r.base.Files.java

/**
 * Utility function to extract information about files on the user's file systems.
 *
 * @param context  current call Context//w w w. j a v a2s  . c  o m
 * @param paths the list of files for which to return information
 * @return list column-oriented table of file information
 * @throws FileSystemException
 */
@Primitive("file.info")
public static ListVector fileInfo(@Current Context context, StringVector paths) throws FileSystemException {

    DoubleVector.Builder size = new DoubleVector.Builder();
    LogicalVector.Builder isdir = new LogicalVector.Builder();
    IntVector.Builder mode = (IntVector.Builder) new IntVector.Builder().setAttribute(Symbols.CLASS,
            new StringVector("octmode"));
    DoubleVector.Builder mtime = new DoubleVector.Builder();
    StringVector.Builder exe = new StringVector.Builder();

    for (String path : paths) {
        FileObject file = context.resolveFile(path);
        if (file.exists()) {
            if (file.getType() == FileType.FILE) {
                size.add((int) file.getContent().getSize());
            } else {
                size.add(0);
            }
            isdir.add(file.getType() == FileType.FOLDER);
            mode.add(mode(file));
            try {
                mtime.add(file.getContent().getLastModifiedTime());
            } catch (Exception e) {
                mtime.add(0);
            }
            exe.add(file.getName().getBaseName().endsWith(".exe") ? "yes" : "no");
        } else {
            size.add(IntVector.NA);
            isdir.add(IntVector.NA);
            mode.add(IntVector.NA);
            mtime.add(DoubleVector.NA);
            exe.add(StringVector.NA);
        }
    }

    return ListVector.newNamedBuilder().add("size", size).add("isdir", isdir).add("mode", mode)
            .add("mtime", mtime).add("ctime", mtime).add("atime", mtime).add("exe", exe).build();
}

From source file:r.base.Files.java

/**
  * Gets the type or storage mode of an object.
        //from  w w  w. j a  v a  2 s .  c o  m
  * @return  unix-style file mode integer
  */
private static int mode(FileObject file) throws FileSystemException {
    int access = 0;
    if (file.isReadable()) {
        access += 4;
    }
    if (file.isWriteable()) {
        access += 2;
    }
    if (file.getType() == FileType.FOLDER) {
        access += 1;
    }
    // i know this is braindead but i can't be bothered
    // to do octal math at the moment
    String digit = Integer.toString(access);
    String octalString = digit + digit + digit;

    return Integer.parseInt(octalString, 8);
}

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 w w.  j a v  a 2 s  .  c o  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  www. ja  v  a2  s  .c om*/
        } 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  ww  .  j  a v a2s.  co  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));

    }
}