You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Sergio Criales <cy...@gmail.com> on 2011/04/20 16:03:58 UTC

[configuration] Adding Nodes in certain position

Hi There,

I want to add a node before another node, but in the API I wasn' t
able to find how to do it, please any ideas to do this???

Sergio Criales

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


Re: [configuration] Adding Nodes in certain position

Posted by Oliver Heger <ol...@oliver-heger.de>.
Am 05.05.2011 16:49, schrieb Sergio Criales:
> I did that you explain, and I wasn't able to insert thet new node:
>
>        List keyss = config1.getList("presentacion/movil/key");
>        System.out.println("Lista de keys: " + keyss);
>        keyss.add(2, "2.5");
>        config1.setProperty("presentacion/movil/key", keyss);
>
> the message was java.lang.IllegalArgumentException: prepareAdd: Passed
> in key must contain a whitespace!

Ah, I see. My example does not work correctly with the XPath expression 
engine which is used by you. As a workaround you can try to replace the 
setProperty() call by a combination of clearProperty() and 
addProperty(). However, the key passed to addProperty() must be 
different than the key passed to clearProperty() because it has to 
comply to the special syntax used by the XPath expression engine for 
adding new properties. Details about these keys can be found in [1].

[1] 
http://commons.apache.org/configuration/userguide/howto_xml.html#The_XPATH_expression_engine

>
> and the xml is
>
> <?xml version="1.0" encoding="UTF-8"?>
> <repositorio ss="">
>    <presentacion>
>      <movil>
>        <key>uno</key>
>        <key>dos</key>
>        <key>tres</key>
>        <key>cuatro</key>
>      </movil>
>    </presentacion>
> </repositorio>
>
> Well, then I found this solution please check if it is right:
>
> I used the document directly to insert the new node doing this:
>
>      Document document = config.getDocument();
>
>      Element nuevoKey = document.createElement("key");
>      nuevoKey.setAttribute("name", leaf.getName());
>      nuevoKey.setTextContent("temporal");
>
> and then add this element whith
>
>          element.insertBefore(nuevoKey, key);
>
> key is the node where I want to insert the new node before
>
> then to leave the config consistent I did this
>
>        String xmlSource = XmlUtils.getStringFromDom(document);
>        config.clear();
>        config.load(new StringReader(xmlSource));

Yes, this solution looks good - despite the fact that Commons 
Configuration does not support you very well for your use case.

Oliver

>
> thanks in advance,
>
> Sergio Criales
>
> 2011/4/21 Oliver Heger<ol...@oliver-heger.de>:
>> Am 20.04.2011 22:14, schrieb Paul Benedict:
>>>
>>> If the ordering matters, I would introduce a new OrderedConfiguration
>>> interface for greater positioning control.
>>
>> Yes, this is a completely new feature. However, I fear that its
>> implementation won't be straightforward.
>>
>> Currently, the hierarchical configuration implementation uses the
>> ConfigurationNode interface to update its internal node structure. The
>> cleanest solution is probably to extend this interface by an insert()
>> method. But this would break binary compatibility and thus require a major
>> release.
>>
>> @Sergio: If you only have simple structures like a list with nodes and
>> values (and no child nodes or even complex sub trees), the following
>> approach should work:
>> - Call getList() with the correct key to obtain the current values of the
>> nodes affected.
>> - Insert the new node value at the desired position in the list.
>> - Call setProperty() with the same key and the modified list. This will
>> update the nodes structure correspondingly.
>>
>> But as I said, this only works for simple scenarios.
>>
>> Oliver
>>
>>>
>>> On Wed, Apr 20, 2011 at 3:02 PM, Oliver Heger
>>> <ol...@oliver-heger.de>wrote:
>>>
>>>> Hi Sergio,
>>>>
>>>> Am 20.04.2011 16:03, schrieb Sergio Criales:
>>>>
>>>>   Hi There,
>>>>>
>>>>> I want to add a node before another node, but in the API I wasn' t
>>>>> able to find how to do it, please any ideas to do this???
>>>>>
>>>>> Sergio Criales
>>>>>
>>>>>   this is indeed a good question! With the standard API defined by the
>>>>
>>>> Configuration interface there does not seem to be an obvious solution for
>>>> this problem. All methods for adding properties append the new node(s) to
>>>> existing ones.
>>>>
>>>> Also the ConfigurationNode API (which represents the nodes stored in a
>>>> hierarchical configuration) does not support inserting a child node at a
>>>> specified position.
>>>>
>>>> Obviously, nobody had this requirement before. Does anybody else have an
>>>> idea?
>>>>
>>>> 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
>>
>>
>
> ---------------------------------------------------------------------
> 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: [configuration] Adding Nodes in certain position

Posted by Sergio Criales <cy...@gmail.com>.
I did that you explain, and I wasn't able to insert thet new node:

      List keyss = config1.getList("presentacion/movil/key");
      System.out.println("Lista de keys: " + keyss);
      keyss.add(2, "2.5");
      config1.setProperty("presentacion/movil/key", keyss);

the message was java.lang.IllegalArgumentException: prepareAdd: Passed
in key must contain a whitespace!

and the xml is

<?xml version="1.0" encoding="UTF-8"?>
<repositorio ss="">
  <presentacion>
    <movil>
      <key>uno</key>
      <key>dos</key>
      <key>tres</key>
      <key>cuatro</key>
    </movil>
  </presentacion>
</repositorio>

Well, then I found this solution please check if it is right:

I used the document directly to insert the new node doing this:

    Document document = config.getDocument();

    Element nuevoKey = document.createElement("key");
    nuevoKey.setAttribute("name", leaf.getName());
    nuevoKey.setTextContent("temporal");

and then add this element whith

        element.insertBefore(nuevoKey, key);

key is the node where I want to insert the new node before

then to leave the config consistent I did this

      String xmlSource = XmlUtils.getStringFromDom(document);
      config.clear();
      config.load(new StringReader(xmlSource));

thanks in advance,

Sergio Criales

2011/4/21 Oliver Heger <ol...@oliver-heger.de>:
> Am 20.04.2011 22:14, schrieb Paul Benedict:
>>
>> If the ordering matters, I would introduce a new OrderedConfiguration
>> interface for greater positioning control.
>
> Yes, this is a completely new feature. However, I fear that its
> implementation won't be straightforward.
>
> Currently, the hierarchical configuration implementation uses the
> ConfigurationNode interface to update its internal node structure. The
> cleanest solution is probably to extend this interface by an insert()
> method. But this would break binary compatibility and thus require a major
> release.
>
> @Sergio: If you only have simple structures like a list with nodes and
> values (and no child nodes or even complex sub trees), the following
> approach should work:
> - Call getList() with the correct key to obtain the current values of the
> nodes affected.
> - Insert the new node value at the desired position in the list.
> - Call setProperty() with the same key and the modified list. This will
> update the nodes structure correspondingly.
>
> But as I said, this only works for simple scenarios.
>
> Oliver
>
>>
>> On Wed, Apr 20, 2011 at 3:02 PM, Oliver Heger
>> <ol...@oliver-heger.de>wrote:
>>
>>> Hi Sergio,
>>>
>>> Am 20.04.2011 16:03, schrieb Sergio Criales:
>>>
>>>  Hi There,
>>>>
>>>> I want to add a node before another node, but in the API I wasn' t
>>>> able to find how to do it, please any ideas to do this???
>>>>
>>>> Sergio Criales
>>>>
>>>>  this is indeed a good question! With the standard API defined by the
>>>
>>> Configuration interface there does not seem to be an obvious solution for
>>> this problem. All methods for adding properties append the new node(s) to
>>> existing ones.
>>>
>>> Also the ConfigurationNode API (which represents the nodes stored in a
>>> hierarchical configuration) does not support inserting a child node at a
>>> specified position.
>>>
>>> Obviously, nobody had this requirement before. Does anybody else have an
>>> idea?
>>>
>>> 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
>
>

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


Re: [configuration] Adding Nodes in certain position

Posted by Oliver Heger <ol...@oliver-heger.de>.
Am 20.04.2011 22:14, schrieb Paul Benedict:
> If the ordering matters, I would introduce a new OrderedConfiguration
> interface for greater positioning control.

Yes, this is a completely new feature. However, I fear that its 
implementation won't be straightforward.

Currently, the hierarchical configuration implementation uses the 
ConfigurationNode interface to update its internal node structure. The 
cleanest solution is probably to extend this interface by an insert() 
method. But this would break binary compatibility and thus require a 
major release.

@Sergio: If you only have simple structures like a list with nodes and 
values (and no child nodes or even complex sub trees), the following 
approach should work:
- Call getList() with the correct key to obtain the current values of 
the nodes affected.
- Insert the new node value at the desired position in the list.
- Call setProperty() with the same key and the modified list. This will 
update the nodes structure correspondingly.

But as I said, this only works for simple scenarios.

Oliver

>
> On Wed, Apr 20, 2011 at 3:02 PM, Oliver Heger
> <ol...@oliver-heger.de>wrote:
>
>> Hi Sergio,
>>
>> Am 20.04.2011 16:03, schrieb Sergio Criales:
>>
>>   Hi There,
>>>
>>> I want to add a node before another node, but in the API I wasn' t
>>> able to find how to do it, please any ideas to do this???
>>>
>>> Sergio Criales
>>>
>>>   this is indeed a good question! With the standard API defined by the
>> Configuration interface there does not seem to be an obvious solution for
>> this problem. All methods for adding properties append the new node(s) to
>> existing ones.
>>
>> Also the ConfigurationNode API (which represents the nodes stored in a
>> hierarchical configuration) does not support inserting a child node at a
>> specified position.
>>
>> Obviously, nobody had this requirement before. Does anybody else have an
>> idea?
>>
>> 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: [configuration] Adding Nodes in certain position

Posted by Paul Benedict <pb...@apache.org>.
If the ordering matters, I would introduce a new OrderedConfiguration
interface for greater positioning control.

On Wed, Apr 20, 2011 at 3:02 PM, Oliver Heger
<ol...@oliver-heger.de>wrote:

> Hi Sergio,
>
> Am 20.04.2011 16:03, schrieb Sergio Criales:
>
>  Hi There,
>>
>> I want to add a node before another node, but in the API I wasn' t
>> able to find how to do it, please any ideas to do this???
>>
>> Sergio Criales
>>
>>  this is indeed a good question! With the standard API defined by the
> Configuration interface there does not seem to be an obvious solution for
> this problem. All methods for adding properties append the new node(s) to
> existing ones.
>
> Also the ConfigurationNode API (which represents the nodes stored in a
> hierarchical configuration) does not support inserting a child node at a
> specified position.
>
> Obviously, nobody had this requirement before. Does anybody else have an
> idea?
>
> Oliver
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
>

Re: [configuration] Adding Nodes in certain position

Posted by Oliver Heger <ol...@oliver-heger.de>.
Hi Sergio,

Am 20.04.2011 16:03, schrieb Sergio Criales:
> Hi There,
>
> I want to add a node before another node, but in the API I wasn' t
> able to find how to do it, please any ideas to do this???
>
> Sergio Criales
>
this is indeed a good question! With the standard API defined by the 
Configuration interface there does not seem to be an obvious solution 
for this problem. All methods for adding properties append the new 
node(s) to existing ones.

Also the ConfigurationNode API (which represents the nodes stored in a 
hierarchical configuration) does not support inserting a child node at a 
specified position.

Obviously, nobody had this requirement before. Does anybody else have an 
idea?

Oliver

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