Java Code Examples for org.apache.commons.vfs2.FileSystemManager#toFileObject()

The following examples show how to use org.apache.commons.vfs2.FileSystemManager#toFileObject() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FileSystemManagerFactoryTestCase.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
/**
 * Sanity test.
 */
public void testDefaultInstance() throws Exception {
    // Locate the default manager
    final FileSystemManager manager = VFS.getManager();

    // Lookup a test jar file
    final File jarFile = getTestResource("test.jar");
    // File
    final FileObject file = manager.toFileObject(jarFile);
    check(manager, file);
    // URI
    final FileObject file2 = manager.resolveFile(jarFile.toURI());
    check(manager, file2);
    // URL
    final FileObject file3 = manager.resolveFile(jarFile.toURI().toURL());
    check(manager, file3);
}
 
Example 2
Source File: DirectoryFileFilterExample.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
public static void main(final String[] args) throws Exception {

        // Example, how to print out a list of the current directory's
        // subdirectories
        final FileSystemManager fsManager = VFS.getManager();
        final FileObject dir = fsManager.toFileObject(new File("."));
        final FileObject[] files = dir.findFiles(new FileFilterSelector(DirectoryFileFilter.DIRECTORY));
        for (FileObject file : files) {
            System.out.println(file);
        }
    }
 
Example 3
Source File: RegexFileFilterExample.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
public static void main(final String[] args) throws Exception {

        // Example, to retrieve and print all java files where the name matched
        // the regular expression in the current directory
        final FileSystemManager fsManager = VFS.getManager();
        final FileObject dir = fsManager.toFileObject(new File("."));
        final FileObject[] files = dir.findFiles(new FileFilterSelector(new RegexFileFilter(
                "ˆ.*[tT]est(-\\d+)?\\.java$")));
        for (FileObject file : files) {
            System.out.println(file);
        }

    }
 
Example 4
Source File: SizeFileFilterExample.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
public static void main(final String[] args) throws Exception {

        // Example, to print all files and directories in the current directory
        // whose size is greater than 1 MB
        final FileSystemManager fsManager = VFS.getManager();
        final FileObject dir = fsManager.toFileObject(new File("."));
        final SizeFileFilter filter = new SizeFileFilter(1024 * 1024);
        final FileObject[] files = dir.findFiles(new FileFilterSelector(filter));
        for (FileObject file : files) {
            System.out.println(file);
        }

    }
 
Example 5
Source File: WildcardFileFilterExample.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
public static void main(final String[] args) throws Exception {

        // Example, to retrieve and print all java files that have the
        // expression test in the name in the current directory
        final FileSystemManager fsManager = VFS.getManager();
        final FileObject dir = fsManager.toFileObject(new File("."));
        final FileObject[] files = dir.findFiles(new FileFilterSelector(new WildcardFileFilter(
                "*test*.java")));
        for (FileObject file : files) {
            System.out.println(file);
        }

    }
 
Example 6
Source File: FileFileFilterExample.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
public static void main(final String[] args) throws Exception {

        // Example, how to print out a list of the real files within the current
        // directory
        final FileSystemManager fsManager = VFS.getManager();
        final FileObject dir = fsManager.toFileObject(new File("."));
        final FileObject[] files = dir.findFiles(new FileFilterSelector(FileFileFilter.FILE));
        for (FileObject file : files) {
            System.out.println(file);
        }
    }
 
Example 7
Source File: PrefixFileFilterExample.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
public static void main(final String[] args) throws Exception {

        // Example, to print all files and directories in the current directory
        // whose name starts with a {@code .}
        final FileSystemManager fsManager = VFS.getManager();
        final FileObject dir = fsManager.toFileObject(new File("."));
        final FileObject[] files = dir.findFiles(new FileFilterSelector(new PrefixFileFilter(".")));
        for (FileObject file : files) {
            System.out.println(file);
        }

    }
 
Example 8
Source File: SuffixFileFilterExample.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
public static void main(final String[] args) throws Exception {

        // Example, to retrieve and print all *.java files in the current
        // directory
        final FileSystemManager fsManager = VFS.getManager();
        final FileObject dir = fsManager.toFileObject(new File("."));
        final FileObject[] files = dir.findFiles(new FileFilterSelector(new SuffixFileFilter(".java")));
        for (FileObject file : files) {
            System.out.println(file);
        }

    }
 
Example 9
Source File: AgeFileFilterExample.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
public static void main(final String[] args) throws Exception {

        final FileSystemManager fsManager = VFS.getManager();
        final FileObject dir = fsManager.toFileObject(new File("."));

        // We are interested in files older than one day
        final long cutoff = System.currentTimeMillis() - (24 * 60 * 60 * 1000);
        final AgeFileFilter filter = new AgeFileFilter(cutoff);

        final FileObject[] files = dir.findFiles(new FileFilterSelector(filter));
        for (FileObject file : files) {
            System.out.println(file);
        }

    }
 
Example 10
Source File: NameFileFilterExample.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
public static void main(final String[] args) throws Exception {

        // Example, to print all files and directories in the current directory
        // whose name is Test
        final FileSystemManager fsManager = VFS.getManager();
        final FileObject dir = fsManager.toFileObject(new File("."));
        final FileObject[] files = dir.findFiles(new FileFilterSelector(new NameFileFilter("Test")));
        for (FileObject file : files) {
            System.out.println(file);
        }

    }
 
Example 11
Source File: VfsClassLoaderTests.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Tests retrieving resources (from local directory with .jar extension).
 * <p>
 * This test is repeated with various provider configurations but works on local files, only.
 * </p>
 */
public void testGetResourcesNoLayerLocal() throws Exception {
    final FileSystemManager manager = getManager();
    try {
        // hasProvider("file") cannot be used as it triggers default provider URL
        manager.toFileObject(new File("."));
    } catch (final FileSystemException e) {
        System.out.println("VfsClassLoaderTests no local file provider, skipping.");
        return;
    }
    final File baseDir = AbstractVfsTestCase.getTestDirectoryFile();

    // setup test folder
    final FileObject dir = manager.resolveFile(baseDir, "read-tests/dir1/subdir4.jar");
    assertSame("subdir4.jar/ is required for testing " + dir, dir.getType(), FileType.FOLDER);
    assertFalse(manager.canCreateFileSystem(dir));

    // prepare classloader
    final FileObject[] search = new FileObject[] { dir };
    final ClassLoader mockClassloader = new MockClassloader();
    final VFSClassLoader loader = new VFSClassLoader(search, getManager(), mockClassloader);

    // verify resource loading
    final Enumeration<URL> urls = loader.getResources("file1.txt");
    final URL url1 = urls.nextElement();
    assertFalse("Only one hit expected", urls.hasMoreElements());
    assertTrue("not pointing to resource " + url1, url1.toString().endsWith("subdir4.jar/file1.txt"));
}
 
Example 12
Source File: VirtualProviderTestCase.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the base folder for tests.
 */
@Override
public FileObject getBaseTestFolder(final FileSystemManager manager) throws Exception {
    final File baseDir = AbstractVfsTestCase.getTestDirectoryFile();
    final FileObject baseFile = manager.toFileObject(baseDir);
    return manager.createVirtualFileSystem(baseFile);
}
 
Example 13
Source File: VfsClassLoaderTests.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
/**
 * Tests retrieving resources (from JAR searchpath).
 * <p>
 * This is run for all providers, but only when a local provider is present and jar extension is registered it will
 * actually carry out all tests.
 * </p>
 */
public void testGetResourcesJARs() throws Exception {
    final FileSystemManager manager = getManager();
    try {
        // hasProvider("file") cannot be used as it triggers default provider URL
        manager.toFileObject(new File("."));
    } catch (final FileSystemException e) {
        System.out.println("VfsClassLoaderTests no local file provider, skipping.");
        return;
    }

    // build search path without using #getBaseFolder()
    // because NestedJarTestCase redefines it
    final File baseDir = AbstractVfsTestCase.getTestDirectoryFile();
    final FileObject nestedJar = manager.resolveFile(baseDir, "nested.jar");
    final FileObject testJar = manager.resolveFile(baseDir, "test.jar");

    // test setup needs to know about .jar extension - i.e. NestedJarTestCase
    if (!manager.canCreateFileSystem(nestedJar)) {
        System.out.println("VfsClassLoaderTests no layered .jar provider, skipping.");
        return;
    }

    // verify test setup
    assertSame("nested.jar is required for testing", nestedJar.getType(), FileType.FILE);
    assertSame("test.jar is required for testing", testJar.getType(), FileType.FILE);

    // System class loader (null) might be unpredictable in regards
    // to returning resources for META-INF/MANIFEST.MF (see VFS-500)
    // so we use our own which is guaranteed to not return any hit
    final ClassLoader mockClassloader = new MockClassloader();
    final FileObject[] search = new FileObject[] { nestedJar, testJar };
    final VFSClassLoader loader = new VFSClassLoader(search, getManager(), mockClassloader);

    final Enumeration<URL> urls = loader.getResources("META-INF/MANIFEST.MF");
    final URL url1 = urls.nextElement();
    final URL url2 = urls.nextElement();

    assertTrue("First resource must refer to nested.jar but was " + url1,
            url1.toString().endsWith("nested.jar!/META-INF/MANIFEST.MF"));
    assertTrue("Second resource must refer to test.jar but was " + url2,
            url2.toString().endsWith("test.jar!/META-INF/MANIFEST.MF"));
}
 
Example 14
Source File: LocalProviderTestCase.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the base folder for tests.
 */
@Override
public FileObject getBaseTestFolder(final FileSystemManager manager) throws Exception {
    final File testDir = AbstractVfsTestCase.getTestDirectoryFile();
    return manager.toFileObject(testDir);
}
 
Example 15
Source File: DefaultFilesCacheTestCase.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
@Override
public FileObject getBaseTestFolder(final FileSystemManager manager) throws Exception {
    final File testDir = AbstractVfsTestCase.getTestDirectoryFile();
    return manager.toFileObject(testDir);
}
 
Example 16
Source File: WeakRefFilesCacheTestCase.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
@Override
public FileObject getBaseTestFolder(final FileSystemManager manager) throws Exception {
    final File testDir = AbstractVfsTestCase.getTestDirectoryFile();
    return manager.toFileObject(testDir);
}
 
Example 17
Source File: SoftRefFilesCacheTestCase.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
@Override
public FileObject getBaseTestFolder(final FileSystemManager manager) throws Exception {
    final File testDir = AbstractVfsTestCase.getTestDirectoryFile();
    return manager.toFileObject(testDir);
}
 
Example 18
Source File: LRUFilesCacheTestCase.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
@Override
public FileObject getBaseTestFolder(final FileSystemManager manager) throws Exception {
    final File testDir = AbstractVfsTestCase.getTestDirectoryFile();
    return manager.toFileObject(testDir);
}
 
Example 19
Source File: NullFilesCacheTestCase.java    From commons-vfs with Apache License 2.0 4 votes vote down vote up
@Override
public FileObject getBaseTestFolder(final FileSystemManager manager) throws Exception {
    final File testDir = AbstractVfsTestCase.getTestDirectoryFile();
    return manager.toFileObject(testDir);
}
 
Example 20
Source File: SchemaResolver.java    From pentaho-reporting with GNU Lesser General Public License v2.1 4 votes vote down vote up
public static String resolveSchema( final ResourceManager resourceManager,
                                    final ResourceKey contextKey,
                                    String catalogUrl )
  throws FileSystemException {
  final FileSystemManager fsManager = VFS.getManager();
  if ( fsManager == null ) {
    throw Util.newError( "Cannot get virtual file system manager" );
  }

  // Workaround VFS bug.
  if ( catalogUrl.startsWith( "file://localhost" ) ) {
    catalogUrl = catalogUrl.substring( "file://localhost".length() );
  }
  if ( catalogUrl.startsWith( "file:" ) ) {
    catalogUrl = catalogUrl.substring( "file:".length() );
  }

  try {
    final File catalogFile = new File( catalogUrl ).getCanonicalFile();
    final FileObject file = fsManager.toFileObject( catalogFile );
    if ( file.isReadable() ) {
      return catalogFile.getPath();
    }
  } catch ( FileSystemException fse ) {
    logger.info( "Failed to resolve schema file '" + catalogUrl + "' as local file. Treating file as non-readable.",
      fse );
  } catch ( IOException e ) {
    logger
      .info( "Failed to resolve schema file '" + catalogUrl + "' as local file. Treating file as non-readable.", e );
  }

  if ( contextKey == null ) {
    return catalogUrl;
  }

  final File contextAsFile = getContextAsFile( contextKey );
  if ( contextAsFile == null ) {
    return catalogUrl;
  }

  final File resolvedFile = new File( contextAsFile.getParentFile(), catalogUrl );
  if ( resolvedFile.isFile() && resolvedFile.canRead() ) {
    return resolvedFile.getAbsolutePath();
  }

  return catalogUrl;
}