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 2009/03/29 20:13:05 UTC

svn commit: r759733 - in /commons/proper/configuration/trunk/src: java/org/apache/commons/configuration/ test/org/apache/commons/configuration/

Author: oheger
Date: Sun Mar 29 18:13:05 2009
New Revision: 759733

URL: http://svn.apache.org/viewvc?rev=759733&view=rev
Log:
CONFIGURATION-371: Added an option to PropertiesConfigurationLayout to specify the separator to be used for all properties.

Modified:
    commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/PropertiesConfiguration.java
    commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/PropertiesConfigurationLayout.java
    commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestPropertiesConfigurationLayout.java

Modified: commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/PropertiesConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/PropertiesConfiguration.java?rev=759733&r1=759732&r2=759733&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/PropertiesConfiguration.java (original)
+++ commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/PropertiesConfiguration.java Sun Mar 29 18:13:05 2009
@@ -952,6 +952,9 @@
         /** The separator to be used for the current property. */
         private String currentSeparator;
 
+        /** The global separator. If set, it overrides the current separator.*/
+        private String globalSeparator;
+
         /**
          * Constructor.
          *
@@ -988,6 +991,32 @@
         }
 
         /**
+         * Returns the global property separator.
+         *
+         * @return the global property separator
+         * @since 1.7
+         */
+        public String getGlobalSeparator()
+        {
+            return globalSeparator;
+        }
+
+        /**
+         * Sets the global property separator. This separator corresponds to the
+         * <code>globalSeparator</code> property of
+         * {@link PropertiesConfigurationLayout}. It defines the separator to be
+         * used for all properties. If it is undefined, the current separator is
+         * used.
+         *
+         * @param globalSeparator the global property separator
+         * @since 1.7
+         */
+        public void setGlobalSeparator(String globalSeparator)
+        {
+            this.globalSeparator = globalSeparator;
+        }
+
+        /**
          * Write a property.
          *
          * @param key the key of the property
@@ -1173,10 +1202,11 @@
          * Returns the separator to be used for the given property. This method
          * is called by <code>writeProperty()</code>. The string returned here
          * is used as separator between the property key and its value. Per
-         * default the separator returned by <code>getCurrentSeparator()</code>
-         * is returned, which was set by the associated layout object. Derived
-         * classes may implement a different strategy for defining the
-         * separator.
+         * default the method checks whether a global separator is set. If this
+         * is the case, it is returned. Otherwise the separator returned by
+         * <code>getCurrentSeparator()</code> is used, which was set by the
+         * associated layout object. Derived classes may implement a different
+         * strategy for defining the separator.
          *
          * @param key the property key
          * @param value the value
@@ -1185,7 +1215,8 @@
          */
         protected String fetchSeparator(String key, Object value)
         {
-            return getCurrentSeparator();
+            return (getGlobalSeparator() != null) ? getGlobalSeparator()
+                    : getCurrentSeparator();
         }
     } // class PropertiesWriter
 

Modified: commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/PropertiesConfigurationLayout.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/PropertiesConfigurationLayout.java?rev=759733&r1=759732&r2=759733&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/PropertiesConfigurationLayout.java (original)
+++ commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/PropertiesConfigurationLayout.java Sun Mar 29 18:13:05 2009
@@ -132,6 +132,9 @@
     /** Stores the header comment. */
     private String headerComment;
 
+    /** The global separator that will be used for all properties. */
+    private String globalSeparator;
+
     /** A counter for determining nested load calls. */
     private int loadCounter;
 
@@ -392,6 +395,36 @@
     }
 
     /**
+     * Returns the global separator.
+     *
+     * @return the global properties separator
+     * @since 1.7
+     */
+    public String getGlobalSeparator()
+    {
+        return globalSeparator;
+    }
+
+    /**
+     * Sets the global separator for properties. With this method a separator
+     * can be set that will be used for all properties when writing the
+     * configuration. This is an easy way of determining the properties
+     * separator globally. To be compatible with the properties format only the
+     * characters <code>=</code> and <code>:</code> (with or without whitespace)
+     * should be used, but this method does not enforce this - it accepts
+     * arbitrary strings. If the global separator is set to <b>null</b>,
+     * property separators are not changed. This is the default behavior as it
+     * produces results that are closer to the original properties file.
+     *
+     * @param globalSeparator the separator to be used for all properties
+     * @since 1.7
+     */
+    public void setGlobalSeparator(String globalSeparator)
+    {
+        this.globalSeparator = globalSeparator;
+    }
+
+    /**
      * Returns a set with all property keys managed by this object.
      *
      * @return a set with all contained property keys
@@ -482,6 +515,8 @@
                     : getConfiguration().getListDelimiter();
             PropertiesConfiguration.PropertiesWriter writer = getConfiguration()
                     .getIOFactory().createPropertiesWriter(out, delimiter);
+            writer.setGlobalSeparator(getGlobalSeparator());
+
             if (headerComment != null)
             {
                 writer.writeln(getCanonicalHeaderComment(true));

Modified: commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestPropertiesConfigurationLayout.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestPropertiesConfigurationLayout.java?rev=759733&r1=759732&r2=759733&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestPropertiesConfigurationLayout.java (original)
+++ commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestPropertiesConfigurationLayout.java Sun Mar 29 18:13:05 2009
@@ -79,6 +79,7 @@
         assertSame("Configuration not stored", config, layout
                 .getConfiguration());
         assertFalse("Force single line flag set", layout.isForceSingleLine());
+        assertNull("Got a global separator", layout.getGlobalSeparator());
     }
 
     /**
@@ -608,6 +609,21 @@
     }
 
     /**
+     * Tests setting the global separator. This separator should override the
+     * separators for all properties.
+     */
+    public void testSetGlobalSeparator() throws ConfigurationException
+    {
+        final String sep = "=";
+        config.addProperty(TEST_KEY, TEST_VALUE);
+        config.addProperty("key2", "value2");
+        layout.setSeparator(TEST_KEY, " : ");
+        layout.setGlobalSeparator(sep);
+        checkLayoutString(TEST_KEY + sep + TEST_VALUE + CR + "key2" + sep
+                + "value2" + CR);
+    }
+
+    /**
      * Helper method for filling the layout object with some properties.
      */
     private void fillLayout()