diff --git a/commons/src/main/java/com/itextpdf/commons/utils/MessageFormatUtil.java b/commons/src/main/java/com/itextpdf/commons/utils/MessageFormatUtil.java index 39d6e3ba41..8fc1586fec 100644 --- a/commons/src/main/java/com/itextpdf/commons/utils/MessageFormatUtil.java +++ b/commons/src/main/java/com/itextpdf/commons/utils/MessageFormatUtil.java @@ -61,82 +61,31 @@ private MessageFormatUtil() { * Indexed arguments can be referred with {index}, * to escape curly braces you have to double them. * + *
+ * Only basic escaping is allowed, single quotes in a set of curly braces are not supported and + * multiple escaped braces in a row are also not supported + * + *
+ * Allowed {{{0}}} + * Allowed '{0}' + * Allowed '{{{0}}}' + * + *
+ * Not allowed {{'{0}'}} + * Not allowed {{{{{0}}}}} + * * @param pattern to format * @param arguments arguments * * @return The formatted string */ public static String format(String pattern, Object... arguments) { - boolean mustClose = false; - StringBuilder result = new StringBuilder(pattern.length()); - int i = 0; - int n = pattern.length(); - while (i < n) { - char current = pattern.charAt(i); - switch (current) { - case '{': { - int curlyCount = 0; - int j; - for (j = i; j < n && pattern.charAt(j) == '{'; j++, curlyCount++) - ; - i += curlyCount - 1; - if (curlyCount > 1) { - if (!mustClose) { - result.append("'"); - } - while (curlyCount >= 2) { - result.append('{'); - curlyCount -= 2; - } - mustClose = true; - } - if (curlyCount == 1) { - if (mustClose) { - result.append('\''); - } - result.append('{'); - mustClose = false; - } - } - break; - case '}': { - int curlyCount = 0; - int j; - for (j = i; j < n && pattern.charAt(j) == '}'; j++, curlyCount++) - ; - i += curlyCount - 1; - if (curlyCount % 2 == 1) { - if (mustClose) { - result.append('\''); - } - result.append('}'); - mustClose = false; - } - if (curlyCount > 1) { - result.append("'"); - while (curlyCount >= 2) { - result.append('}'); - curlyCount -= 2; - } - mustClose = true; - } - } - break; - case '\'': - result.append("''"); - break; - default: - if (mustClose) { - result.append('\''); - mustClose = false; - } - result.append(current); - } - i++; - } - if (mustClose) { - result.append('\''); - } - return new MessageFormat(result.toString(), Locale.ROOT).format(arguments); + return new MessageFormat( + pattern.replace("'", "''") + .replace("{{{","'{'{" ) + .replace("}}}","}'}'" ) + .replace("{{","'{'" ) + .replace("}}","'}'" ) + ,Locale.ROOT).format(arguments); } } diff --git a/commons/src/test/java/com/itextpdf/commons/utils/MessageFormatUtilTest.java b/commons/src/test/java/com/itextpdf/commons/utils/MessageFormatUtilTest.java index d4a49a7c43..e1689d255b 100644 --- a/commons/src/test/java/com/itextpdf/commons/utils/MessageFormatUtilTest.java +++ b/commons/src/test/java/com/itextpdf/commons/utils/MessageFormatUtilTest.java @@ -45,14 +45,12 @@ public class MessageFormatUtilTest extends ExtendedITextTest { private String pattern; private Object[] arguments; - public MessageFormatUtilTest(Object expectedResult, Object pattern, Object arguments, Object name) { this.expectedResult = (String)expectedResult; this.pattern = (String)pattern; this.arguments = (Object[]) arguments; } - @Parameterized.Parameters(name = "{index}: {3} format: {1}; {0}") public static Iterable