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

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

Introduction

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

Prototype

public Iterator getKeys() 

Source Link

Usage

From source file:org.kepler.configuration.CommonsConfigurationReader.java

/**
 * Author: David Welker/*from  w  ww. j a  v  a  2 s  .co  m*/
 *
 * This method applies unrun add directives to the configuration.
 * @param addXml
 * @param xmlconfig
 */
private boolean applyAddDirectives(Module module, File addXml, XMLConfiguration xmlconfig)
        throws ConfigurationException {
    String addXmlFilename = addXml.getName();
    String addedXmlFilename = addXmlFilename.substring(0, addXmlFilename.length() - 4) + "ed.xml";

    File addedXml = new File(DotKeplerManager.getInstance().getModuleConfigurationDirectory(module.getName()),
            addedXmlFilename);

    Iterator i;

    XMLConfiguration addXmlConfig = new XMLConfiguration();
    addXmlConfig.setDelimiterParsingDisabled(true);
    addXmlConfig.load(addXml);

    XMLConfiguration addedXmlConfig = new XMLConfiguration();
    addedXmlConfig.setDelimiterParsingDisabled(true);
    if (addedXml.exists())
        addedXmlConfig.load(addedXml);

    i = addXmlConfig.getKeys();
    if (!i.hasNext())
        return false;

    List<String> firstParts = new ArrayList<String>();
    while (i.hasNext()) {
        String key = (String) i.next();
        if (key.contains(".")) {
            String candidate = key.substring(0, key.indexOf('.'));
            if (!firstParts.contains(candidate))
                firstParts.add(candidate);
        }
    }

    for (String firstPart : firstParts) {

        int maxAddIndex = addXmlConfig.getMaxIndex(firstPart);
        int maxAddedIndex = addedXmlConfig.getMaxIndex(firstPart);
        int addIndex = xmlconfig.getMaxIndex(firstPart) + 1;

        List<String> removeKeys = new ArrayList<String>();
        for (int j = 0; j <= maxAddIndex; j++) {
            List<String> addKeys = new ArrayList<String>();
            Iterator x1 = addXmlConfig.getKeys(firstPart + "(" + j + ")");
            while (x1.hasNext()) {
                String key = (String) x1.next();
                addKeys.add(key);
            }
            for (int k = 0; k <= maxAddedIndex; k++) {
                List<String> addedKeys = new ArrayList<String>();
                Iterator x2 = addedXmlConfig.getKeys(firstPart + "(" + k + ")");
                while (x2.hasNext()) {
                    String key = (String) x2.next();
                    addedKeys.add(key);
                }

                if (addMatch(addKeys, addedKeys, addXmlConfig, addedXmlConfig)) {
                    for (String addKey : addKeys)
                        removeKeys.add(addKey);
                }
            }
        }
        for (int j = removeKeys.size() - 1; j >= 0; j--) {
            String removeKey = removeKeys.get(j);
            addXmlConfig.clearProperty(removeKey);
        }

        for (int j = 0; j <= maxAddIndex; j++) {
            String addXMLKey = firstPart + "(" + j + ")";
            i = addXmlConfig.getKeys(addXMLKey);
            while (i.hasNext()) {
                String addXmlConfigKey = (String) i.next();
                String lastPart = addXmlConfigKey.substring(addXmlConfigKey.indexOf('.') + 1,
                        addXmlConfigKey.length());
                String originalXmlConfigKey = firstPart + "(" + (addIndex + j) + ")." + lastPart;
                String addedXmlConfigKey = firstPart + "(" + (maxAddedIndex + 1 + j) + ")." + lastPart;
                xmlconfig.addProperty(originalXmlConfigKey, addXmlConfig.getProperty(addXmlConfigKey));
                addedXmlConfig.addProperty(addedXmlConfigKey, addXmlConfig.getProperty(addXmlConfigKey));
            }
        }
    }

    List<String> addedKeys = new ArrayList<String>();
    i = addedXmlConfig.getKeys();
    while (i.hasNext())
        addedKeys.add((String) i.next());

    i = addXmlConfig.getKeys();
    while (i.hasNext()) {
        String addKey = (String) i.next();
        if (addKey.contains("."))
            continue;
        Object value = addXmlConfig.getProperty(addKey);
        if (addedKeys.contains(addKey)) {
            if (addedXmlConfig.getProperty(addKey).equals(value))
                continue;
        }

        xmlconfig.addProperty(addKey, value);
        addedXmlConfig.addProperty(addKey, value);
    }

    addedXmlConfig.save(addedXml);
    return true;

}

From source file:org.kepler.configuration.ConfigurationTest.java

public static void addTest() throws Exception {
    System.out.println("AddTest");
    File configDir = Module.make("configuration-manager").getConfigurationsDir();
    File configDirectivesDir = new File(configDir.getParentFile(), "config-directives");
    System.out.println(configDir);
    System.out.println(configDirectivesDir);
    System.out.println();//from   w  w w  . ja v  a 2s.  c  o  m

    File originalXml = new File(configDir, "original.xml");
    File addXml = new File(configDirectivesDir, "add.xml");
    File addedXml = new File(configDirectivesDir, "added.xml");
    File newXml = new File(configDirectivesDir, "new.xml");

    XMLConfiguration xmlconfig = new XMLConfiguration();
    xmlconfig.setDelimiterParsingDisabled(true);
    xmlconfig.load(originalXml);

    Iterator i;

    XMLConfiguration addXmlConfig = new XMLConfiguration();
    addXmlConfig.setDelimiterParsingDisabled(true);
    addXmlConfig.load(addXml);

    XMLConfiguration addedXmlConfig = new XMLConfiguration();
    addedXmlConfig.setDelimiterParsingDisabled(true);
    if (addedXml.exists())
        addedXmlConfig.load(addedXml);

    i = addXmlConfig.getKeys();
    if (!i.hasNext())
        return;

    List<String> firstParts = new ArrayList<String>();
    while (i.hasNext()) {
        String key = (String) i.next();
        if (key.contains(".")) {
            String candidate = key.substring(0, key.indexOf('.'));
            if (!firstParts.contains(candidate))
                firstParts.add(candidate);
        }
    }

    for (String firstPart : firstParts) {
        System.out.println("firstPart = " + firstPart);
        int maxAddIndex = addXmlConfig.getMaxIndex(firstPart);
        int maxAddedIndex = addedXmlConfig.getMaxIndex(firstPart);
        int addIndex = xmlconfig.getMaxIndex(firstPart) + 1;

        List<String> removeKeys = new ArrayList<String>();
        for (int j = 0; j <= maxAddIndex; j++) {
            List<String> addKeys = new ArrayList<String>();
            Iterator x1 = addXmlConfig.getKeys(firstPart + "(" + j + ")");
            while (x1.hasNext()) {
                String key = (String) x1.next();
                addKeys.add(key);
            }
            for (int k = 0; k <= maxAddedIndex; k++) {
                List<String> addedKeys = new ArrayList<String>();
                Iterator x2 = addedXmlConfig.getKeys(firstPart + "(" + k + ")");
                while (x2.hasNext()) {
                    String key = (String) x2.next();
                    addedKeys.add(key);
                }

                if (addMatch(addKeys, addedKeys, addXmlConfig, addedXmlConfig)) {
                    for (String addKey : addKeys)
                        removeKeys.add(addKey);
                }
            }
        }
        for (int j = removeKeys.size() - 1; j >= 0; j--) {
            String removeKey = removeKeys.get(j);
            addXmlConfig.clearProperty(removeKey);
        }

        System.out.println("Adding config...");
        for (int j = 0; j <= maxAddIndex; j++) {
            String addXMLKey = firstPart + "(" + j + ")";
            i = addXmlConfig.getKeys(addXMLKey);
            while (i.hasNext()) {
                String addXmlConfigKey = (String) i.next();
                String lastPart = addXmlConfigKey.substring(addXmlConfigKey.indexOf('.') + 1,
                        addXmlConfigKey.length());
                String originalXmlConfigKey = firstPart + "(" + (addIndex + j) + ")." + lastPart;
                String addedXmlConfigKey = firstPart + "(" + (maxAddedIndex + 1 + j) + ")." + lastPart;
                xmlconfig.addProperty(originalXmlConfigKey, addXmlConfig.getProperty(addXmlConfigKey));
                addedXmlConfig.addProperty(addedXmlConfigKey, addXmlConfig.getProperty(addXmlConfigKey));
            }
        }
    }

    System.out.println("Simple adds...");
    List<String> addedKeys = new ArrayList<String>();
    i = addedXmlConfig.getKeys();
    while (i.hasNext())
        addedKeys.add((String) i.next());

    i = addXmlConfig.getKeys();
    while (i.hasNext()) {
        String addKey = (String) i.next();
        if (addKey.contains("."))
            continue;
        Object value = addXmlConfig.getProperty(addKey);
        if (addedKeys.contains(addKey)) {
            if (addedXmlConfig.getProperty(addKey).equals(value))
                continue;
        }

        xmlconfig.addProperty(addKey, value);
        addedXmlConfig.addProperty(addKey, value);
    }

    addedXmlConfig.save(addedXml);
    xmlconfig.save(newXml);
}

From source file:org.kepler.configuration.ConfigurationTest.java

public static void changeTest() throws Exception {

    System.out.println("ChangeTest");
    File configDir = Module.make("configuration-manager").getConfigurationsDir();
    File configDirectivesDir = new File(configDir.getParentFile(), "config-directives");
    System.out.println(configDir);
    System.out.println(configDirectivesDir);
    System.out.println();//w  w w  . j  a v  a2  s  .  c om

    File originalXml = new File(configDir, "original.xml");
    File changeXml = new File(configDirectivesDir, "change.xml");
    File changedXml = new File(configDirectivesDir, "changed.xml");

    XMLConfiguration originalXmlConfig = new XMLConfiguration();
    originalXmlConfig.setDelimiterParsingDisabled(true);
    originalXmlConfig.load(originalXml);

    Iterator i;

    i = originalXmlConfig.getKeys();

    System.out.println("Original.xml:");
    while (i.hasNext()) {
        String key = (String) i.next();
        System.out.println(key + " = " + originalXmlConfig.getProperty(key));
    }
    System.out.println();

    XMLConfiguration changeXmlConfig = new XMLConfiguration();
    changeXmlConfig.setDelimiterParsingDisabled(true);
    changeXmlConfig.load(changeXml);

    XMLConfiguration changedXmlConfig = new XMLConfiguration();
    changedXmlConfig.setDelimiterParsingDisabled(true);
    if (changedXml.exists())
        changedXmlConfig.load(changedXml);

    i = changeXmlConfig.getKeys();

    System.out.println("Change.xml:");
    while (i.hasNext()) {
        String key = (String) i.next();
        System.out.println(key + " = " + changeXmlConfig.getProperty(key));
    }
    System.out.println();

    i = changeXmlConfig.getKeys();
    while (i.hasNext()) {
        String key = (String) i.next();
        Object value = changeXmlConfig.getProperty(key);
        Object changed = changedXmlConfig.getProperty(key);
        if (changed == null || !value.equals(changed)) {
            originalXmlConfig.setProperty(key, value);
            changedXmlConfig.setProperty(key, value);
        }
    }

    i = originalXmlConfig.getKeys();

    System.out.println("New Config:");
    while (i.hasNext()) {
        String key = (String) i.next();
        System.out.println(key + " = " + originalXmlConfig.getProperty(key));
    }
    System.out.println();

    i = changedXmlConfig.getKeys();

    System.out.println("Changed.xml");
    while (i.hasNext()) {
        String key = (String) i.next();
        System.out.println(key + " = " + changedXmlConfig.getProperty(key));
    }
    System.out.println();

    changedXmlConfig.save(changedXml);
}

From source file:org.kepler.configuration.ConfigurationTest.java

public static void removeTest() throws Exception {

    System.out.println("RemoveTest");
    File configDir = Module.make("configuration-manager").getConfigurationsDir();
    File configDirectivesDir = new File(configDir.getParentFile(), "config-directives");
    System.out.println(configDir);
    System.out.println(configDirectivesDir);
    System.out.println();/*from w ww. j  av a 2  s .c  o  m*/

    File originalXml = new File(configDir, "original.xml");
    File removeXml = new File(configDirectivesDir, "remove.xml");
    File removedXml = new File(configDirectivesDir, "removed.xml");
    File newXml = new File(configDirectivesDir, "new.xml");

    XMLConfiguration originalXmlConfig = new XMLConfiguration();
    originalXmlConfig.setDelimiterParsingDisabled(true);
    originalXmlConfig.load(originalXml);

    Iterator i;

    XMLConfiguration removeXmlConfig = new XMLConfiguration();
    removeXmlConfig.setDelimiterParsingDisabled(true);
    removeXmlConfig.load(removeXml);

    XMLConfiguration removedXmlConfig = new XMLConfiguration();
    removedXmlConfig.setDelimiterParsingDisabled(true);
    if (removedXml.exists())
        removedXmlConfig.load(removedXml);

    i = removeXmlConfig.getKeys();
    while (i.hasNext()) {
        String key = (String) i.next();
        Object value = removeXmlConfig.getProperty(key);
        Object removed = removedXmlConfig.getProperty(key);
        if (removed == null || !value.equals(removed)) {
            originalXmlConfig.clearProperty(key);
            removedXmlConfig.setProperty(key, value);
        }
    }

    removedXmlConfig.save(removedXml);
    originalXmlConfig.save(newXml);
}

From source file:org.onosproject.drivers.utilities.YangXmlUtilsTest.java

/**
 * Tests getting a single object configuration via passing the path and the map of the desired values.
 *
 * @throws ConfigurationException if the testing xml file is not there.
 */// www  . j  a va  2s .c  om
@Test
public void testGetXmlConfigurationFromMap() throws ConfigurationException {
    Map<String, String> pathAndValues = new HashMap<>();
    pathAndValues.put("capable-switch.id", "openvswitch");
    pathAndValues.put("switch.id", "ofc-bridge");
    pathAndValues.put("controller.id", "tcp:1.1.1.1:1");
    pathAndValues.put("controller.ip-address", "1.1.1.1");
    XMLConfiguration cfg = utils.getXmlConfiguration(OF_CONFIG_XML_PATH, pathAndValues);
    testCreateConfig.load(getClass().getResourceAsStream("/testCreateSingleYangConfig.xml"));
    assertNotEquals("Null testConfiguration", new XMLConfiguration(), testCreateConfig);

    assertEquals("Wrong configuaration", IteratorUtils.toList(testCreateConfig.getKeys()),
            IteratorUtils.toList(cfg.getKeys()));

    assertEquals("Wrong string configuaration", utils.getString(testCreateConfig), utils.getString(cfg));
}

From source file:org.onosproject.drivers.utilities.YangXmlUtilsTest.java

/**
 * Tests getting a multiple object nested configuration via passing the path
 * and a list of YangElements containing with the element and desired value.
 *
 * @throws ConfigurationException/*from w  w  w  .j ava 2  s.  c om*/
 */
@Test
public void getXmlConfigurationFromYangElements() throws ConfigurationException {

    assertNotEquals("Null testConfiguration", new XMLConfiguration(), testCreateConfig);
    testCreateConfig.load(getClass().getResourceAsStream("/testYangConfig.xml"));
    List<YangElement> elements = new ArrayList<>();
    elements.add(new YangElement("capable-switch", ImmutableMap.of("id", "openvswitch")));
    elements.add(new YangElement("switch", ImmutableMap.of("id", "ofc-bridge")));
    List<ControllerInfo> controllers = ImmutableList.of(
            new ControllerInfo(IpAddress.valueOf("1.1.1.1"), 1, "tcp"),
            new ControllerInfo(IpAddress.valueOf("2.2.2.2"), 2, "tcp"));
    controllers.forEach(cInfo -> {
        elements.add(new YangElement("controller",
                ImmutableMap.of("id", cInfo.target(), "ip-address", cInfo.ip().toString())));
    });
    XMLConfiguration cfg = new XMLConfiguration(
            YangXmlUtils.getInstance().getXmlConfiguration(OF_CONFIG_XML_PATH, elements));
    assertEquals("Wrong configuaration", IteratorUtils.toList(testCreateConfig.getKeys()),
            IteratorUtils.toList(cfg.getKeys()));
    assertEquals("Wrong string configuaration", utils.getString(testCreateConfig), utils.getString(cfg));
}

From source file:org.xmlactions.action.config.ExecContext.java

private void addXmlConfiguration(XMLConfiguration config) {
    Iterator<String> iterator = config.getKeys();
    while (iterator.hasNext()) {
        String key = iterator.next();
        // log.debug("key:" + key);
        this.put(key, config.getProperty(key));
    }/*from   w w w . j a v a2  s .  c  o m*/
}

From source file:pl.otros.logview.gui.LogViewMainFrame.java

private static XMLConfiguration getConfiguration(String file) {
    XMLConfiguration commonConfiguration = new XMLConfiguration();
    File commonConfigurationFile = new File(file);
    // load common configuration
    if (commonConfigurationFile.exists()) {
        LOGGER.info("Loading common configuration from " + commonConfigurationFile.getAbsolutePath());
        try {/* ww  w  .j a  v a  2 s . c o  m*/
            commonConfiguration.load(commonConfigurationFile);
        } catch (ConfigurationException e) {
            LOGGER.severe("Can't load configuration, creating new " + e.getMessage());
        }
    } else {
        LOGGER.info("Common configuration file do not exist");
    }
    // load user specific configuration
    if (!AllPluginables.USER_CONFIGURATION_DIRECTORY.exists()) {
        LOGGER.info("Creating user specific OtrosLogViewer configuration directory "
                + AllPluginables.USER_CONFIGURATION_DIRECTORY.getAbsolutePath());
        AllPluginables.USER_CONFIGURATION_DIRECTORY.mkdirs();
        AllPluginables.USER_FILTER.mkdirs();
        AllPluginables.USER_LOG_IMPORTERS.mkdirs();
        AllPluginables.USER_MARKERS.mkdirs();
        AllPluginables.USER_MESSAGE_FORMATTER_COLORZIERS.mkdirs();
    }
    XMLConfiguration userConfiguration = new XMLConfiguration();
    File userConfigurationFile = new File(AllPluginables.USER_CONFIGURATION_DIRECTORY + File.separator + file);
    userConfiguration.setFile(userConfigurationFile);
    if (userConfigurationFile.exists()) {
        try {
            userConfiguration.load();
        } catch (ConfigurationException e) {
            LOGGER.severe(String.format("Can't load user configuration from %s: %s",
                    userConfigurationFile.getAbsolutePath(), e.getMessage()));
        }
    }
    Iterator<?> keys = commonConfiguration.getKeys();
    while (keys.hasNext()) {
        String key = (String) keys.next();
        if (!userConfiguration.containsKey(key)) {
            userConfiguration.setProperty(key, commonConfiguration.getProperty(key));
        }
    }
    userConfiguration.setAutoSave(true);
    return userConfiguration;
}