Class HTMLCollectionImpl
- All Implemented Interfaces:
org.w3c.dom.html.HTMLCollection
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:
-
Field Summary
FieldsModifier and TypeFieldDescription(package private) static final short
Request collection of all anchors in document: <A> elements that have aname
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 ahref
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
ConstructorsConstructorDescriptionHTMLCollectionImpl
(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 TypeMethodDescriptionprotected boolean
collectionMatch
(Element elem, String name) Determines if current element matches based on what we're looking for.final int
Returns the length of the collection.private int
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
Recursive function returns an element of a particular type with the specified name (id attribute).protected boolean
recurse()
Returns true if scanning methods should iterate through the collection.
-
Field Details
-
ANCHOR
static final short ANCHORRequest collection of all anchors in document: <A> elements that have aname
attribute.- See Also:
-
FORM
static final short FORMRequest collection of all forms in document: <FORM> elements.- See Also:
-
IMAGE
static final short IMAGERequest collection of all images in document: <IMG> elements.- See Also:
-
APPLET
static final short APPLETRequest collection of all Applets in document: <APPLET> and <OBJECT> elements (<OBJECT> must contain an Applet).- See Also:
-
LINK
static final short LINKRequest collection of all links in document: <A> and <AREA> elements (must have ahref
attribute).- See Also:
-
OPTION
static final short OPTIONRequest collection of all options in selection: <OPTION> elements in <SELECT> or <OPTGROUP>.- See Also:
-
ROW
static final short ROWRequest collection of all rows in table: <TR> elements in table or table section.- See Also:
-
ELEMENT
static final short ELEMENTRequest collection of all form elements: <INPUT>, <BUTTON>, <SELECT>, and <TEXTAREA> elements inside form <FORM>.- See Also:
-
AREA
static final short AREARequest collection of all areas in map: <AREA> element in <MAP> (non recursive).- See Also:
-
TBODY
static final short TBODYRequest collection of all table bodies in table: <TBODY> element in table <TABLE> (non recursive).- See Also:
-
CELL
static final short CELLRequest 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 bycollectionMatch(org.w3c.dom.Element, java.lang.String)
. Set by the constructor and determine the collection's use for its life time. -
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 existslookingFor
- 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 interfaceorg.w3c.dom.html.HTMLCollection
- Returns:
- Length of the collection
-
item
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 interfaceorg.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
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 interfaceorg.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
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
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 scanindex
- The index of the item to retreive- Returns:
- Number of elements
- See Also:
-
namedItem
Recursive function returns an element of a particular type with the specified name (id attribute).- Parameters:
topLevel
- Top level element from which to scanname
- 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
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 theid
attribute (matchname
first for anchors).- Parameters:
elem
- The current elementname
- The identifier name or null- Returns:
- The element matches what we're looking for
-