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:
javax.faces.component.PartialStateHolder, javax.faces.component.StateHolder, javax.faces.convert.Converter

@JSFConverter(configExcluded=true)
public class NumberConverter
extends javax.faces.convert.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(javax.faces.context.FacesContext context, javax.faces.component.UIComponent component, String value)
          Performs strict conversion of string to number.
 String getAsString(javax.faces.context.FacesContext context, javax.faces.component.UIComponent component, Object value)
           
protected  javax.faces.application.FacesMessage getConvertMessage(javax.faces.context.FacesContext context, javax.faces.component.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(javax.faces.context.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()
           
 javax.faces.el.ValueBinding getValueBinding(String name)
          Deprecated.  
 javax.el.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 isDisabled()
          Return whether it is disabled.
 boolean isGroupingUsed()
           
 boolean isIntegerOnly()
           
 boolean isMaximumFractionDigitsSet()
          Return true if the maximum fraction digits have been set.
 boolean isMaximumIntegerDigitsSet()
          Return true if the maximum integer digits have been set.
 boolean isMinimumFractionDigitsSet()
          Return true if the minimum fraction digits have been set.
 boolean isMinimumIntegerDigitsSet()
          Return true if the minimum integer digits have been set.
 void restoreState(javax.faces.context.FacesContext context, Object state)
           
 Object saveState(javax.faces.context.FacesContext context)
           
 void setCurrencyCode(String currencyCode)
           
 void setCurrencySymbol(String currencySymbol)
           
 void setDisabled(boolean isDisabled)
          Set the value to property disabled.
 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, javax.faces.el.ValueBinding binding)
          Deprecated.  
 void setValueExpression(String name, javax.el.ValueExpression expression)
          Set the ValueExpression used to calculate the value for the specified attribute if any.
 
Methods inherited from class javax.faces.convert.NumberConverter
clearInitialState, initialStateMarked, isTransient, markInitialState, 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(javax.faces.context.FacesContext context,
                          javax.faces.component.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 javax.faces.convert.Converter
Overrides:
getAsObject in class javax.faces.convert.NumberConverter

getAsString

public String getAsString(javax.faces.context.FacesContext context,
                          javax.faces.component.UIComponent component,
                          Object value)
Specified by:
getAsString in interface javax.faces.convert.Converter
Overrides:
getAsString in class javax.faces.convert.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:
javax.faces.convert.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(javax.faces.context.FacesContext context,
                         Object state)
Specified by:
restoreState in interface javax.faces.component.StateHolder
Overrides:
restoreState in class javax.faces.convert.NumberConverter

saveState

public Object saveState(javax.faces.context.FacesContext context)
Specified by:
saveState in interface javax.faces.component.StateHolder
Overrides:
saveState in class javax.faces.convert.NumberConverter

setValueExpression

public void setValueExpression(String name,
                               javax.el.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 javax.el.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,
                            javax.faces.el.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 javax.faces.el.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

@JSFProperty
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

@JSFProperty
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

@JSFProperty
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

@JSFProperty
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 javax.faces.convert.NumberConverter

getCurrencyCode

@JSFProperty
public String getCurrencyCode()
Overrides:
getCurrencyCode in class javax.faces.convert.NumberConverter

setCurrencySymbol

public void setCurrencySymbol(String currencySymbol)
Overrides:
setCurrencySymbol in class javax.faces.convert.NumberConverter

getCurrencySymbol

@JSFProperty
public String getCurrencySymbol()
Overrides:
getCurrencySymbol in class javax.faces.convert.NumberConverter

setGroupingUsed

public void setGroupingUsed(boolean groupingUsed)
Overrides:
setGroupingUsed in class javax.faces.convert.NumberConverter

isGroupingUsed

@JSFProperty(defaultValue="true")
public boolean isGroupingUsed()
Overrides:
isGroupingUsed in class javax.faces.convert.NumberConverter

setIntegerOnly

public void setIntegerOnly(boolean integerOnly)
Overrides:
setIntegerOnly in class javax.faces.convert.NumberConverter

isIntegerOnly

@JSFProperty(defaultValue="false")
public boolean isIntegerOnly()
Overrides:
isIntegerOnly in class javax.faces.convert.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 javax.faces.convert.NumberConverter
Parameters:
locale - The new Locale (or null)

getLocale

@JSFProperty
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 javax.faces.convert.NumberConverter

setMaxFractionDigits

public void setMaxFractionDigits(int maxFractionDigits)
Overrides:
setMaxFractionDigits in class javax.faces.convert.NumberConverter

getMaxFractionDigits

@JSFProperty
public int getMaxFractionDigits()
Overrides:
getMaxFractionDigits in class javax.faces.convert.NumberConverter

setMaxIntegerDigits

public void setMaxIntegerDigits(int maxIntegerDigits)
Overrides:
setMaxIntegerDigits in class javax.faces.convert.NumberConverter

getMaxIntegerDigits

@JSFProperty
public int getMaxIntegerDigits()
Overrides:
getMaxIntegerDigits in class javax.faces.convert.NumberConverter

setMinFractionDigits

public void setMinFractionDigits(int minFractionDigits)
Overrides:
setMinFractionDigits in class javax.faces.convert.NumberConverter

getMinFractionDigits

@JSFProperty
public int getMinFractionDigits()
Overrides:
getMinFractionDigits in class javax.faces.convert.NumberConverter

setMinIntegerDigits

public void setMinIntegerDigits(int minIntegerDigits)
Overrides:
setMinIntegerDigits in class javax.faces.convert.NumberConverter

getMinIntegerDigits

@JSFProperty
public int getMinIntegerDigits()
Overrides:
getMinIntegerDigits in class javax.faces.convert.NumberConverter

setPattern

public void setPattern(String pattern)
Overrides:
setPattern in class javax.faces.convert.NumberConverter

getPattern

@JSFProperty
public String getPattern()
Overrides:
getPattern in class javax.faces.convert.NumberConverter

getLocalizedPattern

public String getLocalizedPattern(javax.faces.context.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 javax.faces.convert.NumberConverter

getType

@JSFProperty(defaultValue="number")
public String getType()
Overrides:
getType in class javax.faces.convert.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

setDisabled

public void setDisabled(boolean isDisabled)

Set the value to property disabled. Default value is false.

Parameters:
isDisabled - true if it's disabled, false otherwise.

isDisabled

public boolean isDisabled()
Return whether it is disabled.

Returns:
true if it's disabled and false if it's enabled.

getConvertMessage

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

isMaximumFractionDigitsSet

public boolean isMaximumFractionDigitsSet()
Return true if the maximum fraction digits have been set. If not set, return false;

Returns:
true, if the maximum fraction digits have been set.

isMinimumFractionDigitsSet

public boolean isMinimumFractionDigitsSet()
Return true if the minimum fraction digits have been set. If not set, return false;

Returns:
true, if the minimum fraction digits have been set.

isMaximumIntegerDigitsSet

public boolean isMaximumIntegerDigitsSet()
Return true if the maximum integer digits have been set. If not set, return false;

Returns:
true, if the maximum integer digits have been set.

isMinimumIntegerDigitsSet

public boolean isMinimumIntegerDigitsSet()
Return true if the minimum integer digits have been set. If not set, return false;

Returns:
true, if the minimum integer digits have been set.


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