Package org.htmlunit.cyberneko
Class HTMLTagBalancer
- java.lang.Object
-
- org.htmlunit.cyberneko.HTMLTagBalancer
-
- All Implemented Interfaces:
HTMLComponent,XMLComponent,XMLDocumentFilter,XMLDocumentSource,XMLDocumentHandler
public class HTMLTagBalancer extends java.lang.Object implements XMLDocumentFilter, HTMLComponent
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:
HTMLElements
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classHTMLTagBalancer.ElementEntryStructure to hold information about an element placed in buffer to be comsumed laterstatic classHTMLTagBalancer.InfoElement info for each start element.static classHTMLTagBalancer.InfoStackUnsynchronized stack of element information.
-
Field Summary
Fields Modifier and Type Field Description protected static java.lang.StringAUGMENTATIONSInclude infoset augmentations.private java.util.List<java.lang.String>discardedStartElementsprotected static java.lang.StringDOCUMENT_FRAGMENTDocument fragment balancing only.private XMLDocumentHandlerdocumentHandler_The document handler.private XMLDocumentSourcedocumentSource_private java.util.List<HTMLTagBalancer.ElementEntry>endElementsBuffer_protected static java.lang.StringERROR_REPORTERError reporter.protected booleanfAllowSelfclosingIframeAllows self closing iframe tags.protected booleanfAllowSelfclosingTagsAllows self closing tags.protected booleanfAugmentationsInclude infoset augmentations.protected booleanfDocumentFragmentDocument fragment balancing only.protected HTMLTagBalancer.InfoStackfElementStackThe element stack.protected HTMLErrorReporterfErrorReporterError reporter.protected booleanfIgnoreOutsideContentIgnore outside content.protected HTMLTagBalancer.InfoStackfInlineStackThe inline stack.protected shortfNamesElemsModify HTML element names.protected booleanfNamespacesNamespaces.protected booleanfOpenedFormTrue if a form is in the stack (allow to discard opening of nested forms)protected booleanfOpenedSelectTrue if a select is in the stackprotected booleanfOpenedSvgTrue if a svg is in the stack (no parent checking takes place)private booleanforcedEndElement_private booleanforcedStartElement_private QNamefQNameA qualified name.static java.lang.StringFRAGMENT_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.private QName[]fragmentContextStack_Stack of elements determining the context in which a document fragment should be parsedprivate intfragmentContextStackSize_protected booleanfReportErrorsReport errors.protected booleanfSeenAnythingTrue if seen anything.protected booleanfSeenBodyElementTrue if seenbodyelement.private booleanfSeenBodyElementEndprivate booleanfSeenCharactersTrue if seen non whitespace characters.protected booleanfSeenDoctypeTrue if root element has been seen.private booleanfSeenFramesetElementTrue if seenframesetelement.protected booleanfSeenHeadElementTrue if seenheadelement.protected booleanfSeenRootElementTrue if root element has been seen.protected booleanfSeenRootElementEndTrue if seen the end of the document element.protected booleanfTemplateFragmentTemplate document fragment balancing only.private HTMLConfigurationhtmlConfiguration_protected static java.lang.StringIGNORE_OUTSIDE_CONTENTIgnore outside content.private LostTextlostText_protected static java.lang.StringNAMES_ATTRSModify HTML attribute names: { "upper", "lower", "default" }.protected static java.lang.StringNAMES_ELEMSModify HTML element names: { "upper", "lower", "default" }.private static shortNAMES_LOWERCASELowercase HTML names.private static shortNAMES_NO_CHANGEDon't modify HTML names.private static shortNAMES_UPPERCASEUppercase HTML names.protected static java.lang.StringNAMESPACESNamespaces.private static java.lang.String[]RECOGNIZED_FEATURESRecognized features.private static java.lang.Boolean[]RECOGNIZED_FEATURES_DEFAULTSRecognized features defaults.private static java.lang.String[]RECOGNIZED_PROPERTIESRecognized properties.private static java.lang.Object[]RECOGNIZED_PROPERTIES_DEFAULTSRecognized properties defaults.protected static java.lang.StringREPORT_ERRORSReport errors.private static HTMLEventInfoSYNTHESIZED_ITEMSynthesized event info item.protected HTMLTagBalancingListenertagBalancingListener
-
Constructor Summary
Constructors Constructor Description HTMLTagBalancer(HTMLConfiguration htmlConfiguration)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidaddBodyIfNeeded(short element)protected voidcallEndElement(QName element, Augmentations augs)protected voidcallStartElement(QName element, XMLAttributes attrs, Augmentations augs)voidcharacters(XMLString text, Augmentations augs)Characters.voidcomment(XMLString text, Augmentations augs)Comment.private voidconsumeBufferedEndElements()Consume elements that have been buffered, like that are first consumed at the end of documentprivate voidconsumeEarlyTextIfNeeded()private QNamecreateQName(java.lang.String tagName)voiddoctypeDecl(java.lang.String rootElementName, java.lang.String publicId, java.lang.String systemId, Augmentations augs)Doctype declaration.voidemptyElement(QName element, XMLAttributes attrs, Augmentations augs)Empty element.voidendCDATA(Augmentations augs)End CDATA section.voidendDocument(Augmentations augs)End document.voidendElement(QName element, Augmentations augs)End element.private voidforceStartBody()Generates a missing (which creates missing when needed)private booleanforceStartElement(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.XMLDocumentHandlergetDocumentHandler()Returns the document handler.XMLDocumentSourcegetDocumentSource()protected HTMLElements.ElementgetElement(QName elementName)protected intgetElementDepth(HTMLElements.Element element)java.lang.BooleangetFeatureDefault(java.lang.String featureId)Returns the default state for a feature.protected static shortgetNamesValue(java.lang.String value)protected intgetParentDepth(HTMLElements.Element[] parents, short bounds)java.lang.ObjectgetPropertyDefault(java.lang.String propertyId)Returns the default state for a property.java.lang.String[]getRecognizedFeatures()Returns recognized features.java.lang.String[]getRecognizedProperties()Returns recognized properties.protected static java.lang.StringmodifyName(java.lang.String name, short mode)private voidnotifyDiscardedEndElement(QName element, Augmentations augs)Notifies the tagBalancingListener (if any) of an ignored end elementprivate voidnotifyDiscardedStartElement(QName elem, XMLAttributes attrs, Augmentations augs)Notifies the tagBalancingListener (if any) of an ignored start elementvoidprocessingInstruction(java.lang.String target, XMLString data, Augmentations augs)Processing instruction.voidreset(XMLComponentManager manager)Resets the component.voidsetDocumentHandler(XMLDocumentHandler handler)Sets the document handler.voidsetDocumentSource(XMLDocumentSource source)Sets the document source.voidsetFeature(java.lang.String featureId, boolean state)Sets a feature.voidsetProperty(java.lang.String propertyId, java.lang.Object value)Sets a property.(package private) voidsetTagBalancingListener(HTMLTagBalancingListener tagBalancingListener)voidstartCDATA(Augmentations augs)Start CDATA section.voidstartDocument(XMLLocator locator, java.lang.String encoding, NamespaceContext nscontext, Augmentations augs)Start document.voidstartElement(QName elem, XMLAttributes attrs, Augmentations augs)Start element.protected AugmentationssynthesizedAugs()voidxmlDecl(java.lang.String version, java.lang.String encoding, java.lang.String standalone, Augmentations augs)XML declaration.
-
-
-
Field Detail
-
NAMESPACES
protected static final java.lang.String NAMESPACES
Namespaces.- See Also:
- Constant Field Values
-
AUGMENTATIONS
protected static final java.lang.String AUGMENTATIONS
Include infoset augmentations.- See Also:
- Constant Field Values
-
REPORT_ERRORS
protected static final java.lang.String REPORT_ERRORS
Report errors.- See Also:
- Constant Field Values
-
DOCUMENT_FRAGMENT
protected static final java.lang.String DOCUMENT_FRAGMENT
Document fragment balancing only.- See Also:
- Constant Field Values
-
IGNORE_OUTSIDE_CONTENT
protected static final java.lang.String IGNORE_OUTSIDE_CONTENT
Ignore outside content.- See Also:
- Constant Field Values
-
RECOGNIZED_FEATURES
private static final java.lang.String[] RECOGNIZED_FEATURES
Recognized features.
-
RECOGNIZED_FEATURES_DEFAULTS
private static final java.lang.Boolean[] RECOGNIZED_FEATURES_DEFAULTS
Recognized features defaults.
-
NAMES_ELEMS
protected static final java.lang.String NAMES_ELEMS
Modify HTML element names: { "upper", "lower", "default" }.- See Also:
- Constant Field Values
-
NAMES_ATTRS
protected static final java.lang.String NAMES_ATTRS
Modify HTML attribute names: { "upper", "lower", "default" }.- See Also:
- Constant Field Values
-
ERROR_REPORTER
protected static final java.lang.String ERROR_REPORTER
Error reporter.- See Also:
- Constant Field Values
-
FRAGMENT_CONTEXT_STACK
public static final java.lang.String 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:
- Constant Field Values
-
RECOGNIZED_PROPERTIES
private static final java.lang.String[] RECOGNIZED_PROPERTIES
Recognized properties.
-
RECOGNIZED_PROPERTIES_DEFAULTS
private static final java.lang.Object[] RECOGNIZED_PROPERTIES_DEFAULTS
Recognized properties defaults.
-
NAMES_NO_CHANGE
private static final short NAMES_NO_CHANGE
Don't modify HTML names.- See Also:
- Constant Field Values
-
NAMES_UPPERCASE
private static final short NAMES_UPPERCASE
Uppercase HTML names.- See Also:
- Constant Field Values
-
NAMES_LOWERCASE
private static final short NAMES_LOWERCASE
Lowercase HTML names.- See Also:
- Constant Field Values
-
SYNTHESIZED_ITEM
private static final HTMLEventInfo SYNTHESIZED_ITEM
Synthesized event info item.
-
fNamespaces
protected boolean fNamespaces
Namespaces.
-
fAugmentations
protected boolean fAugmentations
Include infoset augmentations.
-
fReportErrors
protected boolean fReportErrors
Report errors.
-
fDocumentFragment
protected boolean fDocumentFragment
Document fragment balancing only.
-
fTemplateFragment
protected boolean fTemplateFragment
Template document fragment balancing only.
-
fIgnoreOutsideContent
protected boolean fIgnoreOutsideContent
Ignore outside content.
-
fAllowSelfclosingIframe
protected boolean fAllowSelfclosingIframe
Allows self closing iframe tags.
-
fAllowSelfclosingTags
protected boolean fAllowSelfclosingTags
Allows self closing tags.
-
fNamesElems
protected short fNamesElems
Modify HTML element names.
-
fErrorReporter
protected HTMLErrorReporter fErrorReporter
Error reporter.
-
documentSource_
private XMLDocumentSource documentSource_
-
documentHandler_
private XMLDocumentHandler documentHandler_
The document handler.
-
fElementStack
protected final HTMLTagBalancer.InfoStack fElementStack
The element stack.
-
fInlineStack
protected final HTMLTagBalancer.InfoStack fInlineStack
The inline stack.
-
fSeenAnything
protected boolean fSeenAnything
True if seen anything. Important for xml declaration.
-
fSeenDoctype
protected boolean fSeenDoctype
True if root element has been seen.
-
fSeenRootElement
protected boolean fSeenRootElement
True if root element has been seen.
-
fSeenRootElementEnd
protected boolean fSeenRootElementEnd
True 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 fSeenHeadElement
True if seenheadelement.
-
fSeenBodyElement
protected boolean fSeenBodyElement
True if seenbodyelement.
-
fSeenBodyElementEnd
private boolean fSeenBodyElementEnd
-
fSeenFramesetElement
private boolean fSeenFramesetElement
True if seenframesetelement.
-
fSeenCharacters
private boolean fSeenCharacters
True if seen non whitespace characters.
-
fOpenedForm
protected boolean fOpenedForm
True if a form is in the stack (allow to discard opening of nested forms)
-
fOpenedSvg
protected boolean fOpenedSvg
True if a svg is in the stack (no parent checking takes place)
-
fOpenedSelect
protected boolean fOpenedSelect
True if a select is in the stack
-
fQName
private final QName fQName
A qualified name.
-
tagBalancingListener
protected HTMLTagBalancingListener tagBalancingListener
-
lostText_
private final LostText lostText_
-
forcedStartElement_
private boolean forcedStartElement_
-
forcedEndElement_
private boolean forcedEndElement_
-
fragmentContextStack_
private QName[] fragmentContextStack_
Stack of elements determining the context in which a document fragment should be parsed
-
fragmentContextStackSize_
private int fragmentContextStackSize_
-
endElementsBuffer_
private final java.util.List<HTMLTagBalancer.ElementEntry> endElementsBuffer_
-
discardedStartElements
private final java.util.List<java.lang.String> discardedStartElements
-
htmlConfiguration_
private final HTMLConfiguration htmlConfiguration_
-
-
Constructor Detail
-
HTMLTagBalancer
HTMLTagBalancer(HTMLConfiguration htmlConfiguration)
-
-
Method Detail
-
getFeatureDefault
public java.lang.Boolean getFeatureDefault(java.lang.String featureId)
Returns the default state for a feature.- Specified by:
getFeatureDefaultin interfaceHTMLComponent- Specified by:
getFeatureDefaultin 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
public java.lang.Object getPropertyDefault(java.lang.String propertyId)
Returns the default state for a property.- Specified by:
getPropertyDefaultin interfaceHTMLComponent- Specified by:
getPropertyDefaultin 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
public java.lang.String[] getRecognizedFeatures()
Returns recognized features.- Specified by:
getRecognizedFeaturesin 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
public java.lang.String[] getRecognizedProperties()
Returns recognized properties.- Specified by:
getRecognizedPropertiesin 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
public void reset(XMLComponentManager manager) throws XMLConfigurationException
Resets the component.- Specified by:
resetin interfaceXMLComponent- Parameters:
manager- The component manager.- Throws:
XMLConfigurationException
-
setFeature
public void setFeature(java.lang.String featureId, boolean state) throws XMLConfigurationExceptionSets a feature.- Specified by:
setFeaturein 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
public void setProperty(java.lang.String propertyId, java.lang.Object value) throws XMLConfigurationExceptionSets a property.- Specified by:
setPropertyin 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
public void setDocumentHandler(XMLDocumentHandler handler)
Sets the document handler.- Specified by:
setDocumentHandlerin interfaceXMLDocumentSource- Parameters:
handler- the new handler
-
getDocumentHandler
public XMLDocumentHandler getDocumentHandler()
Returns the document handler.- Specified by:
getDocumentHandlerin interfaceXMLDocumentSource- Returns:
- the document handler
-
setDocumentSource
public void setDocumentSource(XMLDocumentSource source)
Sets the document source.- Specified by:
setDocumentSourcein interfaceXMLDocumentHandler- Parameters:
source- the new source
-
getDocumentSource
public XMLDocumentSource getDocumentSource()
- Specified by:
getDocumentSourcein interfaceXMLDocumentHandler- Returns:
- the document source.
-
startDocument
public void startDocument(XMLLocator locator, java.lang.String encoding, NamespaceContext nscontext, Augmentations augs) throws XNIException
Start document.- Specified by:
startDocumentin 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(java.lang.String version, java.lang.String encoding, java.lang.String standalone, Augmentations augs) throws XNIExceptionXML declaration.- Specified by:
xmlDeclin 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(java.lang.String rootElementName, java.lang.String publicId, java.lang.String systemId, Augmentations augs) throws XNIExceptionDoctype declaration.- Specified by:
doctypeDeclin 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
public void endDocument(Augmentations augs) throws XNIException
End document.- Specified by:
endDocumentin 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
-
-