public class HTMLParser

  1. Object
  2. XMLParser
  3. HTMLParser
The HTMLParser class is used to parse an XHTML-MP 1.0 document into a DOM object (Element). Unsupported tags and attributes as well as comments are dropped in the parsing process. The parser is also makes use of CSSParser for external CSS files, embedded CSS segments and CSS within the ‘style’ attribute.

Constructors

public HTMLParser()Creates the legacy validating HTML parser.
public HTMLParser(boolean preserveUnsupportedAttributes)Creates an HTML parser, optionally retaining attributes that are not in the legacy HTMLElement schema.

Methods

protected void parseTagContent(Element element, Reader is) throws IOExceptionOverrides XMLParser.parseTagContent to enable embedded CSS segments (Style tags)
protected Element createNewElement(String name)Overrides XMLParser.createNewElement to return an HTMLElement instance
protected Element createNewTextElement(String text)Overrides XMLParser.createNewTextElement to return an HTMLElement instance
protected String convertCharEntity(String charEntity)Overrides XMLParser.convertCharEntity to add in HTML char entities
protected boolean isEmptyTag(String tagName)Checks whether the specified tag is an empty tag as defined in EMPTY_TAGS
public HTMLElement parseHTML(InputStreamReader isr)A convenience method that casts the returned type of the parse method to HTMLElement.
protected String getSupportedStandardName(){Returns a string identifying the document type this parser supports.
protected boolean isSupported(Element element)Overrides the Element.isSupported to let the parser know which tags are supported in XHTML-MP 1.0
protected boolean shouldEvaluate(Element element)Overrides the Element.shouldEvaluate method to return false on the script tag.

Inherited methods

Constructor details

HTMLParser

public HTMLParser()
Creates the legacy validating HTML parser.

HTMLParser

public HTMLParser(boolean preserveUnsupportedAttributes)
Creates an HTML parser, optionally retaining attributes that are not in the legacy HTMLElement schema. This is useful for modern HTML fragments containing data-* or ARIA metadata while preserving the historic validation behavior by default.

Parameters

preserveUnsupportedAttributes boolean
true to keep unknown attributes in the parsed DOM

Method details

parseTagContent

protected void parseTagContent(Element element, Reader is) throws IOException
Overrides XMLParser.parseTagContent to enable embedded CSS segments (Style tags)

Parameters

element Element
The current parent element
is Reader
The reader containing the XML

Throws

IOException
if an I/O error in the stream is encountered

createNewElement

protected Element createNewElement(String name)
Overrides XMLParser.createNewElement to return an HTMLElement instance

Parameters

name String
The HTMLElement’s name

Returns

a new instance of the names HTMLElement

createNewTextElement

protected Element createNewTextElement(String text)
Overrides XMLParser.createNewTextElement to return an HTMLElement instance

Parameters

text String
The HTMLElement’s text

Returns

a new instance of the HTMLElement

convertCharEntity

protected String convertCharEntity(String charEntity)
Overrides XMLParser.convertCharEntity to add in HTML char entities

Parameters

charEntity String
The char entity to convert

Returns

A string containing a single char, or the original char entity string (with & and ;) if the char entity couldn’t be resolved

isEmptyTag

protected boolean isEmptyTag(String tagName)
Checks whether the specified tag is an empty tag as defined in EMPTY_TAGS

Parameters

tagName String
The tag name to check

Returns

true if that tag is defined as an empty tag, false otherwise

parseHTML

public HTMLElement parseHTML(InputStreamReader isr)
A convenience method that casts the returned type of the parse method to HTMLElement. Basically calling this method is simlar to calling parse and casting to HTMLElement.

Parameters

isr InputStreamReader
The input stream containing the HTML

Returns

The HTML document

getSupportedStandardName

protected String getSupportedStandardName()
{Returns a string identifying the document type this parser supports. This should be overriden by subclassing parsers.}

Returns

a string identifying the document type this parser supports.

isSupported

protected boolean isSupported(Element element)
Overrides the Element.isSupported to let the parser know which tags are supported in XHTML-MP 1.0

Parameters

element Element
The element to check

Returns

true if the tag is a supported XHTML Mobile Profile 1.0 tag, false otherwise

shouldEvaluate

protected boolean shouldEvaluate(Element element)
Overrides the Element.shouldEvaluate method to return false on the script tag. The script tag should be skipped entirely, since it may contain characters like greater-than and lesser-than which may break the HTML All other tags are evaluated (i.e. added including all their children to the tree), even if not supported (But of course their functionality is ignored by HTMLComponent)

Parameters

element Element
The element to check

Returns

false if this is the SCRIPT tag, true otherwise