Debian Codegraph @

net/sf/saxon/tree/linked/ParentNodeImpl.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.tree.linked;
9
10 import net.sf.saxon.om.NodeInfo;
11 import net.sf.saxon.pattern.AnyNodeTest;
12 import net.sf.saxon.pattern.NodeTest;
13 import net.sf.saxon.tree.iter.ArrayIterator;
14 import net.sf.saxon.tree.iter.AxisIterator;
15 import net.sf.saxon.tree.iter.EmptyIterator;
16 import net.sf.saxon.tree.iter.SingleNodeIterator;
17 import net.sf.saxon.tree.util.FastStringBuffer;
18 import net.sf.saxon.tree.util.Navigator;
19 import net.sf.saxon.type.Type;
20
21 import java.util.Arrays;
22
23 /**
24 * ParentNodeImpl is an implementation of a non-leaf node (specifically, an Element node
25 * or a Document node)
26 *
27 * @author Michael H. Kay
28 */
29
30
31 abstract class ParentNodeImpl extends NodeImpl {
32
33 /*@Nullable*/ private Object children = null; // null for no children
34 // a NodeImpl for a single child
35 // a NodeImpl[] for >1 child
36
37 private int sequence; // sequence number allocated during original tree creation.
38 // set to -1 for nodes added subsequently by XQuery update
39
40 /**
41 * Get the node sequence number (in document order). Sequence numbers are monotonic but not
42 * consecutive. In the current implementation, parent nodes (elements and document nodes) have a zero
43 * least-significant word, while namespaces, attributes, text nodes, comments, and PIs have
44 * the top word the same as their owner and the bottom half reflecting their relative position.
45 * For nodes added by XQuery Update, the sequence number is -1L
46 *
47 * @return the sequence number if there is one, or -1L otherwise.
48 */
49
50 protected final long getSequenceNumber() {
51 return getRawSequenceNumber() == -1 ? -1L : (long) getRawSequenceNumber() << 32;
52 }
53
54 protected final int getRawSequenceNumber() {
55 return sequence;
56 }
57
58 protected final void setRawSequenceNumber(int seq) {
59 sequence = seq;
60 }
61
62 /**
63 * Set the children of this node
64 *
65 * @param children null if there are no children, a single NodeInfo if there is one child, an array of NodeInfo
66 * if there are multiple children
67 */
68
69 protected final void setChildren(Object children) {
70 this.children = children;
71 }
72
73 /**
74 * Determine if the node has any children.
75 */
76
77 public final boolean hasChildNodes() {
78 return children != null;
79 }
80
81 /**
82 * Determine how many children the node has
83 *
84 * @return the number of children of this parent node
85 */
86
87 public final int getNumberOfChildren() {
88 if (children == null) {
89 return 0;
90 } else if (children instanceof NodeImpl) {
91 return 1;
92 } else {
93 return ((NodeInfo[]) children).length;
94 }
95 }
96
97 /**
98 * Get an enumeration of the children of this node
99 *
100 * @param test A NodeTest to be satisfied by the child nodes, or null
101 * if all child node are to be returned
102 * @return an iterator over the children of this node
103 */
104
105 protected final AxisIterator iterateChildren(/*@Nullable*/ NodeTest test) {
106 if (children == null) {
107 return EmptyIterator.OfNodes.THE_INSTANCE;
108 } else if (children instanceof NodeImpl) {
109 NodeImpl child = (NodeImpl) children;
110 if (test == null || test == AnyNodeTest.getInstance()) {
111 return SingleNodeIterator.makeIterator(child);
112 } else {
113 return Navigator.filteredSingleton(child, test);
114 }
115 } else {
116 if (test == null || test == AnyNodeTest.getInstance()) {
117 return new ArrayIterator.OfNodes((NodeImpl[]) children);
118 } else {
119 return new ChildEnumeration(this, test);
120 }
121 }
122 }
123
124
125 /**
126 * Get the first child node of the element
127 *
128 * @return the first child node of the required type, or null if there are no children
129 */
130
131 /*@Nullable*/
132 public final NodeImpl getFirstChild() {
133 if (children == null) {
134 return null;
135 } else if (children instanceof NodeImpl) {
136 return (NodeImpl) children;
137 } else {
138 return ((NodeImpl[]) children)[0];
139 }
140 }
141
142 /**
143 * Get the last child node of the element
144 *
145 * @return the last child of the element, or null if there are no children
146 */
147
148 /*@Nullable*/
149 public final NodeImpl getLastChild() {
150 if (children == null) {
151 return null;
152 }
153 if (children instanceof NodeImpl) {
154 return (NodeImpl) children;
155 }
156 NodeImpl[] n = (NodeImpl[]) children;
157 return n[n.length - 1];
158 }
159
160 /**
161 * Get the nth child node of the element (numbering from 0)
162 *
163 * @param n identifies the required child
164 * @return the last child of the element, or null if there is no n'th child
165 */
166
167 /*@Nullable*/
168 protected final NodeImpl getNthChild(int n) {
169 if (children == null) {
170 return null;
171 }
172 if (children instanceof NodeImpl) {
173 return n == 0 ? (NodeImpl) children : null;
174 }
175 NodeImpl[] nodes = (NodeImpl[]) children;
176 if (n < 0 || n >= nodes.length) {
177 return null;
178 }
179 return nodes[n];
180 }
181
182 /**
183 * Remove a given child
184 *
185 * @param child the child to be removed
186 */
187
188 protected void removeChild(NodeImpl child) {
189 if (children == null) {
190 return;
191 }
192 if (children == child) {
193 children = null;
194 return;
195 }
196 NodeImpl[] nodes = (NodeImpl[]) children;
197 for (int i = 0; i < nodes.length; i++) {
198 if (nodes[i] == child) {
199 if (nodes.length == 2) {
200 children = nodes[1 - i];
201 } else {
202 NodeImpl[] n2 = new NodeImpl[nodes.length - 1];
203 if (i > 0) {
204 System.arraycopy(nodes, 0, n2, 0, i);
205 }
206 if (i < nodes.length - 1) {
207 System.arraycopy(nodes, i + 1, n2, i, nodes.length - i - 1);
208 }
209 children = cleanUpChildren(n2);
210 }
211 break;
212 }
213 }
214 }
215
216 /**
217 * Tidy up the children of the node. Merge adjacent text nodes; remove zero-length text nodes;
218 * reallocate index numbers to each of the children
219 *
220 * @param children the existing children
221 * @return the replacement array of children
222 */
223
224 /*@NotNull*/
225 private NodeImpl[] cleanUpChildren(/*@NotNull*/ NodeImpl[] children) {
226 boolean prevText = false;
227 int j = 0;
228 NodeImpl[] c2 = new NodeImpl[children.length];
229 for (NodeImpl node : children) {
230 if (node instanceof TextImpl) {
231 if (prevText) {
232 TextImpl prev = (TextImpl) c2[j - 1];
233 prev.replaceStringValue(prev.getStringValue() + node.getStringValue());
234 } else if (!node.getStringValue().isEmpty()) {
235 prevText = true;
236 node.setSiblingPosition(j);
237 c2[j++] = node;
238 }
239 } else {
240 node.setSiblingPosition(j);
241 c2[j++] = node;
242 prevText = false;
243 }
244 }
245 if (j == c2.length) {
246 return c2;
247 } else {
248 return Arrays.copyOf(c2, j);
249 }
250 }
251
252
253 /**
254 * Return the string-value of the node, that is, the concatenation
255 * of the character content of all descendent elements and text nodes.
256 *
257 * @return the accumulated character content of the element, including descendant elements.
258 */
259
260 public String getStringValue() {
261 return getStringValueCS().toString();
262 }
263
264
265 public CharSequence getStringValueCS() {
266 FastStringBuffer sb = null;
267
268 NodeImpl next = getFirstChild();
269 while (next != null) {
270 if (next instanceof TextImpl) {
271 if (sb == null) {
272 sb = new FastStringBuffer(FastStringBuffer.C64);
273 }
274 sb.append(next.getStringValueCS());
275 }
276 next = next.getNextInDocument(this);
277 }
278 if (sb == null) {
279 return "";
280 }
281 return sb.condense();
282 }
283
284 /**
285 * Add a child node to this node. For system use only. Note: normalizing adjacent text nodes
286 * is the responsibility of the caller.
287 *
288 * @param node the node to be added as a child of this node. This must be an instance of
289 * {@link net.sf.saxon.tree.linked.NodeImpl}. It will be modified as a result of this call (by setting its
290 * parent property and sibling position)
291 * @param index the position where the child is to be added
292 */
293
294 protected synchronized void addChild(/*@NotNull*/ NodeImpl node, int index) {
295 NodeImpl[] c;
296 if (children == null) {
297 c = new NodeImpl[10];
298 } else if (children instanceof NodeImpl) {
299 c = new NodeImpl[10];
300 c[0] = (NodeImpl) children;
301 } else {
302 c = (NodeImpl[]) children;
303 }
304 if (index >= c.length) {
305 c = Arrays.copyOf(c, c.length*2);
306 }
307 c[index] = node;
308 node.setRawParent(this);
309 node.setSiblingPosition(index);
310 children = c;
311 }
312
313
314 /**
315 * Insert a sequence of nodes as children of this node.
316 * <p>This method takes no action unless the target node is a document node or element node. It also
317 * takes no action in respect of any supplied nodes that are not elements, text nodes, comments, or
318 * processing instructions.</p>
319 * <p>The supplied nodes will form the new children. Adjacent text nodes will be merged, and
320 * zero-length text nodes removed. The supplied nodes may be modified in situ, for example to change their
321 * parent property and to add namespace bindings, or they may be copied, at the discretion of
322 * the implementation.</p>
323 *
324 * @param source the nodes to be inserted. The implementation determines what implementation classes
325 * of node it will accept; this implementation will accept text, comment, and processing instruction
326 * nodes belonging to any implementation, but elements must be instances of {@link net.sf.saxon.tree.linked.ElementImpl}.
327 * The supplied nodes will be modified in situ, for example
328 * to change their parent property and to add namespace bindings, if they are instances of
329 * {@link net.sf.saxon.tree.linked.ElementImpl}; otherwise they will be copied. If the nodes are copied, then on return
330 * the supplied source array will contain the copy rather than the original.
331 * @param atStart true if the new nodes are to be inserted before existing children; false if they are
332 * to be inserted after existing children
333 * @param inherit true if the inserted nodes are to inherit the namespaces of their new parent; false
334 * if such namespaces are to be undeclared
335 * @throws IllegalArgumentException if the supplied nodes use a node implementation that this
336 * implementation does not accept.
337 */
338
339 public void insertChildren(/*@NotNull*/ NodeInfo[] source, boolean atStart, boolean inherit) {
340 if (atStart) {
341 insertChildrenAt(source, 0, inherit);
342 } else {
343 insertChildrenAt(source, getNumberOfChildren(), inherit);
344 }
345 }
346
347 /**
348 * Insert children before or after a given existing child
349 *
350 * @param source the children to be inserted. We allow any kind of text, comment, or processing instruction
351 * node, but element nodes must be instances of this NodeInfo implementation.
352 * @param index the position before which they are to be inserted: 0 indicates insertion before the
353 * first child, 1 insertion before the second child, and so on.
354 * @param inherit true if the inserted nodes are to inherit the namespaces that are in-scope for their
355 * new parent; false if such namespaces should be undeclared on the children
356 */
357
358 protected synchronized void insertChildrenAt(/*@NotNull*/ NodeInfo[] source, int index, boolean inherit) {
359 if (source.length == 0) {
360 return;
361 }
362 NodeImpl[] source2 = adjustSuppliedNodeArray(source, inherit);
363 if (children == null) {
364 if (source2.length == 1) {
365 children = source2[0];
366 ((NodeImpl) children).setSiblingPosition(0);
367 } else {
368 children = cleanUpChildren(source2);
369 }
370 } else if (children instanceof NodeImpl) {
371 int adjacent = index == 0 ? source2.length - 1 : 0;
372 if (children instanceof TextImpl && source2[adjacent] instanceof TextImpl) {
373 if (index == 0) {
374 source2[adjacent].replaceStringValue(
375 source2[adjacent].getStringValue() + ((TextImpl) children).getStringValue());
376 } else {
377 source2[adjacent].replaceStringValue(
378 ((TextImpl) children).getStringValue() + source2[adjacent].getStringValue());
379 }
380 children = cleanUpChildren(source2);
381 } else {
382 NodeImpl[] n2 = new NodeImpl[source2.length + 1];
383 if (index == 0) {
384 System.arraycopy(source2, 0, n2, 0, source2.length);
385 n2[source2.length] = (NodeImpl) children;
386 } else {
387 n2[0] = (NodeImpl) children;
388 System.arraycopy(source2, 0, n2, 1, source2.length);
389 }
390 children = cleanUpChildren(n2);
391 }
392 } else {
393 NodeImpl[] n0 = (NodeImpl[]) children;
394 NodeImpl[] n2 = new NodeImpl[n0.length + source2.length];
395 System.arraycopy(n0, 0, n2, 0, index);
396 System.arraycopy(source2, 0, n2, index, source2.length);
397 System.arraycopy(n0, index, n2, index + source2.length, n0.length - index);
398 children = cleanUpChildren(n2);
399 }
400 }
401
402
403 /*@NotNull*/
404 private NodeImpl convertForeignNode(/*@NotNull*/ NodeInfo source) {
405 if (!(source instanceof NodeImpl)) {
406 int kind = source.getNodeKind();
407 switch (kind) {
408 case Type.TEXT:
409 return new TextImpl(source.getStringValue());
410 case Type.COMMENT:
411 return new CommentImpl(source.getStringValue());
412 case Type.PROCESSING_INSTRUCTION:
413 return new ProcInstImpl(source.getLocalPart(), source.getStringValue());
414 case Type.ELEMENT:
415 throw new IllegalArgumentException(
416 "Cannot insert an element node unless it is an instance of net.sf.saxon.om.tree.ElementImpl");
417 default:
418 throw new IllegalArgumentException(
419 "Cannot insert a node unless it is an element, comment, text node, or processing instruction");
420 }
421 }
422 return (NodeImpl) source;
423 }
424
425 /**
426 * Replace child at a given index by new children
427 *
428 * @param source the children to be inserted
429 * @param index the position at which they are to be inserted: 0 indicates replacement of the
430 * first child, replacement of the second child, and so on. The effect is undefined if index
431 * is out of range
432 * @param inherit set to true if the new child elements are to inherit the in-scope namespaces
433 * of their new parent
434 * @throws IllegalArgumentException if any of the replacement nodes is not an element, text,
435 * comment, or processing instruction node
436 */
437
438 protected synchronized void replaceChildrenAt(/*@NotNull*/ NodeInfo[] source, int index, boolean inherit) {
439 if (children == null) {
440 return;
441 }
442 NodeImpl[] source2 = adjustSuppliedNodeArray(source, inherit);
443 if (children instanceof NodeImpl) {
444 if (source2.length == 0) {
445 children = null;
446 } else if (source2.length == 1) {
447 children = source2[0];
448 } else {
449 NodeImpl[] n2 = new NodeImpl[source2.length];
450 System.arraycopy(source2, 0, n2, 0, source.length);
451 children = cleanUpChildren(n2);
452 }
453 } else {
454 NodeImpl[] n0 = (NodeImpl[]) children;
455 NodeImpl[] n2 = new NodeImpl[n0.length + source2.length - 1];
456 System.arraycopy(n0, 0, n2, 0, index);
457 System.arraycopy(source2, 0, n2, index, source2.length);
458 System.arraycopy(n0, index + 1, n2, index + source2.length, n0.length - index - 1);
459 children = cleanUpChildren(n2);
460 }
461 }
462
463 private NodeImpl[] adjustSuppliedNodeArray(NodeInfo[] source, boolean inherit) {
464 NodeImpl[] source2 = new NodeImpl[source.length];
465 for (int i = 0; i < source.length; i++) {
466 source2[i] = convertForeignNode(source[i]);
467 NodeImpl child = source2[i];
468 child.setRawParent(this);
469 if (child instanceof ElementImpl) {
470 // If the child has no xmlns="xxx" declaration, then add an xmlns="" to prevent false inheritance
471 // from the new parent
472 ((ElementImpl) child).fixupInsertedNamespaces(inherit);
473 }
474 }
475 return source2;
476 }
477
478
479 /**
480 * Compact the space used by this node
481 *
482 * @param size the number of actual children
483 */
484
485 public synchronized void compact(int size) {
486 if (size == 0) {
487 children = null;
488 } else if (size == 1) {
489 if (children instanceof NodeImpl[]) {
490 children = ((NodeImpl[]) children)[0];
491 }
492 } else {
493 children = Arrays.copyOf((NodeImpl[])children, size);
494 }
495 }
496
497
498 }
499