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/03/16 18:46:31 UTC
svn commit: r1457277 - in /commons/proper/configuration/trunk/src:
main/java/org/apache/commons/configuration/
test/java/org/apache/commons/configuration/ test/resources/
Author: oheger
Date: Sat Mar 16 17:46:31 2013
New Revision: 1457277
URL: http://svn.apache.org/r1457277
Log:
[CONFIGURATION-525] PropertiesConfiguration now supports a footer comment.
Modified:
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/PropertiesConfiguration.java
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/PropertiesConfigurationLayout.java
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestPropertiesConfiguration.java
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestPropertiesConfigurationLayout.java
commons/proper/configuration/trunk/src/test/resources/test.properties
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=1457277&r1=1457276&r2=1457277&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 Sat Mar 16 17:46:31 2013
@@ -353,6 +353,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 2.0
+ */
+ 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 2.0
+ */
+ 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/trunk/src/main/java/org/apache/commons/configuration/PropertiesConfigurationLayout.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/PropertiesConfigurationLayout.java?rev=1457277&r1=1457276&r2=1457277&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/PropertiesConfigurationLayout.java (original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/PropertiesConfigurationLayout.java Sat Mar 16 17:46:31 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 2.0
+ */
+ 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 2.0
+ */
+ 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 2.0
+ */
+ 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/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=1457277&r1=1457276&r2=1457277&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 Sat Mar 16 17:46:31 2013
@@ -68,6 +68,9 @@ public class TestPropertiesConfiguration
/** Constant for a test property value.*/
private static final String PROP_VALUE = "value";
+ /** Constant for the line break character. */
+ private static final String CR = System.getProperty("line.separator");
+
/** The configuration to be tested.*/
private PropertiesConfiguration conf;
@@ -1078,6 +1081,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.write(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/trunk/src/test/java/org/apache/commons/configuration/TestPropertiesConfigurationLayout.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestPropertiesConfigurationLayout.java?rev=1457277&r1=1457276&r2=1457277&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestPropertiesConfigurationLayout.java (original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestPropertiesConfigurationLayout.java Sat Mar 16 17:46:31 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/trunk/src/test/resources/test.properties
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/resources/test.properties?rev=1457277&r1=1457276&r2=1457277&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/resources/test.properties (original)
+++ commons/proper/configuration/trunk/src/test/resources/test.properties Sat Mar 16 17:46:31 2013
@@ -122,3 +122,5 @@ test.share2 = \\\\share2a
test.share2 = \\\\share2b
test.share3 = \\\\\\\\share3a\\\\\\\\,\\\\\\\\share3b\\
+# This is a foot comment
+