KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > html > dom > NameNodeListImpl


1 /*
2  * Copyright 1999,2000,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.html.dom;
18
19 import org.apache.xerces.dom.DeepNodeListImpl;
20 import org.apache.xerces.dom.ElementImpl;
21 import org.apache.xerces.dom.NodeImpl;
22 import org.w3c.dom.Node JavaDoc;
23 import org.w3c.dom.NodeList JavaDoc;
24
25 /**
26  * This class implements the DOM's NodeList behavior for
27  * HTMLDocuemnt.getElementsByName().
28  *
29  * @xerces.internal
30  *
31  * @version $Id: NameNodeListImpl.java,v 1.8 2004/10/05 03:23:48 mrglavas Exp $
32  * @since PR-DOM-Level-1-19980818.
33  * @see DeepNodeListImpl
34  */

35 public class NameNodeListImpl
36     extends DeepNodeListImpl
37     implements NodeList JavaDoc {
38     
39     
40     /** Constructor. */
41     public NameNodeListImpl(NodeImpl rootNode, String JavaDoc tagName) {
42     super( rootNode, tagName );
43     }
44     
45
46     /**
47      * Iterative tree-walker. When you have a Parent link, there's often no
48      * need to resort to recursion. NOTE THAT only Element nodes are matched
49      * since we're specifically supporting getElementsByTagName().
50      */

51     protected Node JavaDoc nextMatchingElementAfter(Node JavaDoc current) {
52         
53         Node JavaDoc next;
54         while (current != null) {
55             // Look down to first child.
56
if (current.hasChildNodes()) {
57                 current = (current.getFirstChild());
58             }
59             
60             // Look right to sibling (but not from root!)
61
else if (current != rootNode && null != (next = current.getNextSibling())) {
62                 current = next;
63             }
64             
65             // Look up and right (but not past root!)
66
else {
67                 next = null;
68                 for (; current != rootNode; // Stop when we return to starting point
69
current = current.getParentNode()) {
70                     
71                     next = current.getNextSibling();
72                     if (next != null)
73                         break;
74                 }
75                 current = next;
76             }
77             
78             // Have we found an Element with the right tagName?
79
// ("*" matches anything.)
80
if (current != rootNode && current != null
81                 && current.getNodeType() == Node.ELEMENT_NODE ) {
82                 String JavaDoc name = ((ElementImpl) current).getAttribute( "name" );
83                 if ( name.equals("*") || name.equals(tagName))
84                     return current;
85             }
86             
87             // Otherwise continue walking the tree
88
}
89         
90         // Fell out of tree-walk; no more instances found
91
return null;
92         
93     } // nextMatchingElementAfter(int):Node
94

95 } // class NameNodeListImpl
96
Popular Tags