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

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

Introduction

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

Prototype

public void save(Writer writer) throws ConfigurationException 

Source Link

Document

Saves the configuration to the specified writer.

Usage

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

/**
 * Author: David Welker/*from   w w  w .java  2s.  c  o 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.jav a2  s  . 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();/*from  w w  w. j a va 2  s.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();//from   w ww .  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.kitodo.editor.XMLEditor.java

/**
 * Load the content of the XML configuration file with the given name
 * 'configurationFile'.// ww w  . j a v  a2 s  .  com
 *
 * @param configurationFile
 *            name of the configuration to be loaded
 */
public void loadXMLConfiguration(String configurationFile) {
    try (StringWriter stringWriter = new StringWriter()) {
        currentConfigurationFile = configurationFile;
        XMLConfiguration currentConfiguration = new XMLConfiguration(
                ConfigCore.getKitodoConfigDirectory() + currentConfigurationFile);
        currentConfiguration.save(stringWriter);
        this.xmlConfigurationString = stringWriter.toString();
    } catch (ConfigurationException e) {
        String errorMessage = "ERROR: Unable to load configuration file '" + configurationFile + "'.";
        logger.error(errorMessage + " " + e.getMessage());
        this.xmlConfigurationString = errorMessage;
    } catch (IOException e) {
        logger.error(e.getMessage());
    }
}

From source file:org.kitodo.production.editor.XMLEditor.java

/**
 * Load the content of the XML configuration file with the given name
 * 'configurationFile'./*  w  w  w .jav a2  s.  c o m*/
 *
 * @param configurationFile
 *            name of the configuration to be loaded
 */
public void loadXMLConfiguration(String configurationFile) {
    try (StringWriter stringWriter = new StringWriter()) {
        currentConfigurationFile = configurationFile;
        this.configurationFile = KitodoConfigFile.getByName(configurationFile);
        XMLConfiguration currentConfiguration = new XMLConfiguration(this.configurationFile.getAbsolutePath());
        currentConfiguration.save(stringWriter);
        this.xmlConfigurationString = stringWriter.toString();
    } catch (ConfigurationException e) {
        String errorMessage = "ERROR: Unable to load configuration file '" + configurationFile + "'.";
        logger.error(errorMessage + " " + e.getMessage());
        this.xmlConfigurationString = errorMessage;
    } catch (IOException e) {
        logger.error(e.getMessage());
    }
}

From source file:org.mifos.accounts.persistence.AccountPersistence.java

public String dumpChartOfAccounts() throws ConfigurationException {
    XMLConfiguration config = new XMLConfiguration();
    Query topLevelAccounts = getSession().getNamedQuery(NamedQueryConstants.GET_TOP_LEVEL_ACCOUNTS);
    List listAccounts = topLevelAccounts.list();
    Iterator it = listAccounts.iterator();
    AccountPersistence ap = new AccountPersistence();
    String assetsAccountGLCode = ap.getCategory(GLCategoryType.ASSET).getGlCode();
    String liabilitiesAccountGLCode = ap.getCategory(GLCategoryType.LIABILITY).getGlCode();
    String incomeAccountGLCode = ap.getCategory(GLCategoryType.INCOME).getGlCode();
    String expenditureAccountGLCode = ap.getCategory(GLCategoryType.EXPENDITURE).getGlCode();
    while (it.hasNext()) {
        COABO coa = (COABO) it.next();//from   w ww  .j a va  2  s  .c  o  m
        String name = coa.getAccountName();
        String glCode = coa.getAssociatedGlcode().getGlcode();
        String path = "ChartOfAccounts";
        if (glCode.equals(assetsAccountGLCode)) {
            path = path + ASSETS_GL_ACCOUNT_TAG;
        } else if (glCode.equals(liabilitiesAccountGLCode)) {
            path = path + LIABILITIES_GL_ACCOUNT_TAG;
        } else if (glCode.equals(incomeAccountGLCode)) {
            path = path + INCOME_GL_ACCOUNT_TAG;
        } else if (glCode.equals(expenditureAccountGLCode)) {
            path = path + EXPENDITURE_GL_ACCOUNT_TAG;
        } else {
            throw new RuntimeException("Unrecognized top level GLCode: " + glCode);
        }
        config.addProperty(path + "(-1)[@code]", glCode);
        config.addProperty(path + "[@name]", name);
        addAccountSubcategories(config, coa, path + GL_ACCOUNT_TAG);
    }
    StringWriter stringWriter = new StringWriter();
    config.save(stringWriter);
    String chart = stringWriter.toString();

    return chart;
}

From source file:org.mifos.accounts.persistence.LegacyAccountDao.java

public String dumpChartOfAccounts() throws ConfigurationException {
    XMLConfiguration config = new XMLConfiguration();
    Query topLevelAccounts = getSession().getNamedQuery(NamedQueryConstants.GET_TOP_LEVEL_ACCOUNTS);
    List listAccounts = topLevelAccounts.list();
    Iterator it = listAccounts.iterator();
    LegacyAccountDao ap = new LegacyAccountDao();
    String assetsAccountGLCode = ap.getCategory(GLCategoryType.ASSET).getGlCode();
    String liabilitiesAccountGLCode = ap.getCategory(GLCategoryType.LIABILITY).getGlCode();
    String incomeAccountGLCode = ap.getCategory(GLCategoryType.INCOME).getGlCode();
    String expenditureAccountGLCode = ap.getCategory(GLCategoryType.EXPENDITURE).getGlCode();
    while (it.hasNext()) {
        COABO coa = (COABO) it.next();//w  w  w .j a va2  s .co m
        String name = coa.getAccountName();
        String glCode = coa.getAssociatedGlcode().getGlcode();
        String path = "ChartOfAccounts";
        if (glCode.equals(assetsAccountGLCode)) {
            path = path + ASSETS_GL_ACCOUNT_TAG;
        } else if (glCode.equals(liabilitiesAccountGLCode)) {
            path = path + LIABILITIES_GL_ACCOUNT_TAG;
        } else if (glCode.equals(incomeAccountGLCode)) {
            path = path + INCOME_GL_ACCOUNT_TAG;
        } else if (glCode.equals(expenditureAccountGLCode)) {
            path = path + EXPENDITURE_GL_ACCOUNT_TAG;
        } else {
            throw new RuntimeException("Unrecognized top level GLCode: " + glCode);
        }
        config.addProperty(path + "(-1)[@code]", glCode);
        config.addProperty(path + "[@name]", name);
        addAccountSubcategories(config, coa, path + GL_ACCOUNT_TAG);
    }
    StringWriter stringWriter = new StringWriter();
    config.save(stringWriter);
    String chart = stringWriter.toString();

    return chart;
}

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

protected static String createControllersConfig(HierarchicalConfiguration cfg,
        HierarchicalConfiguration actualCfg, String target, String netconfOperation, String controllerOperation,
        List<ControllerInfo> controllers) {
    //cfg.getKeys().forEachRemaining(key -> System.out.println(key));
    cfg.setProperty("edit-config.target", target);
    cfg.setProperty("edit-config.default-operation", netconfOperation);
    cfg.setProperty("edit-config.config.capable-switch.id", parseCapableSwitchId(actualCfg));
    cfg.setProperty("edit-config.config.capable-switch." + "logical-switches.switch.id",
            parseSwitchId(actualCfg));//from w w w.j a va 2s .  c o  m
    List<ConfigurationNode> newControllers = new ArrayList<>();
    for (ControllerInfo ci : controllers) {
        XMLConfiguration controller = new XMLConfiguration();
        controller.setRoot(new HierarchicalConfiguration.Node("controller"));
        String id = ci.type() + ":" + ci.ip() + ":" + ci.port();
        controller.setProperty("id", id);
        controller.setProperty("ip-address", ci.ip());
        controller.setProperty("port", ci.port());
        controller.setProperty("protocol", ci.type());
        newControllers.add(controller.getRootNode());
    }
    cfg.addNodes("edit-config.config.capable-switch.logical-switches." + "switch.controllers", newControllers);
    XMLConfiguration editcfg = (XMLConfiguration) cfg;
    StringWriter stringWriter = new StringWriter();
    try {
        editcfg.save(stringWriter);
    } catch (ConfigurationException e) {
        e.printStackTrace();
    }
    String s = stringWriter.toString().replaceAll("<controller>",
            "<controller nc:operation=\"" + controllerOperation + "\">");
    s = s.replace("<target>" + target + "</target>", "<target><" + target + "/></target>");
    return s;

}

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

public static String createControllersConfig(HierarchicalConfiguration cfg, HierarchicalConfiguration actualCfg,
        String target, String netconfOperation, String controllerOperation, List<ControllerInfo> controllers) {
    //cfg.getKeys().forEachRemaining(key -> System.out.println(key));
    cfg.setProperty("edit-config.target", target);
    cfg.setProperty("edit-config.default-operation", netconfOperation);
    cfg.setProperty("edit-config.config.capable-switch.id", parseCapableSwitchId(actualCfg));
    cfg.setProperty("edit-config.config.capable-switch." + "logical-switches.switch.id",
            parseSwitchId(actualCfg));//from  ww w  .  j  a v  a2 s  . c  o  m
    List<ConfigurationNode> newControllers = new ArrayList<>();
    for (ControllerInfo ci : controllers) {
        XMLConfiguration controller = new XMLConfiguration();
        controller.setRoot(new HierarchicalConfiguration.Node("controller"));
        String id = ci.type() + ":" + ci.ip() + ":" + ci.port();
        controller.setProperty("id", id);
        controller.setProperty("ip-address", ci.ip());
        controller.setProperty("port", ci.port());
        controller.setProperty("protocol", ci.type());
        newControllers.add(controller.getRootNode());
    }
    cfg.addNodes("edit-config.config.capable-switch.logical-switches." + "switch.controllers", newControllers);
    XMLConfiguration editcfg = (XMLConfiguration) cfg;
    StringWriter stringWriter = new StringWriter();
    try {
        editcfg.save(stringWriter);
    } catch (ConfigurationException e) {
        log.error("createControllersConfig()", e);
    }
    String s = stringWriter.toString().replaceAll("<controller>",
            "<controller nc:operation=\"" + controllerOperation + "\">");
    s = s.replace("<target>" + target + "</target>", "<target><" + target + "/></target>");
    return s;

}