org.apache.myfaces.trinidad.convert
Class NumberConverter

java.lang.Object
  extended by javax.faces.convert.NumberConverter
      extended by org.apache.myfaces.trinidad.convert.NumberConverter
All Implemented Interfaces:
StateHolder, Converter

public class NumberConverter
extends NumberConverter

This is an extension of the standard JSF NumberConverter The converter provides all the standard functionality of the default NumberConverter and is strict while converting to object. JSF NumberConverter is lenient and will convert values like 22.22.2 or 22ABC to valid number 22.22 and 22 respectively, here it would result in a conversion failure and would throw ConverterException. If number grouping separator, decimal separator is configured in trinidad-config.xml file, it will be used during call to getAsObject() and getAsString() for parsing and formatting. If it has not been set, number grouping separator, decimal separator is defaulted based on the locale.

If currencyCode is set on the converter then it will be used. Else uses the currencyCode set on trinidad-config.xml file. If it is not set, then it is defaulted based on the locale. The value registered in trinidad-config.xml is obtained using api from RequestContext.

Since Apache Trinidad is compatible only with JDK 1.4 and higher versions, the currencyCode gets preference over currencySymbol See RI's NumberConverter for the way in which currencyCode and currencySymbol gets preference for different version of JDK.

The detail part of the FacesMessage can be customized. For each message id there is a corresponding setter method, which provides for message customization. The customized messages can contain placeholders as specified in the documentation for its corresponding message id.

Example: to customize the message for invalid input values, which will result in conversion error containing CONVERT_NUMBER_MESSAGE_ID, the following, can be done.

String convertNumberMessageDetail = "{0}" in "{1}" is not valid age. // Note that, the string can contain placeholders and it will be replaced // appropriately as specified in the documentation for the corresponding // message id. setMessageDetailConvertNumber(convertNumberMessageDetail); This way user can override detail part of the FacesMessage for different conversion errors that occur for wrong values, that arise during conversion. There is a one to one mapping for message customization which is as given below.

The methods used for customizing the detail message associated with each id is given below:

  1. CONVERT_PATTERN_MESSAGE_ID - setMessageDetailConvertPattern(String)
  2. CONVERT_NUMBER_MESSAGE_ID - setMessageDetailConvertNumber(String)
  3. CONVERT_CURRENCY_MESSAGE_ID - setMessageDetailConvertCurrency(String)
  4. CONVERT_PERCENT_MESSAGE_ID - setMessageDetailConvertPercent(String)
The custom messages can contain placeholders, which will be replaced with values as specified in its corresponding message id.

This NumberConverter is automatically registered under the standard converter ID, and therefore will be used when the <f:convertNumber> tag is used.

See Also:


Field Summary
static String CONVERT_CURRENCY_MESSAGE_ID
          The message identifier of the FacesMessage to be created if the input value is not a valid number when type is set to 'currency'.
static String CONVERT_NUMBER_MESSAGE_ID
          The message identifier of the FacesMessage to be created if the input value is not a valid number.
static String CONVERT_PATTERN_MESSAGE_ID
          The message identifier of the FacesMessage to be created if the input value does not match the specified pattern.
static String CONVERT_PERCENT_MESSAGE_ID
          The message identifier of the FacesMessage to be created if the input value is not a valid number when type is set to 'percent'.
static String CONVERTER_ID
          The standard converter id for this converter.
 
Fields inherited from class javax.faces.convert.NumberConverter
CURRENCY_ID, NUMBER_ID, PATTERN_ID, PERCENT_ID, STRING_ID
 
Constructor Summary
NumberConverter()
           
 
Method Summary
 boolean equals(Object numberConverter)
          Compares this NumberConverter with the specified Object for equality.
 Object getAsObject(FacesContext context, UIComponent component, String value)
          Performs strict conversion of string to number.
 String getAsString(FacesContext context, UIComponent component, Object value)
           
protected  FacesMessage getConvertMessage(FacesContext context, UIComponent component, String inputValue, Object[] params)
           
 String getCurrencyCode()
           
 String getCurrencySymbol()
           
 String getHintPattern()
          Return custom hintPattern message.
 Locale getLocale()
          Return the Locale that was set, returns null if it was not set, while faces RI returns the Locale set on the view root if the locale is null.
 String getLocalizedPattern(FacesContext context, String pattern, DecimalFormatSymbols dfs)
          If pattern contains the generic currency sign '�', this method will replace it with the localized currency symbol (if one exists).
 int getMaxFractionDigits()
           
 int getMaxIntegerDigits()
           
 String getMessageDetailConvertCurrency()
          Return custom detail error message that was set for creating FacesMessage, when conversion fails for values, when type is set to 'currency' and pattern is null or not set.
 String getMessageDetailConvertNumber()
          Return custom detail error message that was set for creating FacesMessage, when conversion fails for values, when type is set to 'number' and pattern is null or not set.
 String getMessageDetailConvertPattern()
          Custom detail error message that was set for creation of FacesMessage when conversion fails for values that does not match the pattern set.
 String getMessageDetailConvertPercent()
          Return custom detail error message that was set for creating FacesMessage, when conversion fails for values, when value cannot be converted to a number, when type is set to 'percent' and pattern is null or not set.
 int getMinFractionDigits()
           
 int getMinIntegerDigits()
           
 String getPattern()
           
 String getType()
           
 ValueBinding getValueBinding(String name)
          Deprecated.  
 ValueExpression getValueExpression(String name)
          Return the ValueExpression used to calculate the value for the specified attribute name, if any.
 int hashCode()
          Returns the hash code for this Converter.
 boolean isGroupingUsed()
           
 boolean isIntegerOnly()
           
protected  boolean isMaximumFractionDigitsSet()
           
protected  boolean isMaximumIntegerDigitsSet()
           
protected  boolean isMinimumFractionDigitsSet()
           
protected  boolean isMinimumIntegerDigitsSet()
           
 void restoreState(FacesContext context, Object state)
           
 Object saveState(FacesContext context)
           
 void setCurrencyCode(String currencyCode)
           
 void setCurrencySymbol(String currencySymbol)
           
 void setGroupingUsed(boolean groupingUsed)
           
 void setHintPattern(String hintPattern)
          Custom hintPattern message.
 void setIntegerOnly(boolean integerOnly)
           
 void setLocale(Locale locale)
          Set the Locale to be used when parsing numbers.
 void setMaxFractionDigits(int maxFractionDigits)
           
 void setMaxIntegerDigits(int maxIntegerDigits)
           
 void setMessageDetailConvertCurrency(String convertCurrencyMessageDetail)
          Custom error message to be used, for creating detail part of the FacesMessage, when value cannot be converted to a number, when type is set to 'currency' and pattern is null or not set.
 void setMessageDetailConvertNumber(String convertNumberMessageDetail)
          Custom error message to be used, for creating detail part of the FacesMessage, when value cannot be converted to a number, when type is set to 'number' and pattern is null or not set.
 void setMessageDetailConvertPattern(String convertPatternMessageDetail)
          Custom error message to be used, for creating detail part of the FacesMessage, message, when value cannot be converted to a number, based on the pattern set.
 void setMessageDetailConvertPercent(String convertPercentMessageDetail)
          Custom error message to be used, for creating detail part of the FacesMessage, when value cannot be converted to a number, when type is set to 'percent' and pattern is null or not set.
 void setMinFractionDigits(int minFractionDigits)
           
 void setMinIntegerDigits(int minIntegerDigits)
           
 void setPattern(String pattern)
           
 void setType(String type)
           
 void setValueBinding(String name, ValueBinding binding)
          Deprecated.  
 void setValueExpression(String name, ValueExpression expression)
          Set the ValueExpression used to calculate the value for the specified attribute if any.
 
Methods inherited from class javax.faces.convert.NumberConverter
isTransient, setTransient
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CONVERTER_ID

public static final String CONVERTER_ID

The standard converter id for this converter.

See Also:
Constant Field Values

CONVERT_PATTERN_MESSAGE_ID

public static final String CONVERT_PATTERN_MESSAGE_ID

The message identifier of the FacesMessage to be created if the input value does not match the specified pattern. The message format string for this message may optionally include {0}, {1} and {4} placeholders, which will be replaced with the input value, label associated with the component and the pattern respectively.

See Also:
Constant Field Values

CONVERT_NUMBER_MESSAGE_ID

public static final String CONVERT_NUMBER_MESSAGE_ID

The message identifier of the FacesMessage to be created if the input value is not a valid number. The message format string for this message may optionally include {0} and {1} placeholders, which will be replaced with the input value and label associated with the component respectively.

See Also:
Constant Field Values

CONVERT_CURRENCY_MESSAGE_ID

public static final String CONVERT_CURRENCY_MESSAGE_ID

The message identifier of the FacesMessage to be created if the input value is not a valid number when type is set to 'currency'. The message format string for this message may optionally include {0} and {1} placeholders, which will be replaced with the input value and label associated with the component respectively.

See Also:
Constant Field Values

CONVERT_PERCENT_MESSAGE_ID

public static final String CONVERT_PERCENT_MESSAGE_ID

The message identifier of the FacesMessage to be created if the input value is not a valid number when type is set to 'percent'. The message format string for this message may optionally include {0} and {1} placeholders, which will be replaced with the input value and label associated with the component respectively.

See Also:
Constant Field Values
Constructor Detail

NumberConverter

public NumberConverter()
Method Detail

getAsObject

public Object getAsObject(FacesContext context,
                          UIComponent component,
                          String value)
Performs strict conversion of string to number. Values having more than one decimal seprator like 22.22.22 and values of the form 22ABC, 22%ABC will result in ConverterException.

Specified by:
getAsObject in interface Converter
Overrides:
getAsObject in class NumberConverter

getAsString

public String getAsString(FacesContext context,
                          UIComponent component,
                          Object value)
Specified by:
getAsString in interface Converter
Overrides:
getAsString in class NumberConverter
Parameters:
context - FacesContext for the request being processed
component - UIComponent with which this model object value is associated.
value - Model object value to be converted (may be null)
Returns:
a zero-length String if value is null, if the passed value is a String, it's returned unchanged, otherwise String representation for the number object based on the attributes set.
Throws:
ConverterException - if conversion cannot be successfully performed
NullPointerException - if context or component is null
IllegalArgumentException - if the value is not of type other than Number, String. value can be null.

restoreState

public void restoreState(FacesContext context,
                         Object state)
Specified by:
restoreState in interface StateHolder
Overrides:
restoreState in class NumberConverter

saveState

public Object saveState(FacesContext context)
Specified by:
saveState in interface StateHolder
Overrides:
saveState in class NumberConverter

setValueExpression

public void setValueExpression(String name,
                               ValueExpression expression)

Set the ValueExpression used to calculate the value for the specified attribute if any.

Parameters:
name - Name of the attribute for which to set a ValueExpression
expression - The ValueExpression to set, or null to remove any currently set ValueExpression
Throws:
NullPointerException - if name is null
IllegalArgumentException - if name is not a valid attribute of this converter

getValueExpression

public ValueExpression getValueExpression(String name)

Return the ValueExpression used to calculate the value for the specified attribute name, if any.

Parameters:
name - Name of the attribute or property for which to retrieve a ValueExpression
Throws:
NullPointerException - if name is null
IllegalArgumentException - if name is not a valid attribute of this converter

setValueBinding

public void setValueBinding(String name,
                            ValueBinding binding)
Deprecated. 

Set the ValueBinding used to calculate the value for the specified attribute if any.

Parameters:
name - Name of the attribute for which to set a ValueBinding
binding - The ValueBinding to set, or null to remove any currently set ValueBinding
Throws:
NullPointerException - if name is null
IllegalArgumentException - if name is not a valid attribute of this converter

getValueBinding

public ValueBinding getValueBinding(String name)
Deprecated. 

Return the ValueBinding used to calculate the value for the specified attribute name, if any.

Parameters:
name - Name of the attribute or property for which to retrieve a ValueBinding
Throws:
NullPointerException - if name is null
IllegalArgumentException - if name is not a valid attribute of this converter

setMessageDetailConvertPattern

public void setMessageDetailConvertPattern(String convertPatternMessageDetail)
Custom error message to be used, for creating detail part of the FacesMessage, message, when value cannot be converted to a number, based on the pattern set. Overrides detail message identified by message id CONVERT_PATTERN_MESSAGE_ID

Parameters:
convertPatternMessageDetail - Custom error message.

getMessageDetailConvertPattern

public String getMessageDetailConvertPattern()
Custom detail error message that was set for creation of FacesMessage when conversion fails for values that does not match the pattern set.

Returns:
Custom error message.
See Also:
setMessageDetailConvertPattern(String)

setMessageDetailConvertNumber

public void setMessageDetailConvertNumber(String convertNumberMessageDetail)

Custom error message to be used, for creating detail part of the FacesMessage, when value cannot be converted to a number, when type is set to 'number' and pattern is null or not set.

Overrides detail message identified by message id CONVERT_NUMBER_MESSAGE_ID

Parameters:
convertNumberMessageDetail - custom error message.

getMessageDetailConvertNumber

public String getMessageDetailConvertNumber()

Return custom detail error message that was set for creating FacesMessage, when conversion fails for values, when type is set to 'number' and pattern is null or not set.

Returns:
Custom error message.
See Also:
setMessageDetailConvertNumber(String)

setMessageDetailConvertCurrency

public void setMessageDetailConvertCurrency(String convertCurrencyMessageDetail)
Custom error message to be used, for creating detail part of the FacesMessage, when value cannot be converted to a number, when type is set to 'currency' and pattern is null or not set.

Overrides detail message identified by message id CONVERT_CURRENCY_MESSAGE_ID.

Parameters:
convertCurrencyMessageDetail - custom error message.

getMessageDetailConvertCurrency

public String getMessageDetailConvertCurrency()

Return custom detail error message that was set for creating FacesMessage, when conversion fails for values, when type is set to 'currency' and pattern is null or not set.

Returns:
Custom error message.
See Also:
setMessageDetailConvertCurrency(String)

setMessageDetailConvertPercent

public void setMessageDetailConvertPercent(String convertPercentMessageDetail)
Custom error message to be used, for creating detail part of the FacesMessage, when value cannot be converted to a number, when type is set to 'percent' and pattern is null or not set.

Overrides detail message identified by message id CONVERT_PERCENT_MESSAGE_ID

Parameters:
convertPercentMessageDetail - custom error message.

getMessageDetailConvertPercent

public String getMessageDetailConvertPercent()

Return custom detail error message that was set for creating FacesMessage, when conversion fails for values, when value cannot be converted to a number, when type is set to 'percent' and pattern is null or not set.

Returns:
Custom error message.
See Also:
setMessageDetailConvertPercent(String)

setHintPattern

public void setHintPattern(String hintPattern)

Custom hintPattern message.

Overrides default hint message

Parameters:
hintPattern - Custom hint message.

getHintPattern

public String getHintPattern()

Return custom hintPattern message.

Returns:
Custom hint message.
See Also:
setHintPattern(String)

setCurrencyCode

public void setCurrencyCode(String currencyCode)
Overrides:
setCurrencyCode in class NumberConverter

getCurrencyCode

public String getCurrencyCode()
Overrides:
getCurrencyCode in class NumberConverter

setCurrencySymbol

public void setCurrencySymbol(String currencySymbol)
Overrides:
setCurrencySymbol in class NumberConverter

getCurrencySymbol

public String getCurrencySymbol()
Overrides:
getCurrencySymbol in class NumberConverter

setGroupingUsed

public void setGroupingUsed(boolean groupingUsed)
Overrides:
setGroupingUsed in class NumberConverter

isGroupingUsed

public boolean isGroupingUsed()
Overrides:
isGroupingUsed in class NumberConverter

setIntegerOnly

public void setIntegerOnly(boolean integerOnly)
Overrides:
setIntegerOnly in class NumberConverter

isIntegerOnly

public boolean isIntegerOnly()
Overrides:
isIntegerOnly in class NumberConverter

setLocale

public void setLocale(Locale locale)

Set the Locale to be used when parsing numbers. If set to null, the Locale stored in the UIViewRoot for the current request will be utilized.

Overrides:
setLocale in class NumberConverter
Parameters:
locale - The new Locale (or null)

getLocale

public Locale getLocale()

Return the Locale that was set, returns null if it was not set, while faces RI returns the Locale set on the view root if the locale is null. If this value is null, the Locale stored in the UIViewRoot for the current request will be utilized during parsing.

Overrides:
getLocale in class NumberConverter

setMaxFractionDigits

public void setMaxFractionDigits(int maxFractionDigits)
Overrides:
setMaxFractionDigits in class NumberConverter

getMaxFractionDigits

public int getMaxFractionDigits()
Overrides:
getMaxFractionDigits in class NumberConverter

setMaxIntegerDigits

public void setMaxIntegerDigits(int maxIntegerDigits)
Overrides:
setMaxIntegerDigits in class NumberConverter

getMaxIntegerDigits

public int getMaxIntegerDigits()
Overrides:
getMaxIntegerDigits in class NumberConverter

setMinFractionDigits

public void setMinFractionDigits(int minFractionDigits)
Overrides:
setMinFractionDigits in class NumberConverter

getMinFractionDigits

public int getMinFractionDigits()
Overrides:
getMinFractionDigits in class NumberConverter

setMinIntegerDigits

public void setMinIntegerDigits(int minIntegerDigits)
Overrides:
setMinIntegerDigits in class NumberConverter

getMinIntegerDigits

public int getMinIntegerDigits()
Overrides:
getMinIntegerDigits in class NumberConverter

setPattern

public void setPattern(String pattern)
Overrides:
setPattern in class NumberConverter

getPattern

public String getPattern()
Overrides:
getPattern in class NumberConverter

getLocalizedPattern

public String getLocalizedPattern(FacesContext context,
                                  String pattern,
                                  DecimalFormatSymbols dfs)
If pattern contains the generic currency sign '�', this method will replace it with the localized currency symbol (if one exists).

Parameters:
context - the FacesContext
pattern - the pattern to be localized
dfs - the DecimalFormatSymbols; if null, will be constructed from the context
Returns:

setType

public void setType(String type)
Overrides:
setType in class NumberConverter

getType

public String getType()
Overrides:
getType in class NumberConverter

hashCode

public int hashCode()
Returns the hash code for this Converter.

Overrides:
hashCode in class Object

equals

public boolean equals(Object numberConverter)

Compares this NumberConverter with the specified Object for equality.

Overrides:
equals in class Object

getConvertMessage

protected final FacesMessage getConvertMessage(FacesContext context,
                                               UIComponent component,
                                               String inputValue,
                                               Object[] params)

isMaximumFractionDigitsSet

protected boolean isMaximumFractionDigitsSet()

isMinimumFractionDigitsSet

protected boolean isMinimumFractionDigitsSet()

isMaximumIntegerDigitsSet

protected boolean isMaximumIntegerDigitsSet()

isMinimumIntegerDigitsSet

protected boolean isMinimumIntegerDigitsSet()


Copyright © 2001-2011 The Apache Software Foundation. All Rights Reserved.