You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Vladimir Stevanovic <vs...@emisia.net> on 2011/06/09 11:27:48 UTC

[configuration] inconsistent behaving of AbstractHierarchicalFileConfiguration.containsKey(String) method

I'm using the following code:

//part1
XMLConfiguration config= new XMLConfiguration();
config.setDelimiterParsingDisabled(true);
config.setExpressionEngine(new XPathExpressionEngine());
config.setFile(f);
if (f.exists()) {
    config.load(f);
}

//part2
config.addProperty(" a", "");
config.addProperty("a b", "");
config.addProperty("a/b c", "");
config.save();
	
//part3
System.out.println("a is a valid key " + config.containsKey("a"));
System.out.println("a/b is a valid key " + config.containsKey("a/b"));
System.out.println("a/b/c is a valid key " + config.containsKey("a/b/c"));

After executing it for the first time, the code behaves as I expected,
the simple nested xml structure is created and the output is:
a is a valid key true
a/b is a valid key true
a/b/c is a valid key true

Then, I removed the "part2" code, so the program should just read the
previously created configuration, bat the output was:
a is a valid key false
a/b is a valid key false
a/b/c is a valid key true

So, sometimes if key doesn't have any values it is seen as a key, and
sometimes not. Any explanation, solution,... ?

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


Re: [configuration] inconsistent behaving of AbstractHierarchicalFileConfiguration.containsKey(String) method

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

Am 09.06.2011 11:27, schrieb Vladimir Stevanovic:
> I'm using the following code:
>
> //part1
> XMLConfiguration config= new XMLConfiguration();
> config.setDelimiterParsingDisabled(true);
> config.setExpressionEngine(new XPathExpressionEngine());
> config.setFile(f);
> if (f.exists()) {
>      config.load(f);
> }
>
> //part2
> config.addProperty(" a", "");
> config.addProperty("a b", "");
> config.addProperty("a/b c", "");
> config.save();
> 	
> //part3
> System.out.println("a is a valid key " + config.containsKey("a"));
> System.out.println("a/b is a valid key " + config.containsKey("a/b"));
> System.out.println("a/b/c is a valid key " + config.containsKey("a/b/c"));
>
> After executing it for the first time, the code behaves as I expected,
> the simple nested xml structure is created and the output is:
> a is a valid key true
> a/b is a valid key true
> a/b/c is a valid key true
>
> Then, I removed the "part2" code, so the program should just read the
> previously created configuration, bat the output was:
> a is a valid key false
> a/b is a valid key false
> a/b/c is a valid key true
>
> So, sometimes if key doesn't have any values it is seen as a key, and
> sometimes not. Any explanation, solution,... ?
>
this is indeed inconsistent, but maybe I can at least explain what is 
happening:

containsKey() is intended to return true if and only if the passed in 
key has been assigned a value. This is not the same as if a 
corresponding node exists in the hierarchical configuration; if there is 
a node, but it does not have a value, containsKey() still returns false 
(even if there a child nodes).

When you added new properties you have specified an empty string as 
value, so there is a value different from null, and containsKey() 
returns true. On loading the configuration from disk, this empty string 
value is obviously ignored for the nodes which have child nodes. 
Therefore containsKey() now returns false. For the leaf node in contrast 
it is kept correctly.

So the actual problem is that XMLConfiguration cannot distinguish 
between non-leaf nodes with an empty value or no value at all. This is 
indeed ambiguous. Currently I have no idea how to solve this.

Oliver

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