nu.validator.htmlparser.impl
Class TreeBuilder<T>

java.lang.Object
  extended by nu.validator.htmlparser.impl.TreeBuilder<T>
All Implemented Interfaces:
TokenHandler
Direct Known Subclasses:
DOMTreeBuilder, SAXStreamer, SAXTreeBuilder, XOMTreeBuilder

public abstract class TreeBuilder<T>
extends Object
implements TokenHandler


Nested Class Summary
private static class TreeBuilder.Phase
           
private  class TreeBuilder.StackNode<S>
           
 
Field Summary
private  int cdataOrRcdataTimesToPop
           
private  char[] charBuffer
           
private  int charBufferLen
           
private  boolean coalescingText
           
private  boolean conformingAndStreaming
           
private  String context
           
private  int currentPtr
           
private  DoctypeExpectation doctypeExpectation
           
private  DocumentModeHandler documentModeHandler
           
private  ErrorHandler errorHandler
           
private  T formPointer
           
private  T headPointer
           
private static String[] HTML4_PUBLIC_IDS
           
private static char[] ISINDEX_PROMPT
           
private  TreeBuilder.StackNode<T>[] listOfActiveFormattingElements
           
private  int listPtr
           
private  TreeBuilder.StackNode<T> MARKER
           
private  boolean needToDropLF
           
private  boolean nonConformingAndStreaming
           
private static int NOT_FOUND_ON_STACK
           
private  TreeBuilder.Phase phase
           
private  TreeBuilder.Phase previousPhaseBeforeTrailingEnd
           
private static String[] QUIRKY_PUBLIC_IDS
           
private  boolean reportingDoctype
           
private  boolean scriptingEnabled
           
private  TreeBuilder.StackNode<T>[] stack
           
protected  Tokenizer tokenizer
           
private  boolean wantingComments
           
 
Constructor Summary
protected TreeBuilder(XmlViolationPolicy streamabilityViolationPolicy, boolean coalescingText)
           
 
Method Summary
private  void accumulateCharacters(char[] buf, int start, int length)
           
private  void addAttributesToBody(Attributes attributes)
           
protected abstract  void addAttributesToElement(T element, Attributes attributes)
           
private  void adoptionAgencyEndTag(String name)
           
private  void append(TreeBuilder.StackNode<T> node)
           
protected abstract  void appendCharacters(T parent, char[] buf, int start, int length)
           
private  void appendCharMayFoster(char[] buf, int i)
           
protected abstract  void appendChildrenToNewParent(T oldParent, T newParent)
           
protected abstract  void appendComment(T parent, char[] buf, int start, int length)
           
protected abstract  void appendCommentToDocument(char[] buf, int start, int length)
           
protected  void appendDoctypeToDocument(String name, String publicIdentifier, String systemIdentifier)
           
private  void appendHtmlElementToDocumentAndPush()
           
private  void appendHtmlElementToDocumentAndPush(Attributes attributes)
           
private  void appendToCurrentNodeAndPushBodyElement()
           
private  void appendToCurrentNodeAndPushBodyElement(Attributes attributes)
           
private  void appendToCurrentNodeAndPushElement(String name, Attributes attributes)
           
private  void appendToCurrentNodeAndPushElementMayFoster(String name, Attributes attributes)
           
private  void appendToCurrentNodeAndPushElementMayFoster(String name, Attributes attributes, T form)
           
private  void appendToCurrentNodeAndPushFormattingElementMayFoster(String name, Attributes attributes)
           
private  void appendToCurrentNodeAndPushFormElementMayFoster(Attributes attributes)
           
private  void appendToCurrentNodeAndPushHeadElement(Attributes attributes)
           
private  void appendVoidElementToCurrentMayFoster(String name, Attributes attributes)
           
private  void appendVoidElementToCurrentMayFoster(String name, Attributes attributes, T form)
           
protected  void bodyClosed(T body)
           
 void characters(char[] buf, int start, int length)
          Receive character tokens.
private  boolean clearLastListSlot()
           
private  boolean clearLastStackSlot()
           
private  void clearStackBackTo(int eltPos)
           
private  void clearTheListOfActiveFormattingElementsUpToTheLastMarker()
           
private  void closeTheCell(int eltPos)
           
 void comment(char[] buf, int length)
          Receive a comment token.
protected abstract  T createElement(String name, Attributes attributes)
           
protected  T createElement(String name, Attributes attributes, T form)
           
protected abstract  T createHtmlElementSetAsRoot(Attributes attributes)
           
protected  T currentNode()
           
protected abstract  void detachFromParent(T element)
           
protected abstract  void detachFromParentAndAppendToNewParent(T child, T newParent)
           
 void doctype(String name, String publicIdentifier, String systemIdentifier, boolean correct)
          Receive a doctype token.
protected  void documentMode(DocumentMode mode, String publicIdentifier, String systemIdentifier, boolean html4SpecificAdditionalErrorChecks)
           
private  void documentModeInternal(DocumentMode mode, String publicIdentifier, String systemIdentifier, boolean html4SpecificAdditionalErrorChecks)
           
protected  void elementPopped(String name, T node)
           
protected  void elementPushed(String name, T node)
           
protected  void end()
           
 void endTag(String name, Attributes attributes)
          Receive an end tag token.
 void eof()
          The end-of-file token.
protected  void err(String message)
          Reports a Parse Error.
protected  void fatal()
          Reports an condition that would make the infoset incompatible with XML 1.0 as fatal.
protected  void fatal(Exception e)
           
private  int findDdOrDtToPop()
           
private  int findInListOfActiveFormattingElements(TreeBuilder.StackNode<T> node)
           
private  int findInListOfActiveFormattingElementsContainsBetweenEndAndLastMarker(String name)
           
private  int findLast(String name)
           
private  int findLastInScope(String name)
           
private  int findLastInScopeHn()
           
private  int findLastInTableScope(String name)
           
private  int findLastInTableScopeOrRootTbodyTheadTfoot()
           
private  int findLastInTableScopeTdTh()
           
private  int findLastOrRoot(String name)
           
private  int findLiToPop()
           
private  void flushCharacters()
           
private  void generateImpliedEndTags()
           
private  void generateImpliedEndTagsExceptFor(String name)
           
protected abstract  boolean hasChildren(T element)
           
protected  void htmlClosed(T html)
           
private  void implicitlyCloseP()
           
protected abstract  void insertBefore(T child, T sibling, T parent)
           
protected abstract  void insertCharactersBefore(char[] buf, int start, int length, T sibling, T parent)
           
private  void insertIntoFosterParent(T child)
           
private  void insertIntoListOfActiveFormattingElements(TreeBuilder.StackNode<T> formattingClone, int bookmark)
           
private  void insertIntoStack(TreeBuilder.StackNode<T> node, int position)
           
private  void insertMarker()
           
private  boolean isAlmostStandards(String publicIdentifierLC, String systemIdentifierLC)
           
private  boolean isCurrent(String name)
           
private  boolean isHtml4Doctype(String publicIdentifier)
           
private  boolean isInStack(TreeBuilder.StackNode<T> node)
           
private  boolean isQuirky(String name, String publicIdentifierLC, String systemIdentifierLC, boolean correct)
           
 boolean isScriptingEnabled()
          Returns the scriptingEnabled.
private  boolean isSecondOnStackBody()
           
protected abstract  T parentElementFor(T child)
          Get the parent element.
private  void pop()
           
private  void push(TreeBuilder.StackNode<T> node)
           
private  void pushHeadPointerOntoStack()
           
private  void reconstructTheActiveFormattingElements()
           
private  void removeFromListOfActiveFormattingElements(int pos)
           
private  void removeFromStack(int pos)
           
private  void removeFromStack(TreeBuilder.StackNode<T> node)
           
private  void resetTheInsertionMode()
           
 void setDoctypeExpectation(DoctypeExpectation doctypeExpectation)
          Sets the doctypeExpectation.
 void setDocumentModeHandler(DocumentModeHandler documentModeHandler)
          Sets the documentModeHandler.
 void setErrorHandler(ErrorHandler errorHandler)
          Sets the errorHandler.
 void setFragmentContext(String context)
           
 void setIgnoringComments(boolean ignoreComments)
           
 void setReportingDoctype(boolean reportingDoctype)
          Sets the reportingDoctype.
 void setScriptingEnabled(boolean scriptingEnabled)
          Sets the scriptingEnabled.
protected abstract  T shallowClone(T element)
           
protected  void start(boolean fragment)
           
 void start(Tokenizer self)
          This method is called at the start of tokenization before any other methods on this interface are called.
 void startTag(String name, Attributes attributes)
          Receive a start tag token.
private  String toAsciiLowerCase(String str)
           
 boolean wantsComments()
          If this handler implementation cares about comments, return true.
protected  void warn(String message)
          Reports a warning
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ISINDEX_PROMPT

private static final char[] ISINDEX_PROMPT

HTML4_PUBLIC_IDS

private static final String[] HTML4_PUBLIC_IDS

QUIRKY_PUBLIC_IDS

private static final String[] QUIRKY_PUBLIC_IDS

NOT_FOUND_ON_STACK

private static final int NOT_FOUND_ON_STACK
See Also:
Constant Field Values

MARKER

private final TreeBuilder.StackNode<T> MARKER

nonConformingAndStreaming

private final boolean nonConformingAndStreaming

conformingAndStreaming

private final boolean conformingAndStreaming

coalescingText

private final boolean coalescingText

phase

private TreeBuilder.Phase phase

tokenizer

protected Tokenizer tokenizer

errorHandler

private ErrorHandler errorHandler

documentModeHandler

private DocumentModeHandler documentModeHandler

doctypeExpectation

private DoctypeExpectation doctypeExpectation

cdataOrRcdataTimesToPop

private int cdataOrRcdataTimesToPop

scriptingEnabled

private boolean scriptingEnabled

needToDropLF

private boolean needToDropLF

wantingComments

private boolean wantingComments

context

private String context

previousPhaseBeforeTrailingEnd

private TreeBuilder.Phase previousPhaseBeforeTrailingEnd

stack

private TreeBuilder.StackNode<T>[] stack

currentPtr

private int currentPtr

listOfActiveFormattingElements

private TreeBuilder.StackNode<T>[] listOfActiveFormattingElements

listPtr

private int listPtr

formPointer

private T formPointer

headPointer

private T headPointer

reportingDoctype

private boolean reportingDoctype

charBuffer

private char[] charBuffer

charBufferLen

private int charBufferLen
Constructor Detail

TreeBuilder

protected TreeBuilder(XmlViolationPolicy streamabilityViolationPolicy,
                      boolean coalescingText)
Method Detail

fatal

protected final void fatal()
                    throws SAXException
Reports an condition that would make the infoset incompatible with XML 1.0 as fatal.

Throws:
SAXException
SAXParseException

fatal

protected final void fatal(Exception e)
                    throws SAXException
Throws:
SAXException

err

protected final void err(String message)
                  throws SAXException
Reports a Parse Error.

Parameters:
message - the message
Throws:
SAXException

warn

protected final void warn(String message)
                   throws SAXException
Reports a warning

Parameters:
message - the message
Throws:
SAXException

start

public final void start(Tokenizer self)
                 throws SAXException
Description copied from interface: TokenHandler
This method is called at the start of tokenization before any other methods on this interface are called. Implementations should hold the reference to the Tokenizer in order to set the content model flag and in order to be able to query for Locator data.

Specified by:
start in interface TokenHandler
Parameters:
self - the Tokenizer.
Throws:
SAXException - if something went wrong

doctype

public final void doctype(String name,
                          String publicIdentifier,
                          String systemIdentifier,
                          boolean correct)
                   throws SAXException
Description copied from interface: TokenHandler
Receive a doctype token.

Specified by:
doctype in interface TokenHandler
Parameters:
name - the name
publicIdentifier - the public id
systemIdentifier - the system id
correct - whether the token is correct
Throws:
SAXException - if something went wrong

isHtml4Doctype

private boolean isHtml4Doctype(String publicIdentifier)

comment

public final void comment(char[] buf,
                          int length)
                   throws SAXException
Description copied from interface: TokenHandler
Receive a comment token. The data is junk if the wantsComments() returned false.

Specified by:
comment in interface TokenHandler
Parameters:
buf - a buffer holding the data
length - the number of code units to read
Throws:
SAXException - if something went wrong

characters

public final void characters(char[] buf,
                             int start,
                             int length)
                      throws SAXException
Description copied from interface: TokenHandler
Receive character tokens. This method has the same semantics as the SAX method of the same name.

Specified by:
characters in interface TokenHandler
Parameters:
buf - a buffer holding the data
start - offset into the buffer
length - the number of code units to read
Throws:
SAXException - if something went wrong
See Also:
TokenHandler.characters(char[], int, int)

eof

public final void eof()
               throws SAXException
Description copied from interface: TokenHandler
The end-of-file token.

Specified by:
eof in interface TokenHandler
Throws:
SAXException - if something went wrong

startTag

public final void startTag(String name,
                           Attributes attributes)
                    throws SAXException
Description copied from interface: TokenHandler
Receive a start tag token.

Specified by:
startTag in interface TokenHandler
Parameters:
name - the tag name
attributes - the attributes
Throws:
SAXException - if something went wrong

endTag

public final void endTag(String name,
                         Attributes attributes)
                  throws SAXException
Description copied from interface: TokenHandler
Receive an end tag token.

Specified by:
endTag in interface TokenHandler
Parameters:
name - the tag name
attributes - the attributes
Throws:
SAXException - if something went wrong

findLastInTableScopeOrRootTbodyTheadTfoot

private int findLastInTableScopeOrRootTbodyTheadTfoot()

findLast

private int findLast(String name)

findLastInTableScope

private int findLastInTableScope(String name)

findLastInScope

private int findLastInScope(String name)

findLastInScopeHn

private int findLastInScopeHn()

generateImpliedEndTagsExceptFor

private void generateImpliedEndTagsExceptFor(String name)
                                      throws SAXException
Throws:
SAXException

generateImpliedEndTags

private void generateImpliedEndTags()
                             throws SAXException
Throws:
SAXException

isSecondOnStackBody

private boolean isSecondOnStackBody()

documentModeInternal

private void documentModeInternal(DocumentMode mode,
                                  String publicIdentifier,
                                  String systemIdentifier,
                                  boolean html4SpecificAdditionalErrorChecks)
                           throws SAXException
Throws:
SAXException

isAlmostStandards

private boolean isAlmostStandards(String publicIdentifierLC,
                                  String systemIdentifierLC)

isQuirky

private boolean isQuirky(String name,
                         String publicIdentifierLC,
                         String systemIdentifierLC,
                         boolean correct)

toAsciiLowerCase

private String toAsciiLowerCase(String str)

closeTheCell

private void closeTheCell(int eltPos)
                   throws SAXException
Throws:
SAXException

findLastInTableScopeTdTh

private int findLastInTableScopeTdTh()

clearStackBackTo

private void clearStackBackTo(int eltPos)
                       throws SAXException
Throws:
SAXException

resetTheInsertionMode

private void resetTheInsertionMode()

implicitlyCloseP

private void implicitlyCloseP()
                       throws SAXException
Throws:
SAXException

clearLastStackSlot

private boolean clearLastStackSlot()

clearLastListSlot

private boolean clearLastListSlot()

push

private void push(TreeBuilder.StackNode<T> node)
           throws SAXException
Throws:
SAXException

append

private void append(TreeBuilder.StackNode<T> node)

insertMarker

private void insertMarker()

clearTheListOfActiveFormattingElementsUpToTheLastMarker

private void clearTheListOfActiveFormattingElementsUpToTheLastMarker()

isCurrent

private boolean isCurrent(String name)

removeFromStack

private void removeFromStack(int pos)
                      throws SAXException
Throws:
SAXException

removeFromStack

private void removeFromStack(TreeBuilder.StackNode<T> node)
                      throws SAXException
Throws:
SAXException

removeFromListOfActiveFormattingElements

private void removeFromListOfActiveFormattingElements(int pos)

adoptionAgencyEndTag

private void adoptionAgencyEndTag(String name)
                           throws SAXException
Throws:
SAXException

insertIntoStack

private void insertIntoStack(TreeBuilder.StackNode<T> node,
                             int position)
                      throws SAXException
Throws:
SAXException

insertIntoListOfActiveFormattingElements

private void insertIntoListOfActiveFormattingElements(TreeBuilder.StackNode<T> formattingClone,
                                                      int bookmark)

findInListOfActiveFormattingElements

private int findInListOfActiveFormattingElements(TreeBuilder.StackNode<T> node)

findInListOfActiveFormattingElementsContainsBetweenEndAndLastMarker

private int findInListOfActiveFormattingElementsContainsBetweenEndAndLastMarker(String name)

findDdOrDtToPop

private int findDdOrDtToPop()

findLiToPop

private int findLiToPop()

findLastOrRoot

private int findLastOrRoot(String name)

addAttributesToBody

private void addAttributesToBody(Attributes attributes)
                          throws SAXException
Throws:
SAXException

pushHeadPointerOntoStack

private void pushHeadPointerOntoStack()
                               throws SAXException
Throws:
SAXException

reconstructTheActiveFormattingElements

private void reconstructTheActiveFormattingElements()
                                             throws SAXException
Throws:
SAXException

insertIntoFosterParent

private void insertIntoFosterParent(T child)
                             throws SAXException
Throws:
SAXException

isInStack

private boolean isInStack(TreeBuilder.StackNode<T> node)

pop

private void pop()
          throws SAXException
Throws:
SAXException

appendCharMayFoster

private void appendCharMayFoster(char[] buf,
                                 int i)
                          throws SAXException
Throws:
SAXException

appendHtmlElementToDocumentAndPush

private void appendHtmlElementToDocumentAndPush(Attributes attributes)
                                         throws SAXException
Throws:
SAXException

appendHtmlElementToDocumentAndPush

private void appendHtmlElementToDocumentAndPush()
                                         throws SAXException
Throws:
SAXException

appendToCurrentNodeAndPushHeadElement

private void appendToCurrentNodeAndPushHeadElement(Attributes attributes)
                                            throws SAXException
Throws:
SAXException

appendToCurrentNodeAndPushBodyElement

private void appendToCurrentNodeAndPushBodyElement(Attributes attributes)
                                            throws SAXException
Throws:
SAXException

appendToCurrentNodeAndPushBodyElement

private void appendToCurrentNodeAndPushBodyElement()
                                            throws SAXException
Throws:
SAXException

appendToCurrentNodeAndPushFormElementMayFoster

private void appendToCurrentNodeAndPushFormElementMayFoster(Attributes attributes)
                                                     throws SAXException
Throws:
SAXException

appendToCurrentNodeAndPushFormattingElementMayFoster

private void appendToCurrentNodeAndPushFormattingElementMayFoster(String name,
                                                                  Attributes attributes)
                                                           throws SAXException
Throws:
SAXException

appendToCurrentNodeAndPushElement

private void appendToCurrentNodeAndPushElement(String name,
                                               Attributes attributes)
                                        throws SAXException
Throws:
SAXException

appendToCurrentNodeAndPushElementMayFoster

private void appendToCurrentNodeAndPushElementMayFoster(String name,
                                                        Attributes attributes)
                                                 throws SAXException
Throws:
SAXException

appendToCurrentNodeAndPushElementMayFoster

private void appendToCurrentNodeAndPushElementMayFoster(String name,
                                                        Attributes attributes,
                                                        T form)
                                                 throws SAXException
Throws:
SAXException

appendVoidElementToCurrentMayFoster

private void appendVoidElementToCurrentMayFoster(String name,
                                                 Attributes attributes,
                                                 T form)
                                          throws SAXException
Throws:
SAXException

appendVoidElementToCurrentMayFoster

private void appendVoidElementToCurrentMayFoster(String name,
                                                 Attributes attributes)
                                          throws SAXException
Throws:
SAXException

accumulateCharacters

private void accumulateCharacters(char[] buf,
                                  int start,
                                  int length)
                           throws SAXException
Throws:
SAXException

flushCharacters

private void flushCharacters()
                      throws SAXException
Throws:
SAXException

createElement

protected abstract T createElement(String name,
                                   Attributes attributes)
                            throws SAXException
Throws:
SAXException

createElement

protected T createElement(String name,
                          Attributes attributes,
                          T form)
                   throws SAXException
Throws:
SAXException

createHtmlElementSetAsRoot

protected abstract T createHtmlElementSetAsRoot(Attributes attributes)
                                         throws SAXException
Throws:
SAXException

detachFromParent

protected abstract void detachFromParent(T element)
                                  throws SAXException
Throws:
SAXException

hasChildren

protected abstract boolean hasChildren(T element)
                                throws SAXException
Throws:
SAXException

shallowClone

protected abstract T shallowClone(T element)
                           throws SAXException
Throws:
SAXException

detachFromParentAndAppendToNewParent

protected abstract void detachFromParentAndAppendToNewParent(T child,
                                                             T newParent)
                                                      throws SAXException
Throws:
SAXException

appendChildrenToNewParent

protected abstract void appendChildrenToNewParent(T oldParent,
                                                  T newParent)
                                           throws SAXException
Throws:
SAXException

parentElementFor

protected abstract T parentElementFor(T child)
                               throws SAXException
Get the parent element. MUST return null if there is no parent or the parent is not an element.

Throws:
SAXException

insertBefore

protected abstract void insertBefore(T child,
                                     T sibling,
                                     T parent)
                              throws SAXException
Throws:
SAXException

insertCharactersBefore

protected abstract void insertCharactersBefore(char[] buf,
                                               int start,
                                               int length,
                                               T sibling,
                                               T parent)
                                        throws SAXException
Throws:
SAXException

appendCharacters

protected abstract void appendCharacters(T parent,
                                         char[] buf,
                                         int start,
                                         int length)
                                  throws SAXException
Throws:
SAXException

appendComment

protected abstract void appendComment(T parent,
                                      char[] buf,
                                      int start,
                                      int length)
                               throws SAXException
Throws:
SAXException

appendCommentToDocument

protected abstract void appendCommentToDocument(char[] buf,
                                                int start,
                                                int length)
                                         throws SAXException
Throws:
SAXException

addAttributesToElement

protected abstract void addAttributesToElement(T element,
                                               Attributes attributes)
                                        throws SAXException
Throws:
SAXException

start

protected void start(boolean fragment)
              throws SAXException
Throws:
SAXException

end

protected void end()
            throws SAXException
Throws:
SAXException

bodyClosed

protected void bodyClosed(T body)
                   throws SAXException
Throws:
SAXException

htmlClosed

protected void htmlClosed(T html)
                   throws SAXException
Throws:
SAXException

appendDoctypeToDocument

protected void appendDoctypeToDocument(String name,
                                       String publicIdentifier,
                                       String systemIdentifier)
                                throws SAXException
Throws:
SAXException

elementPushed

protected void elementPushed(String name,
                             T node)
                      throws SAXException
Throws:
SAXException

elementPopped

protected void elementPopped(String name,
                             T node)
                      throws SAXException
Throws:
SAXException

documentMode

protected void documentMode(DocumentMode mode,
                            String publicIdentifier,
                            String systemIdentifier,
                            boolean html4SpecificAdditionalErrorChecks)
                     throws SAXException
Throws:
SAXException

wantsComments

public boolean wantsComments()
Description copied from interface: TokenHandler
If this handler implementation cares about comments, return true. If not, return false.

Specified by:
wantsComments in interface TokenHandler
Returns:
whether this handler wants comments
See Also:
TokenHandler.wantsComments()

setIgnoringComments

public void setIgnoringComments(boolean ignoreComments)

setErrorHandler

public final void setErrorHandler(ErrorHandler errorHandler)
Sets the errorHandler.

Parameters:
errorHandler - the errorHandler to set

setFragmentContext

public final void setFragmentContext(String context)

currentNode

protected final T currentNode()

isScriptingEnabled

public boolean isScriptingEnabled()
Returns the scriptingEnabled.

Returns:
the scriptingEnabled

setScriptingEnabled

public void setScriptingEnabled(boolean scriptingEnabled)
Sets the scriptingEnabled.

Parameters:
scriptingEnabled - the scriptingEnabled to set

setDoctypeExpectation

public void setDoctypeExpectation(DoctypeExpectation doctypeExpectation)
Sets the doctypeExpectation.

Parameters:
doctypeExpectation - the doctypeExpectation to set

setDocumentModeHandler

public void setDocumentModeHandler(DocumentModeHandler documentModeHandler)
Sets the documentModeHandler.

Parameters:
documentModeHandler - the documentModeHandler to set

setReportingDoctype

public void setReportingDoctype(boolean reportingDoctype)
Sets the reportingDoctype.

Parameters:
reportingDoctype - the reportingDoctype to set