5

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)
cheffe
  • 9,345
  • 2
  • 46
  • 57
Bruce
  • 647
  • 2
  • 12
  • 30

3 Answers3

3

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)
Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
r33tnup
  • 319
  • 2
  • 9
2

Proper way of loading XML configuration data from Input Stream (in commons-collections 2.x) is as follows:

XMLConfiguration cfg = new BasicConfigurationBuilder<>(XMLConfiguration.class).configure(new Parameters().xml()).getConfiguration();
FileHandler fh = new FileHandler(cfg);
fh.load(inputStream);

After calling load() cfg will contain loaded configuration.

Also note, that using XMLConfiguration.read() method should not be used, as this method is designed for internal use and probably will be renamed to _read() in future (see: https://issues.apache.org/jira/browse/CONFIGURATION-641).

Rafi
  • 369
  • 2
  • 5
0

You can use XMLConfiguration.read(InputStream in) , but as far as I know, you need to have a XML file somewhere. The reason is that when you either get the configuration from the builder or call the read method above, there are a few checks in the private load method (line 963 in the XMLConfiguration.java in the source files).

    Parameters params = new Parameters();
    FileBasedConfigurationBuilder<XMLConfiguration> fileBuilder =
            new FileBasedConfigurationBuilder<>(XMLConfiguration.class)
            .configure(params.fileBased().setFileName("/tmp/dummy.xml"));`

    XMLConfiguration xmlConfiguration = fileBuilder.getConfiguration();
    xmlConfiguration.read(inputStream);

The dummy file can be anything as long as it’s well-formed, it doesn’t need to be valid. In my case, /tmp/dummy.xml just contains <_/>.

Alf
  • 1,414
  • 1
  • 15
  • 27