You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by oh...@apache.org on 2013/04/05 15:11:55 UTC

svn commit: r1464966 - in /commons/proper/configuration/trunk/src: changes/changes.xml main/java/org/apache/commons/configuration/PropertiesConfiguration.java test/java/org/apache/commons/configuration/TestPropertiesConfiguration.java

Author: oheger
Date: Fri Apr  5 13:11:54 2013
New Revision: 1464966

URL: http://svn.apache.org/r1464966
Log:
[CONFIGURATION-516] PropertiesConfiguration no longer escapes double quotes on saving.

The escaping logic now uses a CharSequenceTranslator which is a slightly
modified version of the ESCAPE_JAVA translator from Commons Lang 3. It
does not escape double quotes.

Modified:
    commons/proper/configuration/trunk/src/changes/changes.xml
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/PropertiesConfiguration.java
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestPropertiesConfiguration.java

Modified: commons/proper/configuration/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/changes/changes.xml?rev=1464966&r1=1464965&r2=1464966&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/changes/changes.xml (original)
+++ commons/proper/configuration/trunk/src/changes/changes.xml Fri Apr  5 13:11:54 2013
@@ -73,6 +73,9 @@
         Hierarchical configurations now provide methods to obtain sub
         configurations for all child elements of a given key.
       </action>
+      <action dev="oheger" type="update" issue="CONFIGURATION-516">
+        PropertiesConfiguration no longer escapes double quotes on saving.
+      </action>
       <action dev="oheger" type="update" issue="CONFIGURATION-515">
         The visibility of some internal methods of PropertiesConfiguration.PropertiesWriter
         has been increased to protected. This simplifies the implementation of

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/PropertiesConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/PropertiesConfiguration.java?rev=1464966&r1=1464965&r2=1464966&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/PropertiesConfiguration.java (original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/PropertiesConfiguration.java Fri Apr  5 13:11:54 2013
@@ -35,6 +35,11 @@ import org.apache.commons.configuration.
 import org.apache.commons.lang.ArrayUtils;
 import org.apache.commons.lang.StringEscapeUtils;
 import org.apache.commons.lang.StringUtils;
+import org.apache.commons.lang3.text.translate.AggregateTranslator;
+import org.apache.commons.lang3.text.translate.CharSequenceTranslator;
+import org.apache.commons.lang3.text.translate.EntityArrays;
+import org.apache.commons.lang3.text.translate.LookupTranslator;
+import org.apache.commons.lang3.text.translate.UnicodeEscaper;
 
 /**
  * This is the "classic" Properties loader which loads the values from
@@ -855,6 +860,17 @@ public class PropertiesConfiguration ext
      */
     public static class PropertiesWriter extends FilterWriter
     {
+        /**
+         * A translator for escaping property values. This translator performs a
+         * subset of transformations done by the ESCAPE_JAVA translator from
+         * Commons Lang 3.
+         */
+        private static final CharSequenceTranslator ESCAPE_PROPERTIES =
+                new AggregateTranslator(new LookupTranslator(new String[][] {
+                    { "\\", "\\\\" }}),
+                        new LookupTranslator(EntityArrays.JAVA_CTRL_CHARS_ESCAPE()),
+                        UnicodeEscaper.outsideOf(32, 0x7f));
+
         /** Constant for the initial size when creating a string buffer. */
         private static final int BUF_SIZE = 8;
 
@@ -1097,8 +1113,8 @@ public class PropertiesConfiguration ext
         protected String escapeValue(Object value, boolean inList)
         {
             String escapedValue =
-                    StringEscapeUtils
-                            .escapeJava(escapeBackslashs(value, inList));
+                    ESCAPE_PROPERTIES
+                            .translate(escapeBackslashs(value, inList));
             if (getDelimiter() != 0)
             {
                 escapedValue =

Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestPropertiesConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestPropertiesConfiguration.java?rev=1464966&r1=1464965&r2=1464966&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestPropertiesConfiguration.java (original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestPropertiesConfiguration.java Fri Apr  5 13:11:54 2013
@@ -990,6 +990,26 @@ public class TestPropertiesConfiguration
     }
 
     /**
+     * Tests the escaping of quotation marks in a properties value. This test is
+     * related to CONFIGURATION-516.
+     */
+    @Test
+    public void testEscapeQuote() throws ConfigurationException
+    {
+        conf.clear();
+        String text = "\"Hello World!\"";
+        conf.setProperty(PROP_NAME, text);
+        StringWriter out = new StringWriter();
+        new FileHandler(conf).save(out);
+        assertTrue("Value was escaped: " + out,
+                out.toString().indexOf(text) >= 0);
+        saveTestConfig();
+        PropertiesConfiguration c2 = new PropertiesConfiguration();
+        load(c2, testSavePropertiesFile.getAbsolutePath());
+        assertEquals("Wrong value", text, c2.getString(PROP_NAME));
+    }
+
+    /**
      * Helper method for testing the content of a list with elements that
      * contain backslashes.
      *