user3366706 user3366706 - 3 months ago 8
Apache Configuration Question

Modify Apache CommonConfiguration to not to trim values

I'm using Apache common-configuration library to read properties files for various reasons. My code is given below. By default, it strips the spaces before and after the values, which I want to avoid. Is there a way to do that?

CompositeConfiguration config = new CompositeConfiguration();
PropertiesConfiguration propertiesConfiguration = new PropertiesConfiguration();
URL url = CommonConfig.class.getResource("samplePropertiesFile.properties");
Reader reader = new FileReader(new File(url.getFile()));
propertiesConfiguration.read(reader);
config.addConfiguration(propertiesConfiguration);


Content of samplePropertiesFile.properties:

key=<space>value<space>


//both the space before and after must be preserved.

Answer

Values are Java escaped1, so use \u0020 for leading and trailing spaces that shouldn't be removed.

Only the first leading and last trailing space needs this, e.g.

key  =  \u0020  value  \u0020

will be the string <sp><sp><sp>value<sp><sp><sp>.

(OR)

Write CustomPropertiesConfiguration like the example in "Custom properties readers and writers" section in https://commons.apache.org/proper/commons-configuration/userguide/howto_properties.html

1) According to source code.