nu.validator.saxtree
Class TreeBuilder

java.lang.Object
  extended by nu.validator.saxtree.TreeBuilder
All Implemented Interfaces:
org.xml.sax.ContentHandler, org.xml.sax.ext.LexicalHandler

public class TreeBuilder
extends java.lang.Object
implements org.xml.sax.ContentHandler, org.xml.sax.ext.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
TreeBuilder()
          Constructs a reusable TreeBuilder that builds Documents and copies attributes.
TreeBuilder(boolean fragment, boolean retainAttributes)
          The constructor.
 
Method Summary
 void characters(char[] ch, int start, int length)
           
 void comment(char[] ch, int start, int length)
           
 void endCDATA()
           
 void endDocument()
           
 void endDTD()
           
 void endElement(java.lang.String uri, java.lang.String localName, java.lang.String qName)
           
 void endEntity(java.lang.String name)
           
 void endPrefixMapping(java.lang.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(java.lang.String target, java.lang.String data)
           
 void setDocumentLocator(org.xml.sax.Locator locator)
           
 void skippedEntity(java.lang.String name)
           
 void startCDATA()
           
 void startDocument()
           
 void startDTD(java.lang.String name, java.lang.String publicId, java.lang.String systemId)
           
 void startElement(java.lang.String uri, java.lang.String localName, java.lang.String qName, org.xml.sax.Attributes atts)
           
 void startEntity(java.lang.String name)
           
 void startPrefixMapping(java.lang.String prefix, java.lang.String uri)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TreeBuilder

public TreeBuilder()
Constructs a reusable TreeBuilder that builds Documents 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 Detail

characters

public void characters(char[] ch,
                       int start,
                       int length)
                throws org.xml.sax.SAXException
Specified by:
characters in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException
See Also:
ContentHandler.characters(char[], int, int)

endDocument

public void endDocument()
                 throws org.xml.sax.SAXException
Specified by:
endDocument in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException
See Also:
ContentHandler.endDocument()

endElement

public void endElement(java.lang.String uri,
                       java.lang.String localName,
                       java.lang.String qName)
                throws org.xml.sax.SAXException
Specified by:
endElement in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException
See Also:
ContentHandler.endElement(java.lang.String, java.lang.String, java.lang.String)

endPrefixMapping

public void endPrefixMapping(java.lang.String prefix)
                      throws org.xml.sax.SAXException
Specified by:
endPrefixMapping in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException
See Also:
ContentHandler.endPrefixMapping(java.lang.String)

ignorableWhitespace

public void ignorableWhitespace(char[] ch,
                                int start,
                                int length)
                         throws org.xml.sax.SAXException
Specified by:
ignorableWhitespace in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException
See Also:
ContentHandler.ignorableWhitespace(char[], int, int)

processingInstruction

public void processingInstruction(java.lang.String target,
                                  java.lang.String data)
                           throws org.xml.sax.SAXException
Specified by:
processingInstruction in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException
See Also:
ContentHandler.processingInstruction(java.lang.String, java.lang.String)

setDocumentLocator

public void setDocumentLocator(org.xml.sax.Locator locator)
Specified by:
setDocumentLocator in interface org.xml.sax.ContentHandler
See Also:
ContentHandler.setDocumentLocator(org.xml.sax.Locator)

skippedEntity

public void skippedEntity(java.lang.String name)
                   throws org.xml.sax.SAXException
Specified by:
skippedEntity in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException

startDocument

public void startDocument()
                   throws org.xml.sax.SAXException
Specified by:
startDocument in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException
See Also:
ContentHandler.startDocument()

startElement

public void startElement(java.lang.String uri,
                         java.lang.String localName,
                         java.lang.String qName,
                         org.xml.sax.Attributes atts)
                  throws org.xml.sax.SAXException
Specified by:
startElement in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException
See Also:
ContentHandler.startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)

startPrefixMapping

public void startPrefixMapping(java.lang.String prefix,
                               java.lang.String uri)
                        throws org.xml.sax.SAXException
Specified by:
startPrefixMapping in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException
See Also:
ContentHandler.startPrefixMapping(java.lang.String, java.lang.String)

comment

public void comment(char[] ch,
                    int start,
                    int length)
             throws org.xml.sax.SAXException
Specified by:
comment in interface org.xml.sax.ext.LexicalHandler
Throws:
org.xml.sax.SAXException
See Also:
LexicalHandler.comment(char[], int, int)

endCDATA

public void endCDATA()
              throws org.xml.sax.SAXException
Specified by:
endCDATA in interface org.xml.sax.ext.LexicalHandler
Throws:
org.xml.sax.SAXException
See Also:
LexicalHandler.endCDATA()

endDTD

public void endDTD()
            throws org.xml.sax.SAXException
Specified by:
endDTD in interface org.xml.sax.ext.LexicalHandler
Throws:
org.xml.sax.SAXException
See Also:
LexicalHandler.endDTD()

endEntity

public void endEntity(java.lang.String name)
               throws org.xml.sax.SAXException
Specified by:
endEntity in interface org.xml.sax.ext.LexicalHandler
Throws:
org.xml.sax.SAXException
See Also:
LexicalHandler.endEntity(java.lang.String)

startCDATA

public void startCDATA()
                throws org.xml.sax.SAXException
Specified by:
startCDATA in interface org.xml.sax.ext.LexicalHandler
Throws:
org.xml.sax.SAXException
See Also:
LexicalHandler.startCDATA()

startDTD

public void startDTD(java.lang.String name,
                     java.lang.String publicId,
                     java.lang.String systemId)
              throws org.xml.sax.SAXException
Specified by:
startDTD in interface org.xml.sax.ext.LexicalHandler
Throws:
org.xml.sax.SAXException
See Also:
LexicalHandler.startDTD(java.lang.String, java.lang.String, java.lang.String)

startEntity

public void startEntity(java.lang.String name)
                 throws org.xml.sax.SAXException
Specified by:
startEntity in interface org.xml.sax.ext.LexicalHandler
Throws:
org.xml.sax.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