You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Brian Boyle <bb...@gmail.com> on 2008/11/26 21:44:44 UTC

Commons Configuration - Saving a combined configuration

Hi there,

I am using the OverrideCombiner class to combine two XMLConfigurations
together. This works fine and then I set this combined configuration as the
RootNode of a new XMLConfigruation. I am then trying to save this newly
created configuration as a new file and this does not seem to work. Does
anybody know if this is possible or have a missed a step along the way?

Thanks,

Brian
P.S. Here is my code.

           XMLConfiguration masterConf = new XMLConfiguration();
           XMLConfiguration localConf = new XMLConfiguration();
            masterConf.load("resources/Masterconfig.xml");
            localConf.load("resources/localConfig.xml");

            NodeCombiner combiner = new OverrideCombiner();
            ConfigurationNode cn = combiner.combine(localConf.getRootNode(),
masterConf.getRootNode());

            XMLConfiguration result = new XMLConfiguration();
            result.setRootNode(cn);
            result.save("resources/CombinedConfig.xml");

Re: Commons Configuration - Saving a combined configuration

Posted by Brian Boyle <bb...@gmail.com>.
Thanks Oliver, that worked.
Thanks for your help with this.
Brian

On Wed, Nov 26, 2008 at 9:31 PM, Oliver Heger
<ol...@oliver-heger.de>wrote:

> Brian Boyle schrieb:
>
>> Hi Oliver,
>>
>> Thanks for replying so quickly.
>> I tried out your suggestion and it works perfectly.
>> I've had a look at the CombinedConfiguration object and I managed to get
>> it
>> working. I have added my code below.....is this the correct way to do this
>> or is there an even easier way to do it with CombinedConfiguration?
>>
>> Cheers,
>>
>> Brian
>>
>>            CombinedConfiguration cc = new CombinedConfiguration(new
>> OverrideCombiner());
>>            cc.addConfiguration(masterConf);
>>            cc.addConfiguration(localConf);
>>
>>            XMLConfiguration result = new XMLConfiguration();
>>            result.setRootNode(cc.getRootNode());
>>            XMLConfiguration finalResult = new XMLConfiguration(result);
>>            finalResult.setRootElementName("IMConfig");
>>            finalResult.save("resources/CombinedConfig.xml");
>>
>>
> I think you should be able to do
>
>        CombinedConfiguration cc = new CombinedConfiguration(new
>  OverrideCombiner());
>        cc.addConfiguration(masterConf);
>        cc.addConfiguration(localConf);
>        XMLConfiguration result = new XMLConfiguration(cc);
>        result.save(...);
>
> Oliver
>
>
>
>> On Wed, Nov 26, 2008 at 9:04 PM, Oliver Heger
>> <ol...@oliver-heger.de>wrote:
>>
>>  Brian Boyle schrieb:
>>>
>>>  Hi there,
>>>
>>>> I am using the OverrideCombiner class to combine two XMLConfigurations
>>>> together. This works fine and then I set this combined configuration as
>>>> the
>>>> RootNode of a new XMLConfigruation. I am then trying to save this newly
>>>> created configuration as a new file and this does not seem to work. Does
>>>> anybody know if this is possible or have a missed a step along the way?
>>>>
>>>> Thanks,
>>>>
>>>> Brian
>>>> P.S. Here is my code.
>>>>
>>>>          XMLConfiguration masterConf = new XMLConfiguration();
>>>>          XMLConfiguration localConf = new XMLConfiguration();
>>>>           masterConf.load("resources/Masterconfig.xml");
>>>>           localConf.load("resources/localConfig.xml");
>>>>
>>>>           NodeCombiner combiner = new OverrideCombiner();
>>>>           ConfigurationNode cn =
>>>> combiner.combine(localConf.getRootNode(),
>>>> masterConf.getRootNode());
>>>>
>>>>           XMLConfiguration result = new XMLConfiguration();
>>>>           result.setRootNode(cn);
>>>>           result.save("resources/CombinedConfig.xml");
>>>>
>>>>
>>>>  The problem is that the configuration nodes contain references to the
>>> XML
>>> DOM elements they correspond to. These references are also used by
>>> XMLConfiguration to find out, which nodes have been changed and must be
>>> written.
>>>
>>> To solve your problem these references must be cleared. The easiest way
>>> to
>>> do this is using the constructor of XMLConfiguration that takes another
>>> hierarchical configuration as argument. You can try creating another
>>> XMLConfiguration as copy of the existing one:
>>>
>>>       XMLConfiguration result = new XMLConfiguration();
>>>       result.setRootNode(cn);
>>>       XMLConfiguratiion finalResult = new XMLConfiguration(result);
>>>       finalResult.save(...);
>>>
>>> BTW, is there a reason why you do not use CombinedConfiguration? This
>>> class
>>> will do the work with the combiners for you. You can then create the
>>> result
>>> XMLConfiguration from this combined configuration.
>>>
>>> HTH
>>> Oliver
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
>>> For additional commands, e-mail: user-help@commons.apache.org
>>>
>>>
>>>
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
>


-- 
Brian Boyle
Mobile: 087 418 5215
Email: bboyle18@gmail.com

Re: Commons Configuration - Saving a combined configuration

Posted by Oliver Heger <ol...@oliver-heger.de>.
Brian Boyle schrieb:
> Hi Oliver,
> 
> Thanks for replying so quickly.
> I tried out your suggestion and it works perfectly.
> I've had a look at the CombinedConfiguration object and I managed to get it
> working. I have added my code below.....is this the correct way to do this
> or is there an even easier way to do it with CombinedConfiguration?
> 
> Cheers,
> 
> Brian
> 
>             CombinedConfiguration cc = new CombinedConfiguration(new
> OverrideCombiner());
>             cc.addConfiguration(masterConf);
>             cc.addConfiguration(localConf);
> 
>             XMLConfiguration result = new XMLConfiguration();
>             result.setRootNode(cc.getRootNode());
>             XMLConfiguration finalResult = new XMLConfiguration(result);
>             finalResult.setRootElementName("IMConfig");
>             finalResult.save("resources/CombinedConfig.xml");
> 

I think you should be able to do

	CombinedConfiguration cc = new CombinedConfiguration(new
  OverrideCombiner());
         cc.addConfiguration(masterConf);
         cc.addConfiguration(localConf);
	XMLConfiguration result = new XMLConfiguration(cc);
	result.save(...);

Oliver

> 
> On Wed, Nov 26, 2008 at 9:04 PM, Oliver Heger
> <ol...@oliver-heger.de>wrote:
> 
>> Brian Boyle schrieb:
>>
>>  Hi there,
>>> I am using the OverrideCombiner class to combine two XMLConfigurations
>>> together. This works fine and then I set this combined configuration as
>>> the
>>> RootNode of a new XMLConfigruation. I am then trying to save this newly
>>> created configuration as a new file and this does not seem to work. Does
>>> anybody know if this is possible or have a missed a step along the way?
>>>
>>> Thanks,
>>>
>>> Brian
>>> P.S. Here is my code.
>>>
>>>           XMLConfiguration masterConf = new XMLConfiguration();
>>>           XMLConfiguration localConf = new XMLConfiguration();
>>>            masterConf.load("resources/Masterconfig.xml");
>>>            localConf.load("resources/localConfig.xml");
>>>
>>>            NodeCombiner combiner = new OverrideCombiner();
>>>            ConfigurationNode cn =
>>> combiner.combine(localConf.getRootNode(),
>>> masterConf.getRootNode());
>>>
>>>            XMLConfiguration result = new XMLConfiguration();
>>>            result.setRootNode(cn);
>>>            result.save("resources/CombinedConfig.xml");
>>>
>>>
>> The problem is that the configuration nodes contain references to the XML
>> DOM elements they correspond to. These references are also used by
>> XMLConfiguration to find out, which nodes have been changed and must be
>> written.
>>
>> To solve your problem these references must be cleared. The easiest way to
>> do this is using the constructor of XMLConfiguration that takes another
>> hierarchical configuration as argument. You can try creating another
>> XMLConfiguration as copy of the existing one:
>>
>>        XMLConfiguration result = new XMLConfiguration();
>>        result.setRootNode(cn);
>>        XMLConfiguratiion finalResult = new XMLConfiguration(result);
>>        finalResult.save(...);
>>
>> BTW, is there a reason why you do not use CombinedConfiguration? This class
>> will do the work with the combiners for you. You can then create the result
>> XMLConfiguration from this combined configuration.
>>
>> HTH
>> Oliver
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
>> For additional commands, e-mail: user-help@commons.apache.org
>>
>>
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: Commons Configuration - Saving a combined configuration

Posted by Brian Boyle <bb...@gmail.com>.
Hi Oliver,

Thanks for replying so quickly.
I tried out your suggestion and it works perfectly.
I've had a look at the CombinedConfiguration object and I managed to get it
working. I have added my code below.....is this the correct way to do this
or is there an even easier way to do it with CombinedConfiguration?

Cheers,

Brian

            CombinedConfiguration cc = new CombinedConfiguration(new
OverrideCombiner());
            cc.addConfiguration(masterConf);
            cc.addConfiguration(localConf);

            XMLConfiguration result = new XMLConfiguration();
            result.setRootNode(cc.getRootNode());
            XMLConfiguration finalResult = new XMLConfiguration(result);
            finalResult.setRootElementName("IMConfig");
            finalResult.save("resources/CombinedConfig.xml");


On Wed, Nov 26, 2008 at 9:04 PM, Oliver Heger
<ol...@oliver-heger.de>wrote:

> Brian Boyle schrieb:
>
>  Hi there,
>>
>> I am using the OverrideCombiner class to combine two XMLConfigurations
>> together. This works fine and then I set this combined configuration as
>> the
>> RootNode of a new XMLConfigruation. I am then trying to save this newly
>> created configuration as a new file and this does not seem to work. Does
>> anybody know if this is possible or have a missed a step along the way?
>>
>> Thanks,
>>
>> Brian
>> P.S. Here is my code.
>>
>>           XMLConfiguration masterConf = new XMLConfiguration();
>>           XMLConfiguration localConf = new XMLConfiguration();
>>            masterConf.load("resources/Masterconfig.xml");
>>            localConf.load("resources/localConfig.xml");
>>
>>            NodeCombiner combiner = new OverrideCombiner();
>>            ConfigurationNode cn =
>> combiner.combine(localConf.getRootNode(),
>> masterConf.getRootNode());
>>
>>            XMLConfiguration result = new XMLConfiguration();
>>            result.setRootNode(cn);
>>            result.save("resources/CombinedConfig.xml");
>>
>>
> The problem is that the configuration nodes contain references to the XML
> DOM elements they correspond to. These references are also used by
> XMLConfiguration to find out, which nodes have been changed and must be
> written.
>
> To solve your problem these references must be cleared. The easiest way to
> do this is using the constructor of XMLConfiguration that takes another
> hierarchical configuration as argument. You can try creating another
> XMLConfiguration as copy of the existing one:
>
>        XMLConfiguration result = new XMLConfiguration();
>        result.setRootNode(cn);
>        XMLConfiguratiion finalResult = new XMLConfiguration(result);
>        finalResult.save(...);
>
> BTW, is there a reason why you do not use CombinedConfiguration? This class
> will do the work with the combiners for you. You can then create the result
> XMLConfiguration from this combined configuration.
>
> HTH
> Oliver
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
>


-- 
Brian Boyle
Mobile: 087 418 5215
Email: bboyle18@gmail.com

Re: Commons Configuration - Saving a combined configuration

Posted by Oliver Heger <ol...@oliver-heger.de>.
Brian Boyle schrieb:
> Hi there,
> 
> I am using the OverrideCombiner class to combine two XMLConfigurations
> together. This works fine and then I set this combined configuration as the
> RootNode of a new XMLConfigruation. I am then trying to save this newly
> created configuration as a new file and this does not seem to work. Does
> anybody know if this is possible or have a missed a step along the way?
> 
> Thanks,
> 
> Brian
> P.S. Here is my code.
> 
>            XMLConfiguration masterConf = new XMLConfiguration();
>            XMLConfiguration localConf = new XMLConfiguration();
>             masterConf.load("resources/Masterconfig.xml");
>             localConf.load("resources/localConfig.xml");
> 
>             NodeCombiner combiner = new OverrideCombiner();
>             ConfigurationNode cn = combiner.combine(localConf.getRootNode(),
> masterConf.getRootNode());
> 
>             XMLConfiguration result = new XMLConfiguration();
>             result.setRootNode(cn);
>             result.save("resources/CombinedConfig.xml");
> 

The problem is that the configuration nodes contain references to the 
XML DOM elements they correspond to. These references are also used by 
XMLConfiguration to find out, which nodes have been changed and must be 
written.

To solve your problem these references must be cleared. The easiest way 
to do this is using the constructor of XMLConfiguration that takes 
another hierarchical configuration as argument. You can try creating 
another XMLConfiguration as copy of the existing one:

	XMLConfiguration result = new XMLConfiguration();
  	result.setRootNode(cn);
	XMLConfiguratiion finalResult = new XMLConfiguration(result);
	finalResult.save(...);

BTW, is there a reason why you do not use CombinedConfiguration? This 
class will do the work with the combiners for you. You can then create 
the result XMLConfiguration from this combined configuration.

HTH
Oliver

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org