17.5 Using BuilderSupport
We saw how to create a custom builder using
methodMissing
and
propertyMissing
.
If we’re creating more than one builder, chances are we’ll refactor some of the method-recognition code into a common base class.
That has been done for us; the class
BuilderSupport
provides convenience methods that recognize the
node structure. Instead of writing the
logic to deal with the structure, we simply listen to calls as Groovy traverses the structure
and takes appropriate action. Extending the abstract class BuilderSupport
feels like working with Simple API for XML (SAX)—a popular event-driven
parser for XML. It triggers events on a handler we provide as it parses and recognizes
elements and attributes in a document.
Get Programming Groovy 2 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.