Class HtmlParser

java.lang.Object
org.libxmq.imp.xml.HtmlParser

public final class HtmlParser extends Object
An html parser that builds the same tree as libxml2 (2.16) htmlReadMemory does with the options libxmq uses (HTML_PARSE_NOERROR | HTML_PARSE_NOWARNING | HTML_PARSE_NONET, optionally HTML_PARSE_NOBLANKS, encoding UTF-8): html5 tokenization, libxml2's tree construction with implied html/head/body elements and implicit closing of elements, a default html 4.0 transitional doctype if none is given. A port of the C version src/main/c/parts/xml_html_parser.c. Element names are lowercased, there are no namespaces. Character references and html named character references (like  ) are decoded. Raw text (script, style...) is stored as cdata nodes, like libxml2. Like libxml2 the parser works on the UTF-8 bytes of the input, which gives the same chunking of text, which affects the blank detection. Errors are never reported (like xmq which parses without a message callback), the parser always recovers.
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    Check if an attribute is an html boolean attribute, like libxml2 htmlIsBooleanAttr.
    parse(byte[] content, boolean noblanks)
    Parse html content (UTF-8 bytes, invalid sequences become U+FFFD) into a document.
    parse(String content, boolean noblanks)
    Parse html content into a document, like libxml2 htmlReadMemory with the options HTML_PARSE_NOERROR | HTML_PARSE_NOWARNING | HTML_PARSE_NONET.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • parse

      public static XmlDocument parse(String content, boolean noblanks)
      Parse html content into a document, like libxml2 htmlReadMemory with the options HTML_PARSE_NOERROR | HTML_PARSE_NOWARNING | HTML_PARSE_NONET.
      Parameters:
      content - The html source.
      noblanks - If true then also HTML_PARSE_NOBLANKS is used, ie ignorable whitespace text nodes are dropped.
      Returns:
      The html document, without a root element if there was no content.
    • parse

      public static XmlDocument parse(byte[] content, boolean noblanks)
      Parse html content (UTF-8 bytes, invalid sequences become U+FFFD) into a document.
      Parameters:
      content - The html source.
      noblanks - If true then ignorable whitespace text nodes are dropped.
      Returns:
      The html document, without a root element if there was no content.
    • isBooleanAttr

      public static boolean isBooleanAttr(String name)
      Check if an attribute is an html boolean attribute, like libxml2 htmlIsBooleanAttr.
      Parameters:
      name - The attribute name.
      Returns:
      True if the attribute is boolean (checked, selected...).