Groovy for Domain-specific Languages - Second Edition

(nextflipdebug2) #1

Power Groovy DSL Features


[ 154 ]

<table>
<tr>
<td>groovy.util.NodeBuilder</td>
</tr>
<tr>
<td>groovy.swing.SwingBuilder</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>

Although the preceding HTML code looks correct, one of the table tags is
incorrectly terminated as . Displaying the preceding code in a browser would
show an extra layer of nesting in the table tag that was not intended. The same
HTML can be generated using MarkupBuilder, as shown in the following listing:


given:
def writer = new StringWriter()
def html = new groovy.xml.MarkupBuilder(writer)

when:
html.html {
head {
title "Groovy Builders"
}
body {
table(style:"border: 1px solid black;") {
tr {
th "Builder class"
th "Concrete class"
}
tr {
td "groovy.util.BuilderSupport"
td {
table {
tr {
td "groovy.util.AntBuilder"
}
tr {
td "groovy.xml.MarkupBuilder"
}
}

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