commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From brit...@apache.org
Subject svn commit: r1666679 - in /commons/proper/lang/trunk/src: changes/changes.xml main/java/org/apache/commons/lang3/text/ExtendedMessageFormat.java test/java/org/apache/commons/lang3/text/ExtendedMessageFormatTest.java
Date Sat, 14 Mar 2015 12:46:02 GMT
Author: britter
Date: Sat Mar 14 12:46:02 2015
New Revision: 1666679

URL: http://svn.apache.org/r1666679
Log:
LANG-948: Exception while using ExtendedMessageFormat and escaping braces. This fixes #19
from github. Thanks to Andrey Khobnya.

Modified:
    commons/proper/lang/trunk/src/changes/changes.xml
    commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/text/ExtendedMessageFormat.java
    commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/text/ExtendedMessageFormatTest.java

Modified: commons/proper/lang/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/changes/changes.xml?rev=1666679&r1=1666678&r2=1666679&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/changes/changes.xml [utf-8] (original)
+++ commons/proper/lang/trunk/src/changes/changes.xml [utf-8] Sat Mar 14 12:46:02 2015
@@ -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>

Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/text/ExtendedMessageFormat.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/text/ExtendedMessageFormat.java?rev=1666679&r1=1666678&r2=1666679&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/text/ExtendedMessageFormat.java
(original)
+++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/text/ExtendedMessageFormat.java
Sat Mar 14 12:46:02 2015
@@ -157,7 +157,7 @@ public class ExtendedMessageFormat exten
         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 exten
                 }
                 break;
             case QUOTE:
-                getQuotedString(pattern, pos, false);
+                getQuotedString(pattern, pos);
                 break;
             default:
                 break;
@@ -413,7 +413,7 @@ public class ExtendedMessageFormat exten
             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 exten
      * @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 exten
      *
      * @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);
     }
 
     /**

Modified: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/text/ExtendedMessageFormatTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/text/ExtendedMessageFormatTest.java?rev=1666679&r1=1666678&r2=1666679&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/text/ExtendedMessageFormatTest.java
(original)
+++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/text/ExtendedMessageFormatTest.java
Sat Mar 14 12:46:02 2015
@@ -90,6 +90,22 @@ public class ExtendedMessageFormatTest {
     }
 
     /**
+     * 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.
      */
     @Test



Mime
View raw message