Search in sources :

Example 1 with FileHandler

use of org.apache.commons.configuration2.io.FileHandler in project commons by craftercms.

the class ConfigUtils method readXmlConfiguration.

/**
 * Reads the XML configuration from the specified input stream, using the given file encoding.
 *
 * @param input         the input stream from where to read the configuration
 * @param fileEncoding  the encoding of the file. If not specified {@link #DEFAULT_ENCODING} will be used
 *
 * @return the loaded XML configuration, as a an Apache Commons {@link HierarchicalConfiguration}
 *
 * @throws ConfigurationException if an error occurs while reading the configuration
 */
public static HierarchicalConfiguration<ImmutableNode> readXmlConfiguration(InputStream input, Map<String, Lookup> prefixLookups, String fileEncoding) throws ConfigurationException {
    Parameters params = new Parameters();
    FileBasedConfigurationBuilder<XMLConfiguration> builder = new FileBasedConfigurationBuilder<>(XMLConfiguration.class);
    try {
        XMLBuilderParameters xmlParams = params.xml();
        if (MapUtils.isNotEmpty(prefixLookups)) {
            xmlParams = xmlParams.setPrefixLookups(prefixLookups);
        }
        builder.configure(xmlParams);
        XMLConfiguration config = builder.getConfiguration();
        FileHandler fileHandler = new FileHandler(config);
        fileHandler.setEncoding(StringUtils.isNotBlank(fileEncoding) ? fileEncoding : DEFAULT_ENCODING);
        fileHandler.load(input);
        return config;
    } catch (Exception e) {
        throw new ConfigurationException("Unable to read XML configuration", e);
    }
}
Also used : XMLConfiguration(org.apache.commons.configuration2.XMLConfiguration) Parameters(org.apache.commons.configuration2.builder.fluent.Parameters) XMLBuilderParameters(org.apache.commons.configuration2.builder.fluent.XMLBuilderParameters) FileBasedConfigurationBuilder(org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder) XMLBuilderParameters(org.apache.commons.configuration2.builder.fluent.XMLBuilderParameters) FileHandler(org.apache.commons.configuration2.io.FileHandler)

Example 2 with FileHandler

use of org.apache.commons.configuration2.io.FileHandler in project studio by craftercms.

the class ConfigUtils method readXmlConfiguration.

public static HierarchicalConfiguration<ImmutableNode> readXmlConfiguration(InputStream input) throws ConfigurationException {
    Parameters params = new Parameters();
    FileBasedConfigurationBuilder<XMLConfiguration> builder = new FileBasedConfigurationBuilder<>(XMLConfiguration.class);
    XMLConfiguration config = builder.configure(params.xml()).getConfiguration();
    FileHandler fileHandler = new FileHandler(config);
    fileHandler.setEncoding("UTF-8");
    fileHandler.load(input);
    return config;
}
Also used : XMLConfiguration(org.apache.commons.configuration2.XMLConfiguration) Parameters(org.apache.commons.configuration2.builder.fluent.Parameters) FileBasedConfigurationBuilder(org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder) FileHandler(org.apache.commons.configuration2.io.FileHandler)

Example 3 with FileHandler

use of org.apache.commons.configuration2.io.FileHandler in project hugegraph-common by hugegraph.

the class HugeConfigTest method testHugeConfigWithConfiguration.

@Test
public void testHugeConfigWithConfiguration() throws Exception {
    PropertiesConfiguration configuration = new PropertiesConfiguration();
    FileHandler fileHandler = new FileHandler(configuration);
    fileHandler.load(CONF);
    HugeConfig config = new HugeConfig(configuration);
    Assert.assertEquals("file-text1-value", config.get(TestOptions.text1));
    Assert.assertEquals("file-text2-value", config.get(TestOptions.text2));
    Assert.assertEquals("CHOICE-3", config.get(TestOptions.text3));
}
Also used : HugeConfig(com.baidu.hugegraph.config.HugeConfig) PropertiesConfiguration(org.apache.commons.configuration2.PropertiesConfiguration) FileHandler(org.apache.commons.configuration2.io.FileHandler) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Example 4 with FileHandler

use of org.apache.commons.configuration2.io.FileHandler in project jcore-dependencies by JULIELab.

the class DBTestUtils method createTestCostosysConfig.

/**
 * Writes a CoStoSys test configuration to <code>targetPath</code>.
 * @param targetPath The path to write the configuration file to.
 * @param schemaName The data table schema to use.
 * @param maxActiveConnections The DB connection pool size.
 * @param postgres The org.testcontainers postgres test container.
 * @return The path to the CoStoSys configuration file.
 * @throws ConfigurationException If the creation of the configuration file failed.
 */
public static String createTestCostosysConfig(String targetPath, String schemaName, int maxActiveConnections, PostgreSQLContainer postgres) throws ConfigurationException {
    XMLConfiguration costosysconfig = new XMLConfiguration();
    costosysconfig.setProperty("databaseConnectorConfiguration.DBSchemaInformation.activeTableSchema", schemaName);
    costosysconfig.setProperty("databaseConnectorConfiguration.DBConnectionInformation.activeDBConnection", postgres.getDatabaseName());
    costosysconfig.setProperty("databaseConnectorConfiguration.DBConnectionInformation.DBConnections.DBConnection[@name]", postgres.getDatabaseName());
    costosysconfig.setProperty("databaseConnectorConfiguration.DBConnectionInformation.DBConnections.DBConnection[@url]", postgres.getJdbcUrl());
    costosysconfig.setProperty("databaseConnectorConfiguration.DBConnectionInformation.maxActiveDBConnections", maxActiveConnections);
    FileHandler fh = new FileHandler(costosysconfig);
    String costosysConfig = targetPath;
    fh.save(costosysConfig);
    return costosysConfig;
}
Also used : XMLConfiguration(org.apache.commons.configuration2.XMLConfiguration) FileHandler(org.apache.commons.configuration2.io.FileHandler)

Example 5 with FileHandler

use of org.apache.commons.configuration2.io.FileHandler in project modules by assimbly.

the class XMLFileConfiguration method getFlowConfiguration.

public TreeMap<String, String> getFlowConfiguration(String flowId, URI uri) throws Exception {
    String scheme = uri.getScheme();
    // load uri to configuration
    Parameters params = new Parameters();
    DocumentBuilder docBuilder = setDocumentBuilder("integration.xsd");
    if (scheme.startsWith("sonicfs")) {
        URL Url = uri.toURL();
        InputStream is = Url.openStream();
        conf = new BasicConfigurationBuilder<>(XMLConfiguration.class).configure(params.xml()).getConfiguration();
        FileHandler fh = new FileHandler(conf);
        fh.load(is);
    } else if (scheme.startsWith("file")) {
        File xml = new File(uri.getRawPath());
        FileBasedConfigurationBuilder<XMLConfiguration> builder = new FileBasedConfigurationBuilder<XMLConfiguration>(XMLConfiguration.class).configure(params.xml().setFileName("integration.xml").setFile(xml).setDocumentBuilder(docBuilder).setSchemaValidation(true).setExpressionEngine(new XPathExpressionEngine()));
        // This will throw a ConfigurationException if the XML document does not
        // conform to its Schema.
        conf = builder.getConfiguration();
    } else if (scheme.startsWith("http")) {
        URL Url = uri.toURL();
        FileBasedConfigurationBuilder<XMLConfiguration> builder = new FileBasedConfigurationBuilder<XMLConfiguration>(XMLConfiguration.class).configure(params.xml().setURL(Url).setFileName("integration.xml").setDocumentBuilder(docBuilder).setSchemaValidation(true).setExpressionEngine(new XPathExpressionEngine()));
        // This will throw a ConfigurationException if the XML document does not
        // conform to its Schema.
        conf = builder.getConfiguration();
    } else {
        throw new Exception("URI scheme for " + uri.getRawPath() + " is not supported");
    }
    properties = new Unmarshall().getProperties(conf, flowId);
    return properties;
}
Also used : Parameters(org.apache.commons.configuration2.builder.fluent.Parameters) FileBasedConfigurationBuilder(org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder) InputStream(java.io.InputStream) URL(java.net.URL) SAXParseException(org.xml.sax.SAXParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) FileHandler(org.apache.commons.configuration2.io.FileHandler) XMLConfiguration(org.apache.commons.configuration2.XMLConfiguration) Unmarshall(org.assimbly.integration.configuration.marshalling.Unmarshall) DocumentBuilder(javax.xml.parsers.DocumentBuilder) XPathExpressionEngine(org.apache.commons.configuration2.tree.xpath.XPathExpressionEngine) File(java.io.File)

Aggregations

FileHandler (org.apache.commons.configuration2.io.FileHandler)137 Test (org.junit.Test)111 File (java.io.File)26 StringWriter (java.io.StringWriter)22 ConfigurationException (org.apache.commons.configuration2.ex.ConfigurationException)22 PropertiesConfiguration (org.apache.commons.configuration2.PropertiesConfiguration)15 StringReader (java.io.StringReader)14 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)14 XMLConfiguration (org.apache.commons.configuration2.XMLConfiguration)13 XMLPropertiesConfiguration (org.apache.commons.configuration2.XMLPropertiesConfiguration)9 Before (org.junit.Before)9 URL (java.net.URL)7 DefaultListDelimiterHandler (org.apache.commons.configuration2.convert.DefaultListDelimiterHandler)7 IOException (java.io.IOException)6 Parameters (org.apache.commons.configuration2.builder.fluent.Parameters)5 LegacyListDelimiterHandler (org.apache.commons.configuration2.convert.LegacyListDelimiterHandler)5 FileHandlerReloadingDetector (org.apache.commons.configuration2.reloading.FileHandlerReloadingDetector)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 FileBasedConfigurationBuilder (org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder)4 BufferedReader (java.io.BufferedReader)3