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

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

Introduction

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

Prototype

private void load(InputSource source) throws ConfigurationException 

Source Link

Document

Loads a configuration file from the specified input source.

Usage

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();//ww  w .jav  a 2s  .  co  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();/*from   www .  j av a  2s.  co m*/

    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();//  ww  w  . j  av  a2 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.mobicents.servlet.restcomm.http.EndpointMockedTest.java

private Configuration getConfiguration(String path, String homeDirectory, String rootUri)
        throws ConfigurationException {
    Configuration xml = null;//from  ww  w . j  ava  2  s. c om

    XMLConfiguration xmlConfiguration = new XMLConfiguration();
    xmlConfiguration.setDelimiterParsingDisabled(true);
    xmlConfiguration.setAttributeSplittingDisabled(true);
    xmlConfiguration.load(path);
    xml = xmlConfiguration;
    xml.setProperty("runtime-settings.home-directory", homeDirectory);
    xml.setProperty("runtime-settings.root-uri", rootUri);
    return xml;
}

From source file:org.moneta.config.MonetaConfiguration.java

protected static final XMLConfiguration loadConfigurationFromStream(InputStream configurationStream) {
    XMLConfiguration config = new XMLConfiguration();
    try {/*from www.  j  a va 2s . c o  m*/
        config.load(configurationStream);
    } catch (ConfigurationException e) {
        throw new MonetaException("Moneta configuration file not loaded from classpath", e)
                .addContextValue("configFileName", DEFAULT_CONFIGURATION_FILE_NAME);
    } finally {
        IOUtils.closeQuietly(configurationStream);
    }
    return config;
}

From source file:org.n52.car.io.Preferences.java

private Preferences(InputStream stream) {
    XMLConfiguration xml = new XMLConfiguration();

    try {/*from w w w. ja va 2  s.  c o  m*/
        xml.load(stream);
    } catch (ConfigurationException e) {
        logger.warn(e.getMessage(), e);
    }

    this.config = xml;

    initCommonParameters();
}

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

/**
 * Loads a new scenario from the specified input stream.
 *
 * @param stream scenario definition stream
 * @return loaded scenario/*from ww  w  . j a va2  s  . c  o  m*/
 */
public static Scenario loadScenario(InputStream stream) {
    XMLConfiguration cfg = new XMLConfiguration();
    cfg.setAttributeSplittingDisabled(true);
    cfg.setDelimiterParsingDisabled(true);
    cfg.setRootElementName(SCENARIO);
    try {
        cfg.load(stream);
        return loadScenario(cfg);
    } catch (ConfigurationException e) {
        throw new IllegalArgumentException("Unable to load scenario from the stream", e);
    }
}

From source file:org.onosproject.common.app.ApplicationArchive.java

/**
 * Loads the application descriptor from the specified application archive
 * stream and saves the stream in the appropriate application archive
 * directory.//  ww w.j  a v  a2s .c o m
 *
 * @param appName application name
 * @return application descriptor
 * @throws org.onosproject.app.ApplicationException if unable to read application description
 */
public ApplicationDescription getApplicationDescription(String appName) {
    try {
        XMLConfiguration cfg = new XMLConfiguration();
        cfg.setAttributeSplittingDisabled(true);
        cfg.setDelimiterParsingDisabled(true);
        cfg.load(appFile(appName, APP_XML));
        return loadAppDescription(cfg);
    } catch (Exception e) {
        throw new ApplicationException("Unable to get app description", e);
    }
}

From source file:org.onosproject.common.app.ApplicationArchive.java

private ApplicationDescription parsePlainAppDescription(InputStream stream) throws IOException {
    XMLConfiguration cfg = new XMLConfiguration();
    cfg.setAttributeSplittingDisabled(true);
    cfg.setDelimiterParsingDisabled(true);
    try {//  ww  w.  java  2s  .c  om
        cfg.load(stream);
        return loadAppDescription(cfg);
    } catch (ConfigurationException e) {
        throw new IOException("Unable to parse " + APP_XML, e);
    }
}

From source file:org.onosproject.driver.netconf.XmlConfigParser.java

protected static HierarchicalConfiguration loadXml(InputStream xmlStream) {
    XMLConfiguration cfg = new XMLConfiguration();
    try {/*from  ww  w  . j a va 2  s .c  o  m*/
        cfg.load(xmlStream);
        return cfg;
    } catch (ConfigurationException e) {
        throw new IllegalArgumentException("Cannot load xml from Stream", e);
    }
}