uk.ac.essex.common.io
Class ResourceLoader

java.lang.Object
  extended by uk.ac.essex.common.io.ResourceLoader

public class ResourceLoader
extends java.lang.Object

Contains utility functions to load resource files from the classpath using the default class loader and the one used to load this class. You should have received a copy of Lesser GNU public license with this code. If not please visit this site


Field Summary
protected static java.lang.ClassLoader _myClassLoader
           
protected static java.lang.ClassLoader _systemClassLoader
           
 
Constructor Summary
ResourceLoader()
           
 
Method Summary
static java.io.File[] getFiles(java.io.File file, java.lang.String ending)
          Gets all files in a given directory with the given ending
static java.util.List<java.lang.String> getFilesOfType(java.lang.String resource, java.lang.String regex)
          Load all files in the jar of the given class
static java.util.Properties getProperties(java.lang.String fileName)
          Get a properties file, fileName from the classpath
static java.util.Properties getProperties(java.lang.String fileName, java.lang.ClassLoader classLoader)
          Get a properties file, fileName from the classpath using the supplied class loader.
static java.net.URL getResource(java.lang.String name)
          Get a URL to a file on the classpath.
static java.net.URL getResource(java.lang.String name, java.lang.ClassLoader classLoader)
          Deprecated. Use getResource(java.lang.String, java.lang.ClassLoader, boolean)
static java.net.URL getResource(java.lang.String name, java.lang.ClassLoader classLoader, boolean useSuppliedFirst)
          Get a URL to a file on the classpath.
static java.io.File getResourceAsFile(java.lang.String name)
          Get a file on the classpath as a java.io.File
static java.io.InputStream getResourceAsStream(java.lang.String name)
          Loads a resource from the classpath and returns an InputStream handle to it.
static java.io.InputStream getResourceAsStream(java.lang.String name, java.lang.ClassLoader classLoader)
          Deprecated. Use getResourceAsStream(java.lang.String, java.lang.ClassLoader, boolean)
static java.io.InputStream getResourceAsStream(java.lang.String name, java.lang.ClassLoader classLoader, boolean useSuppliedFirst)
          Loads a resource from the classpath and returns an InputStream handle to it.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_myClassLoader

protected static java.lang.ClassLoader _myClassLoader

_systemClassLoader

protected static java.lang.ClassLoader _systemClassLoader
Constructor Detail

ResourceLoader

public ResourceLoader()
Method Detail

getFiles

public static java.io.File[] getFiles(java.io.File file,
                                      java.lang.String ending)
Gets all files in a given directory with the given ending

Parameters:
file - - The directory to translate them from
ending - - The ending of the files wanted eg/ '.xml', '.txt'
Returns:
File[] - the list of files ending with ending, or null if none exist

getProperties

public static java.util.Properties getProperties(java.lang.String fileName)
Get a properties file, fileName from the classpath

Parameters:
fileName -
Returns:
Properties - The properties loaded from fileName

getProperties

public static java.util.Properties getProperties(java.lang.String fileName,
                                                 java.lang.ClassLoader classLoader)
Get a properties file, fileName from the classpath using the supplied class loader. The supplied classloader is searched first before this classes classloader

Parameters:
fileName - - The file name of the properties file
classLoader - - The class loader whose class path to search
Returns:
Properties - The properties loaded from fileName

getResource

public static java.net.URL getResource(java.lang.String name)
Get a URL to a file on the classpath. The file is searched for on the classpath in this order:
1. This classes class loader ( ResourceLoader.class.getClassLoader() )
2. The system class loader ( ClassLoader.getSystemClassLoader() )
If it is still not found an attempt is made to instantiate a url with the given name.

Parameters:
name - - The file to translate from the classpath
Returns:
URL - The URL of the file, name or null if it cannot be found

getResource

public static java.net.URL getResource(java.lang.String name,
                                       java.lang.ClassLoader classLoader)
Deprecated. Use getResource(java.lang.String, java.lang.ClassLoader, boolean)

Get a URL to a file on the classpath. This will first use this classes classloader and then use the supplied classloader

Parameters:
name - - The file to translate from the classpath
classLoader - - A class loader whose classpath you also want to look on
Returns:
URL - The URL of the file, name

getResource

public static java.net.URL getResource(java.lang.String name,
                                       java.lang.ClassLoader classLoader,
                                       boolean useSuppliedFirst)
Get a URL to a file on the classpath. This will first use this classes classloader and then use the supplied classloader. Depending on the value of useSuppliedFirst determines which classloader classpath is searched first.

Parameters:
name - - The file to translate from the classpath
classLoader - - A class loader whose classpath you also want to look on
useSuppliedFirst - - True to use the supplied classloader before this classes class loader
Returns:
URL - The URL of the file, name

getResourceAsFile

public static java.io.File getResourceAsFile(java.lang.String name)
Get a file on the classpath as a java.io.File

Parameters:
name - - The file to translate from the classpath
Returns:
File - The File that represents the file to translate

getResourceAsStream

public static java.io.InputStream getResourceAsStream(java.lang.String name)
Loads a resource from the classpath and returns an InputStream handle to it.

Parameters:
name - - The resource to load
Returns:
InputStream the resource name as an property stream

getResourceAsStream

public static java.io.InputStream getResourceAsStream(java.lang.String name,
                                                      java.lang.ClassLoader classLoader)
Deprecated. Use getResourceAsStream(java.lang.String, java.lang.ClassLoader, boolean)

Loads a resource from the classpath and returns an InputStream handle to it. This will first use this classes classloader and then use the supplied classloader

Parameters:
name - - The resource to load
Returns:
InputStream the resource name as an property stream

getResourceAsStream

public static java.io.InputStream getResourceAsStream(java.lang.String name,
                                                      java.lang.ClassLoader classLoader,
                                                      boolean useSuppliedFirst)
Loads a resource from the classpath and returns an InputStream handle to it. Depending on the value of useSuppliedFirst determines which classloader classpath is searched first.

Parameters:
name - - The resource to load
classLoader -
useSuppliedFirst - - True to use the supplied classloader before this classes class loader
Returns:
InputStream the resource name as an property stream

getFilesOfType

public static java.util.List<java.lang.String> getFilesOfType(java.lang.String resource,
                                                              java.lang.String regex)
Load all files in the jar of the given class

Parameters:
resource - - The resource in the same jar as the resources you are looking for
regex - - A regular expression to match the file names
Returns:
List - Containing the zip entry names that match the regex in the jar of the given class