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

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

Introduction

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

Prototype

public void setFile(File file) 

Source Link

Usage

From source file:io.datalayer.conf.XmlConfigurationTest.java

/**
 * Tests saving to a stream./* w  ww. j  a  v  a 2  s  . com*/
 */
@Test
public void testSaveToStream() throws Exception {
    assertNull(conf.getEncoding());
    conf.setEncoding("UTF8");
    FileOutputStream out = null;
    try {
        out = new FileOutputStream(testSaveConf);
        conf.save(out);
    } finally {
        if (out != null) {
            out.close();
        }
    }

    XMLConfiguration checkConfig = new XMLConfiguration();
    checkConfig.setFile(testSaveConf);
    checkSavedConfig(checkConfig);

    try {
        out = new FileOutputStream(testSaveConf);
        conf.save(out, "UTF8");
    } finally {
        if (out != null) {
            out.close();
        }
    }

    checkConfig.clear();
    checkSavedConfig(checkConfig);
}

From source file:io.datalayer.conf.XmlConfigurationTest.java

/**
 * Tests whether attribute splitting can be disabled.
 *///from w  w w .  j a  v  a2 s .  c o m
@Test
public void testAttributeSplittingDisabled() throws ConfigurationException {
    List<Object> values = conf.getList("expressions[@value2]");
    assertEquals("Wrong number of attribute values", 2, values.size());
    assertEquals("Wrong value 1", "a", values.get(0));
    assertEquals("Wrong value 2", "b|c", values.get(1));
    XMLConfiguration conf2 = new XMLConfiguration();
    conf2.setAttributeSplittingDisabled(true);
    conf2.setFile(conf.getFile());
    conf2.load();
    assertEquals("Attribute was split", "a,b|c", conf2.getString("expressions[@value2]"));
}

From source file:io.datalayer.conf.XmlConfigurationTest.java

/**
 * Helper method for testing saving and loading a configuration when
 * delimiter parsing is disabled./*from ww w . j ava 2  s .  c o m*/
 * 
 * @param key
 *            the key to be checked
 * @throws ConfigurationException
 *             if an error occurs
 */
private void checkSaveDelimiterParsingDisabled(String key) throws ConfigurationException {
    conf.clear();
    conf.setDelimiterParsingDisabled(true);
    conf.load();
    conf.setProperty(key, "C:\\Temp\\,C:\\Data\\");
    conf.addProperty(key, "a,b,c");
    conf.save(testSaveConf);
    XMLConfiguration checkConf = new XMLConfiguration();
    checkConf.setDelimiterParsingDisabled(true);
    checkConf.setFile(testSaveConf);
    checkSavedConfig(checkConf);
}

From source file:io.datalayer.conf.XmlConfigurationTest.java

/**
 * Tests string properties with list delimiters when delimiter parsing is
 * disabled/*from   w  ww .  ja  v a2s .c om*/
 */
@Test
public void testDelimiterParsingDisabled() throws ConfigurationException {
    XMLConfiguration conf2 = new XMLConfiguration();
    conf2.setDelimiterParsingDisabled(true);
    conf2.setFile(new File(testProperties));
    conf2.load();

    assertEquals("a,b,c", conf2.getString("split.list3[@values]"));
    assertEquals(0, conf2.getMaxIndex("split.list3[@values]"));
    assertEquals("a\\,b\\,c", conf2.getString("split.list4[@values]"));
    assertEquals("a,b,c", conf2.getString("split.list1"));
    assertEquals(0, conf2.getMaxIndex("split.list1"));
    assertEquals("a\\,b\\,c", conf2.getString("split.list2"));
    conf2 = new XMLConfiguration();
    conf2.setExpressionEngine(new XPathExpressionEngine());
    conf2.setDelimiterParsingDisabled(true);
    conf2.setFile(new File(testProperties));
    conf2.load();

    assertEquals("a,b,c", conf2.getString("split/list3/@values"));
    assertEquals(0, conf2.getMaxIndex("split/list3/@values"));
    assertEquals("a\\,b\\,c", conf2.getString("split/list4/@values"));
    assertEquals("a,b,c", conf2.getString("split/list1"));
    assertEquals(0, conf2.getMaxIndex("split/list1"));
    assertEquals("a\\,b\\,c", conf2.getString("split/list2"));
}

From source file:io.datalayer.conf.XmlConfigurationTest.java

/**
 * Tests disabling both delimiter parsing and attribute splitting.
 *//*from  w  w  w. j a  va2  s  . c om*/
@Test
public void testAttributeSplittingAndDelimiterParsingDisabled() throws ConfigurationException {
    conf.clear();
    conf.setDelimiterParsingDisabled(true);
    conf.load();
    List<Object> values = conf.getList("expressions[@value2]");
    assertEquals("Wrong number of attribute values", 2, values.size());
    assertEquals("Wrong value 1", "a,b", values.get(0));
    assertEquals("Wrong value 2", "c", values.get(1));
    XMLConfiguration conf2 = new XMLConfiguration();
    conf2.setAttributeSplittingDisabled(true);
    conf2.setDelimiterParsingDisabled(true);
    conf2.setFile(conf.getFile());
    conf2.load();
    assertEquals("Attribute was split", "a,b|c", conf2.getString("expressions[@value2]"));
}

From source file:io.datalayer.conf.XmlConfigurationTest.java

/**
 * Tests string properties with list delimiters when delimiter parsing is
 * disabled// w  ww. j  a  v a 2s.c  om
 */
@Test
public void testSaveWithDelimiterParsingDisabled() throws ConfigurationException {
    XMLConfiguration conf = new XMLConfiguration();
    conf.setExpressionEngine(new XPathExpressionEngine());
    conf.setDelimiterParsingDisabled(true);
    conf.setAttributeSplittingDisabled(true);
    conf.setFile(new File(testProperties));
    conf.load();

    assertEquals("a,b,c", conf.getString("split/list3/@values"));
    assertEquals(0, conf.getMaxIndex("split/list3/@values"));
    assertEquals("a\\,b\\,c", conf.getString("split/list4/@values"));
    assertEquals("a,b,c", conf.getString("split/list1"));
    assertEquals(0, conf.getMaxIndex("split/list1"));
    assertEquals("a\\,b\\,c", conf.getString("split/list2"));
    // save the configuration
    conf.save(testSaveConf.getAbsolutePath());

    // read the configuration and compare the properties
    XMLConfiguration checkConfig = new XMLConfiguration();
    checkConfig.setFileName(testSaveConf.getAbsolutePath());
    checkSavedConfig(checkConfig);
    XMLConfiguration config = new XMLConfiguration();
    config.setFileName(testFile2);
    // config.setExpressionEngine(new XPathExpressionEngine());
    config.setDelimiterParsingDisabled(true);
    config.setAttributeSplittingDisabled(true);
    config.load();
    config.setProperty("Employee[@attr1]", "3,2,1");
    assertEquals("3,2,1", config.getString("Employee[@attr1]"));
    config.save(testSaveFile.getAbsolutePath());
    config = new XMLConfiguration();
    config.setFileName(testSaveFile.getAbsolutePath());
    // config.setExpressionEngine(new XPathExpressionEngine());
    config.setDelimiterParsingDisabled(true);
    config.setAttributeSplittingDisabled(true);
    config.load();
    config.setProperty("Employee[@attr1]", "1,2,3");
    assertEquals("1,2,3", config.getString("Employee[@attr1]"));
    config.setProperty("Employee[@attr2]", "one, two, three");
    assertEquals("one, two, three", config.getString("Employee[@attr2]"));
    config.setProperty("Employee.text", "a,b,d");
    assertEquals("a,b,d", config.getString("Employee.text"));
    config.setProperty("Employee.Salary", "100,000");
    assertEquals("100,000", config.getString("Employee.Salary"));
    config.save(testSaveFile.getAbsolutePath());
    checkConfig = new XMLConfiguration();
    checkConfig.setFileName(testSaveFile.getAbsolutePath());
    checkConfig.setExpressionEngine(new XPathExpressionEngine());
    checkConfig.setDelimiterParsingDisabled(true);
    checkConfig.setAttributeSplittingDisabled(true);
    checkConfig.load();
    assertEquals("1,2,3", checkConfig.getString("Employee/@attr1"));
    assertEquals("one, two, three", checkConfig.getString("Employee/@attr2"));
    assertEquals("a,b,d", checkConfig.getString("Employee/text"));
    assertEquals("100,000", checkConfig.getString("Employee/Salary"));
}

From source file:io.datalayer.conf.XmlConfigurationTest.java

/**
 * Tests the copy constructor.//from   ww w. j  a v  a2 s  .  c o m
 */
@Test
public void testInitCopy() throws ConfigurationException {
    XMLConfiguration copy = new XMLConfiguration(conf);
    assertEquals("value", copy.getProperty("element"));
    assertNull("Document was copied, too", copy.getDocument());
    ConfigurationNode root = copy.getRootNode();
    for (ConfigurationNode node : root.getChildren()) {
        assertNull("Reference was not cleared", node.getReference());
    }

    removeTestFile();
    copy.setFile(testSaveConf);
    copy.save();
    copy.clear();
    checkSavedConfig(copy);
}

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 {/*from ww w  .  ja  v a 2s  .  c om*/
            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;
}

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

private static Configuration getVfsFavoritesConfiguration() {
    File file = new File(AllPluginables.USER_CONFIGURATION_DIRECTORY + File.separator + "vfsFavorites.xml");
    XMLConfiguration configuration = new XMLConfiguration();
    configuration.setFile(file);
    if (file.exists()) {
        try {//from w ww . jav  a2  s.com
            configuration.load();
        } catch (ConfigurationException e) {
            LOGGER.severe(String.format("Can't load user configuration from %s: %s", file.getAbsolutePath(),
                    e.getMessage()));
        }
    }
    configuration.setAutoSave(true);
    return configuration;
}

From source file:pl.otros.logview.persistance.PersistentConfiguration.java

private static XMLConfiguration getXMLConfiguration(String file) {
    XMLConfiguration xmlConfiguration = new XMLConfiguration();
    try {//  w w  w.ja v a2 s  .  c o m
        xmlConfiguration = new XMLConfiguration();
        xmlConfiguration.setFile(new File(file));
        xmlConfiguration.load();
        xmlConfiguration.setAutoSave(true);
    } catch (ConfigurationException e) {
        LOGGER.severe("Can't load configuration, creating new " + e.getMessage());

        try {
            xmlConfiguration.save();
            xmlConfiguration.setAutoSave(true);
        } catch (ConfigurationException e1) {
            LOGGER.severe("Can't create persistent configuration: " + e1.getMessage());
        }
    }
    return xmlConfiguration;
}