Class LocatorConverter

  • All Implemented Interfaces:
    org.apache.commons.beanutils.Converter

    public class LocatorConverter
    extends Object
    implements org.apache.commons.beanutils.Converter

    A specialized Converter implementation dealing with Locator objects.

    This converter implementation is able to transform text representations of locators into concrete Locator instances. This is pretty convenient, especially for bean declarations; here a compact text representation for locators is much more readable than a verbose declaration of a factory method invocation.

    This converter supports most of the standard Locator implementations provided by this package. A text representation of a locator starts with a prefix, followed by a colon. Then the specific data of the concrete Locator subclass is appended. The following table lists the supported prefixes with their meaning and examples:

    Prefix Description Example
    classpath Creates a ClassPathLocator instance. The data is interpreted as a resource name which is looked up on the current class path. Optionally, a class loader name can be provided separated by a semicolon. If this is used, the corresponding class loader is obtained from the ClassLoaderProvider; otherwise, the default class loader is used. classpath:myresource.properties
    classpath:myresource.properties;myClassLoader
    file Creates a FileLocator instance. The data is interpreted as a relative or absolute file name. It is directly passed to the FileLocator instance to be created. file:target/data.txt
    url Creates a URLLocator instance. The data is interpreted as a URL in text form. It is directly passed to the URLLocator instance to be created. url:http://www.mydomain.com/image.jpg

    Prefixes are not case sensitive. If an unknown prefix is encountered, a ConversionException is thrown. An instance of this class is registered as base class converter by the form builder per default. Using the data type conversion mechanism provided by the dependency injection framework, it is possible to add further converters for custom Locator implementations.

    This class does not have any internal state. Thus an instance can be shared between multiple components and called concurrently.

    Version:
    $Id: LocatorConverter.java 213 2012-07-14 19:40:51Z oheger $
    Author:
    Oliver Heger
    • Field Detail

      • PREFIX_SEPARATOR

        public static final char PREFIX_SEPARATOR
        Constant for the prefix separator. This character is used to separate the prefix which identifies the type of the locator from its data.
        See Also:
        Constant Field Values
    • Constructor Detail

      • LocatorConverter

        public LocatorConverter​(ClassLoaderProvider clp)
        Creates a new instance of LocatorConverter and initializes it with the given ClassLoaderProvider. Class loaders for class path locators are obtained from this provider.
        Parameters:
        clp - the ClassLoaderProvider
        Since:
        1.2
      • LocatorConverter

        public LocatorConverter()
        Creates a new instance of LocatorConverter without a class loader provider. A converter constructed this way cannot resolve any class loader names. This constructor exists for reasons of backwards compatibility. It is recommended to always provide a ClassLoaderProvider.
    • Method Detail

      • getClassLoaderProvider

        public ClassLoaderProvider getClassLoaderProvider()
        Returns the ClassLoaderProvider used by this converter. This object is used to determine class loaders when locators for class path resources are to be created. Result may be null if no ClassLoaderProvider has been set.
        Returns:
        the current ClassLoaderProvider
        Since:
        1.2
      • convert

        public Object convert​(Class type,
                              Object value)
        Tries to convert the specified object to a Locator. The conversion is based on prefixes as described in the class comment.
        Specified by:
        convert in interface org.apache.commons.beanutils.Converter
        Parameters:
        type - the target class
        value - the object to be converted (must not be null)
        Returns:
        the converted object; this is a concrete implementation of the Locator interface
        Throws:
        org.apache.commons.beanutils.ConversionException - if conversion is not possible