org.apache.commons.configuration2.convert.DisabledListDelimiterHandler Java Examples

The following examples show how to use org.apache.commons.configuration2.convert.DisabledListDelimiterHandler. 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: FileConfigurationProvider.java    From james-project with Apache License 2.0 6 votes vote down vote up
public static XMLConfiguration getConfig(InputStream configStream) throws ConfigurationException {
    FileBasedConfigurationBuilder<XMLConfiguration> builder = new FileBasedConfigurationBuilder<>(XMLConfiguration.class)
        .configure(new Parameters()
            .xml()
            .setListDelimiterHandler(new DisabledListDelimiterHandler()));
    XMLConfiguration xmlConfiguration = builder.getConfiguration();
    FileHandler fileHandler = new FileHandler(xmlConfiguration);
    fileHandler.load(configStream);
    try {
        configStream.close();
    } catch (IOException ignored) {
        // Ignored
    }

    return xmlConfiguration;
}
 
Example #2
Source File: TestXMLConfiguration.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Tests whether a windows path can be saved correctly. This test is related
 * to CONFIGURATION-428.
 */
@Test
public void testSaveWindowsPath() throws ConfigurationException
{
    conf.clear();
    conf.setListDelimiterHandler(new DisabledListDelimiterHandler());
    conf.addProperty("path", "C:\\Temp");
    final StringWriter writer = new StringWriter();
    new FileHandler(conf).save(writer);
    final String content = writer.toString();
    assertThat("Path not found: ", content,
            containsString("<path>C:\\Temp</path>"));
    saveTestConfig();
    final XMLConfiguration conf2 = new XMLConfiguration();
    load(conf2, testSaveConf.getAbsolutePath());
    assertEquals("Wrong windows path", "C:\\Temp",
            conf2.getString("path"));
}
 
Example #3
Source File: AbstractConfiguration.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a configuration with the same content as this configuration, but
 * with all variables replaced by their actual values. This method tries to
 * clone the configuration and then perform interpolation on all properties.
 * So property values of the form {@code ${var}} will be resolved as
 * far as possible (if a variable cannot be resolved, it remains unchanged).
 * This operation is useful if the content of a configuration is to be
 * exported or processed by an external component that does not support
 * variable interpolation.
 *
 * @return a configuration with all variables interpolated
 * @throws org.apache.commons.configuration2.ex.ConfigurationRuntimeException if this
 * configuration cannot be cloned
 * @since 1.5
 */
public Configuration interpolatedConfiguration()
{
    // first clone this configuration
    final AbstractConfiguration c = (AbstractConfiguration) ConfigurationUtils
            .cloneConfiguration(this);

    // now perform interpolation
    c.setListDelimiterHandler(new DisabledListDelimiterHandler());
    for (final Iterator<String> it = getKeys(); it.hasNext();)
    {
        final String key = it.next();
        c.setProperty(key, getList(key));
    }

    c.setListDelimiterHandler(getListDelimiterHandler());
    return c;
}
 
Example #4
Source File: DatabaseConfiguration.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Adds a property to this configuration. This implementation
 * temporarily disables list delimiter parsing, so that even if the value
 * contains the list delimiter, only a single record is written into
 * the managed table. The implementation of {@code getProperty()}
 * takes care about delimiters. So list delimiters are fully supported
 * by {@code DatabaseConfiguration}, but internally treated a bit
 * differently.
 *
 * @param key the key of the new property
 * @param value the value to be added
 */
@Override
protected void addPropertyInternal(final String key, final Object value)
{
    final ListDelimiterHandler oldHandler = getListDelimiterHandler();
    try
    {
        // temporarily disable delimiter parsing
        setListDelimiterHandler(DisabledListDelimiterHandler.INSTANCE);
        super.addPropertyInternal(key, value);
    }
    finally
    {
        setListDelimiterHandler(oldHandler);
    }
}
 
Example #5
Source File: TestSubnodeConfiguration.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Tests manipulating the list delimiter handler. This object is derived
 * from the parent.
 */
@Test
public void testSetListDelimiterHandler()
{
    final ListDelimiterHandler handler1 = new DefaultListDelimiterHandler('/');
    final ListDelimiterHandler handler2 = new DefaultListDelimiterHandler(';');
    parent.setListDelimiterHandler(handler1);
    setUpSubnodeConfig();
    parent.setListDelimiterHandler(handler2);
    assertEquals("List delimiter handler not obtained from parent",
            handler1, config.getListDelimiterHandler());
    config.addProperty("newProp", "test1,test2/test3");
    assertEquals("List was incorrectly splitted", "test1,test2",
            parent.getString("tables.table(0).newProp"));
    config.setListDelimiterHandler(DisabledListDelimiterHandler.INSTANCE);
    assertEquals("List delimiter changed on parent", handler2,
            parent.getListDelimiterHandler());
}
 
Example #6
Source File: TestPropertiesConfiguration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests whether a list property is handled correctly if delimiter parsing
 * is disabled. This test is related to CONFIGURATION-495.
 */
@Test
public void testSetPropertyListWithDelimiterParsingDisabled()
        throws ConfigurationException
{
    final String prop = "delimiterListProp";
    conf.setListDelimiterHandler(DisabledListDelimiterHandler.INSTANCE);
    final List<String> list = Arrays.asList("val", "val2", "val3");
    conf.setProperty(prop, list);
    saveTestConfig();
    conf.clear();
    load(conf, testSavePropertiesFile.getAbsolutePath());
    assertEquals("Wrong list property", list, conf.getProperty(prop));
}
 
Example #7
Source File: TestBaseConfigurationBuilderProvider.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a configuration declaration based on the given configuration.
 *
 * @param declConfig the configuration for the declaration
 * @return the declaration
 */
private ConfigurationDeclaration createDeclaration(
        final HierarchicalConfiguration<?> declConfig)
{
    final CombinedConfigurationBuilder parentBuilder =
            new CombinedConfigurationBuilder()
            {
                @Override
                protected void initChildBuilderParameters(
                        final BuilderParameters params)
                {
                    // set a property value; this should be overridden by
                    // child builders
                    if (params instanceof BasicBuilderParameters)
                    {
                        ((BasicBuilderParameters) params)
                                .setListDelimiterHandler(DisabledListDelimiterHandler.INSTANCE);
                    }
                }
            };
    final ConfigurationDeclaration decl =
            new ConfigurationDeclaration(parentBuilder, declConfig)
            {
                @Override
                protected Object interpolate(final Object value)
                {
                    return value;
                }
            };
    return decl;
}
 
Example #8
Source File: TestAbstractConfigurationBasicFeatures.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests the default list delimiter hander.
 */
@Test
public void testDefaultListDelimiterHandler()
{
    final BaseConfiguration config = new BaseConfiguration();
    assertTrue(
            "Wrong list delimiter handler",
            config.getListDelimiterHandler() instanceof DisabledListDelimiterHandler);
}
 
Example #9
Source File: TestXMLConfiguration_605.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithOnlyCommaWithStringBuilderWithoutDelimiterParsing() throws Exception
{
    final StringBuilder sourceBuilder = new StringBuilder("<configuration>");
    sourceBuilder.append("<key0></key0>");
    sourceBuilder.append("<key1>,</key1>");
    sourceBuilder.append("<key2></key2>");
    sourceBuilder.append("<key3></key3>");
    sourceBuilder.append("</configuration>");
    checkConfiguration(create(sourceBuilder.toString(), DisabledListDelimiterHandler.INSTANCE));
}
 
Example #10
Source File: TestXMLListHandling.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tries to save the configuration with a different list delimiter handler
 * which does not support escaping of lists. This should fail with a
 * meaningful exception message.
 */
@Test(expected = ConfigurationRuntimeException.class)
public void testIncompatibleListDelimiterOnSaving()
        throws ConfigurationException
{
    config.setListDelimiterHandler(DisabledListDelimiterHandler.INSTANCE);
    saveToString();
}
 
Example #11
Source File: TestXMLConfiguration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests whether list properties are added correctly if delimiter parsing is
 * disabled. This test is related to CONFIGURATION-495.
 */
@Test
public void testAddPropertyListWithDelimiterParsingDisabled()
        throws ConfigurationException
{
    conf.clear();
    final String prop = "delimiterListProp";
    conf.setListDelimiterHandler(DisabledListDelimiterHandler.INSTANCE);
    final List<String> list = Arrays.asList("val", "val2", "val3");
    conf.addProperty(prop, list);
    saveTestConfig();
    final XMLConfiguration conf2 = new XMLConfiguration();
    load(conf2, testSaveConf.getAbsolutePath());
    assertEquals("Wrong list property", list, conf2.getProperty(prop));
}
 
Example #12
Source File: TestXMLConfiguration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Helper method for testing saving and loading a configuration when
 * delimiter parsing is disabled.
 *
 * @param key the key to be checked
 * @throws ConfigurationException if an error occurs
 */
private void checkSaveDelimiterParsingDisabled(final String key)
        throws ConfigurationException
{
    conf.clear();
    conf.setListDelimiterHandler(new DisabledListDelimiterHandler());
    load(conf, testProperties);
    conf.setProperty(key, "C:\\Temp\\,C:\\Data\\");
    conf.addProperty(key, "a,b,c");
    saveTestConfig();
    final XMLConfiguration checkConf = new XMLConfiguration();
    checkConf.setListDelimiterHandler(conf.getListDelimiterHandler());
    load(checkConf, testSaveConf.getAbsolutePath());
    ConfigurationAssert.assertConfigurationEquals(conf, checkConf);
}
 
Example #13
Source File: TestMapConfiguration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests querying a property if trimming is enabled, but list splitting is
 * disabled. In this case no trimming is performed (trimming only works if
 * list splitting is enabled).
 */
@Test
public void testGetPropertyTrimNoSplit()
{
    final MapConfiguration config = (MapConfiguration) getConfiguration();
    config.getMap().put(KEY, SPACE_VALUE);
    config.setListDelimiterHandler(new DisabledListDelimiterHandler());
    assertEquals("Wrong trimmed value", SPACE_VALUE, config.getProperty(KEY));
}
 
Example #14
Source File: TestPropertiesConfiguration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests saving a configuration if delimiter parsing is disabled.
 */
@Test
public void testSaveWithDelimiterParsingDisabled() throws ConfigurationException
{
    conf.clear();
    conf.setListDelimiterHandler(new DisabledListDelimiterHandler());
    conf.addProperty("test.list", "a,b,c");
    conf.addProperty("test.dirs", "C:\\Temp\\,D:\\Data\\");
    saveTestConfig();

    final PropertiesConfiguration checkConfig = new PropertiesConfiguration();
    checkConfig.setListDelimiterHandler(new DisabledListDelimiterHandler());
    new FileHandler(checkConfig).load(testSavePropertiesFile);
    ConfigurationAssert.assertConfigurationEquals(conf, checkConfig);
}
 
Example #15
Source File: AbstractConfiguration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new instance of {@code AbstractConfiguration}.
 */
public AbstractConfiguration()
{
    interpolator = new AtomicReference<>();
    initLogger(null);
    installDefaultInterpolator();
    listDelimiterHandler = DisabledListDelimiterHandler.INSTANCE;
    conversionHandler = DefaultConversionHandler.INSTANCE;
}
 
Example #16
Source File: ConfigurationProviderImpl.java    From james-project with Apache License 2.0 5 votes vote down vote up
/**
 * Load the xmlConfiguration from the given resource.
 * 
 * @param r
 * @return
 * @throws ConfigurationException
 * @throws IOException
 */
private XMLConfiguration getConfig(Resource r) throws ConfigurationException, IOException {
    FileBasedConfigurationBuilder<XMLConfiguration> builder = new FileBasedConfigurationBuilder<>(XMLConfiguration.class)
        .configure(new Parameters()
            .xml()
            .setListDelimiterHandler(new DisabledListDelimiterHandler()));
    XMLConfiguration xmlConfiguration = builder.getConfiguration();
    FileHandler fileHandler = new FileHandler(xmlConfiguration);
    fileHandler.load(r.getInputStream());

    return xmlConfiguration;
}
 
Example #17
Source File: MailetConfigImplTest.java    From james-project with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    FileBasedConfigurationBuilder<XMLConfiguration> builder = new FileBasedConfigurationBuilder<>(XMLConfiguration.class)
        .configure(new Parameters()
            .xml()
            .setListDelimiterHandler(new DisabledListDelimiterHandler()));
    xmlConfiguration = builder.getConfiguration();
    fileHandler = new FileHandler(xmlConfiguration);

    config = new MailetConfigImpl();
}
 
Example #18
Source File: MailetConfigImpl.java    From james-project with Apache License 2.0 5 votes vote down vote up
/**
 * Set the Avalon Configuration object for the mailet.
 * 
 * @param newConfiguration
 *            the new Configuration for the mailet
 */
public void setConfiguration(Configuration newConfiguration) {
    BaseHierarchicalConfiguration builder = new BaseHierarchicalConfiguration();
    
    // Disable the delimiter parsing. See JAMES-1232
    builder.setListDelimiterHandler(new DisabledListDelimiterHandler());
    Iterator<String> keys = newConfiguration.getKeys();
    while (keys.hasNext()) {
        String key = keys.next();
        String[] values = newConfiguration.getStringArray(key);
        // See JAMES-1177
        // Need to replace ".." with "."
        // See
        // http://commons.apache.org/configuration/userguide-1.2/howto_xml.html
        // Escaping dot characters in XML tags
        key = key.replaceAll("\\.\\.", "\\.");
        
        // Convert array values to a "," delimited string value
        StringBuilder valueBuilder = new StringBuilder();
        for (int i = 0; i < values.length; i++) {
            valueBuilder.append(values[i]);
            if (i + 1 < values.length) {
                valueBuilder.append(",");
            }
        }
        builder.addProperty(key, valueBuilder.toString());
    }

    configuration = builder;
}
 
Example #19
Source File: XmlFileConfigReader.java    From obevo with Apache License 2.0 4 votes vote down vote up
@Override
public HierarchicalConfiguration getConfig(FileObject checkoutFolder) {
    try {
        FileObject envFileToRead = getEnvFileToRead(checkoutFolder);
        if (envFileToRead.getName().getExtension().equals("xml")) {
            // For the XML lookup, we want all access to fall to attribute queries if we choose. To do that, we override
            // the expression engine.
            DefaultExpressionEngine engine = new DefaultExpressionEngine(DefaultExpressionEngineSymbols.DEFAULT_SYMBOLS) {
                @Override
                public <T> List<QueryResult<T>> query(T root, String key, NodeHandler<T> handler) {
                    List<QueryResult<T>> results = super.query(root, key, handler);
                    if (!results.isEmpty()) {
                        return results;
                    }

                    // If we find no results, fall back to the query that specifies the attribute handler
                    return super.query(root, this.attributeKey(null, key), handler);
                }
            };

            XMLConfiguration configuration = new FileBasedConfigurationBuilder<>(XMLConfiguration.class)
                    .configure(new Parameters().hierarchical()
                            .setURL(envFileToRead.getURLDa())
                            .setListDelimiterHandler(new DisabledListDelimiterHandler())
                            .setExpressionEngine(engine)
                    )
                    .getConfiguration();
            postProcess(configuration);
            return configuration;
        } else {
            return new FileBasedConfigurationBuilder<>(YAMLConfiguration.class)
                    .configure(new Parameters().hierarchical()
                            .setURL(envFileToRead.getURLDa())
                            .setListDelimiterHandler(new DisabledListDelimiterHandler())
                    )
                    .getConfiguration();
        }
    } catch (ConfigurationException e) {
        throw new DeployerRuntimeException(e);
    }
}
 
Example #20
Source File: TestXMLConfiguration_605.java    From commons-configuration with Apache License 2.0 4 votes vote down vote up
@Test
public void testWithOnlyCommaWithoutDelimiterParsing() throws Exception
{
    final String source = "<configuration><key0></key0><key1>,</key1><key2></key2><key3></key3></configuration>";
    checkConfiguration(create(source, DisabledListDelimiterHandler.INSTANCE));
}
 
Example #21
Source File: XMLRecipientRewriteTableTest.java    From james-project with Apache License 2.0 4 votes vote down vote up
@Override
@Before
public void setUp() throws Exception {
    defaultConfiguration.setListDelimiterHandler(new DisabledListDelimiterHandler());
    super.setUp();
}