Class HTMLCollectionImpl

java.lang.Object
org.htmlunit.cyberneko.html.dom.HTMLCollectionImpl
All Implemented Interfaces:
org.w3c.dom.html.HTMLCollection

class HTMLCollectionImpl extends Object implements org.w3c.dom.html.HTMLCollection
Implements HTMLCollection to traverse any named elements on a HTMLDocument. The elements type to look for is identified in the constructor by code. This collection is not optimized for traversing large trees.

The collection has to meet two requirements: it has to be live, and it has to traverse depth first and always return results in that order. As such, using an object container (such as Vector) is expensive on insert/remove operations. Instead, the collection has been implemented using three traversing functions. As a result, operations on large documents will result in traversal of the entire document tree and consume a considerable amount of time.

Note that synchronization on the traversed document cannot be achieved. The document itself cannot be locked, and locking each traversed node is likely to lead to a dead lock condition. Therefore, there is a chance of the document being changed as results are fetched; in all likelihood, the results might be out dated, but not erroneous.

See Also:
  • HTMLCollection
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    (package private) static final short
    Request collection of all anchors in document: <A> elements that have a name attribute.
    (package private) static final short
    Request collection of all Applets in document: <APPLET> and <OBJECT> elements (<OBJECT> must contain an Applet).
    (package private) static final short
    Request collection of all areas in map: <AREA> element in <MAP> (non recursive).
    (package private) static final short
    Request collection of all cells in row: <TD> and <TH> elements in <TR> (non recursive).
    (package private) static final short
    Request collection of all form elements: <INPUT>, <BUTTON>, <SELECT>, and <TEXTAREA> elements inside form <FORM>.
    (package private) static final short
    Request collection of all forms in document: <FORM> elements.
    (package private) static final short
    Request collection of all images in document: <IMG> elements.
    (package private) static final short
    Request collection of all links in document: <A> and <AREA> elements (must have a href attribute).
    private final short
    Indicates what this collection is looking for.
    (package private) static final short
    Request collection of all options in selection: <OPTION> elements in <SELECT> or <OPTGROUP>.
    (package private) static final short
    Request collection of all rows in table: <TR> elements in table or table section.
    (package private) static final short
    Request collection of all table bodies in table: <TBODY> element in table <TABLE> (non recursive).
    private final Element
    This is the top level element underneath which the collection exists.
  • Constructor Summary

    Constructors
    Constructor
    Description
    HTMLCollectionImpl(org.w3c.dom.html.HTMLElement topLevel, short lookingFor)
    Construct a new collection that retrieves element of the specific type (lookingFor) from the specific document portion (topLevel).
  • Method Summary

    Modifier and Type
    Method
    Description
    protected boolean
    Determines if current element matches based on what we're looking for.
    final int
    Returns the length of the collection.
    private int
    getLength(Element topLevel)
    Recursive function returns the number of elements of a particular type that exist under the top level element.
    final Node
    item(int index)
    Retrieves the indexed node from the collection.
    private Node
    item(Element topLevel, CollectionIndex index)
    Recursive function returns the numbered element of a particular type that exist under the top level element.
    final Node
    Retrieves the named node from the collection.
    private Node
    namedItem(Element topLevel, String name)
    Recursive function returns an element of a particular type with the specified name (id attribute).
    protected boolean
    Returns true if scanning methods should iterate through the collection.

    Methods inherited from class java.lang.Object

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

    • ANCHOR

      static final short ANCHOR
      Request collection of all anchors in document: <A> elements that have a name attribute.
      See Also:
    • FORM

      static final short FORM
      Request collection of all forms in document: <FORM> elements.
      See Also:
    • IMAGE

      static final short IMAGE
      Request collection of all images in document: <IMG> elements.
      See Also:
    • APPLET

      static final short APPLET
      Request collection of all Applets in document: <APPLET> and <OBJECT> elements (<OBJECT> must contain an Applet).
      See Also:
    • OPTION

      static final short OPTION
      Request collection of all options in selection: <OPTION> elements in <SELECT> or <OPTGROUP>.
      See Also:
    • ROW

      static final short ROW
      Request collection of all rows in table: <TR> elements in table or table section.
      See Also:
    • ELEMENT

      static final short ELEMENT
      Request collection of all form elements: <INPUT>, <BUTTON>, <SELECT>, and <TEXTAREA> elements inside form <FORM>.
      See Also:
    • AREA

      static final short AREA
      Request collection of all areas in map: <AREA> element in <MAP> (non recursive).
      See Also:
    • TBODY

      static final short TBODY
      Request collection of all table bodies in table: <TBODY> element in table <TABLE> (non recursive).
      See Also:
    • CELL

      static final short CELL
      Request collection of all cells in row: <TD> and <TH> elements in <TR> (non recursive).
      See Also:
    • lookingFor_

      private final short lookingFor_
      Indicates what this collection is looking for. Holds one of the enumerated values and used by collectionMatch(org.w3c.dom.Element, java.lang.String). Set by the constructor and determine the collection's use for its life time.
    • topLevel_

      private final Element topLevel_
      This is the top level element underneath which the collection exists.
  • Constructor Details

    • HTMLCollectionImpl

      HTMLCollectionImpl(org.w3c.dom.html.HTMLElement topLevel, short lookingFor)
      Construct a new collection that retrieves element of the specific type (lookingFor) from the specific document portion (topLevel).
      Parameters:
      topLevel - The element underneath which the collection exists
      lookingFor - Code indicating what elements to look for
  • Method Details

    • getLength

      public final int getLength()
      Returns the length of the collection. This method might traverse the entire document tree.
      Specified by:
      getLength in interface org.w3c.dom.html.HTMLCollection
      Returns:
      Length of the collection
    • item

      public final Node item(int index)
      Retrieves the indexed node from the collection. Nodes are numbered in tree order - depth-first traversal order. This method might traverse the entire document tree.
      Specified by:
      item in interface org.w3c.dom.html.HTMLCollection
      Parameters:
      index - The index of the node to return
      Returns:
      The specified node or null if no such node found
    • namedItem

      public final Node namedItem(String name)
      Retrieves the named node from the collection. The name is matched case sensitive against the id attribute of each element in the collection, returning the first match. The tree is traversed in depth-first order. This method might traverse the entire document tree.
      Specified by:
      namedItem in interface org.w3c.dom.html.HTMLCollection
      Parameters:
      name - The name of the node to return
      Returns:
      The specified node or null if no such node found
    • getLength

      private int getLength(Element topLevel)
      Recursive function returns the number of elements of a particular type that exist under the top level element. This is a recursive function and the top level element is passed along.
      Parameters:
      topLevel - Top level element from which to scan
      Returns:
      Number of elements
    • item

      private Node item(Element topLevel, CollectionIndex index)
      Recursive function returns the numbered element of a particular type that exist under the top level element. This is a recursive function and the top level element is passed along.

      Note that this function must call itself with an index and get back both the element (if one was found) and the new index which is decremeneted for any like element found. Since integers are only passed by value, this function makes use of a separate class (CollectionIndex) to hold that index.

      Parameters:
      topLevel - Top level element from which to scan
      index - The index of the item to retreive
      Returns:
      Number of elements
      See Also:
    • namedItem

      private Node namedItem(Element topLevel, String name)
      Recursive function returns an element of a particular type with the specified name (id attribute).
      Parameters:
      topLevel - Top level element from which to scan
      name - The named element to look for
      Returns:
      The first named element found
    • recurse

      protected boolean recurse()
      Returns true if scanning methods should iterate through the collection. When looking for elements in the document, recursing is needed to traverse the full document tree. When looking inside a specific element (e.g. for a cell inside a row), recursing can lead to erroneous results.
      Returns:
      True if methods should recurse to traverse entire tree
    • collectionMatch

      protected boolean collectionMatch(Element elem, String name)
      Determines if current element matches based on what we're looking for. The element is passed along with an optional identifier name. If the element is the one we're looking for, return true. If the name is also specified, the name must match the id attribute (match name first for anchors).
      Parameters:
      elem - The current element
      name - The identifier name or null
      Returns:
      The element matches what we're looking for