org.apache.commons.configuration2.INIConfiguration Java Examples

The following examples show how to use org.apache.commons.configuration2.INIConfiguration. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Configuration.java    From Kepler with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static Map<String, String> load(String configPath) throws IOError, IOException, ConfigurationException {
    Map<String, String> config = new ConcurrentHashMap<>();
    Path path = Paths.get(configPath);

    INIConfiguration ini = new INIConfiguration();
    BufferedReader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8);
    ini.read(reader);

    Set<String> sectionNames = ini.getSections();
    //System.out.printf("Section names: %s", sectionNames.toString());

    for (String sectionName : sectionNames) {
        SubnodeConfiguration section = ini.getSection(sectionName);

        if (section != null) {
            Iterator<String> keys = section.getKeys();

            while (keys.hasNext()) {
                String key = keys.next();
                String value = section.getString(key);

                if (value != null) {
                    key = key.replace("..", "."); // TODO: find a better way than this hack
                    config.put(key, value);
                }
            }
        }
    }

    reader.close();

    return config;
}
 
Example #2
Source File: TestConfigurations.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests whether a builder for a INI configuration can be created for a
 * given file.
 */
@Test
public void testINIBuilderFromFile() throws ConfigurationException
{
    final Configurations configs = new Configurations();
    final FileBasedConfigurationBuilder<INIConfiguration> builder =
            configs.iniBuilder(ConfigurationAssert.getTestFile(TEST_INI));
    checkINI(builder.getConfiguration());
}
 
Example #3
Source File: TestConfigurations.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests whether a INI configuration can be loaded from a file.
 */
@Test
public void testINIFromFile() throws ConfigurationException
{
    final Configurations configs = new Configurations();
    final INIConfiguration config =
            configs.ini(ConfigurationAssert.getTestFile(TEST_INI));
    checkINI(config);
}
 
Example #4
Source File: TestConfigurations.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests whether a builder for a INI configuration can be created for a
 * given URL.
 */
@Test
public void testINIBuilderFromURL() throws ConfigurationException
{
    final Configurations configs = new Configurations();
    final FileBasedConfigurationBuilder<INIConfiguration> builder =
            configs.iniBuilder(ConfigurationAssert.getTestURL(TEST_INI));
    checkINI(builder.getConfiguration());
}
 
Example #5
Source File: TestConfigurations.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests whether a INI configuration can be loaded from a URL.
 */
@Test
public void testINIFromURL() throws ConfigurationException
{
    final Configurations configs = new Configurations();
    final INIConfiguration config =
            configs.ini(ConfigurationAssert.getTestURL(TEST_INI));
    checkINI(config);
}
 
Example #6
Source File: TestConfigurations.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests whether a builder for a INI configuration can be created for a
 * given file path.
 */
@Test
public void testINIBuilderFromPath() throws ConfigurationException
{
    final Configurations configs = new Configurations();
    final FileBasedConfigurationBuilder<INIConfiguration> builder =
            configs.iniBuilder(absolutePath(TEST_INI));
    checkINI(builder.getConfiguration());
}
 
Example #7
Source File: TestConfigurations.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests whether a INI configuration can be loaded from a file path.
 */
@Test
public void testINIFromPath() throws ConfigurationException
{
    final Configurations configs = new Configurations();
    final INIConfiguration config = configs.ini(absolutePath(TEST_INI));
    checkINI(config);
}
 
Example #8
Source File: EdgeRcClientCredentialProvider.java    From AkamaiOPEN-edgegrid-java with Apache License 2.0 3 votes vote down vote up
/**
 * Loads an EdgeRc configuration file and returns an {@link EdgeRcClientCredentialProvider} to
 * read {@link ClientCredential}s from it.
 *
 * @param reader an open {@link Reader} to an EdgeRc file
 * @param section a config section ({@code null} for the default section)
 * @throws ConfigurationException If an error occurs while reading the configuration
 * @throws IOException if an I/O error occurs
 */
public EdgeRcClientCredentialProvider(Reader reader, String section)
        throws ConfigurationException, IOException {
    Objects.requireNonNull(reader, "reader cannot be null");
    configuration = new INIConfiguration();
    configuration.read(reader);
    this.defaultSectionName = section;
}
 
Example #9
Source File: Configurations.java    From commons-configuration with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a builder for a {@code INIConfiguration} and initializes it with
 * the given file to be loaded.
 *
 * @param file the file to be loaded
 * @return the newly created {@code FileBasedConfigurationBuilder}
 */
public FileBasedConfigurationBuilder<INIConfiguration> iniBuilder(final File file)
{
    return fileBasedBuilder(INIConfiguration.class, file);
}
 
Example #10
Source File: Configurations.java    From commons-configuration with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a builder for a {@code INIConfiguration} and initializes it with
 * the given URL to be loaded.
 *
 * @param url the URL to be loaded
 * @return the newly created {@code FileBasedConfigurationBuilder}
 */
public FileBasedConfigurationBuilder<INIConfiguration> iniBuilder(final URL url)
{
    return fileBasedBuilder(INIConfiguration.class, url);
}
 
Example #11
Source File: Configurations.java    From commons-configuration with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a builder for a {@code INIConfiguration} and initializes it with
 * the file file identified by the given path.
 *
 * @param path the path to the file to be loaded
 * @return the newly created {@code FileBasedConfigurationBuilder}
 */
public FileBasedConfigurationBuilder<INIConfiguration> iniBuilder(
        final String path)
{
    return fileBasedBuilder(INIConfiguration.class, path);
}
 
Example #12
Source File: Configurations.java    From commons-configuration with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a {@code INIConfiguration} instance from the content of the given
 * file. This is a convenience method which can be used if no builder is
 * needed for managing the configuration object. (Although, behind the
 * scenes a builder is created).
 *
 * @param file the file to be loaded
 * @return a {@code INIConfiguration} object initialized from this file
 * @throws ConfigurationException if an error occurred when loading the
 *         configuration
 */
public INIConfiguration ini(final File file) throws ConfigurationException
{
    return iniBuilder(file).getConfiguration();
}
 
Example #13
Source File: Configurations.java    From commons-configuration with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a {@code INIConfiguration} instance from the content of the given
 * URL. This is a convenience method which can be used if no builder is
 * needed for managing the configuration object. (Although, behind the
 * scenes a builder is created).
 *
 * @param url the URL to be loaded
 * @return a {@code INIConfiguration} object initialized from this file
 * @throws ConfigurationException if an error occurred when loading the
 *         configuration
 */
public INIConfiguration ini(final URL url) throws ConfigurationException
{
    return iniBuilder(url).getConfiguration();
}
 
Example #14
Source File: Configurations.java    From commons-configuration with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a {@code INIConfiguration} instance from the content of the file
 * identified by the given path. This is a convenience method which can be
 * used if no builder is needed for managing the configuration object.
 * (Although, behind the scenes a builder is created).
 *
 * @param path the path to the file to be loaded
 * @return a {@code INIConfiguration} object initialized from this file
 * @throws ConfigurationException if an error occurred when loading the
 *         configuration
 */
public INIConfiguration ini(final String path) throws ConfigurationException
{
    return iniBuilder(path).getConfiguration();
}
 
Example #15
Source File: TestConfigurations.java    From commons-configuration with Apache License 2.0 2 votes vote down vote up
/**
 * Checks whether a test INI configuration was correctly loaded.
 *
 * @param config the configuration instance to be checked.
 */
private static void checkINI(final INIConfiguration config)
{
    assertEquals("yes", config.getProperty("testini.loaded"));
}