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

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

Introduction

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

Prototype

public FileName getName();

Source Link

Document

Returns the name of this file.

Usage

From source file:org.sonatype.gshell.commands.text.CatCommand.java

public Object execute(final CommandContext context) throws Exception {
    assert context != null;
    IO io = context.getIo();/*from w  ww.j  a  v a  2 s  .com*/

    //
    // TODO: Support multi-path cat, and the special '-' token (which is the default if no paths are given)
    //

    FileObject file = resolveFile(context, path);

    new FileObjectAssert(file).exists();
    ensureFileHasContent(file);

    org.apache.commons.vfs.FileContent content = file.getContent();
    FileContentInfo info = content.getContentInfo();
    log.debug("Content type: {}", info.getContentType());
    log.debug("Content encoding: {}", info.getContentEncoding());

    //
    // TODO: Only cat files which we think are text, or warn if its not, allow flag to force
    //

    log.debug("Displaying file: {}", file.getName());

    BufferedReader reader = new BufferedReader(new InputStreamReader(content.getInputStream()));
    try {
        cat(reader, io);
    } finally {
        Closer.close(reader);
    }

    FileObjects.close(file);

    return Result.SUCCESS;
}

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

public Object execute(final CommandContext context) throws Exception {
    assert context != null;
    IO io = context.getIo();//from  w  w w .ja va 2 s. c  om

    FileObject source = resolveFile(context, sourcePath);
    FileObject target = resolveFile(context, targetPath);

    new FileObjectAssert(source).exists();

    // TODO: Validate more

    if (target.exists() && target.getType().hasChildren()) {
        target = target.resolveFile(source.getName().getBaseName());
    }

    log.info("Copying {} -> {}", source, target);

    target.copyFrom(source, Selectors.SELECT_ALL);

    FileObjects.close(source, target);

    return Result.SUCCESS;
}

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

public Object execute(final CommandContext context) throws Exception {
    assert context != null;
    IO io = context.getIo();//from ww  w  .ja v  a 2  s.c  o m

    FileObject dir = getCurrentDirectory(context);
    io.println(dir.getName().getURI());

    FileObjects.close(dir);

    return Result.SUCCESS;
}

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

public Object execute(final CommandContext context) throws Exception {
    assert context != null;
    IO io = context.getIo();/*w w  w . ja  va 2 s. com*/

    FileObject file = resolveFile(context, path);

    io.println("URL: {}", file.getURL());
    io.println("Name: {}", file.getName());
    io.println("BaseName: {}", file.getName().getBaseName());
    io.println("Extension: {}", file.getName().getExtension());
    io.println("Path: {}", file.getName().getPath());
    io.println("Scheme: {}", file.getName().getScheme());
    io.println("URI: {}", file.getName().getURI());
    io.println("Root URI: {}", file.getName().getRootURI());
    io.println("Parent: {}", file.getName().getParent());
    io.println("Type: {}", file.getType());
    io.println("Exists: {}", file.exists());
    io.println("Readable: {}", file.isReadable());
    io.println("Writeable: {}", file.isWriteable());
    io.println("Root path: {}", file.getFileSystem().getRoot().getName().getPath());

    if (file.exists()) {
        FileContent content = file.getContent();
        FileContentInfo contentInfo = content.getContentInfo();
        io.println("Content type: {}", contentInfo.getContentType());
        io.println("Content encoding: {}", contentInfo.getContentEncoding());

        try {
            // noinspection unchecked
            Map<String, Object> attrs = content.getAttributes();
            if (attrs != null && !attrs.isEmpty()) {
                io.println("Attributes:");
                for (Map.Entry<String, Object> entry : attrs.entrySet()) {
                    io.println("    {}='{}'", entry.getKey(), entry.getValue());
                }
            }
        } catch (FileSystemException e) {
            io.println("File attributes are NOT supported");
        }

        try {
            Certificate[] certs = content.getCertificates();
            if (certs != null && certs.length != 0) {
                io.println("Certificate:");
                for (Certificate cert : certs) {
                    io.println("    {}", cert);
                }
            }
        } catch (FileSystemException e) {
            io.println("File certificates are NOT supported");
        }

        if (file.getType().equals(FileType.FILE)) {
            io.println("Size: {} bytes", content.getSize());
        } else if (file.getType().hasChildren() && file.isReadable()) {
            FileObject[] children = file.getChildren();
            io.println("Directory with {} files", children.length);

            for (int iterChildren = 0; iterChildren < children.length; iterChildren++) {
                io.println("#{}:{}", iterChildren, children[iterChildren].getName());
                if (iterChildren > 5) {
                    break;
                }
            }
        }

        io.println("Last modified: {}",
                DateFormat.getInstance().format(new Date(content.getLastModifiedTime())));
    } else {
        io.println("The file does not exist");
    }

    FileObjects.close(file);

    return Result.SUCCESS;
}

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

private void display(final CommandContext context, final FileObject file, final FileObject root)
        throws FileSystemException {
    assert context != null;
    assert file != null;

    String path;/*from   w w w.  j  a  va  2s .  c o m*/
    try {
        path = new URI(this.path).resolve(root.getURL().toURI().relativize(file.getURL().toURI())).toString();
    } catch (Exception e) {
        path = file.getName().getPath();
    }
    IO io = context.getIo();
    io.println(path);
}

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

public Object execute(final CommandContext context) throws Exception {
    assert context != null;
    IO io = context.getIo();// w  ww  .ja  v a2  s .  c o  m

    FileObject file;
    if (path != null) {
        file = resolveFile(context, path);
    } else {
        file = getCurrentDirectory(context);
    }

    if (file.getType().hasChildren()) {
        listChildren(io, file);
    } else {
        io.println(file.getName().getPath());
    }

    FileObjects.close(file);

    return Result.SUCCESS;
}

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;//  w w  w. ja  v a 2s  .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.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  av a  2s . c om*/
        throw new ResultNotification("File has no content: " + file.getName(), Result.FAILURE);
    }
}

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   w w  w  .  j av  a  2  s .c  o  m

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

From source file:org.sonatype.gshell.vfs.provider.truezip.TruezipFileProvider.java

/**
 * Creates a layered file system.  This method is called if the file system is not cached.
 *
 * @param scheme    The URI scheme.//  w  w w.  java2 s  .  c  om
 * @param file      The file to create the file system on top of.
 * @return          The file system.
 */
protected FileSystem doCreateFileSystem(final String scheme, final FileObject file,
        final FileSystemOptions options) throws FileSystemException {
    FileName name = new LayeredFileName(scheme, file.getName(), FileName.ROOT_PATH, FileType.FOLDER);
    return new TruezipFileSystem(name, file, options);
}