KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > xml > dom > html > HTMLElementListImpl


1 /**
2  * org/ozone-db/xml/dom/html/HTMLElementListImpl.java
3  *
4  * The contents of this file are subject to the OpenXML Public
5  * License Version 1.0; you may not use this file except in compliance
6  * with the License. You may obtain a copy of the License at
7  * http://www.openxml.org/license.html
8  *
9  * THIS SOFTWARE IS DISTRIBUTED ON AN "AS IS" BASIS WITHOUT WARRANTY
10  * OF ANY KIND, EITHER EXPRESSED OR IMPLIED. THE INITIAL DEVELOPER
11  * AND ALL CONTRIBUTORS SHALL NOT BE LIABLE FOR ANY DAMAGES AS A
12  * RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
13  * DERIVATIVES. SEE THE LICENSE FOR THE SPECIFIC LANGUAGE GOVERNING
14  * RIGHTS AND LIMITATIONS UNDER THE LICENSE.
15  *
16  * The Initial Developer of this code under the License is Assaf Arkin.
17  * Portions created by Assaf Arkin are Copyright (C) 1998, 1999.
18  * All Rights Reserved.
19  */

20
21
22 package org.ozoneDB.xml.dom.html;
23
24
25 import org.w3c.dom.Element JavaDoc;
26 import org.w3c.dom.Node JavaDoc;
27 import org.w3c.dom.NodeList JavaDoc;
28
29 import java.util.Vector JavaDoc;
30
31
32 /**
33  * Implements a list of HTML elements extracted based on their name attribute.
34  * The constructor recieves the root element and name. It then obtains all the
35  * elements contained within that element that match with their <code>name</code>
36  * attribute. The list is then accessible through the {@link #item} method.
37  * <P>
38  * The resulting list is a snapshot of the searched element at the time this
39  * list was created. Updates to the document tree are not reflected in the list.
40  * The list is implemented as a fast collection and access to elements in the
41  * list is very rapid.
42  *
43  *
44  * @version $Revision: 1.2 $ $Date: 2003/11/20 23:18:42 $
45  * @author <a HREF="mailto:arkin@trendline.co.il">Assaf Arkin</a>
46  * @see org.w3c.dom.NodeList
47  * @see HTMLElementImpl
48  * @see HTMLDocumentImpl
49  */

50 final class HTMLElementListImpl implements NodeList JavaDoc {
51
52
53     public Node JavaDoc item( int index ) {
54         return (Node JavaDoc)_elements.elementAt( index );
55     }
56
57
58     public int getLength() {
59         return _elements.size();
60     }
61
62
63     /**
64      * Add a single element to the list of elements.
65      *
66      * @param newElem The element to add
67      */

68     void addElement( Element JavaDoc newElem ) {
69         _elements.addElement( newElem );
70     }
71
72
73     /**
74      * Add all the elements contained in the root element and matching the
75      * <code>name</code> attribute. Each element is added by calling {@link
76      * #addElement} and the method is recursed on all sub-elements.
77      *
78      * @param element The root element from which to extract all sub elements
79      * @param name The <code>name</code> attribute to match
80      */

81     void addElements( Node JavaDoc element, String JavaDoc name ) {
82         Node JavaDoc node;
83
84         // Traverse all the child nodes of this element. Each node that is an
85
// element is added to the list if its name attribute matches and this
86
// method is recursed on that element.
87
node = element.getFirstChild();
88         while (node != null) {
89             if (node instanceof Element JavaDoc) {
90                 if (((Element JavaDoc)node).getAttribute( "name" ).equals( name )) {
91                     addElement( (Element JavaDoc)node );
92                 }
93                 addElements( node, name );
94             }
95             node = node.getNextSibling();
96         }
97     }
98
99
100     /**
101      * Constructor receieves an element and <code>name</code> attribute value and
102      * extracts all the* sub elements based on that. After construction this object
103      * is ready for element retrieval.
104      *
105      * @param element The root element from which to extract all sub elements
106      * @param name The <code>name</code> attribute to match
107      */

108     HTMLElementListImpl( Node JavaDoc element, String JavaDoc name ) {
109         if (name == null) {
110             throw new NullPointerException JavaDoc( "Argument 'name' is null." );
111         }
112         _elements = new Vector JavaDoc();
113         addElements( element, name );
114     }
115
116
117     /**
118      * Holds a list of all the matching elements. This list is accessed by
119      * {@link #item} and {@link #getLength}. Note that this list is not live,
120      * updates to the node tree are not reflected in this list.
121      */

122     private Vector JavaDoc _elements;
123
124
125 }
126
Popular Tags