public final class ResourceLocator extends Object
An instance of this class provides access to a resource, such as a configuration file.
The resource data is accessed using CharSource or ByteSource.
| Modifier and Type | Field and Description |
|---|---|
static String |
CLASSPATH_URL_PREFIX
The prefix for classpath resource locators.
|
static String |
FILE_URL_PREFIX
The prefix for file resource locators.
|
static String |
URL_PREFIX
The prefix for URL resource locators.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
equals(Object obj)
Checks if this locator equals another locator.
|
BeanByteSource |
getByteSource()
Gets the byte source to access the resource.
|
CharSource |
getCharSource()
Gets the char source to access the resource using UTF-8.
|
CharSource |
getCharSource(Charset charset)
Gets the char source to access the resource specifying the character set.
|
String |
getLocator()
Gets the string form of the locator.
|
int |
hashCode()
Returns a suitable hash code for the locator.
|
static ResourceLocator |
of(String locator)
Creates a resource from a string locator.
|
static ResourceLocator |
ofClasspath(Class<?> cls,
String resourceName)
Creates a resource locator for a classpath resource which is associated with a class.
|
static ResourceLocator |
ofClasspath(String resourceName)
Creates a resource from a fully qualified resource name.
|
static ResourceLocator |
ofClasspathUrl(URL url)
Creates a resource from a
URL. |
static ResourceLocator |
ofFile(File file)
Creates a resource from a
File. |
static ResourceLocator |
ofPath(Path path)
Creates a resource from a
Path. |
static ResourceLocator |
ofUrl(URL url)
Creates a resource from a
URL. |
String |
toString()
Returns a string describing the locator.
|
public static final String CLASSPATH_URL_PREFIX
public static final String FILE_URL_PREFIX
public static final String URL_PREFIX
public static ResourceLocator of(String locator)
This accepts locators starting with 'classpath:', 'url:' or 'file:'. It also accepts unprefixed locators, treated as files.
locator - the string form of the resource locatorpublic static ResourceLocator ofFile(File file)
File.
Windows separators are converted to UNIX style. This takes no account of possible '/' characters in the name.
file - the file to wrappublic static ResourceLocator ofPath(Path path)
Path.
This will return either a file locator or a URL locator.
path - path to the file to wrapIllegalArgumentException - if the path is neither a file nor a URLpublic static ResourceLocator ofUrl(URL url)
URL.url - path to the file to wrappublic static ResourceLocator ofClasspath(String resourceName)
If the resource name does not start with a slash '/', one will be prepended.
Use ofClasspath(Class, String) to get a relative resource.
In Java 9 and later, resources can be encapsulated due to the module system.
As such, this method is caller sensitive.
It finds the class of the method that called this one, and uses that to search for
resources using Class.getResource(String).
resourceName - the resource name, which will have a slash '/' prepended if missingpublic static ResourceLocator ofClasspath(Class<?> cls, String resourceName)
The classpath is searched using the same method as Class.getResource.
cls - the class used to find the resourceresourceName - the resource namepublic static ResourceLocator ofClasspathUrl(URL url)
URL.url - the URL to wrappublic String getLocator()
The string form of the locator describes the location of the resource.
public BeanByteSource getByteSource()
A byte source is a supplier of data. The source itself is neither opened nor closed.
public CharSource getCharSource()
A char source is a supplier of data. The source itself is neither opened nor closed.
This method handles Unicode Byte Order Marks.
public CharSource getCharSource(Charset charset)
A char source is a supplier of data. The source itself is neither opened nor closed.
charset - the character set to usepublic boolean equals(Object obj)
The comparison checks the locator string.
public int hashCode()
public String toString()
This can be parsed using of(String).
Copyright 2009-Present by OpenGamma Inc. and individual contributors
Apache v2 licensed
Additional documentation can be found at strata.opengamma.io.