# Class TreeBuilder<T>

java.lang.Object
nu.validator.htmlparser.impl.TreeBuilder<T>

All Implemented Interfaces:
  `TokenHandler, TreeBuilderState<T>`

Direct Known Subclasses:
  `CoalescingTreeBuilder`

public abstract class TreeBuilder<T> extends Object implements TokenHandler, TreeBuilderState<T>

## Field Summary

Fields
Modifier and Type
Field
Description
`protected char[]`
`charBuffer`
`protected int`
`charBufferLen`
`protected ErrorHandler`
`errorHandler`
`protected Tokenizer`
`tokenizer`

## Constructor Summary

Constructors
Modifier
Constructor
Description
`protected`
`TreeBuilder()`

## Method Summary

Modifier and Type
Method
Description
`protected void`
`accumulateCharacters(char[] buf, int start, int length)`
`protected abstract void`
`addAttributesToElement(T element, HtmlAttributes attributes)`
`protected abstract void`
`appendCharacters(T parent, char[] buf, int start, int length)`
`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)`
`protected abstract void`
`appendElement(T child, T newParent)`
`boolean`
`cdataSectionAllowed()`
Checks if the CDATA sections are allowed.
`final void`
`characters(char[] buf, int start, int length)`
Receive character tokens.
`final void`
`comment(char[] buf, int start, int length)`
Receive a comment token.
`protected abstract T`
`createAndInsertFosterParentedElement(String ns, String name, HtmlAttributes attributes, T table, T stackParent)`
`protected T`
`createAndInsertFosterParentedElement(String ns, String name, HtmlAttributes attributes, T form, T table, T stackParent)`
`protected abstract T`
`createElement(String ns, String name, HtmlAttributes attributes, T intendedParent)`
`protected T`
`createElement(String ns, String name, HtmlAttributes attributes, T form, T intendedParent)`
`protected abstract T`
`createHtmlElementSetAsRoot(HtmlAttributes attributes)`
`protected final T`
`currentNode()`
`protected abstract void`
`detachFromParent(T element)`
`final void`
`doctype(String name, String publicIdentifier, String systemIdentifier, boolean forceQuirks)`
Receive a doctype token.
`protected void`
`documentMode(DocumentMode m, String publicIdentifier, String systemIdentifier)`
`boolean`
`dropBufferIfLongerThan(int length)`
`protected void`
`elementPopped(String ns, String name, T node)`
`protected void`
`elementPushed(String ns, String name, T node)`
`protected void`
`end()`
`final void`
`endTag(ElementName elementName)`
Receive an end tag token.
`final void`
`endTokenization()`
The perform final cleanup.
`void`
`ensureBufferSpace(int inputLength)`
Notifies the token handler of the worst case amount of data to be reported via `characters()` and `zeroOriginatingReplacementCharacter()`.
`final void`
`eof()`
The end-of-file token.
`static String`
`extractCharsetFromContent(String attributeValue)`
C++ memory note: The return value must be released.
`protected void`
`fatal()`
Reports an condition that would make the infoset incompatible with XML 1.0 as fatal.
`protected final void`
`fatal(Exception e)`
`final void`
`flushCharacters()`
Flushes the pending characters.
`ErrorHandler`
`getErrorHandler()`
Returns the errorHandler.
`T`
`getFormPointer()`
Returns the formPointer.
`T`
`getHeadPointer()`
Returns the headPointer.
`nu.validator.htmlparser.impl.StackNode<T>[]`
`getListOfActiveFormattingElements()`
Returns the listOfActiveFormattingElements.
`int`
`getListOfActiveFormattingElementsLength()`
Return the length of the list of active formatting elements.
`int`
`getMode()`
Returns the mode.
`int`
`getOriginalMode()`
Returns the originalMode.
`nu.validator.htmlparser.impl.StackNode<T>[]`
`getStack()`
Returns the stack.
`int`
`getStackLength()`
Return the length of the stack.
`int[]`
`getTemplateModeStack()`
Returns the stack of template insertion modes.
`int`
`getTemplateModeStackLength()`
Return the length of the stack of template insertion modes.
`protected abstract boolean`
`hasChildren(T element)`
`protected abstract void`
`insertFosterParentedCharacters(char[] buf, int start, int length, T table, T stackParent)`
`protected abstract void`
`insertFosterParentedChild(T child, T table, T stackParent)`
`boolean`
`isAllowDeclarativeShadowRoots()`
`boolean`
`isFramesetOk()`
Returns the framesetOk.
`boolean`
`isNeedToDropLF()`
Returns the needToDropLF.
`boolean`
`isQuirks()`
Returns the quirks.
`boolean`
`isScriptingEnabled()`
Returns the scriptingEnabled.
`void`
`loadState(TreeBuilderState<T> snapshot)`
`protected void`
`markMalformedIfScript(T elt)`
`TreeBuilderState<T>`
`newSnapshot()`
Creates a comparable snapshot of the tree builder state.
`protected void`
`optionElementPopped(T option)`
Called when an option element is popped from the stack.
`protected final void`
`requestSuspension()`
`void`
`setAllowDeclarativeShadowRoots(boolean allow)`
`void`
`setDocumentModeHandler(DocumentModeHandler documentModeHandler)`
Sets the documentModeHandler.
`final void`
`setErrorHandler(ErrorHandler errorHandler)`
Sets the errorHandler.
`void`
`setForceNoQuirks(boolean forceNoQuirks)`
`final void`
`setFragmentContext(String context)`
The argument MUST be an interned string or `null`.
`final void`
`setFragmentContext(String context, String ns, T node, boolean quirks)`
The argument MUST be an interned string or `null`.
`void`
`setIgnoringComments(boolean ignoreComments)`
`void`
`setIsSrcdocDocument(boolean isSrcdocDocument)`
`void`
`setKeepBuffer(boolean keepBuffer)`
`void`
`setNamePolicy(XmlViolationPolicy namePolicy)`
`void`
`setReportingDoctype(boolean reportingDoctype)`
Sets the reportingDoctype.
`void`
`setScriptingEnabled(boolean scriptingEnabled)`
Sets the scriptingEnabled.
`boolean`
`snapshotMatches(TreeBuilderState<T> snapshot)`
`protected void`
`start(boolean fragmentMode)`
`final void`
`startTag(ElementName elementName, HtmlAttributes attributes, boolean selfClosing)`
Receive a start tag token.
`final void`
`startTokenization(Tokenizer self)`
This method is called at the start of tokenization before any other methods on this interface are called.
`boolean`
`wantsComments()`
If this handler implementation cares about comments, return `true`.
`void`
`zeroOriginatingReplacementCharacter()`
Reports a U+0000 that's being turned into a U+FFFD.
`void`
`zeroOrReplacementCharacter()`
Emits: * U+0000 if only tokenization is being performed * U+FFFD if tree construction is being performed also

### Methods inherited from class Object

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

## Field Details

### tokenizer

protected Tokenizer tokenizer

### errorHandler

protected ErrorHandler errorHandler

### charBuffer

protected char[] charBuffer

### charBufferLen

protected int charBufferLen

## Constructor Details

### TreeBuilder

protected TreeBuilder()

## Method Details

### fatal

protected 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`

### setKeepBuffer

public void setKeepBuffer(boolean keepBuffer)

### dropBufferIfLongerThan

public boolean dropBufferIfLongerThan(int length)

### startTokenization

public final void startTokenization(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:
  `startTokenization` 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 forceQuirks) 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
  `forceQuirks` - whether the token is correct
Throws:
  `SAXException` - if something went wrong

### comment

public final void comment(char[] buf, int start, 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
  `start` - the offset into the buffer
  `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)`

### zeroOriginatingReplacementCharacter

public void zeroOriginatingReplacementCharacter() throws SAXException
Description copied from interface: `TokenHandler`
Reports a U+0000 that's being turned into a U+FFFD.

Specified by:
  `zeroOriginatingReplacementCharacter` in interface `TokenHandler`
Throws:
  `SAXException` - if something went wrong
See Also:
  - `TokenHandler.zeroOriginatingReplacementCharacter()`

### zeroOrReplacementCharacter

public void zeroOrReplacementCharacter() throws SAXException
Description copied from interface: `TokenHandler`
Emits: * U+0000 if only tokenization is being performed * U+FFFD if tree construction is being performed also

Specified by:
  `zeroOrReplacementCharacter` in interface `TokenHandler`
Throws:
  `SAXException` - if something went wrong
See Also:
  - `TokenHandler.zeroOrReplacementCharacter()`

### 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

### endTokenization

public final void endTokenization() throws SAXException
Description copied from interface: `TokenHandler`
The perform final cleanup.

Specified by:
  `endTokenization` in interface `TokenHandler`
Throws:
  `SAXException` - if something went wrong
See Also:
  - `TokenHandler.endTokenization()`

### startTag

public final void startTag(ElementName elementName, HtmlAttributes attributes, boolean selfClosing) throws SAXException
Description copied from interface: `TokenHandler`
Receive a start tag token.

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

### extractCharsetFromContent

public static String extractCharsetFromContent(String attributeValue)

C++ memory note: The return value must be released.

Returns:

### endTag

public final void endTag(ElementName elementName) throws SAXException
Description copied from interface: `TokenHandler`
Receive an end tag token.

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

### ensureBufferSpace

public void ensureBufferSpace(int inputLength) throws SAXException
Description copied from interface: `TokenHandler`
Notifies the token handler of the worst case amount of data to be reported via `characters()` and `zeroOriginatingReplacementCharacter()`.

Specified by:
  `ensureBufferSpace` in interface `TokenHandler`
Parameters:
  `inputLength` - the maximum number of chars that can be reported via `characters()` and `zeroOriginatingReplacementCharacter()` before a new call to this method.
Throws:
  `SAXException`

### accumulateCharacters

protected void accumulateCharacters(char[] buf, int start, int length) throws SAXException

Throws:
  `SAXException`

### requestSuspension

protected final void requestSuspension()

### createElement

protected abstract T createElement(String ns, String name, HtmlAttributes attributes, T intendedParent) throws SAXException

Throws:
  `SAXException`

### createElement

protected T createElement(String ns, String name, HtmlAttributes attributes, T form, T intendedParent) throws SAXException

Throws:
  `SAXException`

### createHtmlElementSetAsRoot

protected abstract T createHtmlElementSetAsRoot(HtmlAttributes attributes) throws SAXException

Throws:
  `SAXException`

### detachFromParent

protected abstract void detachFromParent(T element) throws SAXException

Throws:
  `SAXException`

### optionElementPopped

protected void optionElementPopped(T option) throws SAXException
Called when an option element is popped from the stack. https://html.spec.whatwg.org/multipage/form-elements.html#maybe-clone-an-option-into-selectedcontent Implements "maybe clone an option into selectedcontent" for customizable select. Subclasses that support DOM operations should override this to perform the cloning.

Throws:
  `SAXException`

### hasChildren

protected abstract boolean hasChildren(T element) throws SAXException

Throws:
  `SAXException`

### appendElement

protected abstract void appendElement(T child, T newParent) throws SAXException

Throws:
  `SAXException`

### appendChildrenToNewParent

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

Throws:
  `SAXException`

### insertFosterParentedChild

protected abstract void insertFosterParentedChild(T child, T table, T stackParent) throws SAXException

Throws:
  `SAXException`

### createAndInsertFosterParentedElement

protected abstract T createAndInsertFosterParentedElement(String ns, String name, HtmlAttributes attributes, T table, T stackParent) throws SAXException

Throws:
  `SAXException`

### createAndInsertFosterParentedElement

protected T createAndInsertFosterParentedElement(String ns, String name, HtmlAttributes attributes, T form, T table, T stackParent) throws SAXException

Throws:
  `SAXException`

### insertFosterParentedCharacters

protected abstract void insertFosterParentedCharacters(char[] buf, int start, int length, T table, T stackParent) 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, HtmlAttributes attributes) throws SAXException

Throws:
  `SAXException`

### markMalformedIfScript

protected void markMalformedIfScript(T elt) throws SAXException

Throws:
  `SAXException`

### start

protected void start(boolean fragmentMode) throws SAXException

Throws:
  `SAXException`

### end

protected void end() throws SAXException

Throws:
  `SAXException`

### appendDoctypeToDocument

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

Throws:
  `SAXException`

### elementPushed

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

Throws:
  `SAXException`

### elementPopped

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

Throws:
  `SAXException`

### documentMode

protected void documentMode(DocumentMode m, String publicIdentifier, String systemIdentifier) 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

### getErrorHandler

public ErrorHandler getErrorHandler()
Returns the errorHandler.

Returns:
  the errorHandler

### setFragmentContext

public final void setFragmentContext(String context)
The argument MUST be an interned string or `null`.

Parameters:
  `context` -

### cdataSectionAllowed

public boolean cdataSectionAllowed() throws SAXException
Description copied from interface: `TokenHandler`
Checks if the CDATA sections are allowed.

Specified by:
  `cdataSectionAllowed` in interface `TokenHandler`
Returns:
  `true` if CDATA sections are allowed
Throws:
  `SAXException` - if something went wrong
See Also:
  - `TokenHandler.cdataSectionAllowed()`

### setFragmentContext

public final void setFragmentContext(String context, String ns, T node, boolean quirks)
The argument MUST be an interned string or `null`.

Parameters:
  `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

### setForceNoQuirks

public void setForceNoQuirks(boolean forceNoQuirks)

### setIsSrcdocDocument

public void setIsSrcdocDocument(boolean isSrcdocDocument)

### isAllowDeclarativeShadowRoots

public boolean isAllowDeclarativeShadowRoots()

### setAllowDeclarativeShadowRoots

public void setAllowDeclarativeShadowRoots(boolean allow)

### setNamePolicy

public void setNamePolicy(XmlViolationPolicy namePolicy)

### 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

### flushCharacters

public final void flushCharacters() throws SAXException
Flushes the pending characters. Public for document.write use cases only.

Throws:
  `SAXException`

### newSnapshot

public TreeBuilderState<T> newSnapshot() throws SAXException
Creates a comparable snapshot of the tree builder state. Snapshot creation is only supported immediately after a script end tag has been processed. In C++ the caller is responsible for calling `delete` on the returned object.

Returns:
  a snapshot.
Throws:
  `SAXException`

### snapshotMatches

public boolean snapshotMatches(TreeBuilderState<T> snapshot)

### loadState

public void loadState(TreeBuilderState<T> snapshot) throws SAXException

Throws:
  `SAXException`

### getFormPointer

public T getFormPointer()
Description copied from interface: `TreeBuilderState`
Returns the formPointer.

Specified by:
  `getFormPointer` in interface `TreeBuilderState<T>`
Returns:
  the formPointer
See Also:
  - `TreeBuilderState.getFormPointer()`

### getHeadPointer

public T getHeadPointer()
Returns the headPointer.

Specified by:
  `getHeadPointer` in interface `TreeBuilderState<T>`
Returns:
  the headPointer

### getListOfActiveFormattingElements

public nu.validator.htmlparser.impl.StackNode<T>[] getListOfActiveFormattingElements()
Description copied from interface: `TreeBuilderState`
Returns the listOfActiveFormattingElements.

Specified by:
  `getListOfActiveFormattingElements` in interface `TreeBuilderState<T>`
Returns:
  the listOfActiveFormattingElements
See Also:
  - `TreeBuilderState.getListOfActiveFormattingElements()`

### getStack

public nu.validator.htmlparser.impl.StackNode<T>[] getStack()
Description copied from interface: `TreeBuilderState`
Returns the stack.

Specified by:
  `getStack` in interface `TreeBuilderState<T>`
Returns:
  the stack
See Also:
  - `TreeBuilderState.getStack()`

### getTemplateModeStack

public int[] getTemplateModeStack()
Description copied from interface: `TreeBuilderState`
Returns the stack of template insertion modes.

Specified by:
  `getTemplateModeStack` in interface `TreeBuilderState<T>`
Returns:
  the stack of template insertion modes
See Also:
  - `TreeBuilderState.getTemplateModeStack()`

### getMode

public int getMode()
Returns the mode.

Specified by:
  `getMode` in interface `TreeBuilderState<T>`
Returns:
  the mode

### getOriginalMode

public int getOriginalMode()
Returns the originalMode.

Specified by:
  `getOriginalMode` in interface `TreeBuilderState<T>`
Returns:
  the originalMode

### isFramesetOk

public boolean isFramesetOk()
Returns the framesetOk.

Specified by:
  `isFramesetOk` in interface `TreeBuilderState<T>`
Returns:
  the framesetOk

### isNeedToDropLF

public boolean isNeedToDropLF()
Returns the needToDropLF.

Specified by:
  `isNeedToDropLF` in interface `TreeBuilderState<T>`
Returns:
  the needToDropLF

### isQuirks

public boolean isQuirks()
Returns the quirks.

Specified by:
  `isQuirks` in interface `TreeBuilderState<T>`
Returns:
  the quirks

### getListOfActiveFormattingElementsLength

public int getListOfActiveFormattingElementsLength()
Description copied from interface: `TreeBuilderState`
Return the length of the list of active formatting elements.

Specified by:
  `getListOfActiveFormattingElementsLength` in interface `TreeBuilderState<T>`
Returns:
  the length of the list of active formatting elements.
See Also:
  - `TreeBuilderState.getListOfActiveFormattingElementsLength()`

### getStackLength

public int getStackLength()
Description copied from interface: `TreeBuilderState`
Return the length of the stack.

Specified by:
  `getStackLength` in interface `TreeBuilderState<T>`
Returns:
  the length of the stack.
See Also:
  - `TreeBuilderState.getStackLength()`

### getTemplateModeStackLength

public int getTemplateModeStackLength()
Description copied from interface: `TreeBuilderState`
Return the length of the stack of template insertion modes.

Specified by:
  `getTemplateModeStackLength` in interface `TreeBuilderState<T>`
Returns:
  the length of the stack of template insertion modes.
See Also:
  - `TreeBuilderState.getTemplateModeStackLength()`