LANG-948: Exception while using ExtendedMessageFormat and escaping braces. This fixes #19 from github. Thanks to Andrey Khobnya.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1666679 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Benedikt Ritter 2015-03-14 12:46:02 +00:00
parent cbeb45b243
commit bf6ee5c56e
3 changed files with 32 additions and 20 deletions

View File

@ -22,6 +22,7 @@
<body>
<release version="3.4" date="tba" description="tba">
<action issue="LANG-948" type="fix" dev="britter" due-to="Andrey Khobnya">Exception while using ExtendedMessageFormat and escaping braces</action>
<action issue="LANG-1098" type="update" dev="britter" due-to="Mikhail Mazurskiy, Fabian Lange">Avoid String allocation in StrBuilder.append(CharSequence)</action>
<action issue="LANG-1098" type="update" dev="britter" due-to="Michał Kordas">Update maven-checkstyle-plugin to 2.14</action>
<action issue="LANG-1097" type="update" dev="britter" due-to="Michał Kordas">Update org.easymock:easymock to 3.3.1</action>

View File

@ -157,7 +157,7 @@ public class ExtendedMessageFormat extends MessageFormat {
while (pos.getIndex() < pattern.length()) {
switch (c[pos.getIndex()]) {
case QUOTE:
appendQuotedString(pattern, pos, stripCustom, true);
appendQuotedString(pattern, pos, stripCustom);
break;
case START_FE:
fmtCount++;
@ -384,7 +384,7 @@ public class ExtendedMessageFormat extends MessageFormat {
}
break;
case QUOTE:
getQuotedString(pattern, pos, false);
getQuotedString(pattern, pos);
break;
default:
break;
@ -413,7 +413,7 @@ public class ExtendedMessageFormat extends MessageFormat {
final char c = pattern.charAt(pos.getIndex());
switch (c) {
case QUOTE:
appendQuotedString(pattern, pos, sb, false);
appendQuotedString(pattern, pos, sb);
break;
case START_FE:
depth++;
@ -471,26 +471,23 @@ public class ExtendedMessageFormat extends MessageFormat {
* @param pattern pattern to parse
* @param pos current parse position
* @param appendTo optional StringBuilder to append
* @param escapingOn whether to process escaped quotes
* @return <code>appendTo</code>
*/
private StringBuilder appendQuotedString(final String pattern, final ParsePosition pos,
final StringBuilder appendTo, final boolean escapingOn) {
final StringBuilder appendTo) {
assert pattern.toCharArray()[pos.getIndex()] == QUOTE :
"Quoted string must start with quote character";
// handle quote character at the beginning of the string
if(appendTo != null) {
appendTo.append(QUOTE);
}
next(pos);
final int start = pos.getIndex();
final char[] c = pattern.toCharArray();
if (escapingOn && c[start] == QUOTE) {
next(pos);
return appendTo == null ? null : appendTo.append(QUOTE);
}
int lastHold = start;
for (int i = pos.getIndex(); i < pattern.length(); i++) {
if (escapingOn && pattern.substring(i).startsWith(ESCAPED_QUOTE)) {
appendTo.append(c, lastHold, pos.getIndex() - lastHold).append(
QUOTE);
pos.setIndex(i + ESCAPED_QUOTE.length());
lastHold = pos.getIndex();
continue;
}
switch (c[pos.getIndex()]) {
case QUOTE:
next(pos);
@ -509,11 +506,9 @@ public class ExtendedMessageFormat extends MessageFormat {
*
* @param pattern pattern to parse
* @param pos current parse position
* @param escapingOn whether to process escaped quotes
*/
private void getQuotedString(final String pattern, final ParsePosition pos,
final boolean escapingOn) {
appendQuotedString(pattern, pos, null, escapingOn);
private void getQuotedString(final String pattern, final ParsePosition pos) {
appendQuotedString(pattern, pos, null);
}
/**

View File

@ -89,6 +89,22 @@ public class ExtendedMessageFormatTest {
assertEquals(emf.format(new Object[] {"there", 3, "great"}), "Hi there, got 3, GREAT!");
}
/**
* Test Bug LANG-948 - Exception while using ExtendedMessageFormat and escaping braces
*/
@Test
public void testEscapedBraces_LANG_948() {
// message without placeholder because braces are escaped by quotes
final String pattern = "Message without placeholders '{}'";
final ExtendedMessageFormat emf = new ExtendedMessageFormat(pattern, registry);
assertEquals("Message without placeholders {}", emf.format(new Object[] {"DUMMY"}));
// message with placeholder because quotes are escaped by quotes
final String pattern2 = "Message with placeholder ''{0}''";
final ExtendedMessageFormat emf2 = new ExtendedMessageFormat(pattern2, registry);
assertEquals("Message with placeholder 'DUMMY'", emf2.format(new Object[] {"DUMMY"}));
}
/**
* Test extended and built in formats.
*/