Groovy for Domain-specific Languages - Second Edition

(nextflipdebug2) #1

Power Groovy DSL Features


[ 156 ]

street:"2 Rock Road",city:"Bedrock")
def customerList = [ fred, barney]

when:
builder.customers {
for (cust in customerList) {
customer(id:cust.id) {
name(firstName:cust.firstName,surname:cust.surname)
address(street:cust.street, city:cust.city)
}
}
}

then:
xmlIsIdentical writer.toString(), "customers.xml"

Builders for every occasion


Out of the box, the Groovy libraries include a suite of builders for most of the
common construction tasks that we might encounter. Here is a list of some of them:



  • MarkupBuilder: This we have already seen. It can be used to generate any
    XML-style tagged output. Class: groovy.xml.MarkupBuilder.

  • NodeBuilder: This is a useful builder for building tree-like structures of node
    instances in memory. Class: groovy.util.NodeBuilder.

  • DOMBuilder: This builder will construct a WC3 DOM tree in memory from
    the GroovyMarkup that we provide. Class: groovy.xml.DOMBuilder.

  • SAXBuilder: This is very similar to the DOMBuilder insofar as the end result
    is a WC3 DOM in memory. The difference is that it works with an existing
    SAX ContentHandler class and fires SAX events to it as the GroovyMarkup is
    executed. Class: groovy.xml.SAXBuilder.

  • JMXBuilder: Also in the same vein as AntBuilder is the JMXBuilder class,
    which can be used to deploy JMX management beans by using simple
    markup-style syntax. JMXBuilder is a Groovy-based DSL for declaratively
    exposing services, POJOs, POGOs, and so on, via the Java Management
    Extensions (JMX). Class: groovy.jmx.builder.JMXBuilder.

  • SwingBuilder: Next we'll cover SwingBuilder in detail with
    an example. This builder constructs Swing-based UIs. Class:
    groovy.swing.SwingBuilder.


http://www.ebook3000.com
Free download pdf