public final class LocatorUtils extends Object
A helper class for locating resources.
This class provides functionality for locating resources like configuration files, which can be specified in multiple ways:
Locator
objects.Modifier and Type | Method and Description |
---|---|
static URL |
fileURL(File file)
Returns the URL for the given file.
|
static URL |
locate(String url,
String name)
Locates a resource either from a URL or a class path resource (using a
default class loader), returning null if the resource cannot be
resolved.
|
static URL |
locate(String url,
String name,
ClassLoader cl)
Locates a resource either from a URL or a class path resource, returning
null if the resource cannot be resolved.
|
static URL |
locateEx(String url,
String name)
Locates a resource either from a URL or a class path resource (using a
default class loader), throwing an exception if the resource cannot be
resolved.
|
static URL |
locateEx(String url,
String name,
ClassLoader cl)
Locates a resource either from a URL or a class path resource, throwing
an exception if the resource cannot be resolved.
|
static URL |
locateResource(String resource)
Tries to locate a resource in the class path.
|
static URL |
locateResource(String resource,
ClassLoader cl)
Tries to locate a resource in the class path using the specified class
loader.
|
static URL |
locateURL(String url)
Locates a file using a string, which can be either a full URL or a file
name.
|
static String |
locatorToDataString(Locator locator)
Returns the data string for the specified
Locator . |
static String |
locatorToDataString(String locatorString)
Extracts the data of the string representation of a
Locator . |
static String |
locatorToString(Locator locator,
String locatorData)
Creates a string representation of a
Locator object. |
static InputStream |
openStream(Locator locator)
Obtains an input stream for the specified locator.
|
public static URL fileURL(File file)
file
- the file to be transformedLocatorException
- if the transformation failspublic static URL locateResource(String resource, ClassLoader cl)
ClassLoader
parameter is not null, this
class loader is tried first. If this fails, this method tries the
context, the default, and the system class loader (in this order) to find
the resource in the class path. Search stops as soon as the resource is
found.resource
- the name of the resourcecl
- the class loader to use for looking up the resource (can be
null)public static URL locateResource(String resource)
resource
- the name of the resourcelocateResource(String, ClassLoader)
public static URL locateURL(String url)
url
- the URL of the file to be locatedpublic static URL locate(String url, String name, ClassLoader cl)
locateURL()
and locateResource()
. If a URL is
defined, it is tried to be resolved. If this fails and a resource name is
defined, this name is tried to be resolved. If both parameters are
defined and valid, the URL takes precedence. If both attempts fail, the
result is null. If a ClassLoader
is specified, it is used
during class path lookup as described at
locateResource(String, ClassLoader)
.url
- specifies a URLname
- specifies a resource namecl
- an optional class loader for the class path lookuppublic static URL locate(String url, String name)
url
- specifies a URLname
- specifies a resource namelocate(String, String, ClassLoader)
public static URL locateEx(String url, String name, ClassLoader cl)
locate()
, but a failing lookup causes a LocatorException
.url
- specifies a URLname
- specifies a resource namecl
- an optional class loader for the class path lookupLocatorException
- if the resource cannot be resolvedpublic static URL locateEx(String url, String name)
url
- specifies a URLname
- specifies a resource nameLocatorException
- if the resource cannot be resolvedlocateEx(String, String, ClassLoader)
public static InputStream openStream(Locator locator) throws IOException
getInputStream()
getFile()
getURL()
locator
- the locatorIOException
- if an IO error occursLocatorException
- if the locator throws an exception or no valid
values are returnedpublic static String locatorToString(Locator locator, String locatorData)
Locator
object. This
string contains the fully qualified class name of the
Locator
(class), its identity hash code (
hash), and the data passed to this method (data, which
is locator specific). It has the following form: class@hash[ data
]
. The concrete Locator
implementations in this package use
this method in the implementation of their toString()
method.locator
- the Locator
to be transformed to a string
(must not be null)locatorData
- the data of this locatorLocator
IllegalArgumentException
- if the locator is nullpublic static String locatorToDataString(String locatorString)
Locator
.
Strings created by the locatorToString()
method contain some
information specific to the Locator
object involved,
especially its identity hash code. This complicates things, for instance
in unit tests, when locators that are equal should produce equal string
representations. In such cases this method can be used. It produces a
string containing only the class name (not fully qualified) and the data
of the locator. The relevant parts are extracted from the given string,
which must conform to the format produced by the
locatorToString()
method.locatorString
- the string for the locator (as generated by
locatorToString()
)IllegalArgumentException
- if the passed in string is null
or does not conform to the expected formatlocatorToString(Locator, String)
public static String locatorToDataString(Locator locator)
Locator
. This is a
short cut of locatorToDataString(locator.toString()
. The
toString()
implementation of the locator must produce
strings conforming to the format of locatorToString()
.locator
- the locator to be transformed into a stringIllegalArgumentException
- if the locator is null or has an
invalid string representationCopyright © 2016 The JGUIraffe Team. All rights reserved.