You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opennlp.apache.org by ma...@apache.org on 2026/03/15 18:46:46 UTC
(opennlp) 03/03: OPENNLP-1735: NumberUtil: Replace Locale.of() with Locale.forLanguageTag() to avoid IllformedLocaleException on malformed language codes, preserving the documented return-false / IllegalArgumentException behavior
This is an automated email from the ASF dual-hosted git repository.
mawiesne pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/opennlp.git
commit c04344c19263824365584e1fbcb7c1887305c681
Author: Richard Zowalla <rz...@apache.org>
AuthorDate: Sun Mar 15 19:30:59 2026 +0100
OPENNLP-1735: NumberUtil: Replace Locale.of() with Locale.forLanguageTag() to avoid IllformedLocaleException on malformed language codes, preserving the documented return-false / IllegalArgumentException behavior
---
.../java/opennlp/uima/normalizer/NumberUtil.java | 23 +++++++++++-----------
1 file changed, 11 insertions(+), 12 deletions(-)
diff --git a/opennlp-extensions/opennlp-uima/src/main/java/opennlp/uima/normalizer/NumberUtil.java b/opennlp-extensions/opennlp-uima/src/main/java/opennlp/uima/normalizer/NumberUtil.java
index f14df8ec..bc1dc6b9 100644
--- a/opennlp-extensions/opennlp-uima/src/main/java/opennlp/uima/normalizer/NumberUtil.java
+++ b/opennlp-extensions/opennlp-uima/src/main/java/opennlp/uima/normalizer/NumberUtil.java
@@ -36,21 +36,19 @@ public final class NumberUtil {
* @return true if the language is supported
*/
public static boolean isLanguageSupported(String languageCode) {
- Locale locale = Locale.of(languageCode);
+ Locale locale = Locale.forLanguageTag(languageCode);
- Locale[] possibleLocales = NumberFormat.getAvailableLocales();
-
- boolean isLocaleSupported = false;
+ if (locale.getLanguage().isEmpty()) {
+ return false;
+ }
- for (Locale possibleLocale : possibleLocales) {
- // search if locale is contained
+ for (Locale possibleLocale : NumberFormat.getAvailableLocales()) {
if (possibleLocale.equals(locale)) {
- isLocaleSupported = true;
- break;
+ return true;
}
}
- return isLocaleSupported;
+ return false;
}
/**
@@ -58,7 +56,7 @@ public final class NumberUtil {
*
* @param number The suspected number to parse.
* @param languageCode A ISO conform language code, e.g. "en", "pt"
- *
+ *
* @return The parsed {@link Number}.
* @throws ParseException Thrown if errors occurred during parsing.
* @throws IllegalArgumentException Thrown if the {@code languageCode} is not supported.
@@ -66,11 +64,12 @@ public final class NumberUtil {
public static Number parse(String number, String languageCode)
throws ParseException {
- if (!isLanguageSupported(languageCode)) {
+ Locale locale = Locale.forLanguageTag(languageCode);
+
+ if (locale.getLanguage().isEmpty() || !isLanguageSupported(languageCode)) {
throw new IllegalArgumentException("Language " + languageCode + " is not supported!");
}
- Locale locale = Locale.of(languageCode);
NumberFormat numberFormat = NumberFormat.getInstance(locale);
number = WHITESPACE_PATTERN.matcher(number).replaceAll("");
return numberFormat.parse(number);