0

I'm using commons-configuration v1.10 and I'm using the class PropertiesConfiguration to read in my applications properties. I have a property which has commas in it, but when I read it in, it gets delimited, and I can't figure out how to get it not to.

It spits back the property all in order and commas included, but the reason why it's being a problem is because I get '[' and ']' surrounding it.

AbstractConfiguration has a function, setDelimiterParsingDisabled(), that disables delimiting, but I couldn't find a class which implemented it to read property files.

private static String readProperty(String property) {
    try {
        Configuration configuration = new PropertiesConfiguration(propertiesFile);
        return configuration.getProperty(property).toString();
    }
    catch(ConfigurationException e) {
        System.out.println("Issue reading " + property + " property");
        e.printStackTrace();
        System.exit(1);
        return "";
    }
}
samwell
  • 2,757
  • 9
  • 33
  • 48

4 Answers4

1

It works, but you should disable it or set it before you load the config.

PropertiesConfiguration config = new PropertiesConfiguration();
config.setDelimiterParsingDisabled(true)
config.setListDelimiter(';');
config.setFile(new File("application.properties"));
config.load();
u_0307
  • 33
  • 5
0

Posting your code would help.

According to the official docs you want AbstractConfiguration.setListDelimiter(null);

You can also use String methods to find and remove the surrounding []. Assuming the property is in a string called prop:

int start = prop.indexOf('[') + 1;
int end = prop.lastIndexOf(']');
String val = prop.substring(start,
    end > 0 ? end : prop.length());

indexOf returns -1 if the character isn't found, so adding 1 to get the start of the actual property value always works, even if the delimiter isn't present.

  • I've posted my code in the original post. If you need me to post more code, let me know. Manually removing the brackets would be my last option. – samwell Jul 25 '14 at 18:46
0

It doesn't look like I can use Apache Commons or PropertiesConfiguration to retrieve properties without it being delimited. However, java.util.Properties does not have this issue, so used that instead of PropertiesConfiguration.

MKYong has a good example of how to set it up, http://www.mkyong.com/java/java-properties-file-examples/

samwell
  • 2,757
  • 9
  • 33
  • 48
0
PropertiesConfiguration properties = new PropertiesConfiguration();
properties.setDelimiterParsingDisabled(true);

The above code solves the problem, there is no need to switch to java.util.Properties. The hierarchy is as follows:

    PropertiesConfiguration
              |
              |(extends)
              |
    AbstractFileConfiguration
              |
              |(extends)
              |
    BaseConfiguration
              |
              |(extends)
              |
    AbstractConfiguration

In my case, I used Apache Properties Configuration specifically as it supports variable substitution which is not supported in java.util.Properties

Malathi
  • 2,119
  • 15
  • 40