org.apache.commons.configuration2.io

## Class HomeDirectoryLocationStrategy

- java.lang.Object
- - org.apache.commons.configuration2.io.HomeDirectoryLocationStrategy

- All Implemented Interfaces:
    FileLocationStrategy

      public class HomeDirectoryLocationStrategy
      extends 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.

  Version:
    $Id: HomeDirectoryLocationStrategy.java 1624601 2014-09-12 18:04:36Z oheger $

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `HomeDirectoryLocationStrategy()` Creates a new instance of `HomeDirectoryLocationStrategy` with default settings.
    `HomeDirectoryLocationStrategy(boolean withBasePath)` Creates a new instance of `HomeDirectoryLocationStrategy` and initializes the base path flag.
    `HomeDirectoryLocationStrategy(String homeDir, boolean withBasePath)` Creates a new instance of `HomeDirectoryLocationStrategy` and initializes it with the specified settings.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `String` | `getHomeDirectory()` Returns the home directory.
    `boolean` | `isEvaluateBasePath()` Returns a flag whether the base path is to be taken into account when searching for a file.
    `URL` | `locate(FileSystem fileSystem, FileLocator locator)` Tries to locate the specified file.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### HomeDirectoryLocationStrategy

          public HomeDirectoryLocationStrategy(String homeDir,
                                               boolean withBasePath)

      Creates a new instance of `HomeDirectoryLocationStrategy` 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

    - #### HomeDirectoryLocationStrategy

          public HomeDirectoryLocationStrategy(boolean withBasePath)

      Creates a new instance of `HomeDirectoryLocationStrategy` 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()

      Creates a new instance of `HomeDirectoryLocationStrategy` 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).

  - ### Method Detail

    - #### getHomeDirectory

          public String getHomeDirectory()

      Returns 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 URL locate(FileSystem fileSystem,
                            FileLocator locator)

      Tries to locate the specified file. The method also expects the `FileSystem` to be used. Note that the `FileLocator` object may also contain a `FileSystem`, but this is optional. The passed in `FileSystem` should be used, and callers must not pass a null reference for this argument. A concrete implementation has to evaluate the properties stored in the `FileLocator` 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 in `FileLocator`. If the locator defines a base path and the `evaluateBasePath` property is true, a sub directory of the home directory is searched.
      Specified by:
        `locate` in interface `FileLocationStrategy`
      Parameters:
        `fileSystem` - the `FileSystem` to be used for this operation
        `locator` - 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

Copyright © 2001–2015 The Apache Software Foundation. All rights reserved.