Skip to end of metadata
Go to start of metadata

To create a new builder like a the MarkupBuilder or AntBuilder, you have to implement in java (in groovy later too) a subclass of the groovy.util.BuilderSupport class.
The main methods to be implemented are the following :

  • protected abstract void setParent(Object parent, Object child);
  • protected abstract Object createNode(Object name); // a node without parameter and closure
  • protected abstract Object createNode(Object name, Object value); //a node without parameters, but with closure
  • protected abstract Object createNode(Object name, Map attributes); // a Node without closure but with parameters
  • protected abstract Object createNode(Object name, Map attributes, Object value); //a node with closure and parameters
  • protected Object getName(String methodName)

The BuilderSupport.java class




The NodeBuilder example

To be able to write such a code :



we need :



The MarkupBuilder.java class as second example




  • No labels

1 Comment

  1.     *  protected abstract void setParent(Object parent, Object child);
        * protected abstract Object createNode(Object name); // anode without parameter and closure
        * protected abstract Object createNode(Object name, Object value); //a node without parameters, but with closure
        * protected abstract Object createNode(Object name, Map attributes); // aNode without closure but with parameters
        * protected abstract Object createNode(Object name, Map attributes, Object value); //a node with closure and parameters
        * protected Object getName(String methodName)

    I think these descriptions are wrong. According these , 'Object value' parameter
    seems to be a Closure object , but it's not.  For example ,

    someNode(key:'item' , 'hello world') {
        // closure body
    }

    in this code , 'hello world' is passed as Object value paramter , and you can not
    access the closure body in createNode method.

    I think these descriptions need some fix ...