The digestor framework

The SaxDigesterHandler is the SAX xml handler that allows registration of ElementDigester, which operates on one particular xml element. For example, a typical xml parsing using the digester has the following structure

          //setup the digester
          SaxDigesterHandler handler = new SaxDigesterHandler();
          handler.addDigester(new MyRootElementDigester());
          handler.addDigester("path/to/element", new MyElementDigester());
          // Parse the input
          SAXParser saxParser = factory.newSAXParser();
          saxParser.parse(in, handler);
You can think the element digester as being a listener to the occurrance of particular xml element that it is interested. The signature of the ElementDigester is like the following:

          public interface ElementDigester
              //the element name observed by this digester
              public String getElementName();
              //when the element is encountered
              public void startDigest(SaxDigesterHandler handler, Attributes attr) throws SAXException;
              //when the element ends
              public void endDigest(SaxDigesterHandler handler) throws SAXException;
              //when theres CDATA in the element
              public void characters(SaxDigesterHandler handler, char[] chars, int offset, int length) throws SAXException;
Through this interface, the normal complex xml parsing could be simplified. The developer only concentrate on the parsing logic of an element at a time. Also, the code become more reusable as the developer can configure an element digestion on any path he wants through the SaxDigesterHandler.addDigester(...) methods.

Another feature that the digester framework try to achieve is to enable dynamic parsing. For example, consider the following xml
            <customizable class="userclass">
              .... userclass specific xml segment here...
The xml section inside "customizable" needs to be controlled by the user applied object "userclass". The digester framework enable this with the ElementDigester interface. The userclass need to extend from ElementDigester interface. The, in the CustomizableDigester that consume the "customizable" element can do this

          public class CustomizableDigester
              //... other code section here ...
              //when the element is encountered
              public void startDigest(SaxDigesterHandler handler, Attributes attr) {
                String class = attr.getValue("class");
                Object userObj = ... //user object instantiate the object from class attribute
As we can see, the handler allows the element digester to change the spec of it's parsing. Here, the addSubdister method add the userObj as a digester for the sub-sequence section (hence the add*Sub*Digester).

The framework is used in jxp and swingweb project.