Class FileExtensionConfigurationBuilderProvider

java.lang.Object
org.apache.commons.configuration2.builder.combined.BaseConfigurationBuilderProvider
org.apache.commons.configuration2.builder.combined.FileExtensionConfigurationBuilderProvider
All Implemented Interfaces:
ConfigurationBuilderProvider

public class FileExtensionConfigurationBuilderProvider extends BaseConfigurationBuilderProvider

A specialized implementation of ConfigurationBuilderProvider which determines the name of the result configuration class based on the extension of the file to load.

This class works analogously to its base class BaseConfigurationBuilderProvider; especially, the resulting builder is created based on reflection. It extends the super class's functionality by a specific mechanism for determining the resulting configuration class: At construction time two configuration class names and a file extension are passed in. If a file name is provided in the builder's initialization parameters and this file name has the specified extension, then the first configuration class name is used; otherwise the default configuration class name is selected.

There are some tags for CombinedConfigurationProvider which can produce different results depending on the configuration files they have to load. This class can be used to implement this feature in a generic way.

Since:
2.0
  • Field Details

    • EXT_SEPARATOR

      private static final char EXT_SEPARATOR
      Constant for the file extension separator.
      See Also:
    • matchingConfigurationClass

      private final String matchingConfigurationClass
      The matching configuration class.
    • extension

      private final String extension
      The file extension.
  • Constructor Details

    • FileExtensionConfigurationBuilderProvider

      public FileExtensionConfigurationBuilderProvider(String bldrCls, String reloadBldrCls, String matchingConfigCls, String defConfigClass, String ext, Collection<String> paramCls)
      Creates a new instance of FileExtensionConfigurationBuilderProvider.
      Parameters:
      bldrCls - the name of the builder class
      reloadBldrCls - the name of a builder class to be used if reloading support is required (null if reloading is not supported)
      matchingConfigCls - the name of the configuration class to be used if the provided file extension matches (must not be null)
      defConfigClass - the name of the configuration class to be used if the provided file extension does not match (must not be null)
      ext - the file extension to select the configuration class (must not be null)
      paramCls - a collection with the names of parameters classes; an instance of a parameters object with basic properties is created automatically and does not need to be contained in this list; the collection can be null if no additional parameter objects are needed
      Throws:
      IllegalArgumentException - if a required parameter is missing
  • Method Details

    • getMatchingConfigurationClass

      public String getMatchingConfigurationClass()
      Gets the name of the matching configuration class. This class is used if the file extension matches the extension of this provider.
      Returns:
      the matching configuration class
    • getExtension

      public String getExtension()
      Gets the file extension of this provider.
      Returns:
      the file extension to match
    • determineConfigurationClass

      protected String determineConfigurationClass(ConfigurationDeclaration decl, Collection<BuilderParameters> params) throws ConfigurationException
      Determines the name of the configuration class produced by the builder. This method is called when obtaining the arguments for invoking the constructor of the builder class. This implementation just returns the pre-configured configuration class name. Derived classes may determine this class name dynamically based on the passed in parameters. This implementation tries to find a FileBasedBuilderParametersImpl object in the parameter objects. If one is found, the extension of the file name is obtained and compared against the stored file extension. In case of a match, the matching configuration class is selected, otherwise the default one.
      Overrides:
      determineConfigurationClass in class BaseConfigurationBuilderProvider
      Parameters:
      decl - the current ConfigurationDeclaration
      params - the collection with parameter objects
      Returns:
      the name of the builder's result configuration class
      Throws:
      ConfigurationException - if an error occurs
    • fetchCurrentFileName

      private static String fetchCurrentFileName(Collection<BuilderParameters> params)
      Tries to obtain the current file name from the given list of parameter objects.
      Parameters:
      params - the parameter objects
      Returns:
      the file name or null if unspecified
    • extractExtension

      private static String extractExtension(String fileName)
      Extracts the extension from the given file name. The name can be null.
      Parameters:
      fileName - the file name
      Returns:
      the extension (null if there is none)