| java.lang.Object org.andromda.core.common.ResourceUtils
ResourceUtils | public class ResourceUtils (Code) | | Provides utilities for loading resources.
author: Chad Brandon |
Method Summary | |
public static void | deleteDirectory(File directory) Recursively deletes a directory and its contents. | public static ZipFile | getArchive(URL resource) If this resource is an archive file, it will return the resource as an archive. | public static List | getClassPathArchiveContents(URL resource) If the resource represents a classpath archive (i.e. | public static URL | getClassResource(String className) Takes a className as an argument and returns the URL for the class. | public static String | getContents(URL resource) Loads the resource and returns the contents as a String.
Parameters: resource - the name of the resource. | public static String | getContents(Reader resource) Loads the resource and returns the contents as a String.
Parameters: resource - the name of the resource. | public static String | getContents(String resourceName) Loads the file resource and returns the contents as a String.
Parameters: resourceName - the name of the resource. | public static List | getDirectoryContents(URL resource, int levels) If this resource happens to be a directory, it will load the contents of that directory into a
List and return the list of names relative to the given resource (otherwise it will return an empty
List).
Parameters: resource - the resource from which to retrieve the contents Parameters: levels - the number of levels to step down if the resource ends up being a directory (if its an artifact,levels will be ignored). | public static List | getDirectoryContents(URL resource, int levels, boolean includeSubdirectories) If this resource happens to be a directory, it will load the contents of that directory into a
List and return the list of names relative to the given resource (otherwise it will return an empty
List).
Parameters: resource - the resource from which to retrieve the contents Parameters: levels - the number of levels to step down if the resource ends up being a directory (if its an artifact,levels will be ignored). Parameters: includeSubdirectories - whether or not to include subdirectories in the contents. | public static List | getDirectoryContents(URL url, boolean absolute, String[] patterns) Gets the contents of this directory and any of its sub directories based on the given patterns .
And returns absolute or relative paths depending on the value of absolute .
Parameters: url - the URL of the directory. Parameters: absolute - whether or not the returned content paths should be absoluate (iffalse paths will be relative to URL). | public static long | getLastModifiedTime(URL resource) Gets the time as a long when this resource was last modified.
If it can not be determined 0 is returned.
Parameters: resource - the resource from which to retrievethe last modified time. | public static URL | getResource(String resourceName) Retrieves a resource from the current classpath. | public static URL | getResource(String resourceName, String directory)
Retrieves a resource from an optionally given directory or from the package on the classpath. | public static URL | getResource(String resourceName, URL directory)
Retrieves a resource from an optionally given directory or from the package on the classpath.
If the directory is specified and is a valid directory then an attempt at finding the resource by appending the
resourceName to the given directory will be made, otherwise an attempt to find the
resourceName directly on the classpath will be initiated. | public static boolean | isArchive(URL resource) Returns true/false on whether or not this resource represents an archive or not (i.e. | public static boolean | isFile(URL url) Indicates whether or not the given url is a file.
Parameters: url - the URL to check. | public static void | makeDirectories(String location) Makes the directory for the given location if it doesn't exist. | public static boolean | matchesAtLeastOnePattern(String path, String[] patterns) Indicates whether or not the given path matches on
one or more of the patterns defined within this class
returns true if no patterns are defined.
Parameters: path - the path to match on. | public static boolean | modifiedAfter(long time, File directory) Indicates whether or not the contents of the given directory
and any of its sub directories have been modified after the given time . | public static String | normalizePath(String path) Removes any extra path separators and converts all from back slashes
to forward slashes.
Parameters: path - the path to normalize. | public static String | renameExtension(String path, String oldExtension, String newExtension) Takes a path and replaces the oldException with the newExtension.
Parameters: path - the path to rename. Parameters: oldExtension - the extension to rename from. Parameters: newExtension - the extension to rename to. | public static URL | resolveClasspathResource(String path) Resolves a URL to a classpath resource, this method will treat occurrences of the exclamation mark
similar to what
URL does with the jar:file protocol. | public static URL | toURL(String path) Attempts to construct the given path
to a URL instance. | public static String | unescapeFilePath(String filePath) Replaces any escape characters in the given file path with their
counterparts.
Parameters: filePath - the path of the file to unescape. | public static void | writeUrlToFile(URL url, String fileLocation, String encoding) Writes the URL contents to a file specified by the fileLocation argument. |
deleteDirectory | public static void deleteDirectory(File directory)(Code) | | Recursively deletes a directory and its contents.
Parameters: directory - the directory to delete. |
getArchive | public static ZipFile getArchive(URL resource)(Code) | | If this resource is an archive file, it will return the resource as an archive.
the archive as a ZipFile |
getClassPathArchiveContents | public static List getClassPathArchiveContents(URL resource)(Code) | | If the resource represents a classpath archive (i.e. jar, zip, etc), this method will retrieve all
contents from that resource as a List of relative paths (relative to the archive base). Otherwise an empty List
will be returned.
Parameters: resource - the resource from which to retrieve the contents a list of Strings containing the names of every nested resource found in this resource. |
getClassResource | public static URL getClassResource(String className)(Code) | | Takes a className as an argument and returns the URL for the class.
Parameters: className - java.net.URL |
getContents | public static String getContents(URL resource)(Code) | | Loads the resource and returns the contents as a String.
Parameters: resource - the name of the resource. String |
getContents | public static String getContents(Reader resource)(Code) | | Loads the resource and returns the contents as a String.
Parameters: resource - the name of the resource. the contents of the resource as a string. |
getContents | public static String getContents(String resourceName)(Code) | | Loads the file resource and returns the contents as a String.
Parameters: resourceName - the name of the resource. String |
getDirectoryContents | public static List getDirectoryContents(URL resource, int levels)(Code) | | If this resource happens to be a directory, it will load the contents of that directory into a
List and return the list of names relative to the given resource (otherwise it will return an empty
List).
Parameters: resource - the resource from which to retrieve the contents Parameters: levels - the number of levels to step down if the resource ends up being a directory (if its an artifact,levels will be ignored). a list of Strings containing the names of every nested resource found in this resource. |
getDirectoryContents | public static List getDirectoryContents(URL resource, int levels, boolean includeSubdirectories)(Code) | | If this resource happens to be a directory, it will load the contents of that directory into a
List and return the list of names relative to the given resource (otherwise it will return an empty
List).
Parameters: resource - the resource from which to retrieve the contents Parameters: levels - the number of levels to step down if the resource ends up being a directory (if its an artifact,levels will be ignored). Parameters: includeSubdirectories - whether or not to include subdirectories in the contents. a list of Strings containing the names of every nested resource found in this resource. |
getDirectoryContents | public static List getDirectoryContents(URL url, boolean absolute, String[] patterns)(Code) | | Gets the contents of this directory and any of its sub directories based on the given patterns .
And returns absolute or relative paths depending on the value of absolute .
Parameters: url - the URL of the directory. Parameters: absolute - whether or not the returned content paths should be absoluate (iffalse paths will be relative to URL). a collection of paths. |
getLastModifiedTime | public static long getLastModifiedTime(URL resource)(Code) | | Gets the time as a long when this resource was last modified.
If it can not be determined 0 is returned.
Parameters: resource - the resource from which to retrievethe last modified time. the last modified time or 0 if it couldn't be retrieved. |
getResource | public static URL getResource(String resourceName)(Code) | | Retrieves a resource from the current classpath.
Parameters: resourceName - the name of the resource the resource url |
getResource | public static URL getResource(String resourceName, String directory)(Code) | |
Retrieves a resource from an optionally given directory or from the package on the classpath.
If the directory is specified and is a valid directory then an attempt at finding the resource by appending the
resourceName to the given directory will be made, otherwise an attempt to find the
resourceName directly on the classpath will be initiated.
Parameters: resourceName - the name of a resource Parameters: directory - the directory location the resource url |
getResource | public static URL getResource(String resourceName, URL directory)(Code) | |
Retrieves a resource from an optionally given directory or from the package on the classpath.
If the directory is specified and is a valid directory then an attempt at finding the resource by appending the
resourceName to the given directory will be made, otherwise an attempt to find the
resourceName directly on the classpath will be initiated.
Parameters: resourceName - the name of a resource Parameters: directory - the directory location the resource url |
isArchive | public static boolean isArchive(URL resource)(Code) | | Returns true/false on whether or not this resource represents an archive or not (i.e. jar, or zip,
etc).
true if its an archive, false otherwise. |
isFile | public static boolean isFile(URL url)(Code) | | Indicates whether or not the given url is a file.
Parameters: url - the URL to check. true/false |
makeDirectories | public static void makeDirectories(String location)(Code) | | Makes the directory for the given location if it doesn't exist.
Parameters: location - the location to make the directory. |
matchesAtLeastOnePattern | public static boolean matchesAtLeastOnePattern(String path, String[] patterns)(Code) | | Indicates whether or not the given path matches on
one or more of the patterns defined within this class
returns true if no patterns are defined.
Parameters: path - the path to match on. true/false |
modifiedAfter | public static boolean modifiedAfter(long time, File directory)(Code) | | Indicates whether or not the contents of the given directory
and any of its sub directories have been modified after the given time .
Parameters: directory - the directory to check Parameters: time - the time to check against true/false |
normalizePath | public static String normalizePath(String path)(Code) | | Removes any extra path separators and converts all from back slashes
to forward slashes.
Parameters: path - the path to normalize. the normalizd path |
renameExtension | public static String renameExtension(String path, String oldExtension, String newExtension)(Code) | | Takes a path and replaces the oldException with the newExtension.
Parameters: path - the path to rename. Parameters: oldExtension - the extension to rename from. Parameters: newExtension - the extension to rename to. the path with the new extension. |
resolveClasspathResource | public static URL resolveClasspathResource(String path)(Code) | | Resolves a URL to a classpath resource, this method will treat occurrences of the exclamation mark
similar to what
URL does with the jar:file protocol.
Example: my/path/to/some.zip!/file.xml represents a resource file.xml
that is located in a ZIP file on the classpath called my/path/to/some.zip
It is possible to have nested ZIP files, example:
my/path/to/first.zip!/subdir/second.zip!/file.xml .
Please note that the extension of the ZIP file can be anything,
but in the case the extension is .jar the JVM will automatically unpack resources
one level deep and put them all on the classpath
Parameters: path - the name of the resource to resolve to a URL, potentially nested in ZIP archives a URL pointing the resource resolved from the argument pathor null if the argument is null or impossible to resolve |
toURL | public static URL toURL(String path)(Code) | | Attempts to construct the given path
to a URL instance. If the argument cannot be resolved as a resource
on the file system this method will attempt to locate it on the
classpath.
Parameters: path - the path from which to construct the URL. the constructed URL or null if one couldn't be constructed. |
unescapeFilePath | public static String unescapeFilePath(String filePath)(Code) | | Replaces any escape characters in the given file path with their
counterparts.
Parameters: filePath - the path of the file to unescape. the unescaped path. |
writeUrlToFile | public static void writeUrlToFile(URL url, String fileLocation, String encoding) throws IOException(Code) | | Writes the URL contents to a file specified by the fileLocation argument.
Parameters: url - the URL to read Parameters: fileLocation - the location which to write. Parameters: encoding - the optional encoding |
|
|