Thank you for updating the subject, Jörg.

I changed the order in which I add defaults and overrides. The following is
the new code:

                    // merge overrides with defaults
                    CompositeConfiguration compositeConfig = new
CompositeConfiguration(overridingConfig);
                    compositeConfig.addConfiguration(defaultConfig);

                    // convert merged properties to string
                    StringWriter writer = new StringWriter();
                    overridingConfig.save(writer);

defaultConfig contains the following properties:
# A property
A = 1
# B property
B = 2

overridingConfig contains the following properties:
B = 3

I expected the merged content written to the writer to contain these
properties:
# A property
A = 1
# B property
B = 3

Instead, the merged content written to the writer is the same as the
overridingConfig:
B = 3

Could anybody tell me what's wrong with my code?

Thanks.

Alec

On Wed, Jun 17, 2009 at 1:45 AM, Jörg Schaible <joerg.schai...@gmx.de>wrote:

> Hi Alec,
>
> as commons contains quite a lot of components, it is best practice to
> include the component's name iln the subject to give a reader the context
> of your question.
>
> Alec Swan wrote at Mittwoch, 17. Juni 2009 00:00:
>
> > I have a property file containing default configurations in a key=value
> > format. I also have another file with the same format which contains
> > overrides for some of the properties. I need to apply the overrides to
> the
> > default configuration while preserving the format and comments. I also
> > need to write the merged content to a string.
> >
> > I am using the following code, but the mergedContent contains the default
> > content and no overrides. I tried reversing the order in which I add
> > defaultConfig and overridingConfig to CompositeConfiguration, but still
> no
> > luck.
> >
> >             PropertiesConfiguration mergedConfig = new
> > PropertiesConfiguration();
> >             CompositeConfiguration compositeConfig = new
> > CompositeConfiguration(defaultConfig);
> >             mergedConfig = defaultConfig;
> >
> >             compositeConfig.addConfiguration(overridingConfig);
> >
> >             StringWriter writer = new StringWriter();
> >             mergedConfig.save(writer);
> >             String mergedContent = writer.toString();
> >
> > What's wrong with the code above?
>
> You add the configurations in the wrong sequence. A configuration that
> contains default values should always added last, since the
> CompositeConfiguration will return the first found value for the requested
> key.
>
> - Jörg
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org
> For additional commands, e-mail: dev-h...@commons.apache.org
>
>

Reply via email to