Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
import groovy.xml.MarkupBuilder

// standard book
def standardBook1(builder) { builder.book(title:'Groovy in Action', author:'Dierk König et al') }
// other standard books
def standardBook2(builder, audience) { builder.book(title:"Groovy for ${audience}") }
def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
xml.truck(id:'ABC123') {
    box(country:'Australia') {
        box(country:'Australia', state:'QLD') {
            standardBook1(thisxml)
            standardBook1(thisxml)
            standardBook2(thisxml, 'VBA Macro writers')
        }
        box(country:'Australia', state:'NSW') {
            box(country:'Australia', state:'NSW', city:'Sydney') {
                standardBook1(thisxml)
                standardBook2(thisxml, 'COBOL Programmers')
            }
            box(country:'Australia', state:'NSW', suburb:'Albury') {
                standardBook1(thisxml)
                standardBook2(thisxml, 'Fortran Programmers')
            }
        }
    }
    box(country:'USA') {
        box(country:'USA', state:'CA') {
            standardBook1(thisxml)
            standardBook2(thisxml, 'Ruby Programmers')
        }
    }
    box(country:'Germany') {
        box(country:'Germany', city:'Berlin') {
            standardBook1(thisxml)
            standardBook2(thisxml, 'PHP Programmers')
        }
    }
    box(country:'UK') {
        box(country:'UK', city:'London') {
            standardBook1(thisxml)
            standardBook2(thisxml, 'Haskel Programmers')
        }
    }
}

println writer.toString()

...

Code Block
import groovy.xml.MarkupBuilder

// define standard book and version allowing multiple copies
def standardBook1(builder) { builder.book(title:'Groovy in Action', author:'Dierk König et al') }
def standardBook1(builder, copies) { (0..<copies).each{ standardBook1(builder) } }
// another standard book
def standardBook2(builder, audience) { builder.book(title:"Groovy for ${audience}") }
// define standard box
def standardBox1(builder, args) {
    def other = args.findAll{it.key != 'audience'}
    builder.box(other) { standardBook1(builder); standardBook2(builder, args['audience']) }
}
// define standard country box
def standardBox2(builder, args) {
    builder.box(country:args['country']) {
        if (args.containsKey('language')) {
            args.put('audience', args['language'] + ' programmers')
            args.remove('language')
        }
        standardBox1(builder, args)
}   }


def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
xml.truck(id:'ABC123') {
    box(country:'Australia') {
        box(country:'Australia', state:'QLD') {
            standardBook1(thisxml, 2)
            standardBook2(thisxml, 'VBA Macro writers')
        }
        box(country:'Australia', state:'NSW') {
            [Sydney:'COBOL', Albury:'Fortran'].each{ city, language ->
                standardBox1(thisxml, [country:'Australia', state:'NSW',
                           city:"${city}", audience:"${language} Programmers"])
    }   }   }
    standardBox2(thisxml, [country:'USA', state:'CA', language:'Ruby'])
    standardBox2(thisxml, [country:'Germany', city:'Berlin', language:'PHP'])
    standardBox2(thisxml, [country:'UK', city:'London', language:'Haskel'])
}

println writer.toString()

...