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 14:20:23 UTC

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

Author: oheger
Date: Fri Apr  5 12:20:23 2013
New Revision: 1464943

URL: http://svn.apache.org/r1464943
Log:
[CONFIGURATION-515] Made some methods of PropertiesConfiguration.PropertiesWriter protected.

Client applications can now adapt the escaping logic when saving properties
files by subclassing PropertiesWriter.

Modified:
    commons/proper/configuration/trunk/src/changes/changes.xml
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/PropertiesConfiguration.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=1464943&r1=1464942&r2=1464943&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/changes/changes.xml (original)
+++ commons/proper/configuration/trunk/src/changes/changes.xml Fri Apr  5 12:20:23 2013
@@ -73,6 +73,11 @@
         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-515">
+        The visibility of some internal methods of PropertiesConfiguration.PropertiesWriter
+        has been increased to protected. This simplifies the implementation of
+        a custom escaping strategy.
+      </action>
       <action dev="oheger" type="add" issue="CONFIGURATION-514">
         Bean declarations now support constructor invocations.
       </action>

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=1464943&r1=1464942&r2=1464943&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 12:20:23 2013
@@ -859,7 +859,7 @@ public class PropertiesConfiguration ext
         private static final int BUF_SIZE = 8;
 
         /** The delimiter for multi-valued properties.*/
-        private char delimiter;
+        private final char delimiter;
 
         /** The separator to be used for the current property. */
         private String currentSeparator;
@@ -883,6 +883,19 @@ public class PropertiesConfiguration ext
         }
 
         /**
+         * Returns the delimiter for properties with multiple values. This is
+         * the list delimiter character. A value of '\0' means that no delimiter
+         * is defined.
+         *
+         * @return the delimiter for properties with multiple values
+         * @since 2.0
+         */
+        public char getDelimiter()
+        {
+            return delimiter;
+        }
+
+        /**
          * Returns the current property separator.
          *
          * @return the current property separator
@@ -1038,13 +1051,16 @@ public class PropertiesConfiguration ext
         }
 
         /**
-         * Escape the separators in the key.
+         * Escapes the key of a property before it gets written to file. This
+         * method is called on saving a configuration for each property key.
+         * It ensures that separator characters contained in the key are
+         * escaped.
          *
          * @param key the key
          * @return the escaped key
-         * @since 1.2
+         * @since 2.0
          */
-        private String escapeKey(String key)
+        protected String escapeKey(String key)
         {
             StringBuilder newkey = new StringBuilder();
 
@@ -1068,20 +1084,27 @@ public class PropertiesConfiguration ext
         }
 
         /**
-         * Escapes the given property value. Delimiter characters in the value
-         * will be escaped.
+         * Escapes the given property value. This method is called on saving the
+         * configuration for each property value. It ensures a correct handling
+         * of backslash characters and also takes care that list delimiter
+         * characters in the value are escaped.
          *
          * @param value the property value
          * @param inList a flag whether the value is part of a list
          * @return the escaped property value
-         * @since 1.3
+         * @since 2.0
          */
-        private String escapeValue(Object value, boolean inList)
+        protected String escapeValue(Object value, boolean inList)
         {
-            String escapedValue = handleBackslashs(value, inList);
-            if (delimiter != 0)
-            {
-                escapedValue = StringUtils.replace(escapedValue, String.valueOf(delimiter), ESCAPE + delimiter);
+            String escapedValue =
+                    StringEscapeUtils
+                            .escapeJava(escapeBackslashs(value, inList));
+            if (getDelimiter() != 0)
+            {
+                escapedValue =
+                        StringUtils.replace(escapedValue,
+                                String.valueOf(getDelimiter()), ESCAPE
+                                        + getDelimiter());
             }
             return escapedValue;
         }
@@ -1092,13 +1115,14 @@ public class PropertiesConfiguration ext
          * character of a list delimiter, double backslashes also have to be
          * escaped if the property is part of a (single line) list. Then, in all
          * cases each backslash has to be doubled in order to produce a valid
-         * properties file.
+         * properties file. This method is called by {@code escapeValue()}.
          *
          * @param value the value to be escaped
          * @param inList a flag whether the value is part of a list
          * @return the value with escaped backslashes as string
+         * @since 2.0
          */
-        private String handleBackslashs(Object value, boolean inList)
+        protected String escapeBackslashs(Object value, boolean inList)
         {
             String strValue = String.valueOf(value);
 
@@ -1123,7 +1147,7 @@ public class PropertiesConfiguration ext
                 strValue = buf.toString();
             }
 
-            return StringEscapeUtils.escapeJava(strValue);
+            return strValue;
         }
 
         /**
@@ -1149,7 +1173,7 @@ public class PropertiesConfiguration ext
                     {
                         buf.append(ESCAPE).append(ESCAPE);
                     }
-                    buf.append(delimiter);
+                    buf.append(getDelimiter());
                     lastValue = escapeValue(it.next(), true);
                     buf.append(lastValue);
                 }