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:gda.device.detector.odccd.CrysalisRunListValidator.java

/**
 * @param configPath  Save validation criteria to an XML doc
 */// w  w  w . j ava 2  s  . co m
public void saveToConfig(String configPath) {
    if (configPath == null) {
        configPath = LocalProperties.get(LocalProperties.GDA_CONFIG) + "/xml/CrysalisRunListValidator.xml";
    }
    try {
        XMLConfiguration config;
        config = new XMLConfiguration();

        domegaindeg.addToConfig(config);
        ddetectorindeg.addToConfig(config);
        dkappaindeg.addToConfig(config);
        dphiindeg.addToConfig(config);
        dscanwidthindeg.addToConfig(config);
        dscanspeedratio.addToConfig(config);
        dexposuretimeinsec.addToConfig(config);
        config.save(configPath);
    } catch (Exception ex) {
        throw new IllegalArgumentException("Unable to read configuration from " + configPath, ex);
    }
}

From source file:com.eyeq.pivot4j.ui.property.SimplePropertyTest.java

@Test
public void testSettingsManagement() throws ConfigurationException {
    SimpleProperty property = new SimpleProperty("bgColor", "red");

    XMLConfiguration configuration = new XMLConfiguration();
    configuration.setRootElementName("property");

    property.saveSettings(configuration);

    SimpleProperty property2 = new SimpleProperty();
    property2.restoreSettings(configuration);

    assertThat("Property name has been changed.", property2.getName(), is(equalTo(property.getName())));
    assertThat("Property value has been changed.", property2.getValue(), is(equalTo(property.getValue())));

    System.out.println("Saved configuration : ");

    configuration.save(System.out);
}

From source file:com.intuit.tank.vm.settings.BaseCommonsXmlConfig.java

/**
 * Constructor pulls file out of the jar or reads from disk and sets up refresh policy.
 * //w  ww.  j a  v  a  2 s. co  m
 * @param expressionEngine
 *            the expression engine to use. Null results in default expression engine
 */
protected void readConfig() {
    try {
        ExpressionEngine expressionEngine = new XPathExpressionEngine();
        String configPath = getConfigName();
        FileChangedReloadingStrategy reloadingStrategy = new FileChangedReloadingStrategy();

        File dataDirConfigFile = new File(configPath);
        //            LOG.info("Reading settings from " + dataDirConfigFile.getAbsolutePath());
        if (!dataDirConfigFile.exists()) {
            // Load a default from the classpath:
            // Note: we don't let new XMLConfiguration() lookup the resource
            // url directly because it may not be able to find the desired
            // classloader to load the URL from.
            URL configResourceUrl = this.getClass().getClassLoader().getResource(configPath);
            if (configResourceUrl == null) {
                throw new RuntimeException("unable to load resource: " + configPath);
            }

            XMLConfiguration tmpConfig = new XMLConfiguration(configResourceUrl);
            // Copy over a default configuration since none exists:
            // Ensure data dir location exists:
            if (dataDirConfigFile.getParentFile() != null && !dataDirConfigFile.getParentFile().exists()
                    && !dataDirConfigFile.getParentFile().mkdirs()) {
                throw new RuntimeException("could not create directories.");
            }
            tmpConfig.save(dataDirConfigFile);
            LOG.info("Saving settings file to " + dataDirConfigFile.getAbsolutePath());
        }

        if (dataDirConfigFile.exists()) {
            config = new XMLConfiguration(dataDirConfigFile);
        } else {
            // extract from jar and write to
            throw new IllegalStateException("Config file does not exist or cannot be created");
        }
        if (expressionEngine != null) {
            config.setExpressionEngine(expressionEngine);
        }
        configFile = dataDirConfigFile;
        // reload at most once per thirty seconds on configuration queries.
        config.setReloadingStrategy(reloadingStrategy);
        initConfig(config);
    } catch (ConfigurationException e) {
        LOG.error("Error reading settings file: " + e, e);
        throw new RuntimeException(e);
    }
}

From source file:com.eyeq.pivot4j.ui.condition.OrConditionTest.java

@Test
public void testSettingsManagement() throws ConfigurationException {
    RenderContext context = createDummyRenderContext();

    OrCondition or = new OrCondition(conditionFactory);
    or.setSubConditions(Arrays.asList(TestCondition.TRUE, TestCondition.FALSE));

    XMLConfiguration configuration = new XMLConfiguration();
    configuration.setRootElementName("condition");

    or.saveSettings(configuration);/*from w ww.  java2 s .c  o  m*/

    or = new OrCondition(conditionFactory);
    or.restoreSettings(configuration);

    assertThat("Sub conditions should not be null.", or.getSubConditions(), is(notNullValue()));
    assertThat("Sub condition count should be 2.", or.getSubConditions().size(), is(2));
    assertThat("'true || false' should be true.", or.matches(context), is(true));

    System.out.println("Saved configuration : ");

    configuration.save(System.out);
}

From source file:com.eyeq.pivot4j.ui.condition.AndConditionTest.java

@Test
public void testSettingsManagement() throws ConfigurationException {
    RenderContext context = createDummyRenderContext();

    AndCondition and = new AndCondition(conditionFactory);
    and.setSubConditions(Arrays.asList(TestCondition.TRUE, TestCondition.TRUE));

    XMLConfiguration configuration = new XMLConfiguration();
    configuration.setRootElementName("condition");

    and.saveSettings(configuration);/*from  w  w  w  .  ja  v a 2 s . co  m*/

    and = new AndCondition(conditionFactory);
    and.restoreSettings(configuration);

    assertThat("Sub conditions should not be null.", and.getSubConditions(), is(notNullValue()));
    assertThat("Sub condition count should be 2.", and.getSubConditions().size(), is(2));
    assertThat("'true && true' should be true.", and.matches(context), is(true));

    System.out.println("Saved configuration : ");

    configuration.save(System.out);
}

From source file:com.eyeq.pivot4j.ui.condition.ExpressionConditionTest.java

@Test
public void testSettingsManagement() throws ConfigurationException {
    RenderContext context = createDummyRenderContext();
    context.setColIndex(2);/*from ww w .  ja  va 2  s.c o m*/
    context.setRowIndex(1);
    context.setAxis(Axis.ROWS);

    String expression = "<#if columnIndex = 2 && rowIndex = 1>true</#if>";

    ExpressionCondition condition = new ExpressionCondition(conditionFactory);
    condition.setExpression(expression);

    XMLConfiguration configuration = new XMLConfiguration();
    configuration.setRootElementName("condition");

    condition.saveSettings(configuration);

    condition = new ExpressionCondition(conditionFactory);
    condition.restoreSettings(configuration);

    assertThat("Expression has been changed.", condition.getExpression(), is(equalTo(expression)));
    assertThat("Expression '" + expression + "' should be true.", condition.matches(context), is(true));

    System.out.println("Saved configuration : ");

    configuration.save(System.out);
}

From source file:edu.usc.goffish.gopher.impl.client.DeploymentTool.java

private void createClientConfig(StartFloeInfo info, String managerHost, String coordinatorHost)
        throws IOException, ConfigurationException {

    XMLConfiguration config = new XMLConfiguration();
    config.setRootElementName("GopherConfiguration");

    config.setProperty(FLOE_MANAGER, managerHost + ":" + MANAGER_PORT);
    config.setProperty(FLOE_COORDINATOR, coordinatorHost + ":" + COORDINATOR_PORT);

    info.getSourceInfo().sourceNodeTransport.values();

    for (List<TransportInfoBase> b : info.getSourceInfo().sourceNodeTransport.values()) {
        for (TransportInfoBase base : b) {
            String host = base.getParams().get("hostAddress");
            int dataPort = Integer.parseInt(base.getParams().get("tcpListenerPort"));

            int controlPort = Integer.parseInt(base.getControlChannelInfo().getParams().get("tcpListenerPort"));

            config.setProperty(DATA_FLOW_HOST, host);
            config.setProperty(DATA_FLOW_DATA_PORT, dataPort);
            config.setProperty(DATA_FLOW_CONTROL_PORT, controlPort);
            break;
        }/*from ww  w.ja v  a  2 s . c  om*/
        break;
    }

    config.save(new FileWriter(CONFIG_FILE_PATH));

}

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

/**
 * Tests the isEmpty() method for an empty configuration that was reloaded.
 *//*from w ww . j ava  2s. com*/
@Test
public void testEmptyReload() throws ConfigurationException {
    XMLConfiguration config = new XMLConfiguration();
    assertTrue("Newly created configuration not empty", config.isEmpty());
    config.save(testSaveConf);
    config.load(testSaveConf);
    assertTrue("Reloaded configuration not empty", config.isEmpty());
}

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

/**
 * Tests whether the name of the root element is copied when a configuration
 * is created using the copy constructor.
 *//*from   ww w.  jav  a 2  s  .co  m*/
@Test
public void testCopyRootName() throws ConfigurationException {
    final String rootName = "rootElement";
    final String xml = "<" + rootName + "><test>true</test></" + rootName + ">";
    conf.clear();
    conf.load(new StringReader(xml));
    XMLConfiguration copy = new XMLConfiguration(conf);
    assertEquals("Wrong name of root element", rootName, copy.getRootElementName());
    copy.save(testSaveConf);
    copy = new XMLConfiguration(testSaveConf);
    assertEquals("Wrong name of root element after save", rootName, copy.getRootElementName());
}

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

/**
 * Tests whether the name of the root element is copied for a configuration
 * for which not yet a document exists./*from   w  w w.  ja v a 2 s  .  c o  m*/
 */
@Test
public void testCopyRootNameNoDocument() throws ConfigurationException {
    final String rootName = "rootElement";
    conf = new XMLConfiguration();
    conf.setRootElementName(rootName);
    conf.setProperty("test", Boolean.TRUE);
    XMLConfiguration copy = new XMLConfiguration(conf);
    assertEquals("Wrong name of root element", rootName, copy.getRootElementName());
    copy.save(testSaveConf);
    copy = new XMLConfiguration(testSaveConf);
    assertEquals("Wrong name of root element after save", rootName, copy.getRootElementName());
}