Class ToStringTransformer
- java.lang.Object
-
- net.sf.jguiraffe.transform.ToStringTransformer
-
- All Implemented Interfaces:
Transformer
public class ToStringTransformer extends Object implements Transformer
A generic
Transformer
implementation that transforms arbitrary objects into formatted strings.This implementation can be used for transforming data of the most common data types to be presented in form input fields (especially text fields). Based on the type of the passed in object a specific transforming method will be invoked. The following data types are supported:
java.util.Date
objects will be formatted using aDateFormat
object for the current locale. With thesetDateFormatStyle()
the specific formatting style can be defined (one of the constants defined by theDateFormat
class likeSHORT
,MEDIUM
, orFULL
. The class will always use a date instance for doing the formatting. So if other formats are needed (e.g. only the time portion or both date and time), a differentTransformer
implementation must be used.- Decimal numbers will be formatted using a
NumberFormat
object initialized for the current locale. For configuring the format a set of properties is available matching the options ofNumberFormat
(e.g. the grouping flag or the maximum number of fraction digits). - Integer numbers are treated in a similar way as decimal numbers. They
are also transformed using a
NumberFormat
object, but no fraction digits will be output. - For all other objects their
toString()
method will be invoked. - A null input results in an empty string.
The properties that configure the formatting can be set through the bean-style set methods. When declaring an input element in a builder script and assigning a
Transformer
to it, it is also possible to specify properties, which will then override the settings stored in the object's properties. These properties have the same names as the bean properties defined by this class, e.g. thegroupingUsed
property corresponds to thesetGroupingUsed()
method.An instance of this
Transformer
implementation can be shared between multiple input components. It is possible to create a single instance (e.g. using the dependency injection framework) and initialize it with default settings. If single input components require different settings, specific properties can be set for them overriding the defaults.- Version:
- $Id: ToStringTransformer.java 205 2012-01-29 18:29:57Z oheger $
- Author:
- Oliver Heger
-
-
Field Summary
Fields Modifier and Type Field Description static String
PROP_DATE_FORMAT_STYLE
Constant for the date format style property.static String
PROP_GROUPING_USED
Constant for the grouping used property.static String
PROP_MAXIMUM_FRACTION_DIGITS
Constant for the maximum fraction digits property.static String
PROP_MINIMUM_FRACTION_DIGITS
Constant for the minimum fraction digits property.
-
Constructor Summary
Constructors Constructor Description ToStringTransformer()
Creates a new instance ofToStringTransformer
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
getDateFormatStyle()
Returns the style to be used for formatting dates.int
getMaximumFractionDigits()
Returns the maximum number of fraction digits.int
getMinimumFractionDigits()
Returns the minimum number of fraction digits.protected boolean
isDecimalNumber(Object o)
Checks whether the passed in object is a decimal number.boolean
isGroupingUsed()
Returns a flag whether a grouping character is to be used when formatting numbers.protected boolean
isIntegerNumber(Object o)
Checks whether the passed in object is an integer number.void
setDateFormatStyle(int dateFormatStyle)
Sets the style to be used for formatting dates.void
setGroupingUsed(boolean groupingUsed)
Sets the grouping used flag.void
setMaximumFractionDigits(int maximumFractionDigits)
Sets the maximum number of fraction digits.void
setMinimumFractionDigits(int minimumFractionDigits)
Sets the minimum number of fraction digits.Object
transform(Object o, TransformerContext ctx)
Transforms the specified object.protected Object
transformDate(Date dt, TransformerContext ctx)
Transforms a date object.protected Object
transformDecimalNumber(Object number, TransformerContext ctx)
Transforms a decimal number object.protected Object
transformIntegerNumber(Object number, TransformerContext ctx)
Transforms an integer number object.protected Object
transformNull(TransformerContext ctx)
Transforms a null object.protected Object
transformObject(Object o, TransformerContext ctx)
Transforms an arbitrary object.
-
-
-
Field Detail
-
PROP_DATE_FORMAT_STYLE
public static final String PROP_DATE_FORMAT_STYLE
Constant for the date format style property.- See Also:
- Constant Field Values
-
PROP_MAXIMUM_FRACTION_DIGITS
public static final String PROP_MAXIMUM_FRACTION_DIGITS
Constant for the maximum fraction digits property.- See Also:
- Constant Field Values
-
PROP_MINIMUM_FRACTION_DIGITS
public static final String PROP_MINIMUM_FRACTION_DIGITS
Constant for the minimum fraction digits property.- See Also:
- Constant Field Values
-
PROP_GROUPING_USED
public static final String PROP_GROUPING_USED
Constant for the grouping used property.- See Also:
- Constant Field Values
-
-
Method Detail
-
getDateFormatStyle
public int getDateFormatStyle()
Returns the style to be used for formatting dates.- Returns:
- the style for the formatting of dates
-
setDateFormatStyle
public void setDateFormatStyle(int dateFormatStyle)
Sets the style to be used for formatting dates. This is one of the style constants defined by thejava.text.DateFormat
class.- Parameters:
dateFormatStyle
- the style to be used
-
getMinimumFractionDigits
public int getMinimumFractionDigits()
Returns the minimum number of fraction digits.- Returns:
- the minimum number of fraction digits
-
setMinimumFractionDigits
public void setMinimumFractionDigits(int minimumFractionDigits)
Sets the minimum number of fraction digits. This value is taken into account when formatting decimal numbers.- Parameters:
minimumFractionDigits
- the new minimum number of fraction digits
-
getMaximumFractionDigits
public int getMaximumFractionDigits()
Returns the maximum number of fraction digits.- Returns:
- the maximum number of fraction digits
-
setMaximumFractionDigits
public void setMaximumFractionDigits(int maximumFractionDigits)
Sets the maximum number of fraction digits. This value is taken into account when formatting decimal numbers.- Parameters:
maximumFractionDigits
- the new maximum number of fraction digits
-
isGroupingUsed
public boolean isGroupingUsed()
Returns a flag whether a grouping character is to be used when formatting numbers.- Returns:
- the grouping flag
-
setGroupingUsed
public void setGroupingUsed(boolean groupingUsed)
Sets the grouping used flag. When formatting numbers this flag controls whether a grouping character is used for big numbers.- Parameters:
groupingUsed
- the grouping flag
-
transform
public Object transform(Object o, TransformerContext ctx) throws Exception
Transforms the specified object. Depending on the type of the object one of the specialized transform methods will be called.- Specified by:
transform
in interfaceTransformer
- Parameters:
o
- the object to be transformedctx
- the transformer context- Returns:
- the transformed object
- Throws:
Exception
- if an error occurs
-
transformNull
protected Object transformNull(TransformerContext ctx) throws Exception
Transforms a null object. This implementation returns an empty string.- Parameters:
ctx
- the transformer context- Returns:
- the transformed object
- Throws:
Exception
- if an error occurs
-
transformDate
protected Object transformDate(Date dt, TransformerContext ctx) throws Exception
Transforms a date object. This implementation uses a date format object.- Parameters:
dt
- the date to be formattedctx
- the transformer context- Returns:
- the transformed object
- Throws:
Exception
- if an error occurs
-
transformDecimalNumber
protected Object transformDecimalNumber(Object number, TransformerContext ctx) throws Exception
Transforms a decimal number object. This implementation uses a number format object.- Parameters:
number
- the number object to be formattedctx
- the transformer context- Returns:
- the transformed object
- Throws:
Exception
- if an error occurs
-
transformIntegerNumber
protected Object transformIntegerNumber(Object number, TransformerContext ctx) throws Exception
Transforms an integer number object. For this purpose a number format object is used.- Parameters:
number
- the number to be transformedctx
- the transformer context- Returns:
- the transformed object
- Throws:
Exception
- if an error occurs
-
transformObject
protected Object transformObject(Object o, TransformerContext ctx) throws Exception
Transforms an arbitrary object. This method is invoked if no other, more specific transformation method can be found. It transforms the object into a string by invoking itstoString()
method.- Parameters:
o
- the object to be transformedctx
- the transformer context- Returns:
- the transformed object
- Throws:
Exception
- if an error occurs
-
isDecimalNumber
protected boolean isDecimalNumber(Object o)
Checks whether the passed in object is a decimal number. This implementation checks for the typical Java types representing decimal numbers.- Parameters:
o
- the object to check- Returns:
- a flag whether the passed in object is a decimal number
-
isIntegerNumber
protected boolean isIntegerNumber(Object o)
Checks whether the passed in object is an integer number. This implementation simply checks whether the object is an instance ofjava.lang.Number
. Because decimal numbers are checked first, the remaining number objects are integers.- Parameters:
o
- the object to check- Returns:
- a flag whether this object is an integer number
-
-