Package org.apache.commons.vfs

Examples of org.apache.commons.vfs.FileObject


        while (copies.size() > 0)
        {
            final File file = (File) copies.remove(0);
            try
            {
                final FileObject fileObject = getContext().toFileObject(file);
                fileObject.delete(Selectors.SELECT_ALL);
            }
            catch (final FileSystemException e)
            {
                final String message = Messages.getString("vfs.impl/delete-temp.warn", file.getName());
                // getLogger().warn(message, e);
View Full Code Here


    {
        final String basename = srcFile.getName().getBaseName();
        final File file = allocateFile(basename);

        // Copy from the source file
        final FileObject destFile = getContext().toFileObject(file);
        destFile.copyFrom(srcFile, selector);

        return file;
    }
View Full Code Here

    /**
     * Tests that folders have no content.
     */
    public void testFolderURL() throws Exception
    {
        final FileObject folder = getReadFolder().resolveFile("dir1");
        if (folder.getFileSystem() instanceof HttpFileSystem)
        {
            // bad hack, but this test might not fail on HttpFileSystem as there are no direcotries.
            // A Directory do have a content on http. e.g a generated directory listing or the index.html page.
            return;
        }

        assertTrue(folder.exists());

        // Try getting the content of a folder
        try
        {
            folder.getURL().openConnection().getInputStream();
            fail();
        }
        catch (final IOException e)
        {
            assertSameMessage("vfs.provider/read-not-file.error", folder, e);
View Full Code Here

    private void addFileObjects(final FileSystemManager manager,
                                final FileObject[] files) throws FileSystemException
    {
        for (int i = 0; i < files.length; i++)
        {
            FileObject file = files[i];
            if (!file.exists())
            {
                // Does not exist - skip
                continue;
            }
View Full Code Here

    protected PermissionCollection getPermissions(final CodeSource cs)
    {
        try
        {
            final String url = cs.getLocation().toString();
            FileObject file = lookupFileObject(url);
            if (file == null)
            {
                return super.getPermissions(cs);
            }

            FileObject parentLayer = file.getFileSystem().getParentLayer();
            if (parentLayer == null)
            {
                return super.getPermissions(cs);
            }
View Full Code Here

    private FileObject lookupFileObject(final String name)
    {
        final Iterator it = resources.iterator();
        while (it.hasNext())
        {
            final FileObject object = (FileObject) it.next();
            if (name.equals(object.getName().getURI()))
            {
                return object;
            }
        }
        return null;
View Full Code Here

    private Resource loadResource(final String name) throws FileSystemException
    {
        final Iterator it = resources.iterator();
        while (it.hasNext())
        {
            final FileObject baseFile = (FileObject) it.next();
            final FileObject file =
                baseFile.resolveFile(name, NameScope.DESCENDENT_OR_SELF);
            if (file.exists())
            {
                return new Resource(name, baseFile, file);
            }
        }
View Full Code Here

    /**
     * Tests file/folder creation with mismatched types.
     */
    public void testFileCreateMismatched() throws Exception
    {
        FileObject scratchFolder = createScratchFolder();

        // Create a test file and folder
        FileObject file = scratchFolder.resolveFile("dir1/file1.txt");
        file.createFile();
        assertEquals(FileType.FILE, file.getType());

        FileObject folder = scratchFolder.resolveFile("dir1/dir2");
        folder.createFolder();
        assertEquals(FileType.FOLDER, folder.getType());

        // Attempt to create a file that already exists as a folder
        try
        {
            folder.createFile();
            fail();
        }
        catch (FileSystemException exc)
        {
        }

        // Attempt to create a folder that already exists as a file
        try
        {
            file.createFolder();
            fail();
        }
        catch (FileSystemException exc)
        {
        }

        // Attempt to create a folder as a child of a file
        FileObject folder2 = file.resolveFile("some-child");
        try
        {
            folder2.createFolder();
            fail();
        }
        catch (FileSystemException exc)
        {
        }
View Full Code Here

     * Tests deletion
     */
    public void testDelete() throws Exception
    {
        // Set-up the test structure
        FileObject folder = createScratchFolder();
        folder.resolveFile("file1.txt").createFile();
        folder.resolveFile("file%25.txt").createFile();
        folder.resolveFile("emptydir").createFolder();
        folder.resolveFile("dir1/file1.txt").createFile();
        folder.resolveFile("dir1/dir2/file2.txt").createFile();

        // Delete a file
        FileObject file = folder.resolveFile("file1.txt");
        assertTrue(file.exists());
        file.delete(Selectors.SELECT_ALL);
        assertTrue(!file.exists());

        // Delete a special name file
        file = folder.resolveFile("file%25.txt");
        assertTrue(file.exists());
        file.delete(Selectors.SELECT_ALL);
        assertTrue(!file.exists());

        // Delete an empty folder
        file = folder.resolveFile("emptydir");
        assertTrue(file.exists());
        file.delete(Selectors.SELECT_ALL);
        assertTrue(!file.exists());

        // Recursive delete
        file = folder.resolveFile("dir1");
        FileObject file2 = file.resolveFile("dir2/file2.txt");
        assertTrue(file.exists());
        assertTrue(file2.exists());
        file.delete(Selectors.SELECT_ALL);
        assertTrue(!file.exists());
        assertTrue(!file2.exists());

        // Delete a file that does not exist
        file = folder.resolveFile("some-folder/some-file");
        assertTrue(!file.exists());
        file.delete(Selectors.SELECT_ALL);
View Full Code Here

     * Tests file copy to and from the same filesystem type.  This was a problem
     * w/ FTP.
     */
    public void testCopySameFileSystem() throws Exception
    {
        final FileObject scratchFolder = createScratchFolder();

        // Create direct child of the test folder
        final FileObject file = scratchFolder.resolveFile("file1.txt");
        assertTrue(!file.exists());

        // Create the source file
        final String content = "Here is some sample content for the file.  Blah Blah Blah.";
        final OutputStream os = file.getContent().getOutputStream();
        try
        {
            os.write(content.getBytes("utf-8"));
        }
        finally
        {
            os.close();
        }

        assertSameContent(content, file);

        // Make sure we can copy the new file to another file on the same filesystem
        FileObject fileCopy = scratchFolder.resolveFile("file1copy.txt");
        assertTrue(!fileCopy.exists());
        fileCopy.copyFrom(file, Selectors.SELECT_SELF);

        assertSameContent(content, fileCopy);
    }
View Full Code Here

TOP

Related Classes of org.apache.commons.vfs.FileObject

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.