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.apache.commons.vfs.example.Shell.java

/**
 * Does a 'cp' command./*from  w w w  . j  a  v  a  2  s. co m*/
 */
private void cp(final String[] cmd) throws Exception {
    if (cmd.length < 3) {
        throw new Exception("USAGE: cp <src> <dest>");
    }

    final FileObject src = mgr.resolveFile(cwd, cmd[1]);
    FileObject dest = mgr.resolveFile(cwd, cmd[2]);
    if (dest.exists() && dest.getType() == FileType.FOLDER) {
        dest = dest.resolveFile(src.getName().getBaseName());
    }

    dest.copyFrom(src, Selectors.SELECT_ALL);
}

From source file:org.apache.commons.vfs.example.Shell.java

/**
 * Does a 'cd' command./*from  ww w. ja  v a  2 s  . co m*/
 * If the taget directory does not exist, a message is printed to <code>System.err</code>.
 */
private void cd(final String[] cmd) throws Exception {
    final String path;
    if (cmd.length > 1) {
        path = cmd[1];
    } else {
        path = System.getProperty("user.home");
    }

    // Locate and validate the folder
    FileObject tmp = mgr.resolveFile(cwd, path);
    if (tmp.exists()) {
        cwd = tmp;
    } else {
        System.out.println("Folder does not exist: " + tmp.getName());
    }
    System.out.println("Current folder is " + cwd.getName());
}

From source file:org.apache.commons.vfs.example.Shell.java

/**
 * Does an 'ls' command.//from  w ww  .j a  va2  s. c o m
 */
private void ls(final String[] cmd) throws FileSystemException {
    int pos = 1;
    final boolean recursive;
    if (cmd.length > pos && cmd[pos].equals("-R")) {
        recursive = true;
        pos++;
    } else {
        recursive = false;
    }

    final FileObject file;
    if (cmd.length > pos) {
        file = mgr.resolveFile(cwd, cmd[pos]);
    } else {
        file = cwd;
    }

    if (file.getType() == FileType.FOLDER) {
        // List the contents
        System.out.println("Contents of " + file.getName());
        listChildren(file, recursive, "");
    } else {
        // Stat the file
        System.out.println(file.getName());
        final FileContent content = file.getContent();
        System.out.println("Size: " + content.getSize() + " bytes.");
        final DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
        final String lastMod = dateFormat.format(new Date(content.getLastModifiedTime()));
        System.out.println("Last modified: " + lastMod);
    }
}

From source file:org.apache.commons.vfs.example.Shell.java

/**
 * Lists the children of a folder./*from ww w. ja v a 2s.  co  m*/
 */
private void listChildren(final FileObject dir, final boolean recursive, final String prefix)
        throws FileSystemException {
    final FileObject[] children = dir.getChildren();
    for (int i = 0; i < children.length; i++) {
        final FileObject child = children[i];
        System.out.print(prefix);
        System.out.print(child.getName().getBaseName());
        if (child.getType() == FileType.FOLDER) {
            System.out.println("/");
            if (recursive) {
                listChildren(child, recursive, prefix + "    ");
            }
        } else {
            System.out.println();
        }
    }
}

From source file:org.apache.commons.vfs.example.ShowProperties.java

public static void main(String[] args) {
    if (args.length == 0) {
        System.err.println("Please pass the name of a file as parameter.");
        System.err.println("e.g. java org.apache.commons.vfs.example.ShowProperties LICENSE.txt");
        return;/*from ww  w  . j  a  v a2  s. co  m*/
    }
    for (int i = 0; i < args.length; i++) {
        try {
            FileSystemManager mgr = VFS.getManager();
            System.out.println();
            System.out.println("Parsing: " + args[i]);
            FileObject file = mgr.resolveFile(args[i]);
            System.out.println("URL: " + file.getURL());
            System.out.println("getName(): " + file.getName());
            System.out.println("BaseName: " + file.getName().getBaseName());
            System.out.println("Extension: " + file.getName().getExtension());
            System.out.println("Path: " + file.getName().getPath());
            System.out.println("Scheme: " + file.getName().getScheme());
            System.out.println("URI: " + file.getName().getURI());
            System.out.println("Root URI: " + file.getName().getRootURI());
            System.out.println("Parent: " + file.getName().getParent());
            System.out.println("Type: " + file.getType());
            System.out.println("Exists: " + file.exists());
            System.out.println("Readable: " + file.isReadable());
            System.out.println("Writeable: " + file.isWriteable());
            System.out.println("Root path: " + file.getFileSystem().getRoot().getName().getPath());
            if (file.exists()) {
                if (file.getType().equals(FileType.FILE)) {
                    System.out.println("Size: " + file.getContent().getSize() + " bytes");
                } else if (file.getType().equals(FileType.FOLDER) && file.isReadable()) {
                    FileObject[] children = file.getChildren();
                    System.out.println("Directory with " + children.length + " files");
                    for (int iterChildren = 0; iterChildren < children.length; iterChildren++) {
                        System.out.println("#" + iterChildren + ": " + children[iterChildren].getName());
                        if (iterChildren > 5) {
                            break;
                        }
                    }
                }
                System.out.println("Last modified: "
                        + DateFormat.getInstance().format(new Date(file.getContent().getLastModifiedTime())));
            } else {
                System.out.println("The file does not exist");
            }
            file.close();
        } catch (FileSystemException ex) {
            ex.printStackTrace();
        }
    }
}

From source file:org.apache.ivy.plugins.repository.vfs.VfsRepository.java

/**
 * Return a listing of the contents of a parent directory. Listing is a set of strings
 * representing VFS URIs.//from  ww w.  java 2  s .c  om
 * 
 * @param vfsURI
 *            providing identifying a VFS provided resource
 * @throws IOException
 *             on failure.
 * @see "Supported File Systems in the jakarta-commons-vfs documentation"
 */
public List list(String vfsURI) throws IOException {
    ArrayList list = new ArrayList();
    Message.debug("list called for URI" + vfsURI);
    FileObject resourceImpl = getVFSManager().resolveFile(vfsURI);
    Message.debug("resourceImpl=" + resourceImpl.toString());
    Message.debug("resourceImpl.exists()" + resourceImpl.exists());
    Message.debug("resourceImpl.getType()" + resourceImpl.getType());
    Message.debug("FileType.FOLDER" + FileType.FOLDER);
    if ((resourceImpl != null) && resourceImpl.exists() && (resourceImpl.getType() == FileType.FOLDER)) {
        FileObject[] children = resourceImpl.getChildren();
        for (int i = 0; i < children.length; i++) {
            FileObject child = children[i];
            Message.debug("child " + i + child.getName().getURI());
            list.add(VfsResource.normalize(child.getName().getURI()));
        }
    }
    return list;
}

From source file:org.apache.ivy.plugins.repository.vfs.VfsResource.java

/**
 * Get a list of direct descendents of the given resource. Note that attempts to get a list of
 * children does <emphasize>not</emphasize> result in an error. Instead an error message is
 * logged and an empty ArrayList returned.
 * /*from  w  w  w.  j av a 2s.  c  om*/
 * @return A <code>ArrayList</code> of VFSResources
 */
public List getChildren() {
    init();
    ArrayList list = new ArrayList();
    try {
        if ((resourceImpl != null) && resourceImpl.exists() && (resourceImpl.getType() == FileType.FOLDER)) {
            FileObject[] children = resourceImpl.getChildren();
            for (int i = 0; i < children.length; i++) {
                FileObject child = children[i];
                list.add(normalize(child.getName().getURI()));
            }
        }
    } catch (IOException e) {
        Message.debug(e);
        Message.verbose(e.getLocalizedMessage());
    }
    return list;
}

From source file:org.bibalex.gallery.storage.BAGStorage.java

public static List<String> listChildren(String dirUrlStr, FileType childrenType, int timeout)
        throws BAGException {

    try {//from  w  w w  .ja v a  2 s. c  om
        List<String> result;

        if (new URI(dirUrlStr).getScheme().startsWith("http")) {
            BasicHttpParams httpParams = new BasicHttpParams();

            HttpConnectionParams.setConnectionTimeout(httpParams, timeout);

            DefaultHttpClient httpClient = new DefaultHttpClient(httpParams);
            try {
                result = listChildrenApacheHttpd(dirUrlStr, childrenType, httpClient);

            } finally {
                // When HttpClient instance is no longer needed,
                // shut down the connection manager to ensure
                // immediate deallocation of all system resources
                httpClient.getConnectionManager().shutdown();
            }
        } else {

            result = new ArrayList<String>();

            FileSystemManager fsMgr = VFS.getManager();

            FileObject dir = fsMgr.resolveFile(dirUrlStr);

            final FileObject[] children = dir.getChildren();

            for (final FileObject child : children) {
                if ((childrenType == FileType.FILE_OR_FOLDER) || (child.getType() == childrenType)) {
                    result.add(child.getName().getBaseName());
                }
            }
        }
        // TODO define a comparator that orders the files properly as in
        // http://forums.sun.com/thread.jspa?threadID=5289401
        Collections.sort(result);

        return result;

    } catch (FileSystemException e) {
        throw new BAGException(e);
    } catch (URISyntaxException e) {
        throw new BAGException(e);
    }
}

From source file:org.cfeclipse.cfml.views.explorer.vfs.view.VFSView.java

/**
 * Traverse the entire tree and update only what has changed.
 * /* w ww  .  j av a 2 s .com*/
 * @param roots the root directory listing
 */
private void treeRefresh(FileObject[] masterFiles) {
    TreeItem[] items = tree.getItems();
    int masterIndex = 0;
    int itemIndex = 0;

    for (int i = 0; i < items.length; ++i) {
        final TreeItem item = items[i];
        //         final String uri = (String)item.getData(TREEITEMDATA_URI);
        final FileObject itemFile = null;

        if ((itemFile == null) || (masterIndex == masterFiles.length)) {
            // remove bad item or placeholder
            item.dispose();
            continue;
        }
        final FileObject masterFile = masterFiles[masterIndex];

        int compare = VFSUtil.compareFiles(masterFile, itemFile);

        if (compare == 0) {
            // same file, update it
            try {
                treeRefreshItem(item, false);
            } catch (Exception e) {
                error("treeRefresh.treeRefreshItem " + e);
            }

            ++itemIndex;
            ++masterIndex;
        } else if (compare < 0) {
            // should appear before file, insert it
            TreeItem newItem = new TreeItem(tree, SWT.NULL, itemIndex);
            treeInitVolume(newItem, masterFile.getName().toString());// masterFile);
            new TreeItem(newItem, SWT.NULL); // placeholder child item to get "expand" button
            ++itemIndex;
            ++masterIndex;
            --i;
        } else {
            // should appear after file, delete stale item
            item.dispose();
        }
    }

    for (; masterIndex < masterFiles.length; ++masterIndex) {
        final FileObject masterFile = masterFiles[masterIndex];
        TreeItem newItem = new TreeItem(tree, SWT.NULL);

        //System.out.println("treeRefresh file=" + masterFile.getName().getURI());
        treeInitVolume(newItem, masterFile.getName().toString());// masterFile);
        new TreeItem(newItem, SWT.NULL); // placeholder child item to get "expand" button
    }
}

From source file:org.cfeclipse.cfml.views.explorer.vfs.view.VFSView.java

/**
 * Traverse an item in the tree and update only what has changed.
 * //from  w  w  w  . j  av a  2 s.c  o  m
 * @param dirItem the tree item of the directory
 * @param forcePopulate true iff we should populate non-expanded items as well
 */
private void treeRefreshItem(TreeItem dirItem, boolean forcePopulate) throws FileSystemException {
    final String uri = (String) dirItem.getData(TREEITEMDATA_URI);

    //System.out.println("treeRefreshItem uri=" + uri);

    if (!forcePopulate && !dirItem.getExpanded()) {
        // Refresh non-expanded item
        if (dirItem.getData(TREEITEMDATA_STUB) != null) {
            treeItemRemoveAll(dirItem);
            new TreeItem(dirItem, SWT.NULL); // placeholder child item to get "expand" button
            dirItem.setData(TREEITEMDATA_STUB, null);
        }
        return;
    }
    // Refresh expanded item
    dirItem.setData(TREEITEMDATA_STUB, this); // clear stub flag

    /* Get directory listing */
    FileObject[] subFiles = (uri != null)
            ? getDirectoryList(resolveURI(uri, (String) dirItem.getData(TREEITEMDATA_CONNECTIONID)))
            : null;

    if (subFiles == null || subFiles.length == 0) {
        /* Error or no contents */
        treeItemRemoveAll(dirItem);
        dirItem.setExpanded(false);
        return;
    }

    /* Refresh sub-items */
    TreeItem[] items = dirItem.getItems();
    final FileObject[] masterFiles = subFiles;

    int masterIndex = 0;
    int itemIndex = 0;
    FileObject masterFile = null;

    String folderBaseName = null;

    for (int i = 0; i < items.length; ++i) {
        while ((masterFile == null) && (masterIndex < masterFiles.length)) {
            masterFile = masterFiles[masterIndex++];

            //if (! masterFile.isDirectory()) masterFile = null;
            if (!VFSUtil.isDirectory(masterFile))
                masterFile = null;
        }

        final TreeItem item = items[i];
        //         final String itemUri = (String) item.getData(TREEITEMDATA_URI);
        //System.out.println("itemUri=" + itemUri);         
        final String itemFile = (String) item.getData(TREEITEMDATA_URI);
        //final FileObject itemFile = (FileObject) item.getData(TREEITEMDATA_FILE);

        if ((itemFile == null) || (masterFile == null)) {
            // remove bad item or placeholder
            item.dispose();
            continue;
        }
        int compare = VFSUtil.compareFiles(masterFile,
                resolveURI(itemFile, (String) item.getData(TREEITEMDATA_CONNECTIONID)));

        if (compare == 0) {
            // same file, update it
            treeRefreshItem(item, false);
            masterFile = null;
            ++itemIndex;
        } else if (compare < 0) {
            // should appear before file, insert it
            TreeItem newItem = new TreeItem(dirItem, SWT.NULL, itemIndex);

            folderBaseName = masterFile.getName().getBaseName();

            //treeInitFolder(newItem, folderBaseName , uri + FileName.SEPARATOR + folderBaseName); //masterFile);
            treeInitFolder(newItem, folderBaseName, masterFile.toString());

            new TreeItem(newItem, SWT.NULL); // add a placeholder child item so we get the "expand" button
            masterFile = null;
            ++itemIndex;
            --i;
        } else {
            // should appear after file, delete stale item
            item.dispose();
        }
    }
    while ((masterFile != null) || (masterIndex < masterFiles.length)) {
        if (masterFile != null) {
            TreeItem newItem = new TreeItem(dirItem, SWT.NULL);

            folderBaseName = masterFile.getName().getBaseName();

            //treeInitFolder(newItem, folderBaseName , uri + FileName.SEPARATOR + folderBaseName );
            treeInitFolder(newItem, folderBaseName, masterFile.toString());

            new TreeItem(newItem, SWT.NULL); // add a placeholder child item so we get the "expand" button
            if (masterIndex == masterFiles.length)
                break;
        }
        masterFile = masterFiles[masterIndex++];

        if (!VFSUtil.isDirectory(masterFile)) {
            masterFile = null;
        }
    }
}