Data Formatters

Overview

Requires Platform 4.2+

A formatter translates data coming from a data source for display. For example, a formatter might transform the number object 1123.8 into the string “$1,123.80” for display by the user interface. Each binding has a chain of formatters that data passes through.

Declaring formatters is similar to declaring data sources.  The following example shows a formatter:

<formatter id=”customers” 
 class=”com.nexaweb.plugin.data.formatter.MessageFormatter” 
 format="There are {0} customers in the database"/>

The following table describes the ways you can define your own formatters: 

Method of Defining FormattersDescription
Supplied tagNexaweb ships with the predefined formatter tag.
Custom tagUse the Nexaweb plug-in architecture to declare a tag that translates data into an appropriate format for the UI to display.

For more information on defining your own formatters, see Creating a Formatter.

In addition, Nexaweb provides the following formatter tags for transforming data for display in a UI:

  • messageFormatter
  • decimalFormatter
  • dateFormatter
  • currencyFormatter
  • stringToDateFormatter
  • formatterChain

For more information on pre-defined formatters, see Supplied Tags.

Note: Nexaweb does not provide validation to suit all needs.  For example, Nexaweb does not provide formatters for state abbreviations, postal codes, phone numbers, and so forth.  However, Nexaweb provides a formatter tag to enable you to easily create formatters that provide these features.

Supplied Formatter Tags

Nexaweb provides the formatter tags described on this page.

formatter Tag

Use the formatter tag to declare a formatter class you create using the com.nexaweb.plugin.data.formatter API.

The formatter tag includes the following attributes:

AttributeDescription
IDUniquely identifies this formatter using a system wide unique value.
classIdentifies the name of a Java class that implements the formatter interface.

In addition to using the formatter tag to declare your own formatter class, you can also create your own tag that uses different attributes.


messageFormatter Tag

Use the messageFormatter tag, based on java.text.message format, to specify the pattern and format to use for string messages.

The messageFormatter tag includes the following attributes:

AttributeDescriptionRequired/Optional
idUniquely identifies this messageFormatter using a system wide unique value.Required
formatDefines a valid java.text.MessageFormat pattern string.Required
localeA locale string such as: "en_US"Optional

Output

The messageFormatter tag always outputs a java.lang.String object, since the class for this formatter uses the java.text.MessageFormat class.

Input

The following table lists the input types allowed for each format string (since this class uses the java.text.MessageFormat class, the specified format string dictates the input accepted): 

Format StringAccepted Input

"There are {0} customers"
  • Object
  • Object[] { Object }
  • Object[] { Object, ... }
  • List( Object,... )
  • Vector( Object,... )
  • any primitive array with > 0 elements

"There are {0,number} customers"
  • Number
  • Object[] { Number }
  • Object[] { Number, ... }
  • List( Number )
  • Vector( Number )
  • any primitive type number array (i.e. int[]{int,...)

"There are {1} customers"
  • String
  • Object[] { Object, Object }
  • Object[] { Object, Object, ... }
  • List(Object,Object,...)
  • Vector(Object,Object,...)
  • Any primitive array with > 1 elements

"There are {0, number} customers with {1} cars"
  • Object[] { Number, String }
  • List(Number,String,...)
  • Vector(Number,String,...)

Note: In 1.4+ JVMs, Nexaweb uses the getFormatsByArgumentIndex() method to accept a string object anywhere a number is expected as long as that string can be parsed into a Number.  In addition, Newxaweb accepts strings anywhere it expects a date as long as Long.parseLong(String) or default DateTime instance can parse that string.

In JVM 1.3 and below environments, Nexaweb cannot reliably convert arguments to the expected types because the MessageFormat API does not provide enough information.  Nexaweb attempts to convert arguments when formatting fails with the original arguments but this works only in some cases (that is,  where getFormats() and getFormatsByArgumentIndex() would return the same format array).  For example, for the following format string: My string with a date: {0,date}, a number {1,number} and some string {2} both getFormats() and getFormatsByArgumentIndex() return the same result.

However these methods would not return the same value given the following format string: My string with some string {2}, a number {1,number} and a date: {0,date}

For both format strings, getFormatsByArgumentIndex() would return the same array of formats: [DateFormat,NumberFormat,null] (null stands for no format or just String). However getFormats() would return [DateFormat,NumberFormat,null] in the first example and [null, NumberFormat,DateFormat] in the second one. Thus, for best result in JVM 1.3 and below environments, pass the correct argument types to the MessageFormatter.


currencyFormatter Tag

Available for Nexaweb Java applications only.

Use the currentyFormatter tag, based on java.text.NumberFormat, to create currency strings from numbers.

The currentyFormatter tag includes the following attributes:

AttributeDescriptionRequired/Optional
idUniquely identifies this currencyFormatter using a system wide unique value.Required
localeA locale string such as: "en_US"Optional


decimalFormatter Tag

Use the decimalFormatter tag, based on java.text.DecimalFormat, to create decimal format strings.

The decimalFormatter tag includes the following attributes:

AttributeDescriptionRequired/Optional
idUniquely identifies this messageFormatter using a system wide unique value.Required
format A decimal format pattern string such as:  "##.0#%"Required
localeA locale string such as: "en_US"Optional


dateFormatter Tag

Available for Nexaweb Java applications only.

Use the dateFormatter tag, based on java.text.DateFormat, to create dates from a string.

The dateFormatter tag includes the following attributes:

AttributeDescriptionRequired/Optional
idUniquely identifies this dateFormatter using a system wide unique value.Required
localeA  locale string such as: "en_US"Optional
formatWhen the input to this formatter is of type Date or Long, the dateFormatter uses this pattern to construct a SimpleDateFormat instance that performs the formatting.
Note:  If you specify a format, Nexaweb ignores the following attributes: dateFormatInstance, dateStyle, timeStyle
Not all locales support SimpleDateFormat, so for full generality, use the attributes specified as ignored instead of format
Optional
 stringToDateFormatWhen the input to this formatter is a String, the embedded StringToDateFormatter uses this pattern to convert a String into a Date. In general, the following steps take place when the argument to the DateFormatter is a String:
  1. The embedded StringToDateFormatter converts the String into a Date using the stringToDateFormat (if specified).  If the stringToDateFormat is specified, it constructs a SimpleDateFormat instance, otherwise Nexaweb uses a default pattern to construct a SimpleDateFormat  instance.  The lenient and timeZone attribute values will be set on this instance.  If parsing using this SimpleDateFormat instance fails, the StringToDateFormatter will also try the following:

    - Parsing the String as a Long and constructing a Date object using the parsed Long.
    - Use RFC1123 format: "EEE, dd MMM yyyy HH:mm:ss z"
    - Use RFC1036 format: "EEEEEEEEE, dd-MMM-yy HH:mm:ss z"  Use ASCTIME format: "EEE MMM d HH:mm:ss yyyy"

    The default locale for these 3 formatters is Locale.US and the timezone is GMT.

  2. Formats the Date object returned from Step1 using either the format (if specified), or the combination of dateFormatInstance, dateStyle and timeStyle attributes.  The DateFormat instance chosen to do the formatting inherits the values of the locale and timeZone attributes.
 Optional
stringToDateLocaleThe locale that the embedded StringToDateFormatter uses to parse a string argument.Optional
dateFormatInstanceSpecifies the form of the date as: date only, time only, or both.
Values: date | time | date,time.
Default value: date
Note: if you use the format attribute, the dateFormatInstance is ignored.  See format for more information.  
Optional
dateStyleIf you include date as part of the date format you specify with the dateFormatInstance attribute, use this attribute to specify the length of the formatted date string.
Values: short | medium | long | full 
Default: medium
Note: if you use the format attribute, this attribute is ignored.  See format for more information.  
Optional
timeStyleIf you include time in the date format you specify with the dateFormatInstance attribute, use this attribute to specify the length of  the formatted time.
Values: short | medium | long | full
Default: medium
Note: if you use the format attribute, this attribute is ignored.  See format for more information. 
Optional
lenientSpecifies whether the StringToDateFormatter performs lenient parsing when the input to the DateFormatter is a String.
Values: true | false
Default: true
Optional
timeZoneSpecifies the time zone string to use for parsing and/or formatting the inputs to this DateFormatter.
Values: A time zone string, for example, PST
If do not use this attribute, TimeZone.getDefault() supplies the default TimeZone.
Optional


stringToDateFormatter Tag

Available for Nexaweb Java applications only.

Use the stringToDateFormatter tag to create dates from a string without any text style formatting.

The stringToDateFormatter tag includes the following attributes:

AttributeDescriptionRequired/Optional
idUniquely identifies this messageFormatter using a system wide unique value.Required
formatThe specified pattern used to convert the String argument of the format() method into a Date. 
Whether you specify this attribute or not, Nexaweb tries the following steps in order to convert a String into a Date:
  • Parse using a SimpleDateFormat instance constructed either with the specified format or its default format.  The locale, lenient and timeZone attribute values will be set on this instance. 
  • Parsing the String as a Long and constructing a Date object using the parsed Long.
  • Use RFC1123 format: "EEE, dd MMM yyyy HH:mm:ss z" 
  • Use RFC1036 format: "EEEEEEEEE, dd-MMM-yy HH:mm:ss z"
  • Use ASCTIME format: "EEE MMM d HH:mm:ss yyyy"

The locale for these three default formatters is Locale.US and the timezone is GMT.

Optional
localeA locale string such as: "en_US"Optional
lenientWhen the input to the DateFormatter is a String, the lenient attribute specifies whether the parsing performed by the StringToDateFormatter is lenient.  What is lenient?
Values: true | false
Default: true
Optional
timeZoneSpecifies the time zone string to use for parsing and/or formatting the inputs to this StringToDateFormatter.
Values: A time zone string, for example, PST
If do not use this attribute, TimeZone.getDefault() supplies the default TimeZone.
Optional


formatterChain Tag

The formatterChain tag defines a chain of formatters where the output of one formatter becomes the input to the next formatter in the chain. You can use this tag anywhere you can use a regular format.  In the following example of a fomatter chain, myStringToDateFormatter, myDateFormatter and myMessageFormatter represent id attributes of the previously defined formatters:

<formatterChain id="myChain">
   <formatterReference formatter="myStringToDateFormatter/>
   <formatterReference formatter="myDateFormatter" />
   <formatterReference formatter="myMessageFormatter" />
</formatterChain>
 

The formatterChain tag uses the ID attribute only.  The ID tag uniquely identifies this messageFormatter using a system wide unique value. The formatterChain tag can use the formatterReference element as a child.

Creating a Formatter

Defining Modules to Format Data

As part of the data binding framework, Nexaweb provides you with the ability to implement your own functionality to transform data for presentation in the UI.  For example, you can implement your own formatter to turn a custom object returned from a data source into an appropriate String.  In order to implement the functionality of a formatter, you must implement the required interface (com.nexaweb.plugin.data.formatter.Formatter) as defined in the API section.

Nexaweb ships with the predefined formatter tag. You can use this tag to utilize the interface that you implement by specifying your implementation's class as the class attribute of the formatter tag.

Formatter tag

The most basic formatter tag, formatter, simply defines a formatter with its implementing class and its ID.

AttributeDescriptionRequired/Optional
idUniquely identifies the formatter; developer should take care to specify a system wide unique value.Required
classIdentifies the implementation of the formatter.Required

 In addition, developers may elect to create their own formatter tag definitions.