Sergi Parpal created LANG-1842:
----------------------------------
Summary: ExtendedMessageFormat does not trim a format name
followed by whitespace
Key: LANG-1842
URL: https://issues.apache.org/jira/browse/LANG-1842
Project: Commons Lang
Issue Type: Bug
Components: lang.text.*
Affects Versions: 3.22.0, 2.6
Reporter: Sergi Parpal
The class Javadoc of {{org.apache.commons.lang3.text.ExtendedMessageFormat}}
says:
{quote}format-name and format-style values are trimmed of surrounding
whitespace in the manner of java.text.MessageFormat.{quote}
A format name followed by whitespace is trimmed only when a format style
follows it. Without a style, the name is looked up in the registry untrimmed
and is not found, so the element is left to {{java.text.MessageFormat}}, which
rejects it. With the registry of {{ExtendedMessageFormatTest}}, where "upper"
is an {{UpperCaseFormatFactory}}:
{code:java}
new ExtendedMessageFormat("{0,upper}", registry); // works
new ExtendedMessageFormat("{0, upper}", registry); // works
new ExtendedMessageFormat("{0,upper ,x}", registry); // works
new ExtendedMessageFormat("{0,upper }", registry); //
IllegalArgumentException: "unknown format type: upper "
new ExtendedMessageFormat("{ 0 , upper }", registry); //
IllegalArgumentException: "unknown format type: upper "
{code}
With 3.22.0 on Java 11:
{noformat}
java.lang.IllegalArgumentException: unknown format type: upper
at java.base/java.text.MessageFormat.makeFormat(MessageFormat.java:1559)
at
java.base/java.text.MessageFormat.applyPattern(MessageFormat.java:492)
at
org.apache.commons.lang3.text.ExtendedMessageFormat.applyPattern(ExtendedMessageFormat.java:244)
at
org.apache.commons.lang3.text.ExtendedMessageFormat.<init>(ExtendedMessageFormat.java:138)
{noformat}
{{java.text.MessageFormat}} itself accepts whitespace after a format type:
{code:java}
new MessageFormat("{0,number }", Locale.ROOT).format(new Object[] { 1.5 }); //
"1.5"
{code}
*Cause*
{{getFormat(String)}} trims the name only when the description contains a comma:
{code:java}
String name = desc;
String args = null;
final int i = desc.indexOf(START_FMT);
if (i > 0) {
name = desc.substring(0, i).trim();
args = desc.substring(i + 1).trim();
}
{code}
The code is the same as when the class was added in 2.4. [PR
#1701|https://github.com/apache/commons-lang/pull/1701] fixed a different
problem in this class: a pattern that ends inside an argument index.
{{ExtendedMessageFormat}} is deprecated in favour of Commons Text, where the
same class has the same bug: TEXT-245.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)