# Class TreeBuilder

java.lang.Object
nu.validator.saxtree.TreeBuilder

All Implemented Interfaces:
  `ContentHandler, LexicalHandler`

public class TreeBuilder extends Object implements ContentHandler, LexicalHandler
Builds a SAX Tree representation of a document or a fragment streamed as `ContentHandler` and `LexicalHandler` events. The start/end event matching is expected to adhere to the SAX API contract. Things will simply break if this is not the case. Fragments are expected to omit `startDocument()` and `endDocument()` calls.

Version:
  $Id$
Author:
  hsivonen

## Constructor Summary

Constructors
Constructor
Description
`TreeBuilder()`
Constructs a reusable `TreeBuilder` that builds `Document`s and copies attributes.
`TreeBuilder(boolean fragment, boolean retainAttributes)`
The constructor.

## Method Summary

Modifier and Type
Method
Description
`void`
`characters(char[] ch, int start, int length)`
`void`
`comment(char[] ch, int start, int length)`
`void`
`endCDATA()`
`void`
`endDocument()`
`void`
`endDTD()`
`void`
`endElement(String uri, String localName, String qName)`
`void`
`endEntity(String name)`
`void`
`endPrefixMapping(String prefix)`
`ParentNode`
`getRoot()`
Returns the root (`Document` if building a full document or `DocumentFragment` if building a fragment.).
`void`
`ignorableWhitespace(char[] ch, int start, int length)`
`void`
`processingInstruction(String target, String data)`
`void`
`setDocumentLocator(Locator locator)`
`void`
`skippedEntity(String name)`
`void`
`startCDATA()`
`void`
`startDocument()`
`void`
`startDTD(String name, String publicId, String systemId)`
`void`
`startElement(String uri, String localName, String qName, Attributes atts)`
`void`
`startEntity(String name)`
`void`
`startPrefixMapping(String prefix, String uri)`

### Methods inherited from class Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Methods inherited from interface ContentHandler

`declaration`

## Constructor Details

### TreeBuilder

public TreeBuilder()
Constructs a reusable `TreeBuilder` that builds `Document`s and copies attributes.

### TreeBuilder

public TreeBuilder(boolean fragment, boolean retainAttributes)
The constructor. The instance will be reusabe if building a full document and not reusable if building a fragment.

Parameters:
  `fragment` - whether this `TreeBuilder` should build a `DocumentFragment` instead of a `Document`.
  `retainAttributes` - whether instances of the `Attributes` interface passed to `startElement` should be retained (the alternative is copying).

## Method Details

### characters

public void characters(char[] ch, int start, int length) throws SAXException

Specified by:
  `characters` in interface `ContentHandler`
Throws:
  `SAXException`
See Also:
  - `ContentHandler.characters(char[], int, int)`

### endDocument

public void endDocument() throws SAXException

Specified by:
  `endDocument` in interface `ContentHandler`
Throws:
  `SAXException`
See Also:
  - `ContentHandler.endDocument()`

### endElement

public void endElement(String uri, String localName, String qName) throws SAXException

Specified by:
  `endElement` in interface `ContentHandler`
Throws:
  `SAXException`
See Also:
  - `ContentHandler.endElement(java.lang.String, java.lang.String, java.lang.String)`

### endPrefixMapping

public void endPrefixMapping(String prefix) throws SAXException

Specified by:
  `endPrefixMapping` in interface `ContentHandler`
Throws:
  `SAXException`
See Also:
  - `ContentHandler.endPrefixMapping(java.lang.String)`

### ignorableWhitespace

public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException

Specified by:
  `ignorableWhitespace` in interface `ContentHandler`
Throws:
  `SAXException`
See Also:
  - `ContentHandler.ignorableWhitespace(char[], int, int)`

### processingInstruction

public void processingInstruction(String target, String data) throws SAXException

Specified by:
  `processingInstruction` in interface `ContentHandler`
Throws:
  `SAXException`
See Also:
  - `ContentHandler.processingInstruction(java.lang.String, java.lang.String)`

### setDocumentLocator

public void setDocumentLocator(Locator locator)

Specified by:
  `setDocumentLocator` in interface `ContentHandler`
See Also:
  - `ContentHandler.setDocumentLocator(org.xml.sax.Locator)`

### skippedEntity

public void skippedEntity(String name) throws SAXException

Specified by:
  `skippedEntity` in interface `ContentHandler`
Throws:
  `SAXException`

### startDocument

public void startDocument() throws SAXException

Specified by:
  `startDocument` in interface `ContentHandler`
Throws:
  `SAXException`
See Also:
  - `ContentHandler.startDocument()`

### startElement

public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException

Specified by:
  `startElement` in interface `ContentHandler`
Throws:
  `SAXException`
See Also:
  - `ContentHandler.startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)`

### startPrefixMapping

public void startPrefixMapping(String prefix, String uri) throws SAXException

Specified by:
  `startPrefixMapping` in interface `ContentHandler`
Throws:
  `SAXException`
See Also:
  - `ContentHandler.startPrefixMapping(java.lang.String, java.lang.String)`

### comment

public void comment(char[] ch, int start, int length) throws SAXException

Specified by:
  `comment` in interface `LexicalHandler`
Throws:
  `SAXException`
See Also:
  - `LexicalHandler.comment(char[], int, int)`

### endCDATA

public void endCDATA() throws SAXException

Specified by:
  `endCDATA` in interface `LexicalHandler`
Throws:
  `SAXException`
See Also:
  - `LexicalHandler.endCDATA()`

### endDTD

public void endDTD() throws SAXException

Specified by:
  `endDTD` in interface `LexicalHandler`
Throws:
  `SAXException`
See Also:
  - `LexicalHandler.endDTD()`

### endEntity

public void endEntity(String name) throws SAXException

Specified by:
  `endEntity` in interface `LexicalHandler`
Throws:
  `SAXException`
See Also:
  - `LexicalHandler.endEntity(java.lang.String)`

### startCDATA

public void startCDATA() throws SAXException

Specified by:
  `startCDATA` in interface `LexicalHandler`
Throws:
  `SAXException`
See Also:
  - `LexicalHandler.startCDATA()`

### startDTD

public void startDTD(String name, String publicId, String systemId) throws SAXException

Specified by:
  `startDTD` in interface `LexicalHandler`
Throws:
  `SAXException`
See Also:
  - `LexicalHandler.startDTD(java.lang.String, java.lang.String, java.lang.String)`

### startEntity

public void startEntity(String name) throws SAXException

Specified by:
  `startEntity` in interface `LexicalHandler`
Throws:
  `SAXException`
See Also:
  - `LexicalHandler.startEntity(java.lang.String)`

### getRoot

public ParentNode getRoot()
Returns the root (`Document` if building a full document or `DocumentFragment` if building a fragment.).

Returns:
  the root