Java Code Examples for org.apache.commons.configuration2.io.FileHandler#setEncoding()

The following examples show how to use org.apache.commons.configuration2.io.FileHandler#setEncoding() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestXMLPropertyListConfiguration.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Tests whether the encoding is written when saving a configuration.
 */
@Test
public void testSaveWithEncoding() throws ConfigurationException
{
    final String encoding = "UTF-8";
    final FileHandler handler = new FileHandler(config);
    handler.setEncoding(encoding);
    final StringWriter writer = new StringWriter();
    handler.save(writer);
    assertTrue(
            "Encoding not found",
            writer.toString()
                    .indexOf(
                            "<?xml version=\"1.0\" encoding=\"" + encoding
                                    + "\"?>") >= 0);
}
 
Example 2
Source File: ConfigUtils.java    From studio with GNU General Public License v3.0 5 votes vote down vote up
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;
}
 
Example 3
Source File: FileBasedConfigurationBuilder.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Initializes the encoding of the specified file handler. If already an
 * encoding is set, it is used. Otherwise, the default encoding for the
 * result configuration class is obtained and set.
 *
 * @param handler the handler to be initialized
 */
private void initEncoding(final FileHandler handler)
{
    if (StringUtils.isEmpty(handler.getEncoding()))
    {
        final String encoding = getDefaultEncoding(getResultClass());
        if (encoding != null)
        {
            handler.setEncoding(encoding);
        }
    }
}
 
Example 4
Source File: TestXMLConfiguration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests whether the encoding is written to the generated XML file.
 */
@Test
public void testSaveWithEncoding() throws ConfigurationException
{
    conf = new XMLConfiguration();
    conf.setProperty("test", "a value");
    final FileHandler handler = new FileHandler(conf);
    handler.setEncoding(ENCODING);

    final StringWriter out = new StringWriter();
    handler.save(out);
    assertThat("Encoding was not written to file", out.toString(),
            containsString("encoding=\"" + ENCODING + "\""));
}
 
Example 5
Source File: TestFileBasedConfigurationBuilder.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests whether the default encoding can be overridden when initializing
 * the file handler.
 */
@Test
public void testInitFileHandlerOverrideDefaultEncoding()
        throws ConfigurationException
{
    final FileBasedConfigurationBuilder<PropertiesConfiguration> builder =
            new FileBasedConfigurationBuilder<>(
                    PropertiesConfiguration.class);
    final FileHandler handler = new FileHandler();
    final String encoding = "testEncoding";
    handler.setEncoding(encoding);
    builder.initFileHandler(handler);
    assertEquals("Encoding was changed", encoding, handler.getEncoding());
}