|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object de.fhg.igd.util.ClassResource
public class ClassResource
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.
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 |
---|
protected static ClassSource classSource_
protected ClassLoader classLoader_
protected String basePath_
getRelative*()
are addressed relative to this base path.
Constructor Detail |
---|
public ClassResource()
obj
- The reference class instance.public ClassResource(ClassLoader classLoader)
classLoader
- The class loader to load resources.public ClassResource(Object obj)
getRelative*()
resources are addresses relative to the path of the given class
instance in class path.
obj
- The reference class instance.public ClassResource(Class clazz)
getRelative*()
resources are addresses relative to the path of the given class
in class path.
clazz
- The reference class.Method Detail |
---|
public static ClassLoader getClassLoader(Object obj)
obj
- The reference class instance.
obj
.public static ClassLoader getClassLoader(Class clazz)
clazz
- The reference class.
clazz
public ClassLoader getClassLoader()
ClassResource
.
public static String getBasePath(Object obj)
de.fhg.igd.util.ClassResource
instance is de/fhg/igd/util
, for example.
obj
- The reference class instance.
obj
.public static String getBasePath(Class clazz)
de.fhg.igd.util.ClassResource
is de/fhg/igd/util
, for example.
clazz
- The reference class.
clazz
.public String getBasePath()
public static String normalizeResourcePath(String path)
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.
name
- The slash ('/') separatoed resource path.
String
, if the path could not be normalized.public static URL getResource(Object obj, String resourceName)
obj
- The reference class instance.resourceName
- slash('/')-separated path name that identifies
the resource.
null
if the resource could not be found or the invoker doesn't have
adequate privileges to get the resource.java.lang.ClassLoader#getResource}
public static URL getResource(Class clazz, String resourceName)
class
- The reference class.resourceName
- slash('/')-separated path name that identifies
the resource.
null
if the resource could not be found or the invoker doesn't have
adequate privileges to get the resource.java.lang.ClassLoader#getResource}
public URL getResource(String resourceName)
resourceName
- slash('/')-separated path name that identifies
the resource.
null
if the resource could not be found or the invoker doesn't have
adequate privileges to get the resource.java.lang.ClassLoader#getResource}
public static URL getRelativeResource(Object obj, String resourceName)
obj
- The reference class instance.resourceName
- slash('/')-separated path name that identifies
the resource.
null
if the resource could not be found or the invoker doesn't have
adequate privileges to get the resource.java.lang.ClassLoader#getResource}
public static URL getRelativeResource(Class clazz, String resourceName)
clazz
- The reference class.resourceName
- slash('/')-separated path name that identifies
the resource.
null
if the resource could not be found or the invoker doesn't have
adequate privileges to get the resource.java.lang.ClassLoader#getResource}
public URL getRelativeResource(String resourceName)
resourceName
- slash('/')-separated path name that identifies
the resource.
null
if the resource could not be found or the invoker doesn't have
adequate privileges to get the resource.java.lang.ClassLoader#getResource}
public static InputStream getResourceAsStream(Object obj, String resourceName)
obj
- The reference class instance.resourceName
- slash('/')-separated path name that identifies
the resource.
null
if the resource could not be found.#getResource}
public static InputStream getResourceAsStream(Class clazz, String resourceName)
clazz
- The reference class.resourceName
- slash('/')-separated path name that identifies
the resource.
null
if the resource could not be found.#getResource}
public InputStream getResourceAsStream(String resourceName)
resourceName
- slash('/')-separated path name that identifies
the resource.
null
if the resource could not be found.#getResource}
public static InputStream getRelativeResourceAsStream(Object obj, String resourceName)
obj
- The reference class instance.resourceName
- slash('/')-separated path name that identifies
the resource.
null
if the resource could not be found.#getRelativeResource}
public static InputStream getRelativeResourceAsStream(Class clazz, String resourceName)
clazz
- The reference class.resourceName
- slash('/')-separated path name that identifies
the resource.
null
if the resource could not be found.#getRelativeResource}
public InputStream getRelativeResourceAsStream(String resourceName)
resourceName
- slash('/')-separated path name that identifies
the resource.
null
if the resource could not be found.#getRelativeResource}
public static Image getImage(Object obj, String imageName)
obj
- The reference class instance.imageName
- slash('/')-separated path name that identifies
the image.
null
if the image could not be
found or the invoker doesn't have adequate privileges to get the
resource.public static Image getImage(Class clazz, String imageName)
clazz
- The reference class.imageName
- slash('/')-separated path name that identifies
the image.
null
if the image could not be
found or the invoker doesn't have adequate privileges to get the
resource.public Image getImage(String imageName)
imageName
- slash('/')-separated path name that identifies
the image.
null
if the image could not be
found or the invoker doesn't have adequate privileges to get the
resource.public static Image getRelativeImage(Object obj, String imageName)
obj
- The reference class instance.resourceName
- slash('/')-separated path name that identifies
the image.
null
if the image could not be
found or the invoker doesn't have adequate privileges to get the
resource.public static Image getRelativeImage(Class clazz, String imageName)
clazz
- The reference class.resourceName
- slash('/')-separated path name that identifies
the image.
null
if the image could not be
found or the invoker doesn't have adequate privileges to get the
resource.public Image getRelativeImage(String imageName)
resourceName
- slash('/')-separated path name that identifies
the image.
null
if the image could not be
found or the invoker doesn't have adequate privileges to get the
resource.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |