Package org.htmlunit.cyberneko
Class HTMLTagBalancer
java.lang.Object
org.htmlunit.cyberneko.HTMLTagBalancer
- All Implemented Interfaces:
HTMLComponent
,XMLComponent
,XMLDocumentFilter
,XMLDocumentSource
,XMLDocumentHandler
Balances tags in an HTML document. This component receives document events
and tries to correct many common mistakes that human (and computer) HTML
document authors make. This tag balancer can:
- add missing parent elements;
- automatically close elements with optional end tags; and
- handle mis-matched inline element tags.
This component recognizes the following features:
- http://cyberneko.org/html/features/augmentations
- http://cyberneko.org/html/features/report-errors
- http://cyberneko.org/html/features/balance-tags/document-fragment
- http://cyberneko.org/html/features/balance-tags/ignore-outside-content
This component recognizes the following properties:
- http://cyberneko.org/html/properties/names/elems
- http://cyberneko.org/html/properties/names/attrs
- http://cyberneko.org/html/properties/error-reporter
- http://cyberneko.org/html/properties/balance-tags/current-stack
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static class
Structure to hold information about an element placed in buffer to be comsumed laterstatic class
Element info for each start element.static class
Unsynchronized stack of element information. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final String
Include infoset augmentations.protected static final String
Document fragment balancing only.private XMLDocumentHandler
The document handler.private XMLDocumentSource
private final List
<HTMLTagBalancer.ElementEntry> protected static final String
Error reporter.protected boolean
Allows self closing iframe tags.protected boolean
Allows self closing tags.protected boolean
Include infoset augmentations.protected boolean
Document fragment balancing only.protected final HTMLTagBalancer.InfoStack
The element stack.protected HTMLErrorReporter
Error reporter.protected boolean
Ignore outside content.protected final HTMLTagBalancer.InfoStack
The inline stack.protected short
Modify HTML element names.protected boolean
Namespaces.protected boolean
True if a form is in the stack (allow to discard opening of nested forms)protected boolean
True if a select is in the stackprotected boolean
True if a svg is in the stack (no parent checking takes place)private boolean
private boolean
private final QName
A qualified name.static final String
<font color="red">EXPERIMENTAL: may change in next release</font><br/> Name of the property holding the stack of elements in which context a document fragment should be parsed.private QName[]
Stack of elements determining the context in which a document fragment should be parsedprivate int
protected boolean
Report errors.protected boolean
True if seen anything.protected boolean
True if seenbody
element.private boolean
private boolean
True if seen non whitespace characters.protected boolean
True if root element has been seen.private boolean
True if seenframeset
element.protected boolean
True if seenhead
element.protected boolean
True if root element has been seen.protected boolean
True if seen the end of the document element.protected boolean
Template document fragment balancing only.private final HTMLConfiguration
protected static final String
Ignore outside content.private final LostText
protected static final String
Modify HTML attribute names: { "upper", "lower", "default" }.protected static final String
Modify HTML element names: { "upper", "lower", "default" }.private static final short
Lowercase HTML names.private static final short
Don't modify HTML names.private static final short
Uppercase HTML names.protected static final String
Namespaces.private static final String[]
Recognized features.private static final Boolean[]
Recognized features defaults.private static final String[]
Recognized properties.private static final Object[]
Recognized properties defaults.protected static final String
Report errors.private static final HTMLEventInfo
Synthesized event info item.protected HTMLTagBalancingListener
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate void
addBodyIfNeeded
(short element) protected final void
callEndElement
(QName element, Augmentations augs) protected final void
callStartElement
(QName element, XMLAttributes attrs, Augmentations augs) void
characters
(XMLString text, Augmentations augs) Characters.void
comment
(XMLString text, Augmentations augs) Comment.private void
Consume elements that have been buffered, like that are first consumed at the end of documentprivate void
private QName
createQName
(String tagName) void
doctypeDecl
(String rootElementName, String publicId, String systemId, Augmentations augs) Doctype declaration.void
emptyElement
(QName element, XMLAttributes attrs, Augmentations augs) Empty element.void
endCDATA
(Augmentations augs) End CDATA section.void
endDocument
(Augmentations augs) End document.void
endElement
(QName element, Augmentations augs) End element.private void
Generates a missing (which creates missing when needed)private boolean
forceStartElement
(QName elem, XMLAttributes attrs, Augmentations augs) Forces an element start, taking care to set the information to allow startElement to "see" that's the element has been forced.Returns the document handler.protected HTMLElements.Element
getElement
(QName elementName) protected final int
getElementDepth
(HTMLElements.Element element) getFeatureDefault
(String featureId) Returns the default state for a feature.protected static short
getNamesValue
(String value) protected int
getParentDepth
(HTMLElements.Element[] parents, short bounds) getPropertyDefault
(String propertyId) Returns the default state for a property.String[]
Returns recognized features.String[]
Returns recognized properties.protected static String
modifyName
(String name, short mode) private void
notifyDiscardedEndElement
(QName element, Augmentations augs) Notifies the tagBalancingListener (if any) of an ignored end elementprivate void
notifyDiscardedStartElement
(QName elem, XMLAttributes attrs, Augmentations augs) Notifies the tagBalancingListener (if any) of an ignored start elementvoid
processingInstruction
(String target, XMLString data, Augmentations augs) Processing instruction.void
reset
(XMLComponentManager manager) Resets the component.void
setDocumentHandler
(XMLDocumentHandler handler) Sets the document handler.void
setDocumentSource
(XMLDocumentSource source) Sets the document source.void
setFeature
(String featureId, boolean state) Sets a feature.void
setProperty
(String propertyId, Object value) Sets a property.(package private) void
setTagBalancingListener
(HTMLTagBalancingListener tagBalancingListener) void
startCDATA
(Augmentations augs) Start CDATA section.void
startDocument
(XMLLocator locator, String encoding, NamespaceContext nscontext, Augmentations augs) Start document.void
startElement
(QName elem, XMLAttributes attrs, Augmentations augs) Start element.protected final Augmentations
void
xmlDecl
(String version, String encoding, String standalone, Augmentations augs) XML declaration.
-
Field Details
-
NAMESPACES
Namespaces.- See Also:
-
AUGMENTATIONS
Include infoset augmentations.- See Also:
-
REPORT_ERRORS
Report errors.- See Also:
-
DOCUMENT_FRAGMENT
Document fragment balancing only.- See Also:
-
IGNORE_OUTSIDE_CONTENT
Ignore outside content.- See Also:
-
RECOGNIZED_FEATURES
Recognized features. -
RECOGNIZED_FEATURES_DEFAULTS
Recognized features defaults. -
NAMES_ELEMS
Modify HTML element names: { "upper", "lower", "default" }.- See Also:
-
NAMES_ATTRS
Modify HTML attribute names: { "upper", "lower", "default" }.- See Also:
-
ERROR_REPORTER
Error reporter.- See Also:
-
FRAGMENT_CONTEXT_STACK
<font color="red">EXPERIMENTAL: may change in next release</font><br/> Name of the property holding the stack of elements in which context a document fragment should be parsed.- See Also:
-
RECOGNIZED_PROPERTIES
Recognized properties. -
RECOGNIZED_PROPERTIES_DEFAULTS
Recognized properties defaults. -
NAMES_NO_CHANGE
private static final short NAMES_NO_CHANGEDon't modify HTML names.- See Also:
-
NAMES_UPPERCASE
private static final short NAMES_UPPERCASEUppercase HTML names.- See Also:
-
NAMES_LOWERCASE
private static final short NAMES_LOWERCASELowercase HTML names.- See Also:
-
SYNTHESIZED_ITEM
Synthesized event info item. -
fNamespaces
protected boolean fNamespacesNamespaces. -
fAugmentations
protected boolean fAugmentationsInclude infoset augmentations. -
fReportErrors
protected boolean fReportErrorsReport errors. -
fDocumentFragment
protected boolean fDocumentFragmentDocument fragment balancing only. -
fTemplateFragment
protected boolean fTemplateFragmentTemplate document fragment balancing only. -
fIgnoreOutsideContent
protected boolean fIgnoreOutsideContentIgnore outside content. -
fAllowSelfclosingIframe
protected boolean fAllowSelfclosingIframeAllows self closing iframe tags. -
fAllowSelfclosingTags
protected boolean fAllowSelfclosingTagsAllows self closing tags. -
fNamesElems
protected short fNamesElemsModify HTML element names. -
fErrorReporter
Error reporter. -
documentSource_
-
documentHandler_
The document handler. -
fElementStack
The element stack. -
fInlineStack
The inline stack. -
fSeenAnything
protected boolean fSeenAnythingTrue if seen anything. Important for xml declaration. -
fSeenDoctype
protected boolean fSeenDoctypeTrue if root element has been seen. -
fSeenRootElement
protected boolean fSeenRootElementTrue if root element has been seen. -
fSeenRootElementEnd
protected boolean fSeenRootElementEndTrue if seen the end of the document element. In other words, this variable is set to false until the end </HTML> tag is seen (or synthesized). This is used to ensure that extraneous events after the end of the document element do not make the document stream ill-formed. -
fSeenHeadElement
protected boolean fSeenHeadElementTrue if seenhead
element. -
fSeenBodyElement
protected boolean fSeenBodyElementTrue if seenbody
element. -
fSeenBodyElementEnd
private boolean fSeenBodyElementEnd -
fSeenFramesetElement
private boolean fSeenFramesetElementTrue if seenframeset
element. -
fSeenCharacters
private boolean fSeenCharactersTrue if seen non whitespace characters. -
fOpenedForm
protected boolean fOpenedFormTrue if a form is in the stack (allow to discard opening of nested forms) -
fOpenedSvg
protected boolean fOpenedSvgTrue if a svg is in the stack (no parent checking takes place) -
fOpenedSelect
protected boolean fOpenedSelectTrue if a select is in the stack -
fQName
A qualified name. -
tagBalancingListener
-
lostText_
-
forcedStartElement_
private boolean forcedStartElement_ -
forcedEndElement_
private boolean forcedEndElement_ -
fragmentContextStack_
Stack of elements determining the context in which a document fragment should be parsed -
fragmentContextStackSize_
private int fragmentContextStackSize_ -
endElementsBuffer_
-
discardedStartElements
-
htmlConfiguration_
-
-
Constructor Details
-
HTMLTagBalancer
HTMLTagBalancer(HTMLConfiguration htmlConfiguration)
-
-
Method Details
-
getFeatureDefault
Returns the default state for a feature.- Specified by:
getFeatureDefault
in interfaceHTMLComponent
- Specified by:
getFeatureDefault
in interfaceXMLComponent
- Parameters:
featureId
- The feature identifier.- Returns:
- the default state for a feature, or null if this component does not want to report a default value for this feature.
-
getPropertyDefault
Returns the default state for a property.- Specified by:
getPropertyDefault
in interfaceHTMLComponent
- Specified by:
getPropertyDefault
in interfaceXMLComponent
- Parameters:
propertyId
- The property identifier.- Returns:
- the default state for a property, or null if this component does not want to report a default value for this property
-
getRecognizedFeatures
Returns recognized features.- Specified by:
getRecognizedFeatures
in interfaceXMLComponent
- Returns:
- an array of feature identifiers that are recognized by this component. This method may return null if no features are recognized by this component.
-
getRecognizedProperties
Returns recognized properties.- Specified by:
getRecognizedProperties
in interfaceXMLComponent
- Returns:
- an array of property identifiers that are recognized by this component. This method may return null if no properties are recognized by this component.
-
reset
Resets the component.- Specified by:
reset
in interfaceXMLComponent
- Parameters:
manager
- The component manager.- Throws:
XMLConfigurationException
-
setFeature
Sets a feature.- Specified by:
setFeature
in interfaceXMLComponent
- Parameters:
featureId
- The feature identifier.state
- The state of the feature.- Throws:
XMLConfigurationException
- Thrown for configuration error. In general, components should only throw this exception if it is really a critical error.
-
setProperty
Sets a property.- Specified by:
setProperty
in interfaceXMLComponent
- Parameters:
propertyId
- The property identifier.value
- The value of the property.- Throws:
XMLConfigurationException
- Thrown for configuration error. In general, components should only throw this exception if it is really a critical error.
-
setDocumentHandler
Sets the document handler.- Specified by:
setDocumentHandler
in interfaceXMLDocumentSource
- Parameters:
handler
- the new handler
-
getDocumentHandler
Returns the document handler.- Specified by:
getDocumentHandler
in interfaceXMLDocumentSource
- Returns:
- the document handler
-
setDocumentSource
Sets the document source.- Specified by:
setDocumentSource
in interfaceXMLDocumentHandler
- Parameters:
source
- the new source
-
getDocumentSource
- Specified by:
getDocumentSource
in interfaceXMLDocumentHandler
- Returns:
- the document source.
-
startDocument
public void startDocument(XMLLocator locator, String encoding, NamespaceContext nscontext, Augmentations augs) throws XNIException Start document.- Specified by:
startDocument
in interfaceXMLDocumentHandler
- Parameters:
locator
- The document locator, or null if the document location cannot be reported during the parsing of this document. However, it is strongly recommended that a locator be supplied that can at least report the system identifier of the document.encoding
- The auto-detected IANA encoding name of the entity stream. This value will be null in those situations where the entity encoding is not auto-detected (e.g. internal entities or a document entity that is parsed from a java.io.Reader).nscontext
- The namespace context in effect at the start of this document. This object represents the current context. Implementors of this class are responsible for copying the namespace bindings from the the current context (and its parent contexts) if that information is important.augs
- Additional information that may include infoset augmentations- Throws:
XNIException
- Thrown by handler to signal an error.
-
xmlDecl
public void xmlDecl(String version, String encoding, String standalone, Augmentations augs) throws XNIException XML declaration.- Specified by:
xmlDecl
in interfaceXMLDocumentHandler
- Parameters:
version
- The XML version.encoding
- The IANA encoding name of the document, or null if not specified.standalone
- The standalone value, or null if not specified.augs
- Additional information that may include infoset augmentations- Throws:
XNIException
- Thrown by handler to signal an error.
-
doctypeDecl
public void doctypeDecl(String rootElementName, String publicId, String systemId, Augmentations augs) throws XNIException Doctype declaration.- Specified by:
doctypeDecl
in interfaceXMLDocumentHandler
- Parameters:
rootElementName
- The name of the root element.publicId
- The public identifier if an external DTD or null if the external DTD is specified using SYSTEM.systemId
- The system identifier if an external DTD, null otherwise.augs
- Additional information that may include infoset augmentations- Throws:
XNIException
- Thrown by handler to signal an error.
-
endDocument
End document.- Specified by:
endDocument
in interfaceXMLDocumentHandler
- Parameters:
augs
- Additional information that may include infoset augmentations- Throws:
XNIException
- Thrown by handler to signal an error.
-
consumeBufferedEndElements
private void consumeBufferedEndElements()Consume elements that have been buffered, like
-