org.andromda.core.common
Class ResourceUtils

java.lang.Object
  extended by org.andromda.core.common.ResourceUtils

public class ResourceUtils
extends java.lang.Object

Provides utilities for loading resources.

Author:
Chad Brandon

Constructor Summary
ResourceUtils()
           
 
Method Summary
static void deleteDirectory(java.io.File directory)
          Recursively deletes a directory and its contents.
static java.util.zip.ZipFile getArchive(java.net.URL resource)
          If this resource is an archive file, it will return the resource as an archive.
static java.util.List getClassPathArchiveContents(java.net.URL resource)
          If the resource represents a classpath archive (i.e.
static java.net.URL getClassResource(java.lang.String className)
          Takes a className as an argument and returns the URL for the class.
static java.lang.String getContents(java.io.Reader resource)
          Loads the resource and returns the contents as a String.
static java.lang.String getContents(java.lang.String resourceName)
          Loads the file resource and returns the contents as a String.
static java.lang.String getContents(java.net.URL resource)
          Loads the resource and returns the contents as a String.
static java.util.List getDirectoryContents(java.net.URL url, boolean absolute, java.lang.String[] patterns)
          Gets the contents of this directory and any of its sub directories based on the given patterns.
static java.util.List getDirectoryContents(java.net.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 java.util.List getDirectoryContents(java.net.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(java.net.URL resource)
          Gets the time as a long when this resource was last modified.
static java.net.URL getResource(java.lang.String resourceName)
          Retrieves a resource from the current classpath.
static java.net.URL getResource(java.lang.String resourceName, java.lang.String directory)
          

Retrieves a resource from an optionally given directory or from the package on the classpath.

static java.net.URL getResource(java.lang.String resourceName, java.net.URL directory)
           Retrieves a resource from an optionally given directory or from the package on the classpath.
static boolean isArchive(java.net.URL resource)
          Returns true/false on whether or not this resource represents an archive or not (i.e.
static boolean isFile(java.net.URL url)
          Indicates whether or not the given url is a file.
static void makeDirectories(java.lang.String location)
          Makes the directory for the given location if it doesn't exist.
static boolean matchesAtLeastOnePattern(java.lang.String path, java.lang.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, java.io.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 java.lang.String normalizePath(java.lang.String path)
          Removes any extra path separators and converts all from back slashes to forward slashes.
static java.lang.String renameExtension(java.lang.String path, java.lang.String oldExtension, java.lang.String newExtension)
          Takes a path and replaces the oldException with the newExtension.
static java.net.URL resolveClasspathResource(java.lang.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 java.net.URL toURL(java.lang.String path)
          Attempts to construct the given path to a URL instance.
static java.lang.String unescapeFilePath(java.lang.String filePath)
          Replaces any escape characters in the given file path with their counterparts.
static void writeUrlToFile(java.net.URL url, java.lang.String fileLocation, java.lang.String encoding)
          Writes the URL contents to a file specified by the fileLocation argument.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ResourceUtils

public ResourceUtils()
Method Detail

getResource

public static java.net.URL getResource(java.lang.String resourceName)
Retrieves a resource from the current classpath.

Parameters:
resourceName - the name of the resource
Returns:
the resource url

getContents

public static java.lang.String getContents(java.net.URL resource)
Loads the resource and returns the contents as a String.

Parameters:
resource - the name of the resource.
Returns:
String

getContents

public static java.lang.String getContents(java.io.Reader resource)
Loads the resource and returns the contents as a String.

Parameters:
resource - the name of the resource.
Returns:
the contents of the resource as a string.

getClassPathArchiveContents

public static java.util.List getClassPathArchiveContents(java.net.URL resource)
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
Returns:
a list of Strings containing the names of every nested resource found in this resource.

getDirectoryContents

public static java.util.List getDirectoryContents(java.net.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
levels - the number of levels to step down if the resource ends up being a directory (if its an artifact, levels will be ignored).
Returns:
a list of Strings containing the names of every nested resource found in this resource.

unescapeFilePath

public static java.lang.String unescapeFilePath(java.lang.String filePath)
Replaces any escape characters in the given file path with their counterparts.

Parameters:
filePath - the path of the file to unescape.
Returns:
the unescaped path.

getDirectoryContents

public static java.util.List getDirectoryContents(java.net.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
levels - 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.
Returns:
a list of Strings containing the names of every nested resource found in this resource.

isArchive

public static boolean isArchive(java.net.URL resource)
Returns true/false on whether or not this resource represents an archive or not (i.e. jar, or zip, etc).

Returns:
true if its an archive, false otherwise.

getArchive

public static java.util.zip.ZipFile getArchive(java.net.URL resource)
If this resource is an archive file, it will return the resource as an archive.

Returns:
the archive as a ZipFile

getContents

public static java.lang.String getContents(java.lang.String resourceName)
Loads the file resource and returns the contents as a String.

Parameters:
resourceName - the name of the resource.
Returns:
String

getClassResource

public static java.net.URL getClassResource(java.lang.String className)
Takes a className as an argument and returns the URL for the class.

Parameters:
className -
Returns:
java.net.URL

getResource

public static java.net.URL getResource(java.lang.String resourceName,
                                       java.lang.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.

Parameters:
resourceName - the name of a resource
directory - the directory location
Returns:
the resource url

makeDirectories

public static void makeDirectories(java.lang.String location)
Makes the directory for the given location if it doesn't exist.

Parameters:
location - the location to make the directory.

getLastModifiedTime

public static long getLastModifiedTime(java.net.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 retrieve the last modified time.
Returns:
the last modified time or 0 if it couldn't be retrieved.

getResource

public static java.net.URL getResource(java.lang.String resourceName,
                                       java.net.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.

Parameters:
resourceName - the name of a resource
directory - the directory location
Returns:
the resource url

toURL

public static java.net.URL toURL(java.lang.String path)
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.
Returns:
the constructed URL or null if one couldn't be constructed.

resolveClasspathResource

public static java.net.URL resolveClasspathResource(java.lang.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.

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
Returns:
a URL pointing the resource resolved from the argument path or null if the argument is null or impossible to resolve

writeUrlToFile

public static void writeUrlToFile(java.net.URL url,
                                  java.lang.String fileLocation,
                                  java.lang.String encoding)
                           throws java.io.IOException
Writes the URL contents to a file specified by the fileLocation argument.

Parameters:
url - the URL to read
fileLocation - the location which to write.
encoding - the optional encoding
Throws:
java.io.IOException

isFile

public static boolean isFile(java.net.URL url)
Indicates whether or not the given url is a file.

Parameters:
url - the URL to check.
Returns:
true/false

deleteDirectory

public static void deleteDirectory(java.io.File directory)
Recursively deletes a directory and its contents.

Parameters:
directory - the directory to delete.

getDirectoryContents

public static java.util.List getDirectoryContents(java.net.URL url,
                                                  boolean absolute,
                                                  java.lang.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.
absolute - whether or not the returned content paths should be absoluate (if false paths will be relative to URL).
Returns:
a collection of paths.

matchesAtLeastOnePattern

public static boolean matchesAtLeastOnePattern(java.lang.String path,
                                               java.lang.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.
Returns:
true/false

modifiedAfter

public static boolean modifiedAfter(long time,
                                    java.io.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.

Parameters:
directory - the directory to check
time - the time to check against
Returns:
true/false

normalizePath

public static java.lang.String normalizePath(java.lang.String path)
Removes any extra path separators and converts all from back slashes to forward slashes.

Parameters:
path - the path to normalize.
Returns:
the normalizd path

renameExtension

public static java.lang.String renameExtension(java.lang.String path,
                                               java.lang.String oldExtension,
                                               java.lang.String newExtension)
Takes a path and replaces the oldException with the newExtension.

Parameters:
path - the path to rename.
oldExtension - the extension to rename from.
newExtension - the extension to rename to.
Returns:
the path with the new extension.


Copyright © 2003-2008 AndroMDA.org. All Rights Reserved.