This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-configuration.git
The following commit(s) were added to refs/heads/master by this push:
new f33137fe CONFIGURATION-844: Add support for empty sections (#408)
f33137fe is described below
commit f33137fe24103abe0317353b993a4026b40d9d15
Author: Thomas Steiner <[email protected]>
AuthorDate: Tue Apr 23 23:59:58 2024 +0200
CONFIGURATION-844: Add support for empty sections (#408)
* Add support for empty sections
* Add documentation
* Add test
* Remove trailing whitespaces
---
.../org/apache/commons/configuration2/INIConfiguration.java | 11 ++++++++++-
.../apache/commons/configuration2/TestINIConfiguration.java | 10 ++++++++++
2 files changed, 20 insertions(+), 1 deletion(-)
diff --git
a/src/main/java/org/apache/commons/configuration2/INIConfiguration.java
b/src/main/java/org/apache/commons/configuration2/INIConfiguration.java
index 4749d930..f0a3280d 100644
--- a/src/main/java/org/apache/commons/configuration2/INIConfiguration.java
+++ b/src/main/java/org/apache/commons/configuration2/INIConfiguration.java
@@ -134,6 +134,9 @@ import
org.apache.commons.configuration2.tree.TrackedNodeModel;
* [sectionSeparators]
* passwd : abc=def
* a:b = "value"
+ *
+ * []
+ * var = emptySection
* </pre>
* <p>
* This ini file will be parsed without error. Note:
@@ -155,6 +158,8 @@ import
org.apache.commons.configuration2.tree.TrackedNodeModel;
* {@code abc=def}. This default behavior can be changed by using quotes. If
there is a separator character before the
* first quote character (ignoring whitespace), this character is used as
separator. Thus the second property definition
* in the section has the key {@code a:b} and the value {@code value}.</li>
+ * <li>The empty section is added using a key consisting of a single space
character. The parameters can be accessed
+ * using {@code getProperty(" .var")}</li>
* </ul>
* <p>
* Internally, this configuration maps the content of the represented ini file
to its node structure in the following
@@ -480,7 +485,11 @@ public class INIConfiguration extends
BaseHierarchicalConfiguration implements F
if (!isCommentLine(line)) {
if (isSectionLine(line)) {
final int length = sectionInLineCommentsAllowed ?
line.indexOf("]") : line.length() - 1;
- final String section = line.substring(1, length);
+ String section = line.substring(1, length);
+ if (section.isEmpty()) {
+ // use space for sections with no key
+ section = " ";
+ }
sectionBuilder = sectionBuilders.computeIfAbsent(section,
k -> new ImmutableNode.Builder());
} else {
String key;
diff --git
a/src/test/java/org/apache/commons/configuration2/TestINIConfiguration.java
b/src/test/java/org/apache/commons/configuration2/TestINIConfiguration.java
index f9b19764..c78969f8 100644
--- a/src/test/java/org/apache/commons/configuration2/TestINIConfiguration.java
+++ b/src/test/java/org/apache/commons/configuration2/TestINIConfiguration.java
@@ -1031,6 +1031,16 @@ public class TestINIConfiguration {
assertEquals(Arrays.asList("1", "2", "3"), list);
}
+ /**
+ * Tests correct handling of empty sections "[ ]".
+ */
+ @Test
+ public void testEmptySection() throws ConfigurationException {
+ final INIConfiguration config = setUpConfig("[]" + LINE_SEPARATOR +
"key=value" + LINE_SEPARATOR);
+ final String value = config.getString(" .key");
+ assertEquals("value", value);
+ }
+
/**
* Tests whether a value which contains a semicolon can be loaded
successfully. This test is related to
* CONFIGURATION-434.