Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

Groovy has native support for various markup languages from XML, HTML, SAX, W3C DOM, Ant tasks, Swing user interfaces and so forth.
This is all accomplished via the following syntax...

Code Block
java
java

def someBuilder = new NodeBuilder()

someBuilder.people(kind:'folks', groovy:true) {
  person(x:123,  name:'James', cheese:'edam') {
    project(name:'groovy')
    project(name:'geronimo')
  }
  person(x:234,  name:'bob', cheese:'cheddar') {
    project(name:'groovy')
    project(name:'drools')
  }
}

...

What's more is this is native Groovy syntax; so you can mix and match this markup syntax with any other Groovy features (iteration, branching, method calls, variables, expressions etc). e.g.

Code Block
java
java

// lets create a form with a label & text field for each property of a bean
def swing = new SwingBuilder()
def widget = swing.frame(title:'My Frame', defaultCloseOperation:javax.swing.WindowConstants.EXIT_ON_CLOSE) {
    panel() {
       for (entry in someBean) {
          label(text:entry.key)
          textField(text:entry.value)
       }
       button(text:'OK', actionPerformed:{ println("I've been clicked with event ${it}") })
    }
}
widget.show()

...

Here's a simple example which shows how you could iterate through some SQL result set and output a dynamic XML document containing the results in a custom format using GroovyMarkup

Code Block
javajava

// lets output some XML builder (could be SAX / DOM / TrAX / text)
def xml = new NodeBuilder()
xml.customers() {
	    loc = 'London'
	    sql.eachRow("select * from customer where location = ${loc}) {

		        // lets process each row by emitting some markup
		        xml.customer(id:it.id, type:'Customer', foo:someVariable)) {
			            role(it.person_role)
			            name(it.customer_name)
			            location(id:it.location_id, name:it.location_name)
		        }
	    }
}

The interesting thing about the above is that the XML technology used at the other end could be push-event based (SAX) or pull-event based (StAX) or a DOM-ish API (W3C, dom4j, JDOM, EXML, XOM) or some JAXB-ish thing (XMLBeans, Castor) or just beans or just good old text files.
e.g. a pull parser could literally pull the data out of the database - or the data could be pushed into data some structure or piped straight to a file using IO or async NIO.

...