You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by de...@apache.org on 2017/09/17 12:30:43 UTC

[myfaces-trinidad] 20/36: TRINIDAD-1683 client-side convertNumber causes loss of precision

This is an automated email from the ASF dual-hosted git repository.

deki pushed a commit to branch 1.2.12.2-branch
in repository https://gitbox.apache.org/repos/asf/myfaces-trinidad.git

commit 5601f68d8aca2186ebcd69bd7132f5928a332926
Author: Gabrielle Crawford <gc...@apache.org>
AuthorDate: Tue Feb 23 18:55:21 2010 +0000

    TRINIDAD-1683 client-side convertNumber causes loss of precision
---
 .../trinidadinternal/convert/NumberConverter.java  | 41 ++++++++++++++++++++++
 .../trinidadinternal/resource/LoggerBundle.xrts    |  3 ++
 2 files changed, 44 insertions(+)

diff --git a/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/convert/NumberConverter.java b/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/convert/NumberConverter.java
index 71df002..23cb8cc 100644
--- a/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/convert/NumberConverter.java
+++ b/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/convert/NumberConverter.java
@@ -25,6 +25,8 @@ import java.util.HashMap;
 import java.util.Locale;
 import java.util.Map;
 
+import javax.el.ValueExpression;
+
 import javax.faces.component.UIComponent;
 import javax.faces.context.FacesContext;
 import javax.faces.convert.ConverterException;
@@ -193,6 +195,45 @@ public class NumberConverter extends org.apache.myfaces.trinidad.convert.NumberC
       UIComponent  component,
       Map<?, ?>    messages)
     {
+      Object value = component.getAttributes().get("value");
+      if (value == null)
+      {
+        ValueExpression expression = component.getValueExpression("value");
+        if (expression != null)
+        {
+          value = expression.getValue(context.getELContext());
+        
+          // If value is bound to an Object that is initially null, then we must devise another way 
+          // to determine the type of the Object we are bound to, as there's no way for null to be 
+          // interpreted as any type other than Object; thus, we determine the type then instantiate 
+          // an Object of that type, initializing it to an arbitrarily chosen value. 
+          // Specifically, it is assumed that value is bound to a numeric type that provides a one 
+          // argument constructor that takes a String, so we instantiate an object of that type with 
+          // an arbitrarily chosen value of "0".
+          if (value == null)
+          {
+            try
+            {
+              value = expression.getType(context.getELContext()).getConstructor(String.class).newInstance("0");
+            }
+            catch (Exception e)
+            {
+              _LOG.warning(e.getLocalizedMessage());
+            }
+          }
+        }
+      }
+      
+      // Only render a client converter if the input value is bound to a supported type 
+      // (Float, Double, Integer, Short, Byte). The JavaScript number is a 64-bit floating type and 
+      // has enough precision to represent any of these supported types.
+      if (!(value instanceof Float || value instanceof Double || value instanceof Integer 
+            || value instanceof Short || value instanceof Byte))
+      {
+        _LOG.warning("UNSUPPORTED_NUMBERCONVERTER_TYPE");
+        return null;
+      }
+      
       StringBuilder outBuffer = new StringBuilder(250);
       outBuffer.append("new TrNumberConverter(");
 
diff --git a/trinidad-impl/src/main/xrts/org/apache/myfaces/trinidadinternal/resource/LoggerBundle.xrts b/trinidad-impl/src/main/xrts/org/apache/myfaces/trinidadinternal/resource/LoggerBundle.xrts
index f8e7a71..cb51498 100644
--- a/trinidad-impl/src/main/xrts/org/apache/myfaces/trinidadinternal/resource/LoggerBundle.xrts
+++ b/trinidad-impl/src/main/xrts/org/apache/myfaces/trinidadinternal/resource/LoggerBundle.xrts
@@ -1042,6 +1042,9 @@ The skin {0} specified on the requestMap will be used even though the consumer''
 
  <!-- FILEDOWNLOADACTIONLISTENERS_METHOD_MUST_BE_EL_EXPRESSION -->
  <resource key="FILEDOWNLOADACTIONLISTENERS_METHOD_MUST_BE_EL_EXPRESSION">fileDownloadActionListener's 'method' attribute must be an EL expression.</resource>
+ 
+ <!-- UNSUPPORTED_NUMBERCONVERTER_TYPE -->
+ <resource key="UNSUPPORTED_NUMBERCONVERTER_TYPE">Only a server-side NumberConverter will be available; a client-side NumberConverter is provided only if the input value is bound to one of the following supported types: Float Double Integer Short Byte</resource>
 
 <!-- STATUS_INDICATOR_MISSING_ICONS -->
 <resource key="STATUS_INDICATOR_MISSING_ICONS">The statusIndicator component requires both a 'ready' and 'busy' icon;  one is missing.</resource>

-- 
To stop receiving notification emails like this one, please contact
"commits@myfaces.apache.org" <co...@myfaces.apache.org>.