1

Since I do not know of a better solution, I am currently writing small Java classes to process .properties file to merge them, remove duplicate properties, override properties, etc. (I need to process many files and a huge number of properties).

org.apache.commons.configuration.PropertiesConfiguration works great for reading a properties file (using org.apache.commons.configuration.AbstractFileConfiguration.load(InputStream, String), however if I rewrite the file using org.apache.commons.configuration.AbstractFileConfiguration.save(File), I have two problems:

  1. the original layout and comments are lost. I am going to try the PropertiesConfigurationLayout, which is supposed to help here (see How to overwrite one property in .properties without overwriting the whole file?) and post the results
  2. the properties are slightly modified. Accents é and è are rewritten as unicode characters (\u00E9), which I do not want. Afaik .properties files are generally ISO-8859-1 (and I think mine are), so escaping shouldn't be necessary. Specifying the encoding when calling org.apache.commons.configuration.AbstractFileConfiguration.load(InputStream, String) does not make a difference, because when it is not specified, the same encoding is used by default anyway (private static final String DEFAULT_ENCODING = "ISO-8859-1";). What could I do about that ?
Community
  • 1
  • 1
Toto
  • 397
  • 6
  • 17
  • I can't reproduce number 1, if I override the loaded file it contains the same layout with the corresponding overriden properties and the new ones using the same classes you mentioned `PropertiesConfiguration` and `AbstractFileConfiguration`. On number 2 I have the same problem. – yamilmedina Oct 15 '15 at 21:23
  • If you follow the [source code](http://svn.apache.org/viewvc/commons/proper/configuration/tags/CONFIGURATION_1_10/src/main/java/org/apache/commons/configuration/) you'll find that the DEFAULT_ENCODING is NOT used when saving (it's quite a web of internal classes and abstractions). I can only see AbstractFileConfiguration.save(OutputStream out, String encoding) using the actual encoding specified, all other methods appear to use to the platform default encoding in the end. You might want to try Ini4j's [Options](http://ini4j.sourceforge.net/tutorial/OptTutorial.java.html). – vanOekel Oct 15 '15 at 23:39

1 Answers1

2

Doing some tests I think you can do what you want, using CombinedConfiguration plus a OverrideCombiner. Basically the properties will be merged automatically and the trick for the layout is to get the layout from one of the loaded files:

CombinedConfiguration props = new CombinedConfiguration();

final PropertiesConfiguration defaultsProps = new PropertiesConfiguration(new File("/tmp/default.properties"));
final PropertiesConfiguration customProps = new PropertiesConfiguration(new File("/tmp/custom.properties"));
props.setNodeCombiner(new OverrideCombiner());
props.addConfiguration(customProps); //first should be loaded the override values
props.addConfiguration(defaultsProps); // last your 'default' values

PropertiesConfiguration finalFile = new PropertiesConfiguration();   
finalFile.append(props);
PropertiesConfigurationLayout layout = new PropertiesConfigurationLayout(finalFile, defaultsProps.getLayout()); //here we copy the layout from the 'base file'
layout.save(new FileWriter(new File("/tmp/app.properties")));

The issue with the encoding I don't know if its possible to find a solution.

yamilmedina
  • 3,315
  • 2
  • 20
  • 28