Class LocatorConverter
- java.lang.Object
-
- net.sf.jguiraffe.locators.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 withLocator
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 concreteLocator
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 theClassLoaderProvider
; otherwise, the default class loader is used.classpath:myresource.properties
classpath:myresource.properties;myClassLoaderfile Creates a FileLocator
instance. The data is interpreted as a relative or absolute file name. It is directly passed to theFileLocator
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 theURLLocator
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 customLocator
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 Summary
Fields Modifier and Type Field Description static char
PREFIX_SEPARATOR
Constant for the prefix separator.
-
Constructor Summary
Constructors Constructor Description LocatorConverter()
Creates a new instance ofLocatorConverter
without a class loader provider.LocatorConverter(ClassLoaderProvider clp)
Creates a new instance ofLocatorConverter
and initializes it with the givenClassLoaderProvider
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Object
convert(Class type, Object value)
Tries to convert the specified object to aLocator
.ClassLoaderProvider
getClassLoaderProvider()
Returns theClassLoaderProvider
used by this converter.
-
-
-
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 ofLocatorConverter
and initializes it with the givenClassLoaderProvider
. Class loaders for class path locators are obtained from this provider.- Parameters:
clp
- theClassLoaderProvider
- Since:
- 1.2
-
LocatorConverter
public LocatorConverter()
Creates a new instance ofLocatorConverter
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 aClassLoaderProvider
.
-
-
Method Detail
-
getClassLoaderProvider
public ClassLoaderProvider getClassLoaderProvider()
Returns theClassLoaderProvider
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 noClassLoaderProvider
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 aLocator
. The conversion is based on prefixes as described in the class comment.- Specified by:
convert
in interfaceorg.apache.commons.beanutils.Converter
- Parameters:
type
- the target classvalue
- 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
-
-