Java Reference
In-Depth Information
There's one final piece of the puzzle needed, which is the driver used to call the system for
each date. I use a “groovlet” for this purpose in the next section.
2.3.3. HTML builders and groovlets
The classes used so far access XML box score information and convert it into a series of
game result objects. For the view layer, however, I need objects in a form that can be pro-
cessed by JavaScript. There are several ways to accomplish this, but one of them is to use
an XML builder to write out the information in XML form. [ 11 ]
11 The data could just as easily be written in JSON format. Other JSON examples are used throughout the topic.
Generating XML
The standard Groovy library includes a class called
groovy.xml.MarkupBuilder , [ 12 ] which is one of several builders (much like the
SwingBuilder shown at the beginning of this chapter) in the standard library. Each of
the builders intercepts method calls that don't exist (so-called pretended methods) and con-
structs nodes out of them to make a tree structure. The tree is then exported appropriately
for that kind of builder.
12 I would bet that if this class were created today, it would be called XmlBuilder instead.
This is actually easier to see than to explain. Consider the GameResult class from the
previous section, which held the home and away team names and scores and a reference to
a Stadium object. Here's the syntax for creating XML out of that object:
MarkupBuilder builder = new MarkupBuilder()
builder.games {
results.each { g ->
game(
outcome:"$g.away $g.aScore, $g.home $g.hScore",
lat:g.stadium.latitude,
lng:g.stadium.longitude
)
}
}
After instantiating the MarkupBuilder and calling the reference builder , the second
line invokes the games method on it. It may not look like a method, but recall that in
 
 
Search WWH ::




Custom Search