Author: oheger
Date: Sun Mar 29 19:15:36 2009
New Revision: 759750
URL: http://svn.apache.org/viewvc?rev=759750&view=rev
Log:
CONFIGURATION-314: PropertiesConfigurationLayout now supports setting the line
separator to be used when writing properties files.
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=759750&r1=759749&r2=759750&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 19:15:36 2009
@@ -955,6 +955,9 @@
/** The global separator. If set, it overrides the current separator.*/
private String globalSeparator;
+ /** The line separator.*/
+ private String lineSeparator;
+
/**
* Constructor.
*
@@ -1017,6 +1020,30 @@
}
/**
+ * Returns the line separator.
+ *
+ * @return the line separator
+ * @since 1.7
+ */
+ public String getLineSeparator()
+ {
+ return (lineSeparator != null) ? lineSeparator : LINE_SEPARATOR;
+ }
+
+ /**
+ * Sets the line separator. Each line written by this writer is
+ * terminated with this separator. If not set, the platform-specific
+ * line separator is used.
+ *
+ * @param lineSeparator the line separator to be used
+ * @since 1.7
+ */
+ public void setLineSeparator(String lineSeparator)
+ {
+ this.lineSeparator = lineSeparator;
+ }
+
+ /**
* Write a property.
*
* @param key the key of the property
@@ -1195,7 +1222,7 @@
{
write(s);
}
- write(LINE_SEPARATOR);
+ write(getLineSeparator());
}
/**
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=759750&r1=759749&r2=759750&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 19:15:36 2009
@@ -118,7 +118,7 @@
public class PropertiesConfigurationLayout implements ConfigurationListener
{
/** Constant for the line break character. */
- private static final String CR = System.getProperty("line.separator");
+ private static final String CR = "\n";
/** Constant for the default comment prefix. */
private static final String COMMENT_PREFIX = "# ";
@@ -135,6 +135,9 @@
/** The global separator that will be used for all properties. */
private String globalSeparator;
+ /** The line separator.*/
+ private String lineSeparator;
+
/** A counter for determining nested load calls. */
private int loadCounter;
@@ -189,12 +192,13 @@
}
/**
- * Returns the comment for the specified property key in a cononical form.
+ * Returns the comment for the specified property key in a canonical form.
* "Canonical" means that either all lines start with a comment
- * character or none. The <code>commentChar</code> parameter is
<b>false</b>,
+ * character or none. If the <code>commentChar</code> parameter is
<b>false</b>,
* all comment characters are removed, so that the result is only the plain
* text of the comment. Otherwise it is ensured that each line of the
- * comment starts with a comment character.
+ * comment starts with a comment character. Also, line breaks in the
comment
+ * are normalized to the line separator "\n".
*
* @param key the key of the property
* @param commentChar determines whether all lines should start with
comment
@@ -425,6 +429,30 @@
}
/**
+ * Returns the line separator.
+ *
+ * @return the line separator
+ * @since 1.7
+ */
+ public String getLineSeparator()
+ {
+ return lineSeparator;
+ }
+
+ /**
+ * Sets the line separator. When writing the properties configuration, all
+ * lines are terminated with this separator. If no separator was set, the
+ * platform-specific default line separator is used.
+ *
+ * @param lineSeparator the line separator
+ * @since 1.7
+ */
+ public void setLineSeparator(String lineSeparator)
+ {
+ this.lineSeparator = lineSeparator;
+ }
+
+ /**
* Returns a set with all property keys managed by this object.
*
* @return a set with all contained property keys
@@ -516,10 +544,14 @@
PropertiesConfiguration.PropertiesWriter writer =
getConfiguration()
.getIOFactory().createPropertiesWriter(out, delimiter);
writer.setGlobalSeparator(getGlobalSeparator());
+ if (getLineSeparator() != null)
+ {
+ writer.setLineSeparator(getLineSeparator());
+ }
if (headerComment != null)
{
- writer.writeln(getCanonicalHeaderComment(true));
+ writeComment(writer, getCanonicalHeaderComment(true));
writer.writeln(null);
}
@@ -536,10 +568,7 @@
}
// Output the comment
- if (getComment(key) != null)
- {
- writer.writeln(getCanonicalComment(key, true));
- }
+ writeComment(writer, getCanonicalComment(key, true));
// Output the property and its value
boolean singleLine = (isForceSingleLine() ||
isSingleLine(key))
@@ -755,7 +784,7 @@
* Checks if parts of the passed in comment can be used as header comment.
* This method checks whether a header comment can be defined (i.e. whether
* this is the first comment in the loaded file). If this is the case, it
is
- * searched for the lates blanc line. This line will mark the end of the
+ * searched for the latest blanc line. This line will mark the end of the
* header comment. The return value is the index of the first line in the
* passed in list, which does not belong to the header comment.
*
@@ -800,6 +829,25 @@
}
/**
+ * Helper method for writing a comment line. This method ensures that the
+ * correct line separator is used if the comment spans multiple lines.
+ *
+ * @param writer the writer
+ * @param comment the comment to write
+ * @throws IOException if an IO error occurs
+ */
+ private static void writeComment(
+ PropertiesConfiguration.PropertiesWriter writer, String comment)
+ throws IOException
+ {
+ if (comment != null)
+ {
+ writer.writeln(StringUtils.replace(comment, CR, writer
+ .getLineSeparator()));
+ }
+ }
+
+ /**
* A helper class for storing all layout related information for a
* configuration property.
*/
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=759750&r1=759749&r2=759750&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 19:15:36 2009
@@ -36,7 +36,10 @@
public class TestPropertiesConfigurationLayout extends TestCase
{
/** Constant for the line break character. */
- static final String CR = System.getProperty("line.separator");
+ private static final String CR = System.getProperty("line.separator");
+
+ /** Constant for the normalized line break character. */
+ private static final String CRNORM = "\n";
/** Constant for a test property key. */
static final String TEST_KEY = "myProperty";
@@ -133,7 +136,7 @@
layout.load(builder.getReader());
assertEquals("Wrong number of blanc lines", 2, layout
.getBlancLinesBefore(TEST_KEY));
- assertEquals("Wrong comment", TEST_COMMENT + CR, layout
+ assertEquals("Wrong comment", TEST_COMMENT + CRNORM, layout
.getCanonicalComment(TEST_KEY, false));
assertEquals("Wrong property value", TEST_VALUE, config
.getString(TEST_KEY));
@@ -177,7 +180,7 @@
builder.addProperty(TEST_KEY, TEST_VALUE + "2");
layout.load(builder.getReader());
assertEquals("Wrong combined comment",
- TEST_COMMENT + CR + TEST_COMMENT, layout.getCanonicalComment(
+ TEST_COMMENT + CRNORM + TEST_COMMENT,
layout.getCanonicalComment(
TEST_KEY, false));
assertEquals("Wrong combined blanc numbers", 0, layout
.getBlancLinesBefore(TEST_KEY));
@@ -209,7 +212,7 @@
builder.addComment(null);
builder.addProperty(TEST_KEY, TEST_VALUE);
layout.load(builder.getReader());
- assertEquals("Wrong header comment", TEST_COMMENT + CR + CR
+ assertEquals("Wrong header comment", TEST_COMMENT + CRNORM + CRNORM
+ TEST_COMMENT, layout.getCanonicalHeaderComment(false));
assertNull("Wrong comment for property", layout.getComment(TEST_KEY));
}
@@ -228,7 +231,7 @@
builder.addComment(TEST_COMMENT);
builder.addProperty(TEST_KEY, TEST_VALUE);
layout.load(builder.getReader());
- assertEquals("Wrong header comment", TEST_COMMENT + CR + CR
+ assertEquals("Wrong header comment", TEST_COMMENT + CRNORM + CRNORM
+ TEST_COMMENT, layout.getCanonicalHeaderComment(false));
assertEquals("Wrong comment for property", TEST_COMMENT, layout
.getCanonicalComment(TEST_KEY, false));
@@ -404,9 +407,9 @@
.getCanonicalHeaderComment(false));
assertFalse("Include property was stored", layout.getKeys().contains(
PropertiesConfiguration.getInclude()));
- assertEquals("Wrong comment for property", TEST_COMMENT + CR
- + "A nested header comment." + CR + "With multiple lines" + CR
- + CR + "Second comment", layout.getCanonicalComment(TEST_KEY,
+ assertEquals("Wrong comment for property", TEST_COMMENT + CRNORM
+ + "A nested header comment." + CRNORM + "With multiple lines"
+ CRNORM
+ + CRNORM + "Second comment",
layout.getCanonicalComment(TEST_KEY,
false));
}
@@ -442,7 +445,7 @@
layout.setComment("AnotherProperty", "AnotherComment");
layout.setBlancLinesBefore("AnotherProperty", 2);
layout.setSingleLine("AnotherProperty", true);
- layout.setHeaderComment("A header comment" + CR + "for my properties");
+ layout.setHeaderComment("A header comment" + CRNORM + "for my
properties");
checkLayoutString("# A header comment" + CR + "# for my properties"
+ CR + CR + "# " + TEST_COMMENT + CR + TEST_KEY + " = "
+ TEST_VALUE + CR + TEST_KEY + " = " + TEST_VALUE + "2" + CR
@@ -624,6 +627,37 @@
}
/**
+ * Tests setting the line separator.
+ */
+ public void testSetLineSeparator() throws ConfigurationException
+ {
+ final String lf = CR + CR;
+ config.addProperty(TEST_KEY, TEST_VALUE);
+ layout.setBlancLinesBefore(TEST_KEY, 2);
+ layout.setComment(TEST_KEY, TEST_COMMENT);
+ layout.setHeaderComment("Header comment");
+ layout.setLineSeparator(lf);
+ checkLayoutString("# Header comment" + lf + lf + lf + lf + "# "
+ + TEST_COMMENT + lf + TEST_KEY + " = " + TEST_VALUE + lf);
+ }
+
+ /**
+ * Tests whether the line separator is also taken into account within
+ * comments.
+ */
+ public void testSetLineSeparatorInComments() throws ConfigurationException
+ {
+ final String lf = "<-\n";
+ config.addProperty(TEST_KEY, TEST_VALUE);
+ layout.setComment(TEST_KEY, TEST_COMMENT + "\nMore comment");
+ layout.setHeaderComment("Header\ncomment");
+ layout.setLineSeparator(lf);
+ checkLayoutString("# Header" + lf + "# comment" + lf + lf + "# "
+ + TEST_COMMENT + lf + "# More comment" + lf + TEST_KEY + " = "
+ + TEST_VALUE + lf);
+ }
+
+ /**
* Helper method for filling the layout object with some properties.
*/
private void fillLayout()
@@ -711,6 +745,7 @@
buf.append("! ");
}
buf.append(s);
+ commentCounter++;
}
buf.append(CR);
}