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
Converterimplementation dealing withLocatorobjects.This converter implementation is able to transform text representations of locators into concrete
Locatorinstances. 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
Locatorimplementations provided by this package. A text representation of a locator starts with a prefix, followed by a colon. Then the specific data of the concreteLocatorsubclass is appended. The following table lists the supported prefixes with their meaning and examples:Prefix Description Example classpath Creates a ClassPathLocatorinstance. 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 FileLocatorinstance. The data is interpreted as a relative or absolute file name. It is directly passed to theFileLocatorinstance to be created.file:target/data.txt url Creates a URLLocatorinstance. The data is interpreted as a URL in text form. It is directly passed to theURLLocatorinstance to be created.url:http://www.mydomain.com/image.jpg Prefixes are not case sensitive. If an unknown prefix is encountered, a
ConversionExceptionis 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 customLocatorimplementations.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 charPREFIX_SEPARATORConstant for the prefix separator.
-
Constructor Summary
Constructors Constructor Description LocatorConverter()Creates a new instance ofLocatorConverterwithout a class loader provider.LocatorConverter(ClassLoaderProvider clp)Creates a new instance ofLocatorConverterand initializes it with the givenClassLoaderProvider.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Objectconvert(Class type, Object value)Tries to convert the specified object to aLocator.ClassLoaderProvidergetClassLoaderProvider()Returns theClassLoaderProviderused 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 ofLocatorConverterand 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 ofLocatorConverterwithout 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 theClassLoaderProviderused 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 noClassLoaderProviderhas 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:
convertin 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
Locatorinterface - Throws:
org.apache.commons.beanutils.ConversionException- if conversion is not possible
-
-