org.xerial.util
Class FileResource

java.lang.Object
  extended by org.xerial.util.FileResource

public class FileResource
extends Object

Gets file URLs by using the ClassLoader facility. This utility can be used to hold file resources within a package. Usage:

 // if YourClass belongs to a package org.xerial.util, the following code searches org/xerial/util/sample.txt in the source folder (or jar file) 
 URL fileResourceURL = FileResource.find(YourClass.class, "sample.txt");
 
 // another form of the previous line
 URL fileResourceURL2 = FileResource.find("org.xerial.util", "sample.txt");
 
 // short-hand code to get Reader (equivalent to call new BufferedReader(new InputStreamReader(URL#openStream())) )
 BufferedReader reader = FileResource.open(YourClass.class, "sample.txt");
 

Author:
leo

Nested Class Summary
static class FileResource.FileInJarArchive
          A virtual file implementation for file resources contained in a JAR file
static class FileResource.SystemFile
          A virtual file implementation for usual files
 
Constructor Summary
FileResource()
           
 
Method Summary
static URL find(Class referenceClass, String resourceFileName)
          Gets the URL of the file resource
static URL find(Package basePackage, String resourceFileName)
          Finds the URL of the resource
static URL find(String resourceFileName)
          Finds the URL of the resource
static URL find(String packageName, String resourceFileName)
          Finds the URL of the resource
static URL findFromJAR(String jarPath, String filePath)
           
static ArrayList<URLClassLoader> getAllAncestorOrSelfClassLoaders(ClassLoader classLoader)
          Retrieves all URLClassLoaders that can be found from this class
static List<VirtualFile> listResources(ClassLoader classLoader, ResourceFilter resourceFilter)
          Lists up all resources satisfying the given resourceFilter from the given current class loader
static List<VirtualFile> listResources(ClassLoader classLoader, String basePackageName, ResourceFilter resourceFilter)
          list up resources recursively under the given base package name
static List<VirtualFile> listResources(ResourceFilter resourceFilter)
          Lists up all resources satisfying the given resourceFilter from the current class loader
static List<VirtualFile> listResources(String basePackageName)
          list up resources recursively under the given base package name
static List<VirtualFile> listResources(String basePackageName, ResourceFilter resourceFilter)
          list up resources recursively under the given base package name
static List<VirtualFile> listResources(URL resourceURL, String packagePath, ResourceFilter resourceFilter)
          Lists up all resources recursively under the given resourceURL.
static BufferedReader open(Class referenceClass, String resouceFileName)
          Opens a stream to a file resource that belongs to the same package to the specified referenceClass
static BufferedReader open(Package basePackage, String resourceFileName)
          Opens a stream to a file resource that belongs to the specified package
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileResource

public FileResource()
Method Detail

listResources

public static List<VirtualFile> listResources(String basePackageName)
list up resources recursively under the given base package name

Parameters:
basePackageName - package name to search. e.g. org.xerial.util
Returns:
the list of resources represented as VirtualFile

listResources

public static List<VirtualFile> listResources(ResourceFilter resourceFilter)
Lists up all resources satisfying the given resourceFilter from the current class loader

Parameters:
resourceFilter -
Returns:
the list of resources matching the condition specified in the resourceFilter

listResources

public static List<VirtualFile> listResources(ClassLoader classLoader,
                                              ResourceFilter resourceFilter)
Lists up all resources satisfying the given resourceFilter from the given current class loader

Parameters:
resourceFilter -
classLoader -
Returns:
the list of resources matching the condition specified in the resourceFilter

listResources

public static List<VirtualFile> listResources(URL resourceURL,
                                              String packagePath,
                                              ResourceFilter resourceFilter)
Lists up all resources recursively under the given resourceURL. If the resourceURL is file, this method searches its sub directories, when it is Jar file, it searches contents of the Jar file for other resources.

Parameters:
resourceURL -
resourceFilter -
Returns:
the list of resources matching the given resource filter

listResources

public static List<VirtualFile> listResources(String basePackageName,
                                              ResourceFilter resourceFilter)
list up resources recursively under the given base package name

Parameters:
basePackageName - package name to search. e.g. org.xerial.util
Returns:
the list of resources represented as VirtualFile

listResources

public static List<VirtualFile> listResources(ClassLoader classLoader,
                                              String basePackageName,
                                              ResourceFilter resourceFilter)
list up resources recursively under the given base package name

Parameters:
classLoader -
basePackageName - package name to search. e.g. org.xerial.util
Returns:
the list of resources represented as VirtualFile

getAllAncestorOrSelfClassLoaders

public static ArrayList<URLClassLoader> getAllAncestorOrSelfClassLoaders(ClassLoader classLoader)
Retrieves all URLClassLoaders that can be found from this class

Returns:
the list of URLClassLoaders

open

public static BufferedReader open(Class referenceClass,
                                  String resouceFileName)
                           throws IOException
Opens a stream to a file resource that belongs to the same package to the specified referenceClass

Parameters:
referenceClass - the reference class
resouceFileName - the file name of the resource, relative to the package of the referenceClass
Returns:
BufferedReader of the resource
Throws:
IOException - when failed to open the file resource

open

public static BufferedReader open(Package basePackage,
                                  String resourceFileName)
                           throws IOException
Opens a stream to a file resource that belongs to the specified package

Parameters:
basePackage - the base package to load resources
resouceFileName - the file name of the resource, relative to the package
Returns:
BufferedReader of the resource
Throws:
IOException - when failed to open the file resource

find

public static URL find(Class referenceClass,
                       String resourceFileName)
Gets the URL of the file resource

Parameters:
referenceClass - the base class to find resources
resourceFileName - the resource file name relative to the package of the referenceClass
Returns:
the URL of the file resource

find

public static URL find(Package basePackage,
                       String resourceFileName)
Finds the URL of the resource

Parameters:
basePackage - the base package to find the resource
resourceFileName - the resource file name relative to the package folder
Returns:
the URL of the specified resource

find

public static URL find(String packageName,
                       String resourceFileName)
Finds the URL of the resource

Parameters:
packageName - the base package name to find the resource
resourceFileName - the resource file name relative to the package folder
Returns:
the URL of the specified resource

findFromJAR

public static URL findFromJAR(String jarPath,
                              String filePath)

find

public static URL find(String resourceFileName)
Finds the URL of the resource

Parameters:
resourceFileName - the resource file name, relative to the root package
Returns:
the URL of the specified resource


Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 2.1 Japan License.