KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > w3c > tidy > DOMNodeListByTagNameImpl


1 /*
2  * @(#)DOMNodeListByTagNameImpl.java 1.11 2000/08/16
3  *
4  */

5
6 package org.w3c.tidy;
7
8 /**
9  *
10  * DOMNodeListByTagNameImpl
11  *
12  * (c) 1998-2000 (W3C) MIT, INRIA, Keio University
13  * See Tidy.java for the copyright notice.
14  * Derived from <a HREF="http://www.w3.org/People/Raggett/tidy">
15  * HTML Tidy Release 4 Aug 2000</a>
16  *
17  * @author Dave Raggett <dsr@w3.org>
18  * @author Andy Quick <ac.quick@sympatico.ca> (translation to Java)
19  * @version 1.4, 1999/09/04 DOM support
20  * @version 1.5, 1999/10/23 Tidy Release 27 Sep 1999
21  * @version 1.6, 1999/11/01 Tidy Release 22 Oct 1999
22  * @version 1.7, 1999/12/06 Tidy Release 30 Nov 1999
23  * @version 1.8, 2000/01/22 Tidy Release 13 Jan 2000
24  * @version 1.9, 2000/06/03 Tidy Release 30 Apr 2000
25  * @version 1.10, 2000/07/22 Tidy Release 8 Jul 2000
26  * @version 1.11, 2000/08/16 Tidy Release 4 Aug 2000
27  */

28
29 /**
30  * <p>The items in the <code>NodeList</code> are accessible via an integral
31  * index, starting from 0.
32  *
33  */

34 public class DOMNodeListByTagNameImpl implements org.w3c.dom.NodeList JavaDoc {
35
36     private Node first = null;
37     private String JavaDoc tagName = "*";
38     private int currIndex = 0;
39     private int maxIndex = 0;
40     private Node currNode = null;
41
42     protected DOMNodeListByTagNameImpl(Node first, String JavaDoc tagName)
43     {
44         this.first = first;
45         this.tagName = tagName;
46     }
47
48     /**
49      * @see org.w3c.dom.NodeList#item
50      */

51     public org.w3c.dom.Node JavaDoc item(int index)
52     {
53         currIndex = 0;
54         maxIndex = index;
55         preTraverse(first);
56
57         if (currIndex > maxIndex && currNode != null)
58             return currNode.getAdapter();
59         else
60             return null;
61     }
62
63     /**
64      * @see org.w3c.dom.NodeList#getLength
65      */

66     public int getLength()
67     {
68         currIndex = 0;
69         maxIndex = Integer.MAX_VALUE;
70         preTraverse(first);
71         return currIndex;
72     }
73
74     protected void preTraverse(Node node)
75     {
76         if (node == null)
77             return;
78
79         if (node.type == Node.StartTag || node.type == Node.StartEndTag)
80         {
81             if (currIndex <= maxIndex &&
82                 (tagName.equals("*") || tagName.equals(node.element)))
83             {
84                 currIndex += 1;
85                 currNode = node;
86             }
87         }
88         if (currIndex > maxIndex)
89             return;
90
91         node = node.content;
92         while (node != null)
93         {
94             preTraverse(node);
95             node = node.next;
96         }
97     }
98
99 }
100
Popular Tags