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 2017/03/01 20:58:32 UTC
svn commit: r1785035 - in /commons/proper/configuration/trunk/src:
main/java/org/apache/commons/configuration2/
main/java/org/apache/commons/configuration2/builder/
main/java/org/apache/commons/configuration2/builder/fluent/
test/java/org/apache/common...
Author: oheger
Date: Wed Mar 1 20:58:32 2017
New Revision: 1785035
URL: http://svn.apache.org/viewvc?rev=1785035&view=rev
Log:
[CONFIGURATION-647] Separator for ini files can now be set.
INIConfiguration can now be configured to use a custom separator
between properties and values when writing an ini file.
Thanks to vladimir dot martinek at cenarion dot com for the patch.
Added:
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/builder/INIBuilderParametersImpl.java
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/builder/INIBuilderProperties.java
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/builder/fluent/INIBuilderParameters.java
Modified:
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/INIConfiguration.java
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/builder/fluent/Parameters.java
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestINIConfiguration.java
Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/INIConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/INIConfiguration.java?rev=1785035&r1=1785034&r2=1785035&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/INIConfiguration.java (original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/INIConfiguration.java Wed Mar 1 20:58:32 2017
@@ -248,6 +248,11 @@ public class INIConfiguration extends Ba
private static final String LINE_CONT = "\\";
/**
+ * The separator used when writing an INI file.
+ */
+ private String separatorUsedInOutput = " = ";
+
+ /**
* Create a new empty INI Configuration.
*/
public INIConfiguration()
@@ -268,6 +273,46 @@ public class INIConfiguration extends Ba
}
/**
+ * Get separator used in INI output. see {@code setSeparatorUsedInOutput}
+ * for further explanation
+ *
+ * @return the current separator for writing the INI output
+ * @since 2.2
+ */
+ public String getSeparatorUsedInOutput()
+ {
+ beginRead(false);
+ try
+ {
+ return separatorUsedInOutput;
+ }
+ finally
+ {
+ endRead();
+ }
+ }
+
+ /**
+ * Allows setting the key and value separator which is used for the creation
+ * of the resulting INI output
+ *
+ * @param separator String of the new separator for INI output
+ * @since 2.2
+ */
+ public void setSeparatorUsedInOutput(String separator)
+ {
+ beginWrite(false);
+ try
+ {
+ this.separatorUsedInOutput = separator;
+ }
+ finally
+ {
+ endWrite();
+ }
+ }
+
+ /**
* Save the configuration to the specified writer.
*
* @param writer - The writer to save the configuration to.
@@ -280,6 +325,7 @@ public class INIConfiguration extends Ba
{
PrintWriter out = new PrintWriter(writer);
boolean first = true;
+ final String separator = getSeparatorUsedInOutput();
beginRead(false);
try
@@ -301,12 +347,12 @@ public class INIConfiguration extends Ba
for (ImmutableNode child : node.getChildren())
{
writeProperty(out, child.getNodeName(),
- child.getValue());
+ child.getValue(), separator);
}
}
else
{
- writeProperty(out, node.getNodeName(), node.getValue());
+ writeProperty(out, node.getNodeName(), node.getValue(), separator);
}
first = false;
}
@@ -451,10 +497,10 @@ public class INIConfiguration extends Ba
* @param key the key
* @param value the value
*/
- private void writeProperty(PrintWriter out, String key, Object value)
+ private void writeProperty(PrintWriter out, String key, Object value, String separator)
{
out.print(key);
- out.print(" = ");
+ out.print(separator);
out.print(escapeValue(value.toString()));
out.println();
}
Added: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/builder/INIBuilderParametersImpl.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/builder/INIBuilderParametersImpl.java?rev=1785035&view=auto
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/builder/INIBuilderParametersImpl.java (added)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/builder/INIBuilderParametersImpl.java Wed Mar 1 20:58:32 2017
@@ -0,0 +1,56 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.configuration2.builder;
+
+import java.util.Map;
+
+/**
+ * <p>
+ * A specialized parameters class for INI configuration.
+ * </p>
+ * <p>
+ * This parameters class defines some properties which allow customizing the
+ * parsing and writing of INI documents.
+ * </p>
+ * <p>
+ * This class is not thread-safe. It is intended that an instance is constructed
+ * and initialized by a single thread during configuration of a
+ * {@code ConfigurationBuilder}.
+ * </p>
+ *
+ * @since 2.2
+ */
+public class INIBuilderParametersImpl extends HierarchicalBuilderParametersImpl
+ implements INIBuilderProperties<INIBuilderParametersImpl>
+{
+ /** The key for the separatorUsedInINIOutput property. */
+ private static final String PROP_SEPARATOR_USED_IN_INI_OUTPUT
+ = "separatorUsedInOutput";
+
+ @Override
+ public void inheritFrom(Map<String, ?> source)
+ {
+ super.inheritFrom(source);
+ copyPropertiesFrom(source, PROP_SEPARATOR_USED_IN_INI_OUTPUT);
+ }
+
+ @Override
+ public INIBuilderParametersImpl setSeparatorUsedInOutput(String separator) {
+ storeProperty(PROP_SEPARATOR_USED_IN_INI_OUTPUT, separator);
+ return this;
+ }
+}
Added: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/builder/INIBuilderProperties.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/builder/INIBuilderProperties.java?rev=1785035&view=auto
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/builder/INIBuilderProperties.java (added)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/builder/INIBuilderProperties.java Wed Mar 1 20:58:32 2017
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.configuration2.builder;
+
+/**
+ * <p>
+ * Definition of a parameters interface for INI configurations.
+ * </p>
+ * <p>
+ * The {@code INIConfiguration} class defines a bunch of additional properties
+ * related to INI processing.
+ * </p>
+ * <p>
+ * <strong>Important note:</strong> This interface is not intended to be
+ * implemented by client code! It defines a set of available properties and may
+ * be extended even in minor releases.
+ * </p>
+ *
+ * @since 2.2
+ * @param <T> the type of the result of all set methods for method chaining
+ */
+public interface INIBuilderProperties<T>
+{
+ /**
+ * Allows setting the separator between key and value to be used when
+ * writing an INI file.
+ *
+ * @param separator the new separator for INI output
+ * @return a reference to this object for method chaining
+ */
+ T setSeparatorUsedInOutput(String separator);
+}
Added: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/builder/fluent/INIBuilderParameters.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/builder/fluent/INIBuilderParameters.java?rev=1785035&view=auto
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/builder/fluent/INIBuilderParameters.java (added)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/builder/fluent/INIBuilderParameters.java Wed Mar 1 20:58:32 2017
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.configuration2.builder.fluent;
+
+import org.apache.commons.configuration2.builder.BasicBuilderProperties;
+import org.apache.commons.configuration2.builder.BuilderParameters;
+import org.apache.commons.configuration2.builder.FileBasedBuilderProperties;
+import org.apache.commons.configuration2.builder.HierarchicalBuilderProperties;
+import org.apache.commons.configuration2.builder.INIBuilderProperties;
+
+/**
+ * <p>
+ * Definition of a parameters interface providing a fluent API for setting all
+ * properties for a INI configuration.
+ * </p>
+ * <p>
+ * <strong>Important note:</strong> This interface is not intended to be
+ * implemented by client code! It defines a set of available properties and may
+ * be extended even in minor releases.
+ * </p>
+ *
+ * @since 2.2
+ */
+public interface INIBuilderParameters extends
+ BasicBuilderProperties<INIBuilderParameters>,
+ FileBasedBuilderProperties<INIBuilderParameters>,
+ HierarchicalBuilderProperties<INIBuilderParameters>,
+ INIBuilderProperties<INIBuilderParameters>, BuilderParameters
+{
+}
Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/builder/fluent/Parameters.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/builder/fluent/Parameters.java?rev=1785035&r1=1785034&r2=1785035&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/builder/fluent/Parameters.java (original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/builder/fluent/Parameters.java Wed Mar 1 20:58:32 2017
@@ -27,6 +27,7 @@ import org.apache.commons.configuration2
import org.apache.commons.configuration2.builder.DefaultParametersManager;
import org.apache.commons.configuration2.builder.FileBasedBuilderParametersImpl;
import org.apache.commons.configuration2.builder.HierarchicalBuilderParametersImpl;
+import org.apache.commons.configuration2.builder.INIBuilderParametersImpl;
import org.apache.commons.configuration2.builder.JndiBuilderParametersImpl;
import org.apache.commons.configuration2.builder.PropertiesBuilderParametersImpl;
import org.apache.commons.configuration2.builder.XMLBuilderParametersImpl;
@@ -272,6 +273,18 @@ public final class Parameters
}
/**
+ * Creates a new instance of a parameters object for INI configurations.
+ *
+ * @return the new parameters object
+ */
+ public INIBuilderParameters ini()
+ {
+ return createParametersProxy(new INIBuilderParametersImpl(),
+ INIBuilderParameters.class, FileBasedBuilderParameters.class,
+ HierarchicalBuilderParameters.class);
+ }
+
+ /**
* Creates a proxy object for a given parameters interface based on the
* given implementation object. The newly created object is initialized
* with default values if there are matching {@link DefaultParametersHandler}
Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestINIConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestINIConfiguration.java?rev=1785035&r1=1785034&r2=1785035&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestINIConfiguration.java (original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestINIConfiguration.java Wed Mar 1 20:58:32 2017
@@ -31,6 +31,7 @@ import java.io.PrintWriter;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
+import java.text.MessageFormat;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
@@ -40,6 +41,7 @@ import java.util.Set;
import org.apache.commons.configuration2.SynchronizerTestImpl.Methods;
import org.apache.commons.configuration2.builder.FileBasedBuilderParametersImpl;
import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder;
+import org.apache.commons.configuration2.builder.fluent.Parameters;
import org.apache.commons.configuration2.convert.DefaultListDelimiterHandler;
import org.apache.commons.configuration2.ex.ConfigurationException;
import org.apache.commons.configuration2.sync.ReadWriteSynchronizer;
@@ -95,6 +97,12 @@ public class TestINIConfiguration
+ " line 2" + LINE_SEPARATOR
+ "continueNoLine = one \\" + LINE_SEPARATOR;
+ private static final String INI_DATA4 = "[section6]" + LINE_SEPARATOR
+ + "key1{0}value1" + LINE_SEPARATOR
+ + "key2{0}value2" + LINE_SEPARATOR + LINE_SEPARATOR
+ + "[section7]" + LINE_SEPARATOR
+ + "key3{0}value3" + LINE_SEPARATOR;
+
private static final String INI_DATA_SEPARATORS = "[section]"
+ LINE_SEPARATOR + "var1 = value1" + LINE_SEPARATOR
+ "var2 : value2" + LINE_SEPARATOR
@@ -223,6 +231,29 @@ public class TestINIConfiguration
}
/**
+ * Test of save method with changed separator
+ */
+ @Test
+ public void testSeparatorUsedInINIOutput() throws Exception
+ {
+ final String outputSeparator = ": ";
+ String input = MessageFormat.format(INI_DATA4, "=").trim();
+ String expectedOutput = MessageFormat.format(INI_DATA4, outputSeparator).trim();
+
+ INIConfiguration instance = new FileBasedConfigurationBuilder<INIConfiguration>(
+ INIConfiguration.class)
+ .configure(new Parameters().ini().setSeparatorUsedInOutput(outputSeparator))
+ .getConfiguration();
+ load(instance, input);
+
+ Writer writer = new StringWriter();
+ instance.write(writer);
+ String result = writer.toString().trim();
+
+ assertEquals("Wrong content of ini file", expectedOutput, result);
+ }
+
+ /**
* Helper method for testing a save operation. This method constructs a
* configuration from the specified content string. Then it saves this
* configuration and checks whether the result matches the original content.