Author: henning
Date: Mon Oct 21 22:35:52 2013
New Revision: 1534402

URL: http://svn.apache.org/r1534402
Log:
Backport CONFIGURATION-525 from r1457277.

Modified:
    
commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/RELEASE-NOTES.txt
    
commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/changes/changes.xml
    
commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/main/java/org/apache/commons/configuration/PropertiesConfiguration.java
    
commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/main/java/org/apache/commons/configuration/PropertiesConfigurationLayout.java
    
commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/test/java/org/apache/commons/configuration/TestPropertiesConfiguration.java
    
commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/test/java/org/apache/commons/configuration/TestPropertiesConfigurationLayout.java
    
commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/test/resources/test.properties

Modified: 
commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/RELEASE-NOTES.txt
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/RELEASE-NOTES.txt?rev=1534402&r1=1534401&r2=1534402&view=diff
==============================================================================
--- 
commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/RELEASE-NOTES.txt
 (original)
+++ 
commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/RELEASE-NOTES.txt
 Mon Oct 21 22:35:52 2013
@@ -74,17 +74,23 @@ BUG FIXES IN 1.10
 IMPROVEMENTS AND NEW FEATURES IN 1.10
 =====================================
 
-* [CONFIGURATION-534] PropertyConfiguration's handling of includes depends on 
the
-                      existence of a base path
+* [CONFIGURATION-525] PropertiesConfigurationLayout does not preserve comments 
at bottom of a file
 
-  The includesAllowed property of PropertyConfiguration is now independent
-  from the existence of a base path.
+  PropertiesConfiguration now keeps a comment at the bottom of a
+  properties file. A new footer property was added for reading and
+  writing this footer comment.
 
 * [CONFIGURATION-526] Support loading from and saving to DOM nodes
 
   XMLPropertiesConfiguration now supports loading from and saving to DOM
   nodes.
 
+* [CONFIGURATION-534] PropertyConfiguration's handling of includes depends on 
the
+                      existence of a base path
+
+  The includesAllowed property of PropertyConfiguration is now independent
+  from the existence of a base path.
+
 * [CONFIGURATION-550] Missing conversion to char
 
   Conversion to Character is now supported.

Modified: 
commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/changes/changes.xml
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/changes/changes.xml?rev=1534402&r1=1534401&r2=1534402&view=diff
==============================================================================
--- 
commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/changes/changes.xml
 (original)
+++ 
commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/changes/changes.xml
 Mon Oct 21 22:35:52 2013
@@ -31,6 +31,11 @@
         XMLConfiguration now adds attributes of elements defining a list to
         all list nodes.
       </action>
+      <action dev="oheger" type="add" issue="CONFIGURATION-525">
+        PropertiesConfiguration now keeps a comment at the bottom of a
+        properties file. A new footer property was added for reading and
+        writing this footer comment.
+      </action>
       <action dev="oheger" type="update" issue="CONFIGURATION-526" 
due-to="Oliver Kopp">
         XMLPropertiesConfiguration now supports loading from and saving to DOM
         nodes.

Modified: 
commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/main/java/org/apache/commons/configuration/PropertiesConfiguration.java
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/main/java/org/apache/commons/configuration/PropertiesConfiguration.java?rev=1534402&r1=1534401&r2=1534402&view=diff
==============================================================================
--- 
commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/main/java/org/apache/commons/configuration/PropertiesConfiguration.java
 (original)
+++ 
commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/main/java/org/apache/commons/configuration/PropertiesConfiguration.java
 Mon Oct 21 22:35:52 2013
@@ -366,6 +366,30 @@ public class PropertiesConfiguration ext
     }
 
     /**
+     * Returns the footer comment. This is a comment at the very end of the
+     * file.
+     *
+     * @return the footer comment
+     * @since 1.10
+     */
+    public String getFooter()
+    {
+        return getLayout().getFooterComment();
+    }
+
+    /**
+     * Sets the footer comment. If set, this comment is written after all
+     * properties at the end of the file.
+     *
+     * @param footer the footer comment
+     * @since 1.10
+     */
+    public void setFooter(String footer)
+    {
+        getLayout().setFooterComment(footer);
+    }
+
+    /**
      * Returns the encoding to be used when loading or storing configuration
      * data. This implementation ensures that the default encoding will be used
      * if none has been set explicitly.

Modified: 
commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/main/java/org/apache/commons/configuration/PropertiesConfigurationLayout.java
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/main/java/org/apache/commons/configuration/PropertiesConfigurationLayout.java?rev=1534402&r1=1534401&r2=1534402&view=diff
==============================================================================
--- 
commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/main/java/org/apache/commons/configuration/PropertiesConfigurationLayout.java
 (original)
+++ 
commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/main/java/org/apache/commons/configuration/PropertiesConfigurationLayout.java
 Mon Oct 21 22:35:52 2013
@@ -131,6 +131,9 @@ public class PropertiesConfigurationLayo
     /** Stores the header comment. */
     private String headerComment;
 
+    /** Stores the footer comment. */
+    private String footerComment;
+
     /** The global separator that will be used for all properties. */
     private String globalSeparator;
 
@@ -206,15 +209,7 @@ public class PropertiesConfigurationLayo
      */
     public String getCanonicalComment(String key, boolean commentChar)
     {
-        String comment = getComment(key);
-        if (comment == null)
-        {
-            return null;
-        }
-        else
-        {
-            return trimComment(comment, commentChar);
-        }
+        return constructCanonicalComment(getComment(key), commentChar);
     }
 
     /**
@@ -283,8 +278,7 @@ public class PropertiesConfigurationLayo
      */
     public String getCanonicalHeaderComment(boolean commentChar)
     {
-        return (getHeaderComment() == null) ? null : trimComment(
-                getHeaderComment(), commentChar);
+        return constructCanonicalComment(getHeaderComment(), commentChar);
     }
 
     /**
@@ -312,6 +306,47 @@ public class PropertiesConfigurationLayo
     }
 
     /**
+     * Returns the footer comment of the represented properties file in a
+     * canonical form. This method works like
+     * {@code getCanonicalHeaderComment()}, but reads the footer comment.
+     *
+     * @param commentChar determines the presence of comment characters
+     * @return the footer comment (can be <b>null</b>)
+     * @see #getCanonicalHeaderComment(boolean)
+     * @since 1.10
+     */
+    public String getCanonicalFooterCooment(boolean commentChar)
+    {
+        return constructCanonicalComment(getFooterComment(), commentChar);
+    }
+
+    /**
+     * Returns the footer comment of the represented properties file. This
+     * method returns the footer comment exactly as it was set using
+     * {@code setFooterComment()} or extracted from the loaded properties
+     * file.
+     *
+     * @return the footer comment (can be <b>null</b>)
+     * @since 1.10
+     */
+    public String getFooterComment()
+    {
+        return footerComment;
+    }
+
+    /**
+     * Sets the footer comment for the represented properties file. This 
comment
+     * will be output at the bottom of the file.
+     *
+     * @param footerComment the footer comment
+     * @since 1.10
+     */
+    public void setFooterComment(String footerComment)
+    {
+        this.footerComment = footerComment;
+    }
+
+    /**
      * Returns a flag whether the specified property is defined on a single
      * line. This is meaningful only if this property has multiple values.
      *
@@ -512,6 +547,9 @@ public class PropertiesConfigurationLayo
                     }
                 }
             }
+
+            setFooterComment(extractComment(reader.getCommentLines(), 0, reader
+                    .getCommentLines().size() - 1));
         }
         catch (IOException ioex)
         {
@@ -575,6 +613,8 @@ public class PropertiesConfigurationLayo
                             key), singleLine);
                 }
             }
+
+            writeComment(writer, getCanonicalFooterCooment(true));
             writer.flush();
         }
         catch (IOException ioex)
@@ -821,6 +861,9 @@ public class PropertiesConfigurationLayo
             PropertyLayoutData data = c.layoutData.get(key);
             layoutData.put(key, data.clone());
         }
+
+        setHeaderComment(c.getHeaderComment());
+        setFooterComment(c.getFooterComment());
     }
 
     /**
@@ -843,6 +886,21 @@ public class PropertiesConfigurationLayo
     }
 
     /**
+     * Helper method for generating a comment string. Depending on the boolean
+     * argument the resulting string either has no comment characters or a
+     * leading comment character at each line.
+     *
+     * @param comment the comment string to be processed
+     * @param commentChar determines the presence of comment characters
+     * @return the canonical comment string (can be <b>null</b>)
+     */
+    private static String constructCanonicalComment(String comment,
+            boolean commentChar)
+    {
+        return (comment == null) ? null : trimComment(comment, commentChar);
+    }
+
+    /**
      * A helper class for storing all layout related information for a
      * configuration property.
      */

Modified: 
commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/test/java/org/apache/commons/configuration/TestPropertiesConfiguration.java
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/test/java/org/apache/commons/configuration/TestPropertiesConfiguration.java?rev=1534402&r1=1534401&r2=1534402&view=diff
==============================================================================
--- 
commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/test/java/org/apache/commons/configuration/TestPropertiesConfiguration.java
 (original)
+++ 
commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/test/java/org/apache/commons/configuration/TestPropertiesConfiguration.java
 Mon Oct 21 22:35:52 2013
@@ -1116,6 +1116,36 @@ public class TestPropertiesConfiguration
     }
 
     /**
+     * Tests whether a footer comment is correctly read.
+     */
+    @Test
+    public void testReadFooterComment()
+    {
+        assertEquals("Wrong footer comment", "\n# This is a foot comment\n",
+                conf.getFooter());
+        assertEquals("Wrong footer comment from layout",
+                "\nThis is a foot comment\n", conf.getLayout()
+                        .getCanonicalFooterCooment(false));
+    }
+
+    /**
+     * Tests whether a footer comment is correctly written out.
+     */
+    @Test
+    public void testWriteFooterComment() throws ConfigurationException,
+            IOException
+    {
+        final String footer = "my footer";
+        conf.clear();
+        conf.setProperty(PROP_NAME, PROP_VALUE);
+        conf.setFooter(footer);
+        StringWriter out = new StringWriter();
+        conf.save(out);
+        assertEquals("Wrong result", PROP_NAME + " = " + PROP_VALUE + CR + "# "
+                + footer + CR, out.toString());
+    }
+
+    /**
      * Helper method for testing the content of a list with elements that
      * contain backslashes.
      *

Modified: 
commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/test/java/org/apache/commons/configuration/TestPropertiesConfigurationLayout.java
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/test/java/org/apache/commons/configuration/TestPropertiesConfigurationLayout.java?rev=1534402&r1=1534401&r2=1534402&view=diff
==============================================================================
--- 
commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/test/java/org/apache/commons/configuration/TestPropertiesConfigurationLayout.java
 (original)
+++ 
commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/test/java/org/apache/commons/configuration/TestPropertiesConfigurationLayout.java
 Mon Oct 21 22:35:52 2013
@@ -601,11 +601,14 @@ public class TestPropertiesConfiguration
                 config, layout);
         assertEquals("Wrong number of keys", layout.getKeys().size(), l2
                 .getKeys().size());
-        for (Iterator<String> it = layout.getKeys().iterator(); it.hasNext();)
+        for (String key : layout.getKeys())
         {
-            Object key = it.next();
             assertTrue("Key was not found: " + key, 
l2.getKeys().contains(key));
         }
+        assertEquals("Wrong header comment", layout.getHeaderComment(),
+                l2.getHeaderComment());
+        assertEquals("Wrong footer comment", layout.getFooterComment(),
+                l2.getFooterComment());
     }
 
     /**
@@ -699,6 +702,7 @@ public class TestPropertiesConfiguration
         builder.addComment(TEST_COMMENT);
         builder.addProperty(TEST_KEY, TEST_VALUE);
         builder.addProperty("anotherProp", "anotherValue");
+        builder.addComment("A footer comment");
         try
         {
             layout.load(builder.getReader());

Modified: 
commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/test/resources/test.properties
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/test/resources/test.properties?rev=1534402&r1=1534401&r2=1534402&view=diff
==============================================================================
--- 
commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/test/resources/test.properties
 (original)
+++ 
commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/test/resources/test.properties
 Mon Oct 21 22:35:52 2013
@@ -122,3 +122,5 @@ test.share2 = \\\\share2a
 test.share2 = \\\\share2b
 test.share3 = \\\\\\\\share3a\\\\\\\\,\\\\\\\\share3b\\
 
+# This is a foot comment
+


Reply via email to