This is an automated email from the ASF dual-hosted git repository. jleroux pushed a commit to branch release24.09 in repository https://gitbox.apache.org/repos/asf/ofbiz-framework.git
The following commit(s) were added to refs/heads/release24.09 by this push: new 50feff8675 Fixed: Support non-breaking spaces in numeric strings (OFBIZ-13168) 50feff8675 is described below commit 50feff86755799f993eabc185b922610903073f6 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. Conflicts handled by hand in ObjectType.java ObjectTypeTests.java --- .../base/src/main/java/org/apache/ofbiz/base/util/ObjectType.java | 6 ++++++ .../src/test/java/org/apache/ofbiz/base/util/ObjectTypeTests.java | 6 ++++++ 2 files changed, 12 insertions(+) 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 6e98f5bd66..b2d2cdd973 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 @@ -330,6 +330,12 @@ public class ObjectType { } if (converter != null) { + // 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)) { + obj = ((String) obj).replaceAll("[^\\p{IsAlnum}\\p{IsPunct}]", ""); + } + if (converter instanceof LocalizedConverter) { LocalizedConverter<Object, Object> localizedConverter = UtilGenerics.cast(converter); if (timeZone == null) { 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 c6308597fd..4b5ace55f5 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 @@ -385,6 +385,12 @@ public class ObjectTypeTests { new String[] {"TimeDuration", "org.apache.ofbiz.base.util.TimeDuration"}, duration); simpleTypeOrObjectConvertTestError("String->error-TimeDuration", "o", new String[] {"TimeDuration", "org.apache.ofbiz.base.util.TimeDuration"}); + + // 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, + LOCALE_DATA.goodLocale, false), largeBigDecimal); } @Test