author | joehw |
Wed, 18 Oct 2017 13:25:49 -0700 | |
changeset 47359 | e1a6c0168741 |
parent 47216 | 71c04702a3d5 |
child 48409 | 5ab69533994b |
permissions | -rw-r--r-- |
6 | 1 |
/* |
47359
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
3 |
* @LastModified: Oct 2017 |
6 | 4 |
*/ |
5 |
/* |
|
44797
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
6 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
7 |
* contributor license agreements. See the NOTICE file distributed with |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
8 |
* this work for additional information regarding copyright ownership. |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
9 |
* The ASF licenses this file to You under the Apache License, Version 2.0 |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
10 |
* (the "License"); you may not use this file except in compliance with |
8b3b3b911b8a
8162572: Update License Header for all JAXP sources
joehw
parents:
25868
diff
changeset
|
11 |
* the License. You may obtain a copy of the License at |
6 | 12 |
* |
13 |
* http://www.apache.org/licenses/LICENSE-2.0 |
|
14 |
* |
|
15 |
* Unless required by applicable law or agreed to in writing, software |
|
16 |
* distributed under the License is distributed on an "AS IS" BASIS, |
|
17 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
18 |
* See the License for the specific language governing permissions and |
|
19 |
* limitations under the License. |
|
20 |
*/ |
|
21 |
package com.sun.org.apache.xerces.internal.dom; |
|
22 |
||
47359
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
23 |
import java.util.ArrayList; |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
24 |
import java.util.List; |
6 | 25 |
import org.w3c.dom.Node; |
26 |
import org.w3c.dom.NodeList; |
|
27 |
||
28 |
/** |
|
29 |
* This class implements the DOM's NodeList behavior for |
|
30 |
* Element.getElementsByTagName() |
|
31 |
* <P> |
|
32 |
* The DOM describes NodeList as follows: |
|
33 |
* <P> |
|
34 |
* 1) It may represent EITHER nodes scattered through a subtree (when |
|
35 |
* returned by Element.getElementsByTagName), or just the immediate |
|
36 |
* children (when returned by Node.getChildNodes). The latter is easy, |
|
37 |
* but the former (which this class addresses) is more challenging. |
|
38 |
* <P> |
|
39 |
* 2) Its behavior is "live" -- that is, it always reflects the |
|
40 |
* current state of the document tree. To put it another way, the |
|
41 |
* NodeLists obtained before and after a series of insertions and |
|
42 |
* deletions are effectively identical (as far as the user is |
|
43 |
* concerned, the former has been dynamically updated as the changes |
|
44 |
* have been made). |
|
45 |
* <P> |
|
46 |
* 3) Its API accesses individual nodes via an integer index, with the |
|
47 |
* listed nodes numbered sequentially in the order that they were |
|
48 |
* found during a preorder depth-first left-to-right search of the tree. |
|
49 |
* (Of course in the case of getChildNodes, depth is not involved.) As |
|
50 |
* nodes are inserted or deleted in the tree, and hence the NodeList, |
|
51 |
* the numbering of nodes that follow them in the NodeList will |
|
52 |
* change. |
|
53 |
* <P> |
|
54 |
* It is rather painful to support the latter two in the |
|
55 |
* getElementsByTagName case. The current solution is for Nodes to |
|
56 |
* maintain a change count (eventually that may be a Digest instead), |
|
57 |
* which the NodeList tracks and uses to invalidate itself. |
|
58 |
* <P> |
|
59 |
* Unfortunately, this does _not_ respond efficiently in the case that |
|
60 |
* the dynamic behavior was supposed to address: scanning a tree while |
|
61 |
* it is being extended. That requires knowing which subtrees have |
|
62 |
* changed, which can become an arbitrarily complex problem. |
|
63 |
* <P> |
|
64 |
* We save some work by filling the vector only as we access the |
|
65 |
* item()s... but I suspect the same users who demanded index-based |
|
66 |
* access will also start by doing a getLength() to control their loop, |
|
67 |
* blowing this optimization out of the water. |
|
68 |
* <P> |
|
69 |
* NOTE: Level 2 of the DOM will probably _not_ use NodeList for its |
|
70 |
* extended search mechanisms, partly for the reasons just discussed. |
|
71 |
* |
|
72 |
* @xerces.internal |
|
73 |
* |
|
74 |
* @since PR-DOM-Level-1-19980818. |
|
75 |
*/ |
|
76 |
public class DeepNodeListImpl |
|
47359
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
77 |
implements NodeList { |
6 | 78 |
|
79 |
// |
|
80 |
// Data |
|
81 |
// |
|
82 |
protected NodeImpl rootNode; // Where the search started |
|
83 |
protected String tagName; // Or "*" to mean all-tags-acceptable |
|
47359
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
84 |
protected int changes = 0; |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
85 |
protected List<Node> nodes; |
6 | 86 |
|
87 |
protected String nsName; |
|
88 |
protected boolean enableNS = false; |
|
89 |
||
90 |
// |
|
91 |
// Constructors |
|
92 |
// |
|
93 |
||
94 |
/** Constructor. */ |
|
95 |
public DeepNodeListImpl(NodeImpl rootNode, String tagName) { |
|
96 |
this.rootNode = rootNode; |
|
47359
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
97 |
this.tagName = tagName; |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
98 |
nodes = new ArrayList<>(); |
6 | 99 |
} |
100 |
||
101 |
/** Constructor for Namespace support. */ |
|
102 |
public DeepNodeListImpl(NodeImpl rootNode, |
|
47359
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
103 |
String nsName, String tagName) { |
6 | 104 |
this(rootNode, tagName); |
47359
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
105 |
this.nsName = (nsName != null && nsName.length() != 0) ? nsName : null; |
6 | 106 |
enableNS = true; |
107 |
} |
|
108 |
||
109 |
// |
|
110 |
// NodeList methods |
|
111 |
// |
|
112 |
||
113 |
/** Returns the length of the node list. */ |
|
114 |
public int getLength() { |
|
115 |
// Preload all matching elements. (Stops when we run out of subtree!) |
|
116 |
item(java.lang.Integer.MAX_VALUE); |
|
117 |
return nodes.size(); |
|
118 |
} |
|
119 |
||
120 |
/** Returns the node at the specified index. */ |
|
121 |
public Node item(int index) { |
|
122 |
Node thisNode; |
|
123 |
||
124 |
// Tree changed. Do it all from scratch! |
|
47359
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
125 |
if (rootNode.changes() != changes) { |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
126 |
nodes = new ArrayList<>(); |
6 | 127 |
changes = rootNode.changes(); |
128 |
} |
|
129 |
||
130 |
// In the cache |
|
47359
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
131 |
final int currentSize = nodes.size(); |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
132 |
if (index < currentSize) { |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
133 |
return nodes.get(index); |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
134 |
} // Not yet seen |
6 | 135 |
else { |
136 |
||
137 |
// Pick up where we left off (Which may be the beginning) |
|
47359
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
138 |
if (currentSize == 0) { |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
139 |
thisNode = rootNode; |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
140 |
} else { |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
141 |
thisNode = (NodeImpl) (nodes.get(currentSize - 1)); |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
142 |
} |
6 | 143 |
|
47359
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
144 |
// Add nodes up to the one we're looking for |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
145 |
while (thisNode != null && index >= nodes.size()) { |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
146 |
thisNode = nextMatchingElementAfter(thisNode); |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
147 |
if (thisNode != null) { |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
148 |
nodes.add(thisNode); |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
149 |
} |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
150 |
} |
6 | 151 |
|
152 |
// Either what we want, or null (not avail.) |
|
47359
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
153 |
return thisNode; |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
154 |
} |
6 | 155 |
|
156 |
} // item(int):Node |
|
157 |
||
158 |
// |
|
159 |
// Protected methods (might be overridden by an extending DOM) |
|
160 |
// |
|
161 |
||
162 |
/** |
|
163 |
* Iterative tree-walker. When you have a Parent link, there's often no |
|
164 |
* need to resort to recursion. NOTE THAT only Element nodes are matched |
|
165 |
* since we're specifically supporting getElementsByTagName(). |
|
166 |
*/ |
|
167 |
protected Node nextMatchingElementAfter(Node current) { |
|
168 |
||
47359
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
169 |
Node next; |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
170 |
while (current != null) { |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
171 |
// Look down to first child. |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
172 |
if (current.hasChildNodes()) { |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
173 |
current = (current.getFirstChild()); |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
174 |
} // Look right to sibling (but not from root!) |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
175 |
else if (current != rootNode && null != (next = current.getNextSibling())) { |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
176 |
current = next; |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
177 |
} // Look up and right (but not past root!) |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
178 |
else { |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
179 |
next = null; |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
180 |
for (; current != rootNode; // Stop when we return to starting point |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
181 |
current = current.getParentNode()) { |
6 | 182 |
|
47359
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
183 |
next = current.getNextSibling(); |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
184 |
if (next != null) { |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
185 |
break; |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
186 |
} |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
187 |
} |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
188 |
current = next; |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
189 |
} |
6 | 190 |
|
191 |
// Have we found an Element with the right tagName? |
|
47359
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
192 |
// ("*" matches anything.) |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
193 |
if (current != rootNode |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
194 |
&& current != null |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
195 |
&& current.getNodeType() == Node.ELEMENT_NODE) { |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
196 |
if (!enableNS) { |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
197 |
if (tagName.equals("*") |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
198 |
|| ((ElementImpl) current).getTagName().equals(tagName)) { |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
199 |
return current; |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
200 |
} |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
201 |
} else { |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
202 |
// DOM2: Namespace logic. |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
203 |
if (tagName.equals("*")) { |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
204 |
if (nsName != null && nsName.equals("*")) { |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
205 |
return current; |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
206 |
} else { |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
207 |
ElementImpl el = (ElementImpl) current; |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
208 |
if ((nsName == null |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
209 |
&& el.getNamespaceURI() == null) |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
210 |
|| (nsName != null |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
211 |
&& nsName.equals(el.getNamespaceURI()))) { |
6 | 212 |
return current; |
213 |
} |
|
47359
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
214 |
} |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
215 |
} else { |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
216 |
ElementImpl el = (ElementImpl) current; |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
217 |
if (el.getLocalName() != null |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
218 |
&& el.getLocalName().equals(tagName)) { |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
219 |
if (nsName != null && nsName.equals("*")) { |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
220 |
return current; |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
221 |
} else { |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
222 |
if ((nsName == null |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
223 |
&& el.getNamespaceURI() == null) |
6 | 224 |
|| (nsName != null |
47359
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
225 |
&& nsName.equals(el.getNamespaceURI()))) { |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
226 |
return current; |
6 | 227 |
} |
228 |
} |
|
229 |
} |
|
230 |
} |
|
47359
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
231 |
} |
6 | 232 |
} |
233 |
||
47359
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
234 |
// Otherwise continue walking the tree |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
235 |
} |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
236 |
|
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
237 |
// Fell out of tree-walk; no more instances found |
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47216
diff
changeset
|
238 |
return null; |
6 | 239 |
|
240 |
} // nextMatchingElementAfter(int):Node |
|
241 |
||
242 |
} // class DeepNodeListImpl |