package groovy.util class MapMindBuilder extends BuilderSupport { def racine Set declarations protected void setParent(Object parent, Object child) { } protected Object createNode(Object name) { createNode(name, null, null) } protected Object createNode(Object name, Object value) { createNode(name, null, value) } protected Object createNode(Object name, Map attributes) { createNode(name, attributes, null) } protected Object createNode(Object name, Map attributes, Object value) { switch (name) { case 'mapmind': declarations = ['text'] racine = new Node(null, 'tree') return racine case 'node': def node = new Node(null, 'branch') new Node(node, 'attribute', [name: 'text', value: value]) attributes.each { declarations << it.key new Node(node, 'attribute', [name: it.key, value: it.value]) if (it.key == 'url') { declarations << 'image' new Node(node, 'attribute', [name: 'image', value: 'globe.gif']) } } return node } } protected void nodeCompleted(Object parent, Object node) { if (!parent) return parent.children().add(node) } protected Object postNodeCompletion(Object parent, Object node) { if (node.get('branch').size() == 0 && node.get('leaf').size() == 0 ) node.name = 'leaf' if (parent == null) assert node.children().size() == 1, "Il ne peut y avoir qu'un seul noeud à la racine !" } def build = { def decls = new Node(null, 'declarations') declarations.each { new Node(decls, 'attributeDecl', [name: it, type: 'String']) } racine.children().add(0, decls) def printer = new XmlNodePrinter() printer.print(racine) } } def builder = new MapMindBuilder() builder.mapmind() { node 'MapMindBuilder', { node 'Builders Groovy', { node 'Groovy', url: 'http://groovy.codehaus.org/', { node 'Documentation', url: 'http://groovy.codehaus.org/Documentation' node 'JavaDoc', url: 'http://groovy.codehaus.org/api/index.html' node 'GDK', url: 'http://groovy.codehaus.org/groovy-jdk/' } } node 'prefuse', url: 'http://prefuse.org/' node 'odelia technologies', { node 'Site web', url: 'http://www.odelia-technologies.com' node 'bientôt :', url: 'http://www.grailsworks.com' } } } builder.build()