001: /**
002: * org/ozone-db/xml/dom/html/HTMLElementListImpl.java
003: *
004: * The contents of this file are subject to the OpenXML Public
005: * License Version 1.0; you may not use this file except in compliance
006: * with the License. You may obtain a copy of the License at
007: * http://www.openxml.org/license.html
008: *
009: * THIS SOFTWARE IS DISTRIBUTED ON AN "AS IS" BASIS WITHOUT WARRANTY
010: * OF ANY KIND, EITHER EXPRESSED OR IMPLIED. THE INITIAL DEVELOPER
011: * AND ALL CONTRIBUTORS SHALL NOT BE LIABLE FOR ANY DAMAGES AS A
012: * RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
013: * DERIVATIVES. SEE THE LICENSE FOR THE SPECIFIC LANGUAGE GOVERNING
014: * RIGHTS AND LIMITATIONS UNDER THE LICENSE.
015: *
016: * The Initial Developer of this code under the License is Assaf Arkin.
017: * Portions created by Assaf Arkin are Copyright (C) 1998, 1999.
018: * All Rights Reserved.
019: */package org.ozoneDB.xml.dom.html;
020:
021: import java.util.*;
022: import org.w3c.dom.*;
023:
024: /**
025: * Implements a list of HTML elements extracted based on their name attribute.
026: * The constructor recieves the root element and name. It then obtains all the
027: * elements contained within that element that match with their <code>name</code>
028: * attribute. The list is then accessible through the {@link #item} method.
029: * <P>
030: * The resulting list is a snapshot of the searched element at the time this
031: * list was created. Updates to the document tree are not reflected in the list.
032: * The list is implemented as a fast collection and access to elements in the
033: * list is very rapid.
034: *
035: *
036: * @version $Revision: 1.1 $ $Date: 2001/12/18 11:03:24 $
037: * @author <a href="mailto:arkin@trendline.co.il">Assaf Arkin</a>
038: * @see org.w3c.dom.NodeList
039: * @see HTMLElementImpl
040: * @see HTMLDocumentImpl
041: */
042: final class HTMLElementListImpl implements NodeList {
043:
044: public Node item(int index) {
045: return (Node) _elements.elementAt(index);
046: }
047:
048: public int getLength() {
049: return _elements.size();
050: }
051:
052: /**
053: * Add a single element to the list of elements.
054: *
055: * @param newElem The element to add
056: */
057: void addElement(Element newElem) {
058: _elements.addElement(newElem);
059: }
060:
061: /**
062: * Add all the elements contained in the root element and matching the
063: * <code>name</code> attribute. Each element is added by calling {@link
064: * #addElement} and the method is recursed on all sub-elements.
065: *
066: * @param element The root element from which to extract all sub elements
067: * @param name The <code>name</code> attribute to match
068: */
069: void addElements(Node element, String name) {
070: Node node;
071:
072: // Traverse all the child nodes of this element. Each node that is an
073: // element is added to the list if its name attribute matches and this
074: // method is recursed on that element.
075: node = element.getFirstChild();
076: while (node != null) {
077: if (node instanceof Element) {
078: if (((Element) node).getAttribute("name").equals(name)) {
079: addElement((Element) node);
080: }
081: addElements(node, name);
082: }
083: node = node.getNextSibling();
084: }
085: }
086:
087: /**
088: * Constructor receieves an element and <code>name</code> attribute value and
089: * extracts all the* sub elements based on that. After construction this object
090: * is ready for element retrieval.
091: *
092: * @param element The root element from which to extract all sub elements
093: * @param name The <code>name</code> attribute to match
094: */
095: HTMLElementListImpl(Node element, String name) {
096: if (name == null) {
097: throw new NullPointerException("Argument 'name' is null.");
098: }
099: _elements = new Vector();
100: addElements(element, name);
101: }
102:
103: /**
104: * Holds a list of all the matching elements. This list is accessed by
105: * {@link #item} and {@link #getLength}. Note that this list is not live,
106: * updates to the node tree are not reflected in this list.
107: */
108: private Vector _elements;
109:
110: }
|