Example usage for org.apache.commons.configuration PropertiesConfiguration getKeys

List of usage examples for org.apache.commons.configuration PropertiesConfiguration getKeys

Introduction

In this page you can find the example usage for org.apache.commons.configuration PropertiesConfiguration getKeys.

Prototype

public Iterator getKeys() 

Source Link

Document

Returns an Iterator with the keys contained in this configuration.

Usage

From source file:org.onlab.stc.ScenarioStore.java

/**
 * Loads the states from disk.//w  w  w. j av  a2 s  .  c  o  m
 */
private void load() {
    try {
        PropertiesConfiguration cfg = new PropertiesConfiguration(storeFile);
        cfg.getKeys().forEachRemaining(prop -> add(StepEvent.fromString(cfg.getString(prop))));
        cfg.save();
    } catch (ConfigurationException e) {
        print("Unable to load file %s", storeFile);
    }
}

From source file:org.sakuli.utils.SakuliPropertyPlaceholderConfigurer.java

/**
 * Reads in the properties for a specific file
 *
 * @param props    Properties to update/*  ww w .jav  a 2s.  c om*/
 * @param filePath path to readable properties file
 * @param active   activate or deactivate the  function
 */
protected void addPropertiesFromFile(Properties props, String filePath, boolean active) {
    if (active) {
        logger.info("read in properties from '{}'", filePath);
        try {
            PropertiesConfiguration propertiesConfiguration = new PropertiesConfiguration(filePath);
            Iterator<String> keyIt = propertiesConfiguration.getKeys();
            while (keyIt.hasNext()) {
                String key = keyIt.next();
                Object value = propertiesConfiguration.getProperty(key);
                props.put(key, value);
            }
        } catch (ConfigurationException | NullPointerException e) {
            throw new RuntimeException("Error by reading the property file '" + filePath + "'", e);
        }
    }
}

From source file:org.sonar.plugins.l10n.L10nHackyPropertiesUpdater.java

public static void main(String[] args) throws ConfigurationException, IOException {
    URL l10nRoot = FrenchPackPlugin.class.getResource(BundleSynchronizedMatcher.L10N_PATH);

    Collection<File> bundles = FileUtils.listFiles(FileUtils.toFile(l10nRoot), new String[] { "properties" },
            false);// w  w w  .  j ava2  s.  co  m

    for (File localizedBundle : bundles) {
        String originalVersion = localizedBundle.getName().replaceFirst("_fr\\.", ".");
        System.out.println("Processing " + localizedBundle + " looking for " + originalVersion);
        URL originalBundle = FrenchPackPlugin.class
                .getResource(BundleSynchronizedMatcher.L10N_PATH + originalVersion);
        if (originalBundle == null) {
            System.out.println("\tOriginal bundle not found");
        } else {
            System.out.println("\tOriginal bundle found, let's try to update the localized version");
            Properties localizedProps = new Properties();
            localizedProps.load(new FileInputStream(localizedBundle));

            PropertiesConfiguration config = new PropertiesConfiguration();
            PropertiesConfigurationLayout layout = new PropertiesConfigurationLayout(config);
            layout.load(new InputStreamReader(FrenchPackPlugin.class
                    .getResourceAsStream(BundleSynchronizedMatcher.L10N_PATH + originalVersion)));

            for (@SuppressWarnings("unchecked")
            Iterator<String> it = config.getKeys(); it.hasNext();) {
                String key = it.next();
                Object localizedValue = localizedProps.get(key);
                if (localizedValue != null) {
                    config.setProperty(key, localizedValue);
                } else {
                    System.out.println("Nothing found for " + key);
                    String currentValue = config.getString(key);
                    config.setProperty(key, currentValue + " <== TODO");
                }
            }

            layout.save(new FileWriter(localizedBundle));

            System.out.println("\tFixing spaces");
            fixSpacesAroundEqualsAndScrewUpEncoding(localizedBundle);
            System.out.println("OK: file " + localizedBundle + " contains ready-to-translate updated file.");
        }
    }

}

From source file:org.structr.api.config.Settings.java

public static void loadConfiguration(final String fileName) {

    try {/*w  ww  . j a  v  a2s.  co m*/

        PropertiesConfiguration.setDefaultListDelimiter('\0');

        final PropertiesConfiguration config = new PropertiesConfiguration(fileName);
        final Iterator<String> keys = config.getKeys();

        while (keys.hasNext()) {

            final String key = keys.next();
            final String value = trim(config.getString(key));
            Setting<?> setting = Settings.getSetting(key);

            if (setting != null) {

                setting.fromString(value);

            } else {

                SettingsGroup targetGroup = miscGroup;

                // put key in cron group if it contains ".cronExpression"
                if (key.contains(".cronExpression")) {
                    targetGroup = cronGroup;
                }

                // create new StringSetting for unknown key
                Settings.createSettingForValue(targetGroup, key, value);
            }
        }

    } catch (ConfigurationException ex) {
        System.err.println("Unable to load configuration: " + ex.getMessage());
    }

}

From source file:org.structr.api.service.StructrServices.java

public static void loadConfiguration(final Properties properties, final PropertiesConfiguration config) {

    final Iterator<String> keys = config.getKeys();

    while (keys.hasNext()) {

        final String key = keys.next();
        properties.setProperty(key, config.getString(key));
    }/*  www  .  jav  a 2 s.  c o m*/
}

From source file:org.structr.common.StructrConf.java

public void load(final PropertiesConfiguration config) {

    final Iterator<String> keys = config.getKeys();

    while (keys.hasNext()) {
        final String key = keys.next();
        this.setProperty(key, config.getString(key));
    }//from w ww  . j a v  a2s.c o m
}

From source file:org.waveprotocol.box.server.util.ClientFlagsUtil.java

/**
 * Extracts parameters from configuration file and writes errors to the log.
 * //from ww w .  j  a  v  a 2 s.c om
 * @param configFileName configuration file name
 * @param checkParams true, if params should be checked for validity
 * @param log message log
 * @return pairs (name, value) of parameters
 */
public static List<Pair<String, String>> extractParamsFromConfigFile(String configFileName, boolean checkParams,
        Log log) {
    try {
        List<Pair<String, String>> params = new ArrayList<>();
        PropertiesConfiguration configuration = new PropertiesConfiguration(configFileName);
        Iterator<String> keys = configuration.getKeys();
        while (keys.hasNext()) {
            String key = keys.next().trim();
            String name = ValueUtils.toCamelCase(key, "_", false);
            String value = configuration.getString(key).trim();
            if (checkParams) {
                try {
                    checkParameter(name, value);
                } catch (IllegalArgumentException e) {
                    warning(log, e.getMessage());
                    continue;
                }
            }
            params.add(new Pair<>(name, value));
        }
        return params;
    } catch (ConfigurationException e) {
        severe(log, "Failed to extract parameters from configuration file: " + e.getMessage());
        return new ArrayList<>();
    }
}

From source file:org.waveprotocol.wave.util.flags.ClientFlagsGenerator.java

private static List<Parameter> extractParametersFromDefaultConfigFile(String fileName) {
    try {//from   w  w  w  .  ja v  a  2s . c  o m
        List<Parameter> parameters = new ArrayList<>();
        PropertiesConfiguration configuration = new PropertiesConfiguration(fileName);
        Iterator<String> keys = configuration.getKeys();
        while (keys.hasNext()) {
            String key = keys.next();
            parameters.add(Parameter.create(key, configuration.getString(key).trim(),
                    configuration.getLayout().getComment(key)));
        }
        return parameters;
    } catch (ConfigurationException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.wisdom.maven.utils.Properties2HoconConverterTest.java

@Test
public void testOnWikipediaSample() throws IOException {
    File props = new File(root, "/wiki.properties");
    File hocon = Properties2HoconConverter.convert(props, true);

    PropertiesConfiguration properties = loadPropertiesWithApacheConfiguration(props);
    assertThat(properties).isNotNull();/*from   ww w .  ja  va2s  . c o  m*/
    Config config = load(hocon);
    assertThat(properties.isEmpty()).isFalse();
    assertThat(config.isEmpty()).isFalse();

    Iterator<String> iterator = properties.getKeys();
    String[] names = Iterators.toArray(iterator, String.class);
    for (String name : names) {
        if (!name.isEmpty()) {
            // 'cheeses' is not supported by commons-config.
            String o = properties.getString(name);
            String v = config.getString(name);
            assertThat(o).isEqualTo(v);
        }
    }

    assertThat(config.getString("cheeses")).isEmpty();

}

From source file:pt.spms.epsos.main.MigrateProperties.java

/**
 * This method will process the properties and insert them in the database,
 * based on the specified properties file name.
 *
 * @param epsosPropsFile the properties file name.
 * @throws ConfigurationException if the properties file reading wen wrong.
 *//*from  w ww .  ja v  a  2 s .  c  o m*/
private static void processProperties(final String epsosPropsFile)
        throws ConfigurationException, FileNotFoundException, IOException {
    LOGGER.info("READING CONFIGURATION FILE FROM: " + epsosPropsFile);

    File propsFile = new File(epsosPropsFile);

    processCommaProperties(propsFile, epsosPropsFile);

    propsFile = new File(epsosPropsFile);

    final PropertiesConfiguration config = new PropertiesConfiguration();
    config.setReloadingStrategy(new FileChangedReloadingStrategy());

    final Session session = HibernateUtil.getSessionFactory().openSession();

    LOGGER.info("INSERTING PROPERTIES INTO DATABASE...");

    session.beginTransaction();

    final Iterator it = config.getKeys();

    while (it.hasNext()) {
        final String key = (String) it.next();
        final String value = (String) config.getString(key);

        LOGGER.info("INSERTING: { KEY: " + key + ", VALUE: " + value + " }");

        final Property p = new Property(key, value);
        session.save(p);
    }

    session.getTransaction().commit();

    session.close();
}