Sergi Parpal created TEXT-245:
---------------------------------
Summary: ExtendedMessageFormat does not trim a format name
followed by whitespace
Key: TEXT-245
URL: https://issues.apache.org/jira/browse/TEXT-245
Project: Commons Text
Issue Type: Bug
Affects Versions: 1.15.0, 1.0
Reporter: Sergi Parpal
The class Javadoc 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 {{UpperCaseFormat.FACTORY}}:
{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 1.15.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.text.ExtendedMessageFormat.applyPattern(ExtendedMessageFormat.java:247)
at
org.apache.commons.text.ExtendedMessageFormat.<init>(ExtendedMessageFormat.java:146)
{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 in 1.0. [PR
#750|https://github.com/apache/commons-text/pull/750] fixed a different problem
in this class: a pattern that ends inside an argument index.
Commons Lang's deprecated
{{org.apache.commons.lang3.text.ExtendedMessageFormat}} has the same code; I am
reporting it separately in LANG.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)