public class ResourceUtils extends Object
Constructor and Description |
---|
ResourceUtils() |
Modifier and Type | Method and Description |
---|---|
static ZipFile |
getArchive(URL resource)
If this
resource is an archive file, it will return the resource as an archive. |
static List<String> |
getClassPathArchiveContents(URL resource)
If the
resource represents a classpath archive (i.e. |
static URL |
getClassResource(String className)
Takes a className as an argument and returns the URL for the class.
|
static String |
getContents(Reader resource)
Loads the resource and returns the contents as a String.
|
static String |
getContents(String resourceName)
Loads the file resource and returns the contents as a String.
|
static String |
getContents(URL resource)
Loads the resource and returns the contents as a String.
|
static List<String> |
getDirectoryContents(URL url,
boolean absolute,
String[] patterns)
Gets the contents of this directory and any of its sub directories based on the given
patterns . |
static List<String> |
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). |
static List<String> |
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). |
static long |
getLastModifiedTime(URL resource)
Gets the time as a
long when this resource was last modified. |
static URL |
getResource(String resourceName)
Retrieves a resource from the current classpath.
|
static URL |
getResource(String resourceName,
String directory)
Retrieves a resource from an optionally given
directory or from the package on the classpath. |
static URL |
getResource(String resourceName,
URL directory)
Retrieves a resource from an optionally given
directory or from the package on the classpath. |
static boolean |
isArchive(URL resource)
Returns true/false on whether or not this
resource represents an archive or not (i.e. |
static boolean |
isFile(URL url)
Indicates whether or not the given
url is a file. |
static void |
makeDirectories(File location)
Makes the directory for the given location if it doesn't exist.
|
static void |
makeDirectories(String location)
Makes the directory for the given location if it doesn't exist.
|
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. |
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 . |
static String |
normalizePath(String path)
Removes any extra path separators and converts all from back slashes
to forward slashes.
|
static String |
renameExtension(String path,
String oldExtension,
String newExtension)
Takes a path and replaces the oldException with the newExtension.
|
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. |
static URL |
toURL(String path)
Attempts to construct the given
path
to a URL instance. |
static String |
unescapeFilePath(String filePath)
Replaces any escape characters in the given file path with their
counterparts.
|
static void |
writeUrlToFile(URL url,
String fileLocation)
Writes the URL contents to a file specified by the fileLocation argument.
|
public ResourceUtils()
public static URL getResource(String resourceName)
resourceName
- the name of the resourcepublic static String getContents(URL resource)
resource
- the name of the resource.public static String getContents(Reader resource)
resource
- the name of the resource.public static List<String> getClassPathArchiveContents(URL resource)
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.resource
- the resource from which to retrieve the contentspublic static List<String> getDirectoryContents(URL resource, int levels)
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).resource
- the resource from which to retrieve the contentslevels
- 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 String unescapeFilePath(String filePath)
filePath
- the path of the file to unescape.public static List<String> getDirectoryContents(URL resource, int levels, boolean includeSubdirectories)
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).resource
- the resource from which to retrieve the contentslevels
- the number of levels to step down if the resource ends up being a directory (if its an artifact,
levels will be ignored).includeSubdirectories
- whether or not to include subdirectories in the contents.public static boolean isArchive(URL resource)
resource
represents an archive or not (i.e. jar, or zip,
etc).resource
- public static ZipFile getArchive(URL resource)
resource
is an archive file, it will return the resource as an archive.resource
- public static String getContents(String resourceName)
resourceName
- the name of the resource.public static URL getClassResource(String className)
className
- name of classpublic static URL getResource(String resourceName, String 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.
resourceName
- the name of a resourcedirectory
- the directory locationpublic static void makeDirectories(String location)
location
- the location to make the directory.public static void makeDirectories(File location)
location
- the location to make the directory.public static long getLastModifiedTime(URL resource)
long
when this resource
was last modified.
If it can not be determined 0
is returned.resource
- the resource from which to retrieve
the last modified time.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.
resourceName
- the name of a resourcedirectory
- the directory locationpublic static URL toURL(String path)
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.path
- the path from which to construct the URL.public static URL resolveClasspathResource(String path)
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
path
- the name of the resource to resolve to a URL, potentially nested in ZIP archivesnull
if the argument is null
or impossible to resolvepublic static void writeUrlToFile(URL url, String fileLocation) throws IOException
url
- the URL to readfileLocation
- the location which to write.IOException
- if error writing filepublic static boolean isFile(URL url)
url
is a file.url
- the URL to check.public static List<String> getDirectoryContents(URL url, boolean absolute, String[] patterns)
patterns
.
And returns absolute or relative paths depending on the value of absolute
.url
- the URL of the directory.absolute
- whether or not the returned content paths should be absolute (if
false paths will be relative to URL).patterns
- public static boolean matchesAtLeastOnePattern(String path, String[] patterns)
path
matches on
one or more of the patterns defined within this class
returns true if no patterns are defined.path
- the path to match on.patterns
- public static boolean modifiedAfter(long time, File directory)
directory
and any of its sub directories have been modified after the given time
.directory
- the directory to checktime
- the time to check againstpublic static String normalizePath(String path)
path
- the path to normalize.public static String renameExtension(String path, String oldExtension, String newExtension)
path
- the path to rename.oldExtension
- the extension to rename from.newExtension
- the extension to rename to.Copyright © 2003–2014 AndroMDA.org. All rights reserved.