Apache Commons Configuration2 how to read data from InputStream

Bruce picture Bruce · Sep 19, 2016 · Viewed 8.1k times · Source

How can I read the data from InputStream by using Apache Commons Configuration2?

FileBasedConfigurationBuilder<XMLConfiguration> builder = 
    new FileBasedConfigurationBuilder<XMLConfiguration>(XMLConfiguration.class)
        .configure(
            new Parameters()
            .xml()
            .setFileName("")
            .setExpressionEngine(new XPathExpressionEngine())
        );

XMLConfiguration config = builder.getConfiguration();
config.read(sourceJarFile.getInputStream(sourcePropertiesEntry))

Gives the above code, I will get the below exception if the setFileName is given empty string.

org.apache.commons.configuration2.ex.ConfigurationException: Could not locate: org.apache.commons.configuration2.io.FileLocator@61dc03ce[fileName=tmp.xml,basePath=<null>,sourceURL=,encoding=<null>,fileSystem=<null>,locationStrategy=<null>]
at org.apache.commons.configuration2.io.FileLocatorUtils.locateOrThrow(FileLocatorUtils.java:346)
at org.apache.commons.configuration2.io.FileHandler.load(FileHandler.java:972)
at org.apache.commons.configuration2.io.FileHandler.load(FileHandler.java:702)
at org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder.initFileHandler(FileBasedConfigurationBuilder.java:312)
at org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder.initResultInstance(FileBasedConfigurationBuilder.java:291)
at org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder.initResultInstance(FileBasedConfigurationBuilder.java:60)
at org.apache.commons.configuration2.builder.BasicConfigurationBuilder.createResult(BasicConfigurationBuilder.java:421)
at org.apache.commons.configuration2.builder.BasicConfigurationBuilder.getConfiguration(BasicConfigurationBuilder.java:285)
at com.test.installer.App.getXMLConfigurationProperties(App.java:185)

If I give null or just not call setFileName(); I will get the unable to load configuration exception at the read() line.

org.apache.commons.configuration2.ex.ConfigurationException: Unable to load the configuration
    at org.apache.commons.configuration2.XMLConfiguration.load(XMLConfiguration.java:986)
    at org.apache.commons.configuration2.XMLConfiguration.read(XMLConfiguration.java:954)
    at com.test.installer.App.updateExistedProperties(App.java:84)

Answer

r33tnup picture r33tnup · Feb 7, 2017

From the example in the API documentation:

Set up your file parameters (encoding and such):

   FileBasedBuilderParameters fileparams = ...    
   FileBasedConfigurationBuilder<PropertiesConfiguration> builder =
                    new FileBasedConfigurationBuilder<>(PropertiesConfiguration.class).configure(fileparams);

and then:

FileBasedConfiguration config = builder.getConfiguration();
FileHandler fileHandler = new FileHandler(config);
Inputstream istream = ...
fileHandler.load(istream);

Note that you cannot use autosave with this. To save you'd probably need to provide an OutputStream. Something like:

fh.save(ostream)