Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »



= Tree Based Syntax =

We have native syntax support for List and Maps. We should also support arbitrary nested tree structures. e.g. DOMish APIs or Ant tasks or Jelly tags or Swing widgets or whatever. Each may have their own particular factory mechanism to create the tree of objects - however they can share the same markup syntax to define them - in a concise alternative to XML.

In general we'd like to support a few kinds of markup...

generic tree structures making groovy.lang.Node a first class syntax like Map and List are
custom tree structures beans, DOMs, Swing, Ant, Jelly etc
metadata JDK 1.5 style

== Example ==

Here's an example syntax

f = framesize:[300,300], text:'My Window' {
labelbounds:[10,10,290,30], text:'Save changes'
panelbounds:[10,40,290,290] {
buttontext:'OK', action:

Unknown macro: { save close }

buttontext:'Cancel', action:

Unknown macro: { close }


The above invokes a number of methods on the owner class using named-parameter passing syntax. Then the button method would create JButton etc. The { } is used to define a closure which adds its content to the newly created node. Also notice that the action parameter is passed as a closure - which is ideal for working with UI centric listeners etc.

Note that within the 'markup' you can embed normal expressions - i.e. this markup syntax is a normal part of the Groovy language. e.g.

f = frametext: calculateFieldNamefoo, 1234

// lets iterate through some map
map = [1:"hello", 2:"there"]

for e in map

Unknown macro: { labelname}


Using this simple mechanism we can easily create any structured tree of data - or provide an event based model too. Note in Groovy you can just overload the invokeMethodname, arguments to have a simple polymorphic tree creation - such as for DOMis structures or Ant tasks or Jelly tags etc.

Here's an example of some HTML using some mixed content which is typically hard to do neatly in some markup languages

html {

Unknown macro: { title"XML encoding with Groovy" }


Unknown macro: { h1"XML encoding with Groovy" p"this format can be used as an alternative markup to XML" / an element with attributes and text content / ahref}


Finally here's an example of creating some namespaced XML structure XSD...


xsd = xmlns.namespace''
xsd.schemaxmlns=[xmlns.xsd:''] {

Unknown macro: { xsd.documentationxmlns=[xml.lang}

xsd.elementname:'purchaseOrder', type:'PurchaseOrderType'
xsd.elementname:'comment', type:'xsd:string'
xsd.complexTypename:'PurchaseOrderType' {

Unknown macro: { xsd.elementname}

xsd.attributename:'orderDate', type:'xsd:date'
xsd.complexTypename:'USAddress' {

Unknown macro: { xsd.elementname}

xsd.attributefixed:'US', name:'country', type:'xsd:NMTOKEN'
xsd.complexTypename:'Items' {
xsd.sequence {
xsd.elementmaxOccurs:'unbounded', minOccurs:'0', name:'item' {
xsd.complexType {
xsd.sequence {
xsd.elementname:'productName', type:'xsd:string'
xsd.elementname:'quantity' {
xsd.simpleType {

Unknown macro: { xsd.maxExclusivevalue}

xsd.elementname:'USPrice', type:'xsd:decimal'
xsd.elementminOccurs:'0', ref:'comment'
xsd.elementminOccurs:'0', name:'shipDate', type:'xsd:date'
xsd.attributename:'partNum', type:'SKU', use:'required'

/ Stock Keeping Unit, a code for identifying products /
xsd.simpleTypename:'SKU' {
xsd.restrictionbase:'xsd:string' {

Unknown macro: {3}


Unknown macro: {2}


There's a converter from XML to groovy markup so you can try out this new markup language on any XML documents you have already.

= References =

List references if any

= Comments =

Comments below -
Having a format output for Excel would be "groovy". One could plug in HSSF from the jakarta poi project.

The html example above does not run using groovy 1.0 beta 5. It does not seem to like the mixed content a'link' [["The Text"]] syntax.

To output elements or attributes with a '-' in their name, you need to use '_' in Groovy. For example, to generate a web-app descriptor for a Servlet app:
builder = new groovy.xml.MarkupBuilder
builder.web_app {
display_name"My Web Application"
<display-name>My Web Application</display-name>
Which leaves the question of how to generate elements with '_' in the name.

  • No labels