Detering Dirk
2005-11-05 10:00:16 UTC
Hello all,
I actually stumbled over a thing where I
was unfortunately 'suprised' by Groovy:
Trying to read an XML file via XmlParser
and iterate over the Node tree, I intuitively
wrote this code but branches were never executed.
I tried to debug with println statements:
First variant:
(content is the result of new XmlParser.parse('filename'))
println content // => all seems fine, content is shown as
// nested list.
content.children.each { node ->
// => Never reached ...
println "INSIDE NODE"
println node
}
Second variant:
println content
println content.children // => Empty list: []
println content.iterator.toString() // => Emtpy list: []
println content.text // => Empty list: []
println content.value // => NullPointerException !?
.... Well, it took me one recreative night
to realise, that I used Property syntax instead of
method calls ...
Last variant:
println content
println content.value()
println content.text()
println content.children()
println content.iterator().toString()
content.children().each { node ->
...
and all works fine.
The first point here is, that handling of nodes
would be groovier if the methods would be named
getValue(), getChildren(), getIterator(), getText()
and so could be called without the braces (especially
as they take no arguments), just like a Path expression.
Or in another way: If the Node class would contain
Properties instead of methods.
Second it was very misleading that the access
without braces did not result in a 'no such property'
message but in a return value of the correct type
(but without the correct content).
Indeed I expected (without logical reason, I know!) to be
able to call the methods without parameters listed in:
http://groovy.codehaus.org/apidocs/groovy/util/Node.html
without the braces.
My question now: Is it correct as it is (and the problem
is my wrong intuitive expectation)?
Should Node contain Properties instead?
Anything else?
Bye
Det
***********************************************************************
Die Information in dieser email ist vertraulich und ist ausschliesslich
fuer den/die benannten Adressaten bestimmt. Ein Zugriff auf diese
email durch andere Personen als den/die benannten Adressaten ist
nicht gestattet. Sollten Sie nicht der benannte Adressat sein, löschen
Sie bitte diese email.
***********************************************************************
I actually stumbled over a thing where I
was unfortunately 'suprised' by Groovy:
Trying to read an XML file via XmlParser
and iterate over the Node tree, I intuitively
wrote this code but branches were never executed.
I tried to debug with println statements:
First variant:
(content is the result of new XmlParser.parse('filename'))
println content // => all seems fine, content is shown as
// nested list.
content.children.each { node ->
// => Never reached ...
println "INSIDE NODE"
println node
}
Second variant:
println content
println content.children // => Empty list: []
println content.iterator.toString() // => Emtpy list: []
println content.text // => Empty list: []
println content.value // => NullPointerException !?
.... Well, it took me one recreative night
to realise, that I used Property syntax instead of
method calls ...
Last variant:
println content
println content.value()
println content.text()
println content.children()
println content.iterator().toString()
content.children().each { node ->
...
and all works fine.
The first point here is, that handling of nodes
would be groovier if the methods would be named
getValue(), getChildren(), getIterator(), getText()
and so could be called without the braces (especially
as they take no arguments), just like a Path expression.
Or in another way: If the Node class would contain
Properties instead of methods.
Second it was very misleading that the access
without braces did not result in a 'no such property'
message but in a return value of the correct type
(but without the correct content).
Indeed I expected (without logical reason, I know!) to be
able to call the methods without parameters listed in:
http://groovy.codehaus.org/apidocs/groovy/util/Node.html
without the braces.
My question now: Is it correct as it is (and the problem
is my wrong intuitive expectation)?
Should Node contain Properties instead?
Anything else?
Bye
Det
***********************************************************************
Die Information in dieser email ist vertraulich und ist ausschliesslich
fuer den/die benannten Adressaten bestimmt. Ein Zugriff auf diese
email durch andere Personen als den/die benannten Adressaten ist
nicht gestattet. Sollten Sie nicht der benannte Adressat sein, löschen
Sie bitte diese email.
***********************************************************************