Class 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 a DateFormat object for the current locale. With the setDateFormatStyle() the specific formatting style can be defined (one of the constants defined by the DateFormat class like SHORT, MEDIUM, or FULL. 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 different Transformer 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 of NumberFormat (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. the groupingUsed property corresponds to the setGroupingUsed() 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 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
    • Constructor Detail

      • ToStringTransformer

        public ToStringTransformer()
        Creates a new instance of ToStringTransformer.
    • 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 the java.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 interface Transformer
        Parameters:
        o - the object to be transformed
        ctx - 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 formatted
        ctx - 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 formatted
        ctx - 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 transformed
        ctx - 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 its toString() method.
        Parameters:
        o - the object to be transformed
        ctx - 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 of java.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