de.fhg.igd.util
Class ClassResource

java.lang.Object
  extended by de.fhg.igd.util.ClassResource

public class ClassResource
extends Object

This class provides static helper methods to load resources bound to specific caller classes or given class loaders. A resource is some data (images, audio, text, etc) that can be accessed by class code in a way that is independent of the location of the code. Thereby, the class loader of the caller class is used, which enables to load resources from a caller class JAR file, for example. When methods of this class are used the resourceName has to be given with slashes ('/') to separarte absolute or relative path elements. This is necessary, since the resources are referenced internally by using URLs. Since the resource could reside inside a JAR file, the resourceName is automatically by removing references to local and parent directories ('.', '..') from it as well as elements with length 0.

Version:
"$Id: ClassResource.java 1913 2007-08-08 02:41:53Z jpeters $"
Author:
Jan Peters

Field Summary
protected  String basePath_
          The base path of the caller package.
protected  ClassLoader classLoader_
          The class loader used to find and load resources.
protected static ClassSource classSource_
           
 
Constructor Summary
ClassResource()
          Default constructor of this class.
ClassResource(Class clazz)
          Constructor of this class.
ClassResource(ClassLoader classLoader)
          Constructor of this class.
ClassResource(Object obj)
          Constructor of this class.
 
Method Summary
 String getBasePath()
          Returns the base path which is internally used to address relative resources.
static String getBasePath(Class clazz)
          Returns the base path of the given class within the class path.
static String getBasePath(Object obj)
          Returns the base path of the given class instance within the class path.
 ClassLoader getClassLoader()
          Returns the class loader which is internally used to load resources by this instance of ClassResource.
static ClassLoader getClassLoader(Class clazz)
          Returns the class loader of the given class.
static ClassLoader getClassLoader(Object obj)
          Returns the class loader of the given class instance.
static Image getImage(Class clazz, String imageName)
          Returns the image with the given name using the class loader of the given reference class.
static Image getImage(Object obj, String imageName)
          Returns the image with the given name using the class loader of the given reference class instance.
 Image getImage(String imageName)
          Returns the image with the given name.
static Image getRelativeImage(Class clazz, String imageName)
          Returns the image with the given name using the class loader of the given reference class.
static Image getRelativeImage(Object obj, String imageName)
          Returns the image with the given name using the class loader of the given reference class instance.
 Image getRelativeImage(String imageName)
          Returns the image with the given name.
static URL getRelativeResource(Class clazz, String resourceName)
          Finds the resource with the given name using the class loader of the given reference class.
static URL getRelativeResource(Object obj, String resourceName)
          Finds the resource with the given name using the class loader of the given reference class instance.
 URL getRelativeResource(String resourceName)
          Finds the resource with the given name relative to the initialized base path (see constructors of the class).
static InputStream getRelativeResourceAsStream(Class clazz, String resourceName)
          Returns an input stream for reading the specified resource.
static InputStream getRelativeResourceAsStream(Object obj, String resourceName)
          Returns an input stream for reading the specified resource.
 InputStream getRelativeResourceAsStream(String resourceName)
          Returns an input stream for reading the specified resource.
static URL getResource(Class clazz, String resourceName)
          Finds the resource with the given name using the class loader of the given reference class.
static URL getResource(Object obj, String resourceName)
          Finds the resource with the given name using the class loader of the given reference class instance.
 URL getResource(String resourceName)
          Finds the resource with the given name.
static InputStream getResourceAsStream(Class clazz, String resourceName)
          Returns an input stream for reading the specified resource.
static InputStream getResourceAsStream(Object obj, String resourceName)
          Returns an input stream for reading the specified resource.
 InputStream getResourceAsStream(String resourceName)
          Returns an input stream for reading the specified resource.
static String normalizeResourcePath(String path)
          Sanitizes the given resource path by removing references to local and parent directories ('.', '..') from it as well as elements with length 0.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

classSource_

protected static ClassSource classSource_

classLoader_

protected ClassLoader classLoader_
The class loader used to find and load resources.


basePath_

protected String basePath_
The base path of the caller package. Resources given in methods getRelative*() are addressed relative to this base path.

Constructor Detail

ClassResource

public ClassResource()
Default constructor of this class. The system class loader is subsequently used to load resources. Further, no base path is appended when loading relative resources.

Parameters:
obj - The reference class instance.

ClassResource

public ClassResource(ClassLoader classLoader)
Constructor of this class. The given class loader is subsequently used to load resources. Further, no base path is appended when loading relative resources.

Parameters:
classLoader - The class loader to load resources.

ClassResource

public ClassResource(Object obj)
Constructor of this class. The class loader of the reference class instance is subsequently used to load resources. When using the methods getRelative*() resources are addresses relative to the path of the given class instance in class path.

Parameters:
obj - The reference class instance.

ClassResource

public ClassResource(Class clazz)
Constructor of this class. The class loader of the reference class is subsequently used to load resources. When using the methods getRelative*() resources are addresses relative to the path of the given class in class path.

Parameters:
clazz - The reference class.
Method Detail

getClassLoader

public static ClassLoader getClassLoader(Object obj)
Returns the class loader of the given class instance.

Parameters:
obj - The reference class instance.
Returns:
the class loader of obj.

getClassLoader

public static ClassLoader getClassLoader(Class clazz)
Returns the class loader of the given class.

Parameters:
clazz - The reference class.
Returns:
the class loader of clazz

getClassLoader

public ClassLoader getClassLoader()
Returns the class loader which is internally used to load resources by this instance of ClassResource.

Returns:
the internal class loader.

getBasePath

public static String getBasePath(Object obj)
Returns the base path of the given class instance within the class path. The base path of an de.fhg.igd.util.ClassResource instance is de/fhg/igd/util, for example.

Parameters:
obj - The reference class instance.
Returns:
the base path of obj.

getBasePath

public static String getBasePath(Class clazz)
Returns the base path of the given class within the class path. The base path of class de.fhg.igd.util.ClassResource is de/fhg/igd/util, for example.

Parameters:
clazz - The reference class.
Returns:
the base path of clazz.

getBasePath

public String getBasePath()
Returns the base path which is internally used to address relative resources.

Returns:
the internal base path.

normalizeResourcePath

public static String normalizeResourcePath(String path)
Sanitizes the given resource path by removing references to local and parent directories ('.', '..') from it as well as elements with length 0. If the given resource path refers to a directory above the start of the path, e.g. "foo/.."., or the given path is null, an empty String is returned.

Parameters:
name - The slash ('/') separatoed resource path.
Returns:
The normalized resource path, or an empty String, if the path could not be normalized.

getResource

public static URL getResource(Object obj,
                              String resourceName)
Finds the resource with the given name using the class loader of the given reference class instance.

Parameters:
obj - The reference class instance.
resourceName - slash('/')-separated path name that identifies the resource.
Returns:
A URL object for reading the resource, or null if the resource could not be found or the invoker doesn't have adequate privileges to get the resource.
See Also:
java.lang.ClassLoader#getResource}

getResource

public static URL getResource(Class clazz,
                              String resourceName)
Finds the resource with the given name using the class loader of the given reference class.

Parameters:
class - The reference class.
resourceName - slash('/')-separated path name that identifies the resource.
Returns:
A URL object for reading the resource, or null if the resource could not be found or the invoker doesn't have adequate privileges to get the resource.
See Also:
java.lang.ClassLoader#getResource}

getResource

public URL getResource(String resourceName)
Finds the resource with the given name.

Parameters:
resourceName - slash('/')-separated path name that identifies the resource.
Returns:
A URL object for reading the resource, or null if the resource could not be found or the invoker doesn't have adequate privileges to get the resource.
See Also:
java.lang.ClassLoader#getResource}

getRelativeResource

public static URL getRelativeResource(Object obj,
                                      String resourceName)
Finds the resource with the given name using the class loader of the given reference class instance. The resource is addresses relative to the base path of the given class instance.

Parameters:
obj - The reference class instance.
resourceName - slash('/')-separated path name that identifies the resource.
Returns:
A URL object for reading the resource, or null if the resource could not be found or the invoker doesn't have adequate privileges to get the resource.
See Also:
java.lang.ClassLoader#getResource}

getRelativeResource

public static URL getRelativeResource(Class clazz,
                                      String resourceName)
Finds the resource with the given name using the class loader of the given reference class. The resource is addresses relative to the base path of the given class.

Parameters:
clazz - The reference class.
resourceName - slash('/')-separated path name that identifies the resource.
Returns:
A URL object for reading the resource, or null if the resource could not be found or the invoker doesn't have adequate privileges to get the resource.
See Also:
java.lang.ClassLoader#getResource}

getRelativeResource

public URL getRelativeResource(String resourceName)
Finds the resource with the given name relative to the initialized base path (see constructors of the class).

Parameters:
resourceName - slash('/')-separated path name that identifies the resource.
Returns:
A URL object for reading the resource, or null if the resource could not be found or the invoker doesn't have adequate privileges to get the resource.
See Also:
java.lang.ClassLoader#getResource}

getResourceAsStream

public static InputStream getResourceAsStream(Object obj,
                                              String resourceName)
Returns an input stream for reading the specified resource. The class loader of the given reference class instance is used.

Parameters:
obj - The reference class instance.
resourceName - slash('/')-separated path name that identifies the resource.
Returns:
An input stream for reading the resource, or null if the resource could not be found.
See Also:
#getResource}

getResourceAsStream

public static InputStream getResourceAsStream(Class clazz,
                                              String resourceName)
Returns an input stream for reading the specified resource. The class loader of the given reference class is used.

Parameters:
clazz - The reference class.
resourceName - slash('/')-separated path name that identifies the resource.
Returns:
An input stream for reading the resource, or null if the resource could not be found.
See Also:
#getResource}

getResourceAsStream

public InputStream getResourceAsStream(String resourceName)
Returns an input stream for reading the specified resource.

Parameters:
resourceName - slash('/')-separated path name that identifies the resource.
Returns:
An input stream for reading the resource, or null if the resource could not be found.
See Also:
#getResource}

getRelativeResourceAsStream

public static InputStream getRelativeResourceAsStream(Object obj,
                                                      String resourceName)
Returns an input stream for reading the specified resource. The class loader of the given reference class instance is used. The resource is addresses relative to the base path of the given class instance.

Parameters:
obj - The reference class instance.
resourceName - slash('/')-separated path name that identifies the resource.
Returns:
An input stream for reading the resource, or null if the resource could not be found.
See Also:
#getRelativeResource}

getRelativeResourceAsStream

public static InputStream getRelativeResourceAsStream(Class clazz,
                                                      String resourceName)
Returns an input stream for reading the specified resource. The class loader of the given reference class is used. The resource is addresses relative to the base path of the given class.

Parameters:
clazz - The reference class.
resourceName - slash('/')-separated path name that identifies the resource.
Returns:
An input stream for reading the resource, or null if the resource could not be found.
See Also:
#getRelativeResource}

getRelativeResourceAsStream

public InputStream getRelativeResourceAsStream(String resourceName)
Returns an input stream for reading the specified resource. The resource is addresses relative to the initialized base path (see constructors of the class).

Parameters:
resourceName - slash('/')-separated path name that identifies the resource.
Returns:
An input stream for reading the resource, or null if the resource could not be found.
See Also:
#getRelativeResource}

getImage

public static Image getImage(Object obj,
                             String imageName)
Returns the image with the given name using the class loader of the given reference class instance.

Parameters:
obj - The reference class instance.
imageName - slash('/')-separated path name that identifies the image.
Returns:
The image, or null if the image could not be found or the invoker doesn't have adequate privileges to get the resource.

getImage

public static Image getImage(Class clazz,
                             String imageName)
Returns the image with the given name using the class loader of the given reference class.

Parameters:
clazz - The reference class.
imageName - slash('/')-separated path name that identifies the image.
Returns:
The image, or null if the image could not be found or the invoker doesn't have adequate privileges to get the resource.

getImage

public Image getImage(String imageName)
Returns the image with the given name.

Parameters:
imageName - slash('/')-separated path name that identifies the image.
Returns:
The image, or null if the image could not be found or the invoker doesn't have adequate privileges to get the resource.

getRelativeImage

public static Image getRelativeImage(Object obj,
                                     String imageName)
Returns the image with the given name using the class loader of the given reference class instance. The image is addresses relative to the base path of the given class instance.

Parameters:
obj - The reference class instance.
resourceName - slash('/')-separated path name that identifies the image.
Returns:
The image, or null if the image could not be found or the invoker doesn't have adequate privileges to get the resource.

getRelativeImage

public static Image getRelativeImage(Class clazz,
                                     String imageName)
Returns the image with the given name using the class loader of the given reference class. The image is addresses relative to the base path of the given class.

Parameters:
clazz - The reference class.
resourceName - slash('/')-separated path name that identifies the image.
Returns:
The image, or null if the image could not be found or the invoker doesn't have adequate privileges to get the resource.

getRelativeImage

public Image getRelativeImage(String imageName)
Returns the image with the given name. The image is addresses relative to the internal base path (see class constructors).

Parameters:
resourceName - slash('/')-separated path name that identifies the image.
Returns:
The image, or null if the image could not be found or the invoker doesn't have adequate privileges to get the resource.


Copyright © Fraunhofer Gesellschaft. All Rights Reserved.