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

jleroux pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/ofbiz-framework.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 74262bafe4 Fixed: Support non-breaking spaces in numeric strings 
(OFBIZ-13168)
74262bafe4 is described below

commit 74262bafe4da8392086d6fbe51844ea709cd911d
Author: Jacques Le Roux <jacques.le.r...@les7arts.com>
AuthorDate: Tue Nov 12 19:17:49 2024 +0100

    Fixed: Support non-breaking spaces in numeric strings (OFBIZ-13168)
    
    In forms, numeric fields are represented by an input with type "text", which
    allows user to enter/paste all characters, including non-breaking spaces
    ('\u00A0', '\u202F', '\u2007'), like "29 000" (the space is \u202F).
    
    More specifically, a user can copy/paste a string from an external tool 
which
    uses non-breaking spaces as a thousands separator, and expect that a 
visually
    correct string will be correctly interpreted by OFBiz.
    
    OFBiz uses java.text.NumberFormat::parse method, which does not support
    non-breaking spaces : characters after this kind of spaces are simply 
ignored,
    and "29 000" becomes "29".
    
    Using [^\\p{IsAlnum}\\p{IsPunct}] regexp this replaces all chars that can be
    a problem for NumberFormat class.
---
 .../src/main/java/org/apache/ofbiz/base/util/ObjectType.java     | 9 +++------
 .../test/java/org/apache/ofbiz/base/util/ObjectTypeTests.java    | 2 +-
 2 files changed, 4 insertions(+), 7 deletions(-)

diff --git 
a/framework/base/src/main/java/org/apache/ofbiz/base/util/ObjectType.java 
b/framework/base/src/main/java/org/apache/ofbiz/base/util/ObjectType.java
index 74b54b8032..67b73acb61 100644
--- a/framework/base/src/main/java/org/apache/ofbiz/base/util/ObjectType.java
+++ b/framework/base/src/main/java/org/apache/ofbiz/base/util/ObjectType.java
@@ -331,13 +331,10 @@ public class ObjectType {
         }
 
         if (converter != null) {
-            // numeric types : replace non-breaking spaces (which break 
parsing) by normal spaces
-            List<?> numericClasses = UtilMisc.toList(BigDecimal.class, 
Double.class, Float.class);
+            // numeric types : replace everything that's not in [:IsAlnum:] or 
[:IsPunct:] classes by an empty string
+            List<?> numericClasses = UtilMisc.toList(BigDecimal.class, 
Double.class, Float.class, Long.class);
             if (obj instanceof String && numericClasses.contains(targetClass)) 
{
-                final String[] tmp = {String.valueOf(obj)};
-                List<Character> nonBreakingWhitespaces = 
UtilMisc.toList('\u00A0', '\u202F', '\u2007');
-                nonBreakingWhitespaces.forEach(character -> tmp[0] = 
tmp[0].replace(character, ' '));
-                obj = tmp[0];
+                obj = ((String) obj).replaceAll("[^\\p{IsAlnum}\\p{IsPunct}]", 
"");
             }
 
             if (converter instanceof LocalizedConverter) {
diff --git 
a/framework/base/src/test/java/org/apache/ofbiz/base/util/ObjectTypeTests.java 
b/framework/base/src/test/java/org/apache/ofbiz/base/util/ObjectTypeTests.java
index 0afdd88b73..0a0318d5ff 100644
--- 
a/framework/base/src/test/java/org/apache/ofbiz/base/util/ObjectTypeTests.java
+++ 
b/framework/base/src/test/java/org/apache/ofbiz/base/util/ObjectTypeTests.java
@@ -390,7 +390,7 @@ public class ObjectTypeTests {
         // usual pattern assumes that the String->BigDecimal conversion will 
break with bad timezone/locale
         // which is not the case for this particular test
         assertEquals("String->BigDecimal supports NBSP",
-                simpleTypeOrObjectConvert("29 000", "BigDecimal", null, 
LOCALE_DATA.goodTimeZone,
+                simpleTypeOrObjectConvert("29 000", "BigDecimal", null, 
LOCALE_DATA.goodTimeZone,
                         LOCALE_DATA.goodLocale, false), largeBigDecimal);
     }
 

Reply via email to