|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object anon.util.ResourceLoader
public final class ResourceLoader
This class loads resources from the file system. It allows to specify resource paths like "dir1/dir2/resource" or "dir1/dir2/" relative to the classpath or the current directory. It allows also for going up directories like "dir1/dir2/../resource" that would be translated to "dir1/resource".
Nested Class Summary | |
---|---|
private class |
ResourceLoader.ByteArrayInstantiator
This class is used to get resources as byte arrays. |
private class |
ResourceLoader.FileTypeInstantiator
Does not load or instantiate resources but returns the file type of resources. |
Field Summary | |
---|---|
private static java.lang.String |
DIR_CURRENT
|
private static java.lang.String |
DIR_UP
|
private static boolean |
ms_bTriedToLoadParentResourceFile
|
private static java.lang.String |
ms_classpath
|
private static java.util.Vector |
ms_classpathFiles
|
private static java.lang.Object |
ms_classpathResourceLock
|
private static java.util.Vector |
ms_classpathResourceTypes
|
private static java.io.File |
ms_parentResourceFile
|
private static java.lang.String |
ms_parentResourceFileResourceType
|
private static java.lang.String |
ms_parentResourceFileResourceURL
|
private static int |
READ_BUFFER
|
private static java.lang.String |
RESOURCE_NO_CLASSES_FOUND
|
private static java.lang.Object |
SYNC_RESOURCE
|
private static java.lang.String |
SYSTEM_RESOURCE
|
private static java.lang.String |
SYSTEM_RESOURCE_ENDSIGN
|
static java.lang.String |
SYSTEM_RESOURCE_TYPE_FILE
|
static java.lang.String |
SYSTEM_RESOURCE_TYPE_GENERIC
|
static java.lang.String |
SYSTEM_RESOURCE_TYPE_JAR
|
static java.lang.String |
SYSTEM_RESOURCE_TYPE_ZIP
|
Constructor Summary | |
---|---|
private |
ResourceLoader()
|
Method Summary | |
---|---|
private ResourceLoader.ByteArrayInstantiator |
createByteArrayInstantiator()
Returns a new ByteArrayInstantiator. |
private ResourceLoader.FileTypeInstantiator |
createFileTypeInstantiator()
Returns a new FileTypeInstantiator. |
private static java.lang.String |
formatResourcePath(java.lang.String a_strRelativeResourcePath)
Reformats a given resource name in a way it can be easily interpreted by the resource loader methods. |
private static java.lang.String |
getCurrentResourcePath(java.io.File a_currentFile,
java.io.File a_topDirectory)
Gets the relative resource path for the currently parsed file. |
private static java.lang.String |
getCurrentResourcePath(java.util.zip.ZipEntry a_currentEntry)
Gets the relative resource path for the currently parsed zip entry. |
static java.util.Vector |
getFilesInClassPath()
|
private static java.io.File |
getParentResourceFile()
|
static java.net.URL |
getResourceURL(java.lang.String a_strRelativeResourcePath)
Gets the absolute URL to a requested resource if the resource is found in the class path or in the local directory. |
private static java.net.URL |
getResourceURL(java.lang.String a_strRelativeResourcePath,
java.util.Vector a_resourceFiles,
java.util.Vector a_resourceURLs,
java.util.Vector a_resourceTypes)
Gets the absolute URL to a requested resource if the resource is found in the given resource files. |
static byte[] |
getStreamAsBytes(java.io.InputStream a_iStream)
Reads a java.io.InputStream into a byte array and closes the stream. |
protected static java.io.File |
getSystemResource(java.lang.String a_systemResource)
Returns a given a requested system resource as a file. |
private static boolean |
isResourceInSearchPath(java.lang.String a_strCurrentResourcePath,
java.lang.String a_strResourceSearchPath,
boolean a_bRecursive)
Tests if the currently parsed resource is in the search path. |
static byte[] |
loadResource(java.lang.String a_strRelativeResourcePath)
Loads a resource from the classpath or the current directory. |
static java.io.InputStream |
loadResourceAsStream(java.lang.String a_strRelativeResourcePath)
Loads a resource from the classpath or the current directory. |
static java.io.InputStream |
loadResourceAsStream(java.lang.String a_strRelativeResourcePath,
boolean a_bFileFirst)
|
static java.util.Hashtable |
loadResources(java.lang.String a_strResourceSearchPath,
boolean a_bRecursive)
Loads resources from the classpath or the current directory and instantiates them as byte arrays. |
static java.util.Hashtable |
loadResources(java.lang.String a_strResourceSearchPath,
java.io.File a_directory,
boolean a_bRecursive)
Loads resources from a directory and instantiates them as byte arrays. |
static java.util.Hashtable |
loadResources(java.lang.String a_strResourceSearchPath,
java.io.File a_directory,
IResourceInstantiator a_instantiator,
boolean a_bRecursive)
Loads resources from a directory and instantiates them as objects of a type defined by the resource instantiator. |
protected static void |
loadResources(java.lang.String a_strResourceSearchPath,
java.io.File a_Directory,
IResourceInstantiator a_instantiator,
boolean a_bRecursive,
boolean a_bStopAtFirstResource,
java.util.Hashtable a_loadedResources)
Loads resources from a given directory (simple file, directory or zip file) that are found in the given resource search path and instantiates them as objects of a type defined by the resource instantiator. |
static java.util.Hashtable |
loadResources(java.lang.String a_strResourceSearchPath,
IResourceInstantiator a_instantiator,
boolean a_bRecursive)
Loads resources from the classpath or the current directory and instantiates them as objects of a type defined by the resource instantiator. |
private static void |
loadResourcesFromFile(java.lang.String a_strResourceSearchPath,
java.io.File a_file,
java.io.File a_topDirectory,
IResourceInstantiator a_instantiator,
java.util.Hashtable a_loadedResources,
boolean a_bRecursive,
boolean a_bStopAtFirstResource)
Returns all resources in a directory and the directory itself as Objects if they are resources of the type specified by the ResourceInstantiator. |
private static java.util.Vector |
readFilesFromClasspath(boolean a_bPreventLoop)
Reads all resources from the classpath and stores them as files. |
static java.lang.String |
replaceFileSeparatorsSystemSpecific(java.lang.String a_filename)
Interprets a String as a filename and converts its file separators to system specific file separators. |
private static byte[] |
trimByteArray(byte[] a_trimmedArray,
int a_maxLength,
byte[] a_arrayToAppendTo)
Trims a byte array in a way that all bytes after the given length a_maxLength
are cut off. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final java.lang.String SYSTEM_RESOURCE_TYPE_ZIP
public static final java.lang.String SYSTEM_RESOURCE_TYPE_JAR
public static final java.lang.String SYSTEM_RESOURCE_TYPE_FILE
public static final java.lang.String SYSTEM_RESOURCE_TYPE_GENERIC
private static final java.lang.String SYSTEM_RESOURCE
private static final java.lang.String SYSTEM_RESOURCE_ENDSIGN
private static final java.lang.String DIR_UP
private static final java.lang.String DIR_CURRENT
private static final int READ_BUFFER
private static final java.lang.String RESOURCE_NO_CLASSES_FOUND
private static java.util.Vector ms_classpathFiles
private static java.lang.Object ms_classpathResourceLock
private static java.util.Vector ms_classpathResourceTypes
private static java.io.File ms_parentResourceFile
private static boolean ms_bTriedToLoadParentResourceFile
private static final java.lang.Object SYNC_RESOURCE
private static java.lang.String ms_parentResourceFileResourceURL
private static java.lang.String ms_parentResourceFileResourceType
private static java.lang.String ms_classpath
Constructor Detail |
---|
private ResourceLoader()
Method Detail |
---|
public static java.util.Vector getFilesInClassPath()
public static byte[] getStreamAsBytes(java.io.InputStream a_iStream) throws java.io.IOException
a_iStream
- an InputStream
java.io.IOException
- if an I/O error occurspublic static java.net.URL getResourceURL(java.lang.String a_strRelativeResourcePath)
a_strRelativeResourcePath
- the relative path to a resource
public static java.io.InputStream loadResourceAsStream(java.lang.String a_strRelativeResourcePath)
a_strRelativeResourcePath
- a relative filename for the resource
public static java.io.InputStream loadResourceAsStream(java.lang.String a_strRelativeResourcePath, boolean a_bFileFirst)
public static byte[] loadResource(java.lang.String a_strRelativeResourcePath)
a_strRelativeResourcePath
- a relative filename for the resource
public static java.util.Hashtable loadResources(java.lang.String a_strResourceSearchPath, boolean a_bRecursive)
a_strResourceSearchPath
- a relative filename for the resourcea_bRecursive
- true if (virtual) subdirectories should be searched for resources;
false otherwise (has an effect only for resource paths ending with "/")
public static java.util.Hashtable loadResources(java.lang.String a_strResourceSearchPath, IResourceInstantiator a_instantiator, boolean a_bRecursive)
a_strResourceSearchPath
- a relative filename for the resourcea_instantiator
- an object that instantiates the loaded resourcea_bRecursive
- true if (virtual) subdirectories should be searched for resources;
false otherwise (has an effect only for resource paths ending with "/")
public static java.util.Hashtable loadResources(java.lang.String a_strResourceSearchPath, java.io.File a_directory, boolean a_bRecursive)
a_strResourceSearchPath
- a relative filename for the resourcea_directory
- a simple file, directory or zip filea_bRecursive
- true if (virtual) subdirectories should be searched for resources;
false otherwise (has an effect only for resource paths ending with "/")
public static java.util.Hashtable loadResources(java.lang.String a_strResourceSearchPath, java.io.File a_directory, IResourceInstantiator a_instantiator, boolean a_bRecursive)
a_strResourceSearchPath
- a relative filename for the resourcea_directory
- a simple file, directory or zip filea_instantiator
- an object that instantiates the loaded resourcea_bRecursive
- true if (virtual) subdirectories should be searched for resources;
false otherwise (has an effect only for resource paths ending with "/")
public static java.lang.String replaceFileSeparatorsSystemSpecific(java.lang.String a_filename)
a_filename
- a generic file name
protected static java.io.File getSystemResource(java.lang.String a_systemResource)
java.class.path
). The resource must be specified with by
the following protocol syntax:
a_systemResource
- a system resource a String
protected static void loadResources(java.lang.String a_strResourceSearchPath, java.io.File a_Directory, IResourceInstantiator a_instantiator, boolean a_bRecursive, boolean a_bStopAtFirstResource, java.util.Hashtable a_loadedResources)
a_strResourceSearchPath
- a (virtual) path in the directory to load resources froma_Directory
- a simple file, directory or zip filea_instantiator
- a ResourceInstantiator that is used to instantiate the loaded resourcesa_bRecursive
- true if (virtual) subdirectories should be searched for resources;
false otherwise (has an effect only for resource paths ending with "/")a_bStopAtFirstResource
- true if the search should stop with the first loaded resource;
false otherwisea_loadedResources
- a Hashtable where the loaded and instantiated resources are storedprivate static void loadResourcesFromFile(java.lang.String a_strResourceSearchPath, java.io.File a_file, java.io.File a_topDirectory, IResourceInstantiator a_instantiator, java.util.Hashtable a_loadedResources, boolean a_bRecursive, boolean a_bStopAtFirstResource) throws IResourceInstantiator.ResourceInstantiationException
a_strResourceSearchPath
- the (virtual) path in the file to load resources froma_file
- a resource file or directorya_topDirectory
- the directory where all other files and directories residea_instantiator
- an object that instantiates the loaded resourcea_loadedResources
- a Vector where the loaded and instantiated resources are storeda_bRecursive
- true if subdirectories should be visited; false otherwisea_bStopAtFirstResource
- true if the search should stop with the first loaded resource;
false otherwise
ResourceInstantiator.ResourceInstantiationException
- if the ResourceInstantiator
has become invalid because of too many errors
IResourceInstantiator.ResourceInstantiationException
private static java.net.URL getResourceURL(java.lang.String a_strRelativeResourcePath, java.util.Vector a_resourceFiles, java.util.Vector a_resourceURLs, java.util.Vector a_resourceTypes)
a_strRelativeResourcePath
- the relative path to a resourcea_resourceFiles
- the resource filesa_resourceURLs
- the absolute URLs to the resource filesa_resourceTypes
- the file types of the resource files, either SYSTEM_RESOURCE_TYPE_ZIP
or SYSTEM_RESOURCE_TYPE_FILE
private static java.lang.String getCurrentResourcePath(java.io.File a_currentFile, java.io.File a_topDirectory)
a_currentFile
- the currently parsed filea_topDirectory
- the top directory in that this file resides; the resource path is
given relative to this directory
private static java.io.File getParentResourceFile()
private static java.lang.String getCurrentResourcePath(java.util.zip.ZipEntry a_currentEntry)
a_currentEntry
- the currently parsed zip entry
private static boolean isResourceInSearchPath(java.lang.String a_strCurrentResourcePath, java.lang.String a_strResourceSearchPath, boolean a_bRecursive)
a_strCurrentResourcePath
- the currently parsed resourcea_strResourceSearchPath
- the resource search patha_bRecursive
- true if subdirectories should be visited; false otherwise
private static java.lang.String formatResourcePath(java.lang.String a_strRelativeResourcePath)
a_strRelativeResourcePath
- the relative path to a resource
private static byte[] trimByteArray(byte[] a_trimmedArray, int a_maxLength, byte[] a_arrayToAppendTo)
a_maxLength
are cut off.
Afterwards, a new byte array is constructed with the bytes from the given
a_arrayToAppendTo
and the trimmed array. If a_maxLength
is
smaller than or equal to zero a reference to a_arrayToAppendTo
is returned.
a_trimmedArray
- the byte array to trima_maxLength
- the maximum length of the trimmed byte arraya_arrayToAppendTo
- the array to append the trimmed byte array to
private static java.util.Vector readFilesFromClasspath(boolean a_bPreventLoop)
a_bPreventLoop
- set true in case there may be an endless loop by calling ClassUtil.getClassPath()
private ResourceLoader.ByteArrayInstantiator createByteArrayInstantiator()
private ResourceLoader.FileTypeInstantiator createFileTypeInstantiator()
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |