Wednesday, December 17, 2003

Groovy GPaths

I finally got a chance to play around with Groovie's GPaths. I encountered some confusion in using them but did surmount the problems with the help of some System.out.println statements (why no println command?). I started my exploration with a another look at tree syntax but this time I used a NodeBuilder instead of a MarkupBuilder. This change let me build a tree of objects instead of output some markup.
 

protected createTree() {
builder = NodeBuilder.newInstance()

root = builder.people() {
person('James') {
location('London')
projects {
project('geronimo')
}
}
person('Bob') {
location('Atlanta')
projects {
project('drools')
project('jessy')
}
}
}
return root;
}

I then tried to build a Gpath that selected a person who participated in the jessy project.

tree = createTree()
s = tree.person.find {
it.projects.project.any {
return it.value() == 'jessy'
};
}

This snippet finds a person who has any project that has a value of 'jessy'. If I call System.out.println(s) I will see a value like:

person[attributes={}; value=[Bob, location[attributes={}; value=Atlanta],
projects[attributes={};value=[project[attributes={}; value=drools],
project[attributes={}; value=jessy]]]]]

I did find it confusing that I was required to call .value() in the comparison. I was assuming that some sort if automatic string transformation would occur when I compared node to string.
Post a Comment
 
The Out Campaign: Scarlet Letter of Atheism