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:com.newatlanta.appengine.junit.vfs.gae.GaeFolderTestCase.java

private static void assertChildren(FileObject fileObject, String[] childNames) throws Exception {
    FileObject[] children = fileObject.getChildren();
    for (int i = 0; i < children.length; i++) {
        assertTrue(children[i].getName().getPath().endsWith(childNames[i]));
    }/*from w  w w. j  a v  a  2 s  .c o m*/
}

From source file:net.sf.vfsjfilechooser.utils.VFSUtils.java

/**
 * Returns all the files of a folder//  w  ww  .j a  v a  2  s .  com
 * @param folder A folder
 * @return the files of a folder
 */
public static FileObject[] getFiles(FileObject folder) {
    try {
        return folder.getChildren();
    } catch (FileSystemException ex) {
        return new FileObject[0];
    }
}

From source file:com.threecrickets.sincerity.util.IoUtil.java

/**
 * Unpacks all files in an archive using Apache Commons VFS.
 * <p>/*  w  w w .j ava 2 s. c  om*/
 * Supported formats: zip, tar.gz/tgz, tar.bz2.
 * 
 * @param archiveFile
 *        The archive file
 * @param destinationDir
 *        The destination directory
 * @param workDir
 *        The work directory
 * @throws IOException
 *         In case of an I/O error
 */
public static void unpack(File archiveFile, File destinationDir, File workDir) throws IOException {
    String scheme = null;
    String name = archiveFile.getName();
    DefaultFileSystemManager manager = new DefaultFileSystemManager();
    try {
        boolean untar = false;
        if (name.endsWith(".zip")) {
            scheme = "zip:";
            manager.addProvider("zip", new ZipFileProvider());
        } else if (name.endsWith(".tar.gz") || name.endsWith(".tgz")) {
            scheme = "gz:";
            untar = true;
            manager.addProvider("tar", new TarFileProvider());
            manager.addProvider("gz", new GzipFileProvider());
            manager.addProvider("tgz", new TgzFileProvider());
        } else if (name.endsWith(".tar.bz2")) {
            scheme = "bz2:";
            untar = true;
            manager.addProvider("tar", new TarFileProvider());
            manager.addProvider("bz2", new Bzip2FileProvider());
            manager.addProvider("tbz2", new Tbz2FileProvider());
        }

        if (scheme != null) {
            DefaultFileReplicator replicator = new DefaultFileReplicator(workDir);
            replicator.init();
            manager.setReplicator(replicator);
            manager.setTemporaryFileStore(replicator);
            DefaultLocalFileProvider fileProvider = new DefaultLocalFileProvider();
            manager.addProvider("file", fileProvider);
            manager.setDefaultProvider(fileProvider);
            manager.setFilesCache(new DefaultFilesCache());
            manager.init();

            String path = scheme + archiveFile.toURI();
            FileObject fileObject = manager.resolveFile(path);
            FileObject[] children = fileObject.getChildren();
            if (untar && children.length > 0) {
                FileObject tar = manager
                        .resolveFile(new File(workDir, children[0].getName().getBaseName()).toURI().toString());
                org.apache.commons.vfs.FileUtil.copyContent(children[0], tar);
                tar = manager.resolveFile("tar:" + tar.getName());
                children = tar.getChildren();
            }

            for (FileObject child : children)
                copyRecursive(manager, child, manager.resolveFile(destinationDir.toURI().toString()));
        }
    } finally {
        manager.close();
    }
}

From source file:com.threecrickets.sincerity.util.IoUtil.java

/**
 * Copies a file or a complete subdirectory tree using Apache Commons VFS.
 * //from w w  w  .jav a2  s. co m
 * @param manager
 *        The file system manager
 * @param file
 *        The source file or directory
 * @param folder
 *        The target folder
 * @throws IOException
 *         In case of an I/O error
 */
public static void copyRecursive(FileSystemManager manager, FileObject file, FileObject folder)
        throws IOException {
    folder.createFolder();
    FileType type = file.getType();
    FileObject target = manager.resolveFile(folder + "/" + file.getName().getBaseName());
    if (type == FileType.FILE)
        org.apache.commons.vfs.FileUtil.copyContent(file, target);
    else if (type == FileType.FOLDER) {
        for (FileObject child : file.getChildren())
            copyRecursive(manager, child, target);
    }
}

From source file:com.newatlanta.appengine.junit.vfs.gae.GaeVfsTestCase.java

public static void assertEquals(File file, FileObject fileObject) throws Exception {
    assertEqualPaths(file, fileObject);/*from   w  w w.  j  a  v  a2  s  .  c  om*/
    assertEquals(file.canRead(), fileObject.isReadable());
    assertEquals(file.canWrite(), fileObject.isWriteable());
    assertEquals(file.exists(), fileObject.exists());
    if (file.getParentFile() == null) {
        assertNull(fileObject.getParent());
    } else {
        assertEqualPaths(file.getParentFile(), fileObject.getParent());
    }
    assertEquals(file.isDirectory(), fileObject.getType().hasChildren());
    assertEquals(file.isFile(), fileObject.getType().hasContent());
    assertEquals(file.isHidden(), fileObject.isHidden());
    if (file.isFile()) {
        assertEquals(file.length(), fileObject.getContent().getSize());
    }
    if (file.isDirectory()) { // same children
        File[] childFiles = file.listFiles();
        FileObject[] childObjects = fileObject.getChildren();
        assertEquals(childFiles.length, childObjects.length);
        for (int i = 0; i < childFiles.length; i++) {
            assertEqualPaths(childFiles[i], childObjects[i]);
        }
    }
}

From source file:com.pongasoft.kiwidoc.builder.DirectoryContentHandler.java

/**
 * Logic to determine whether a resource exists (depend on implementation).
 *
 * @param resourceFile the file for the content
 * @return <code>true</code> if the content for the given resource exists
 * @throws StoreException//from   w w w.  j  av  a  2s.c  o  m
 */
@Override
protected boolean doExists(FileObject resourceFile) throws StoreException {
    try {
        return resourceFile.getChildren().length > 0;
    } catch (FileSystemException e) {
        throw new StoreException(e);
    }
}

From source file:com.bedatadriven.renjin.appengine.AppEngineLocalFilesSystemProviderTest.java

@Test
public void test() throws FileSystemException {

    File basePath = new File(getClass().getResource("/jarfiletest.jar").getFile()).getParentFile();

    FileSystemManager dfsm = AppEngineContextFactory
            .createFileSystemManager(new AppEngineLocalFilesSystemProvider(basePath));

    FileObject jarFile = dfsm.resolveFile("/jarfiletest.jar");
    assertThat(jarFile.getName().getURI(), equalTo("file:///jarfiletest.jar"));
    assertThat(jarFile.exists(), equalTo(true));

    FileObject jarRoot = dfsm.resolveFile("jar:file:///jarfiletest.jar!/r/library");
    assertThat(jarRoot.exists(), equalTo(true));
    assertThat(jarRoot.getType(), equalTo(FileType.FOLDER));
    assertThat(jarRoot.getChildren().length, equalTo(1));
}

From source file:com.thinkberg.vfs.s3.tests.S3FileProviderTest.java

public void testGetFolderListing() throws FileSystemException {
    FileObject object = ROOT.resolveFile(FOLDER);
    assertEquals(2, object.getChildren().length);
}

From source file:com.pongasoft.kiwidoc.builder.DirectoryContentHandler.java

/**
 * Loads the content of the given resource.
 *
 * @param resource the resource to load//from   www  . j  a  va 2  s.c  om
 * @return the content as an object since it depends on which content is being read (ex: manifest,
 *         library, packages, class) (never <code>null</code>)
 * @throws NoSuchContentException if the content does not exist
 * @throws StoreException         if there is a problem reading the content.
 */
public M loadContent(R resource) throws NoSuchContentException, StoreException {
    if (resource == null)
        throw new NoSuchContentException(resource);

    Collection<String> childResources = new ArrayList<String>();

    FileObject root = getResourceFile(resource);

    try {
        if (!root.exists())
            throw new NoSuchContentException(resource);

        if (root.getType() == FileType.FOLDER) {
            FileObject[] children = root.getChildren();
            for (FileObject child : children) {
                if (child.getType() == FileType.FOLDER) {
                    childResources.add(child.getName().getBaseName());
                }
            }
        }
    } catch (FileSystemException e) {
        throw new StoreException(e);
    }

    return _modelFactory.buildModel(resource, childResources);
}

From source file:com.adito.networkplaces.store.cifs.CIFSMount.java

public FileObject createVFSFileObject(String path,
        PasswordCredentials credentials/*, DAVTransaction transaction*/)
        throws IOException, DAVAuthenticationRequiredException {

    super.getStore().getName();

    URI uri = getRootVFSURI(SystemProperties.get("jcifs.encoding", "cp860"));

    try {//from ww  w .j av  a2s  . c om
        uri.setScheme("smb");
        if (credentials != null) {
            uri.setUserinfo(DAVUtilities.encodeURIUserInfo(credentials.getUsername()
                    + (credentials.getPassword() != null ? ":" + new String(credentials.getPassword()) : "")));
        }
        uri.setPath(uri.getPath() + (uri.getPath().endsWith("/") ? "" : "/")
                + DAVUtilities.encodePath(path, SystemProperties.get("jcifs.encoding", "cp860")));
        FileObject root = getStore().getRepository().getFileSystemManager().resolveFile(uri.toString());
        if (root.getType().equals(FileType.FOLDER)) {
            // Extra check so that the correct exception is thrown.
            root.getChildren();
        }
        return root;
    } catch (FileSystemException fse) {
        if (fse.getCause().getClass().getName().equals("jcifs.smb.SmbAuthException")) {
            throw new DAVAuthenticationRequiredException(getMountString());
        }
        if (fse.getCause() != null && fse.getCause() instanceof SmbException
                && ((SmbException) fse.getCause()).getRootCause() != null
                && "Connection timeout".equals(((SmbException) fse.getCause()).getRootCause().getMessage())) {
            throw new UnknownHostException(uri.getHost());
        }
        if (log.isDebugEnabled())
            log.debug("File system exception! ", fse);
        throw fse;
    }
}