Class NodeCreateRule.NodeBuilder

java.lang.Object
org.xml.sax.helpers.DefaultHandler
org.apache.commons.digester.NodeCreateRule.NodeBuilder
All Implemented Interfaces:
ContentHandler, DTDHandler, EntityResolver, ErrorHandler
Enclosing class:
NodeCreateRule

private class NodeCreateRule.NodeBuilder extends DefaultHandler
The SAX content handler that does all the actual work of assembling the DOM node tree from the SAX events.
  • Field Details

    • oldContentHandler

      protected ContentHandler oldContentHandler
      The content handler used by Digester before it was set to this content handler.
    • depth

      protected int depth
      Depth of the current node, relative to the element where the content handler was put into action.
    • doc

      protected Document doc
      A DOM Document used to create the various Node instances.
    • root

      protected Node root
      The DOM node that will be pushed on Digester's stack.
    • top

      protected Node top
      The current top DOM mode.
    • topText

      protected StringBuffer topText
      The text content of the current top DOM node.
  • Constructor Details

    • NodeBuilder

      public NodeBuilder(Document doc, Node root) throws ParserConfigurationException, SAXException
      Constructor.

      Stores the content handler currently used by Digester so it can be reset when done, and initializes the DOM objects needed to build the node.

      Parameters:
      doc - the document to use to create nodes
      root - the root node
      Throws:
      ParserConfigurationException - if the DocumentBuilderFactory could not be instantiated
      SAXException - if the XMLReader could not be instantiated by Digester (should not happen)
  • Method Details

    • addTextIfPresent

      private void addTextIfPresent() throws SAXException
      Appends a Text node to the current node if the content reported by the parser is not purely whitespace.
      Throws:
      SAXException
    • characters

      public void characters(char[] ch, int start, int length) throws SAXException
      Handle notification about text embedded within the current node.

      An xml parser calls this when text is found. We need to ensure that this text gets attached to the new Node we are creating - except in the case where the only text in the node is whitespace.

      There is a catch, however. According to the sax specification, a parser does not need to pass all of the text content of a node in one go; it can make multiple calls passing part of the data on each call. In particular, when the body of an element includes xml entity-references, at least some parsers make a separate call to this method to pass just the entity content.

      In this method, we therefore just append the provided text to a "current text" buffer. When the element end is found, or a child element is found then we can check whether we have all-whitespace. See method addTextIfPresent.

      Specified by:
      characters in interface ContentHandler
      Overrides:
      characters in class DefaultHandler
      Parameters:
      ch - the characters from the XML document
      start - the start position in the array
      length - the number of characters to read from the array
      Throws:
      SAXException - if the DOM implementation throws an exception
    • endElement

      public void endElement(String namespaceURI, String localName, String qName) throws SAXException
      Checks whether control needs to be returned to Digester.
      Specified by:
      endElement in interface ContentHandler
      Overrides:
      endElement in class DefaultHandler
      Parameters:
      namespaceURI - the namespace URI
      localName - the local name
      qName - the qualified (prefixed) name
      Throws:
      SAXException - if the DOM implementation throws an exception
    • processingInstruction

      public void processingInstruction(String target, String data) throws SAXException
      Adds a new ProcessingInstruction to the current node.
      Specified by:
      processingInstruction in interface ContentHandler
      Overrides:
      processingInstruction in class DefaultHandler
      Parameters:
      target - the processing instruction target
      data - the processing instruction data, or null if none was supplied
      Throws:
      SAXException - if the DOM implementation throws an exception
    • startElement

      public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException
      Adds a new child Element to the current node.
      Specified by:
      startElement in interface ContentHandler
      Overrides:
      startElement in class DefaultHandler
      Parameters:
      namespaceURI - the namespace URI
      localName - the local name
      qName - the qualified (prefixed) name
      atts - the list of attributes
      Throws:
      SAXException - if the DOM implementation throws an exception