Class HomeDirectoryLocationStrategy
- java.lang.Object
-
- org.apache.commons.configuration2.io.HomeDirectoryLocationStrategy
-
- All Implemented Interfaces:
FileLocationStrategy
public class HomeDirectoryLocationStrategy extends java.lang.Object implements FileLocationStrategy
A specialized implementation of
FileLocationStrategy
which searches for files in the user's home directory or another special configurable directory.This strategy implementation ignores the URL stored in the passed in
FileLocator
. It constructs a file path from the configured home directory (which is the user's home directory per default, but can be changed to another path), optionally the base path, and the file name. If the resulting path points to an existing file, its URL is returned.When constructing an instance it can be configured whether the base path should be taken into account. If this option is set, the base path is appended to the home directory if it is not null. This is useful for instance to select a specific sub directory of the user's home directory. If this option is set to false, the base path is always ignored, and only the file name is evaluated.
-
-
Field Summary
Fields Modifier and Type Field Description private boolean
evaluateBasePath
The flag whether the base path is to be taken into account.private java.lang.String
homeDirectory
The home directory to be searched for the requested file.
-
Constructor Summary
Constructors Constructor Description HomeDirectoryLocationStrategy()
Creates a new instance ofHomeDirectoryLocationStrategy
with default settings.HomeDirectoryLocationStrategy(boolean withBasePath)
Creates a new instance ofHomeDirectoryLocationStrategy
and initializes the base path flag.HomeDirectoryLocationStrategy(java.lang.String homeDir, boolean withBasePath)
Creates a new instance ofHomeDirectoryLocationStrategy
and initializes it with the specified settings.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private java.lang.String
fetchBasePath(FileLocator locator)
Determines the base path to be used for the current locate() operation.private static java.lang.String
fetchHomeDirectory(java.lang.String homeDir)
Obtains the home directory to be used by a new instance.java.lang.String
getHomeDirectory()
Gets the home directory.boolean
isEvaluateBasePath()
Returns a flag whether the base path is to be taken into account when searching for a file.java.net.URL
locate(FileSystem fileSystem, FileLocator locator)
Tries to locate the specified file.
-
-
-
Constructor Detail
-
HomeDirectoryLocationStrategy
public HomeDirectoryLocationStrategy()
Creates a new instance ofHomeDirectoryLocationStrategy
with default settings. The home directory is set to the user's home directory. The base path flag is set to false (which means that the base path is ignored).
-
HomeDirectoryLocationStrategy
public HomeDirectoryLocationStrategy(boolean withBasePath)
Creates a new instance ofHomeDirectoryLocationStrategy
and initializes the base path flag. The home directory is set to the user's home directory.- Parameters:
withBasePath
- a flag whether the base path should be evaluated
-
HomeDirectoryLocationStrategy
public HomeDirectoryLocationStrategy(java.lang.String homeDir, boolean withBasePath)
Creates a new instance ofHomeDirectoryLocationStrategy
and initializes it with the specified settings.- Parameters:
homeDir
- the path to the home directory (can be null)withBasePath
- a flag whether the base path should be evaluated
-
-
Method Detail
-
fetchHomeDirectory
private static java.lang.String fetchHomeDirectory(java.lang.String homeDir)
Obtains the home directory to be used by a new instance. If a directory name is provided, it is used. Otherwise, the user's home directory is looked up.- Parameters:
homeDir
- the passed in home directory- Returns:
- the directory to be used
-
fetchBasePath
private java.lang.String fetchBasePath(FileLocator locator)
Determines the base path to be used for the current locate() operation.- Parameters:
locator
- theFileLocator
- Returns:
- the base path to be used
-
getHomeDirectory
public java.lang.String getHomeDirectory()
Gets the home directory. In this directory the strategy searches for files.- Returns:
- the home directory used by this object
-
isEvaluateBasePath
public boolean isEvaluateBasePath()
Returns a flag whether the base path is to be taken into account when searching for a file.- Returns:
- the flag whether the base path is evaluated
-
locate
public java.net.URL locate(FileSystem fileSystem, FileLocator locator)
Tries to locate the specified file. The method also expects theFileSystem
to be used. Note that theFileLocator
object may also contain aFileSystem
, but this is optional. The passed inFileSystem
should be used, and callers must not pass a null reference for this argument. A concrete implementation has to evaluate the properties stored in theFileLocator
object and try to match them to an existing file. If this can be done, a corresponding URL is returned. Otherwise, result is null. Implementations should not throw an exception (unless parameters are null) as there might be alternative strategies which can find the file in question. This implementation searches in the home directory for a file described by the passed inFileLocator
. If the locator defines a base path and theevaluateBasePath
property is true, a sub directory of the home directory is searched.- Specified by:
locate
in interfaceFileLocationStrategy
- Parameters:
fileSystem
- theFileSystem
to be used for this operationlocator
- the object describing the file to be located- Returns:
- a URL pointing to the referenced file if location was successful; null if the file could not be resolved
-
-