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 2016/10/07 20:00:42 UTC
svn commit: r1763820 - in /commons/proper/configuration/trunk/src:
main/java/org/apache/commons/configuration2/PropertiesConfiguration.java
test/java/org/apache/commons/configuration2/TestPropertiesConfiguration.java
test/resources/test.properties
Author: oheger
Date: Fri Oct 7 20:00:41 2016
New Revision: 1763820
URL: http://svn.apache.org/viewvc?rev=1763820&view=rev
Log:
[CONFIGURATION-640] Added unescaping for special characters.
In Java properties files some special characters are always escaped
and need to be unescaped on loading. In Commons Configuration 2.x
this unescaping did not work correctly. This is fixed by this commit
which takes the affected special characters into account.
Modified:
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/PropertiesConfiguration.java
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestPropertiesConfiguration.java
commons/proper/configuration/trunk/src/test/resources/test.properties
Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/PropertiesConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/PropertiesConfiguration.java?rev=1763820&r1=1763819&r2=1763820&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/PropertiesConfiguration.java (original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/PropertiesConfiguration.java Fri Oct 7 20:00:41 2016
@@ -229,6 +229,13 @@ public class PropertiesConfiguration ext
private static final IOFactory DEFAULT_IO_FACTORY = new DefaultIOFactory();
/**
+ * A string with special characters that need to be unescaped when reading
+ * a properties file. {@code java.util.Properties} escapes these characters
+ * when writing out a properties file.
+ */
+ private static final String UNESCAPE_CHARACTERS = ":#=!\\\'\"";
+
+ /**
* This is the name of the property that can point to other
* properties file for including other properties files.
*/
@@ -1332,19 +1339,7 @@ public class PropertiesConfiguration ext
// handle an escaped value
hadSlash = false;
- if (ch == '\\')
- {
- out.append('\\');
- }
- else if (ch == '\'')
- {
- out.append('\'');
- }
- else if (ch == '\"')
- {
- out.append('"');
- }
- else if (ch == 'r')
+ if (ch == 'r')
{
out.append('\r');
}
@@ -1369,6 +1364,10 @@ public class PropertiesConfiguration ext
// uh-oh, we're in unicode country....
inUnicode = true;
}
+ else if (needsUnescape(ch))
+ {
+ out.append(ch);
+ }
else
{
out.append('\\');
@@ -1396,6 +1395,21 @@ public class PropertiesConfiguration ext
}
/**
+ * Checks whether the specified character needs to be unescaped. This method
+ * is called when during reading a property file an escape character ('\')
+ * is detected. If the character following the escape character is
+ * recognized as a special character which is escaped per default in a Java
+ * properties file, it has to be unescaped.
+ *
+ * @param ch the character in question
+ * @return a flag whether this character has to be unescaped
+ */
+ private static boolean needsUnescape(char ch)
+ {
+ return UNESCAPE_CHARACTERS.indexOf(ch) >= 0;
+ }
+
+ /**
* Helper method for loading an included properties file. This method is
* called by {@code load()} when an {@code include} property
* is encountered. It tries to resolve relative file names based on the
Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestPropertiesConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestPropertiesConfiguration.java?rev=1763820&r1=1763819&r2=1763820&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestPropertiesConfiguration.java (original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestPropertiesConfiguration.java Fri Oct 7 20:00:41 2016
@@ -1179,6 +1179,17 @@ public class TestPropertiesConfiguration
}
/**
+ * Tests whether special characters in a property value are un-escaped. This
+ * test is related to CONFIGURATION-640.
+ */
+ @Test
+ public void testUnEscapeCharacters()
+ {
+ assertEquals("Wrong value", "#1 =: me!",
+ conf.getString("test.unescape.characters"));
+ }
+
+ /**
* Helper method for testing the content of a list with elements that
* contain backslashes.
*
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=1763820&r1=1763819&r2=1763820&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/resources/test.properties (original)
+++ commons/proper/configuration/trunk/src/test/resources/test.properties Fri Oct 7 20:00:41 2016
@@ -24,6 +24,7 @@ include = ${include.file}
test.unescape = This \n string \t contains \" escaped \\ character\u0073
test.unescape.list-separator = This string contains \, an escaped list separator
+test.unescape.characters = \#1 \=\: me\!
#
# Other test properties