01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: package org.apache.html.dom;
19:
20: import org.apache.xerces.dom.DeepNodeListImpl;
21: import org.apache.xerces.dom.ElementImpl;
22: import org.apache.xerces.dom.NodeImpl;
23: import org.w3c.dom.Node;
24: import org.w3c.dom.NodeList;
25:
26: /**
27: * This class implements the DOM's NodeList behavior for
28: * HTMLDocuemnt.getElementsByName().
29: *
30: * @xerces.internal
31: *
32: * @version $Id: NameNodeListImpl.java 447255 2006-09-18 05:36:42Z mrglavas $
33: * @since PR-DOM-Level-1-19980818.
34: * @see DeepNodeListImpl
35: */
36: public class NameNodeListImpl extends DeepNodeListImpl implements
37: NodeList {
38:
39: /** Constructor. */
40: public NameNodeListImpl(NodeImpl rootNode, String tagName) {
41: super (rootNode, tagName);
42: }
43:
44: /**
45: * Iterative tree-walker. When you have a Parent link, there's often no
46: * need to resort to recursion. NOTE THAT only Element nodes are matched
47: * since we're specifically supporting getElementsByTagName().
48: */
49: protected Node nextMatchingElementAfter(Node current) {
50:
51: Node next;
52: while (current != null) {
53: // Look down to first child.
54: if (current.hasChildNodes()) {
55: current = (current.getFirstChild());
56: }
57:
58: // Look right to sibling (but not from root!)
59: else if (current != rootNode
60: && null != (next = current.getNextSibling())) {
61: current = next;
62: }
63:
64: // Look up and right (but not past root!)
65: else {
66: next = null;
67: for (; current != rootNode; // Stop when we return to starting point
68: current = current.getParentNode()) {
69:
70: next = current.getNextSibling();
71: if (next != null)
72: break;
73: }
74: current = next;
75: }
76:
77: // Have we found an Element with the right tagName?
78: // ("*" matches anything.)
79: if (current != rootNode && current != null
80: && current.getNodeType() == Node.ELEMENT_NODE) {
81: String name = ((ElementImpl) current)
82: .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
|