Debian Codegraph @

net/sf/saxon/tree/linked/NodeImpl.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.Configuration;
11 import net.sf.saxon.event.Builder;
12 import net.sf.saxon.expr.parser.Location;
13 import net.sf.saxon.om.*;
14 import net.sf.saxon.pattern.AnyNodeTest;
15 import net.sf.saxon.pattern.NameTest;
16 import net.sf.saxon.pattern.NodeTest;
17 import net.sf.saxon.trans.XPathException;
18 import net.sf.saxon.tree.NamespaceNode;
19 import net.sf.saxon.tree.iter.AxisIterator;
20 import net.sf.saxon.tree.iter.EmptyIterator;
21 import net.sf.saxon.tree.iter.SingletonIterator;
22 import net.sf.saxon.tree.iter.UnfailingIterator;
23 import net.sf.saxon.tree.util.FastStringBuffer;
24 import net.sf.saxon.tree.util.Navigator;
25 import net.sf.saxon.tree.util.SteppingNavigator;
26 import net.sf.saxon.tree.util.SteppingNode;
27 import net.sf.saxon.tree.wrapper.SiblingCountingNode;
28 import net.sf.saxon.type.*;
29 import net.sf.saxon.value.UntypedAtomicValue;
30
31 import javax.xml.transform.SourceLocator;
32
33
34 /**
35 * A node in the "linked" tree representing any kind of node except a namespace node.
36 * Specific node kinds are represented by concrete subclasses.
37 *
38 * @author Michael H. Kay
39 */
40
41 public abstract class NodeImpl
42 implements MutableNodeInfo, SteppingNode<NodeImpl>, SiblingCountingNode, SourceLocator {
43
44 /*@Nullable*/ private ParentNodeImpl parent;
45 private int index; // Set to -1 when the node is deleted
46 /**
47 * Chararacteristic letters to identify each type of node, indexed using the node type
48 * values. These are used as the initial letter of the result of generate-id()
49 */
50
51 /*@NotNull*/ public static final char[] NODE_LETTER =
52 {'x', 'e', 'a', 't', 'x', 'x', 'x', 'p', 'c', 'r', 'x', 'x', 'x', 'n'};
53
54 /**
55 * To implement {@link Sequence}, this method returns the item itself
56 *
57 * @return this item
58 */
59
60 public NodeImpl head() {
61 return this;
62 }
63
64 /**
65 * To implement {@link Sequence}, this method returns a singleton iterator
66 * that delivers this item in the form of a sequence
67 *
68 * @return a singleton iterator that returns this item
69 */
70
71 public UnfailingIterator<NodeInfo> iterate() {
72 return SingletonIterator.makeIterator(this);
73 }
74
75 /**
76 * Get information about the tree to which this NodeInfo belongs
77 *
78 * @return the TreeInfo
79 * @since 9.7
80 */
81 public TreeInfo getTreeInfo() {
82 return getPhysicalRoot();
83 }
84
85 /**
86 * Get the value of the item as a CharSequence. This is in some cases more efficient than
87 * the version of the method that returns a String.
88 */
89
90 public CharSequence getStringValueCS() {
91 return getStringValue();
92 }
93
94 /**
95 * Get the type annotation
96 *
97 * @return the type annotation of the base node
98 */
99
100 public SchemaType getSchemaType() {
101 return Untyped.getInstance();
102 }
103
104 /**
105 * Get the column number of the node.
106 * The default implementation returns -1, meaning unknown
107 */
108
109 public int getColumnNumber() {
110 if (parent == null) {
111 return -1;
112 } else {
113 return parent.getColumnNumber();
114 }
115 }
116
117 /**
118 * Get the index position of this node among its siblings (starting from 0)
119 *
120 * @return 0 for the first child, 1 for the second child, etc. Returns -1 for a node
121 * that has been deleted.
122 */
123 public final int getSiblingPosition() {
124 return index;
125 }
126
127 /**
128 * Set the index position. For internal use only
129 *
130 * @param index the position of the node among its siblings, counting from zero.
131 */
132
133 protected final void setSiblingPosition(int index) {
134 this.index = index;
135 }
136
137 /**
138 * Get the typed value.
139 *
140 * @return the typed value. If requireSingleton is set to true, the result will always be an
141 * AtomicValue. In other cases it may be a Value representing a sequence whose items are atomic
142 * values.
143 * @since 8.5
144 */
145
146 public AtomicSequence atomize() throws XPathException {
147 SchemaType stype = getSchemaType();
148 if (stype == Untyped.getInstance() || stype == BuiltInAtomicType.UNTYPED_ATOMIC) {
149 return new UntypedAtomicValue(getStringValueCS());
150 } else {
151 return stype.atomize(this);
152 }
153 }
154
155 /**
156 * Set the system ID of this node. This method is provided so that a NodeInfo
157 * implements the javax.xml.transform.Source interface, allowing a node to be
158 * used directly as the Source of a transformation
159 */
160
161 public void setSystemId(String uri) {
162 // overridden in DocumentImpl and ElementImpl
163 NodeInfo p = getParent();
164 if (p != null) {
165 p.setSystemId(uri);
166 }
167 }
168
169 /**
170 * Determine whether this is the same node as another node
171 *
172 * @return true if this Node object and the supplied Node object represent the
173 * same node in the tree.
174 */
175
176 public boolean equals(NodeInfo other) {
177 // default implementation: differs for attribute and namespace nodes
178 return this == other;
179 }
180
181 /**
182 * Get the name of the node. Returns null for an unnamed node
183 *
184 * @return the name of the node
185 */
186
187 public NodeName getNodeName() {
188 return null;
189 }
190
191 /**
192 * Ask whether this NodeInfo implementation holds a fingerprint identifying the name of the
193 * node in the NamePool. If the answer is true, then the {@link #getFingerprint} method must
194 * return the fingerprint of the node. If the answer is false, then the {@link #getFingerprint}
195 * method should throw an {@code UnsupportedOperationException}. In the case of unnamed nodes
196 * such as text nodes, the result can be either true (in which case getFingerprint() should
197 * return -1) or false (in which case getFingerprint may throw an exception).
198 *
199 * @return true if the implementation of this node provides fingerprints.
200 * @since 9.8; previously Saxon relied on using <code>FingerprintedNode</code> as a marker interface.
201 */
202 @Override
203 public boolean hasFingerprint() {
204 return true;
205 }
206
207 /**
208 * Get the fingerprint of the node. This is used to compare whether two nodes
209 * have equivalent names. Return -1 for a node with no name.
210 */
211
212 public int getFingerprint() {
213 NodeName name = getNodeName();
214 if (name == null) {
215 return -1;
216 } else {
217 return name.obtainFingerprint(getConfiguration().getNamePool());
218 }
219 }
220
221 /**
222 * Get a character string that uniquely identifies this node
223 */
224
225 public void generateId(/*@NotNull*/ FastStringBuffer buffer) {
226 long seq = getSequenceNumber();
227 if (seq == -1L) {
228 getPhysicalRoot().generateId(buffer);
229 buffer.append(NODE_LETTER[getNodeKind()]);
230 buffer.append(Long.toString(seq) + "h" + hashCode());
231 } else {
232 parent.generateId(buffer);
233 buffer.append(NODE_LETTER[getNodeKind()]);
234 buffer.append(Integer.toString(index));
235 }
236 }
237
238 /**
239 * Get the system ID for the node. Default implementation for child nodes.
240 */
241
242 /*@Nullable*/
243 public String getSystemId() {
244 return parent.getSystemId();
245 }
246
247 /**
248 * Get the base URI for the node. Default implementation for child nodes.
249 */
250
251 public String getBaseURI() {
252 return parent.getBaseURI();
253 }
254
255 /**
256 * Get the node sequence number (in document order). Sequence numbers are monotonic but not
257 * consecutive. In the current implementation, parent nodes (elements and roots) have a zero
258 * least-significant word, while namespaces, attributes, text nodes, comments, and PIs have
259 * the top word the same as their owner and the bottom half reflecting their relative position.
260 * This is the default implementation for child nodes.
261 * For nodes added by XQuery Update, the sequence number is -1L
262 *
263 * @return the sequence number if there is one, or -1L otherwise.
264 */
265
266 protected long getSequenceNumber() {
267 NodeImpl prev = this;
268 for (int i = 0; ; i++) {
269 if (prev instanceof ParentNodeImpl) {
270 long prevseq = prev.getSequenceNumber();
271 return prevseq == -1L ? prevseq : prevseq + 0x10000 + i;
272 // note the 0x10000 is to leave room for namespace and attribute nodes.
273 }
274 assert prev != null;
275 prev = prev.getPreviousInDocument();
276 }
277
278 }
279
280 /**
281 * Determine the relative position of this node and another node, in document order.
282 * The other node will always be in the same document.
283 *
284 * @param other The other node, whose position is to be compared with this node
285 * @return -1 if this node precedes the other node, +1 if it follows the other
286 * node, or 0 if they are the same node. (In this case, isSameNode() will always
287 * return true, and the two nodes will produce the same result for generateId())
288 */
289
290 public final int compareOrder(/*@NotNull*/ NodeInfo other) {
291 if (other instanceof NamespaceNode) {
292 return 0 - other.compareOrder(this);
293 }
294 long a = getSequenceNumber();
295 long b = ((NodeImpl) other).getSequenceNumber();
296 if (a == -1L || b == -1L) {
297 // Nodes added by XQuery Update do not have sequence numbers
298 return Navigator.compareOrder(this, (NodeImpl) other);
299 }
300 return Long.compare(a, b);
301 }
302
303 /**
304 * Get the configuration
305 */
306
307 public Configuration getConfiguration() {
308 return getPhysicalRoot().getConfiguration();
309 }
310
311 /**
312 * Get the NamePool
313 */
314
315 public NamePool getNamePool() {
316 return getPhysicalRoot().getNamePool();
317 }
318
319 /**
320 * Get the prefix part of the name of this node. This is the name before the ":" if any.
321 *
322 * @return the prefix part of the name. For an unnamed node, return an empty string.
323 */
324
325 public String getPrefix() {
326 NodeName qName = getNodeName();
327 return qName == null ? "" : qName.getPrefix();
328 }
329
330 /**
331 * Get the URI part of the name of this node. This is the URI corresponding to the
332 * prefix, or the URI of the default namespace if appropriate.
333 *
334 * @return The URI of the namespace of this node. For the null namespace, return an
335 * empty string. For an unnamed node, return the empty string.
336 */
337
338 public String getURI() {
339 NodeName qName = getNodeName();
340 return qName == null ? "" : qName.getURI();
341 }
342
343 /**
344 * Get the display name of this node. For elements and attributes this is [prefix:]localname.
345 * For unnamed nodes, it is an empty string.
346 *
347 * @return The display name of this node.
348 * For a node with no name, return an empty string.
349 */
350
351 public String getDisplayName() {
352 NodeName qName = getNodeName();
353 return qName == null ? "" : qName.getDisplayName();
354 }
355
356 /**
357 * Get the local name of this node.
358 *
359 * @return The local name of this node.
360 * For a node with no name, return "",.
361 */
362
363 public String getLocalPart() {
364 NodeName qName = getNodeName();
365 return qName == null ? "" : qName.getLocalPart();
366 }
367
368 /**
369 * Get the line number of the node within its source document entity
370 */
371
372 public int getLineNumber() {
373 return parent.getLineNumber();
374 }
375
376 /**
377 * Get an immutable copy of this Location object. By default Location objects may be mutable, so they
378 * should not be saved for later use. The result of this operation holds the same location information,
379 * but in an immutable form.
380 */
381
382 public Location saveLocation() {
383 return this;
384 }
385
386 /**
387 * Find the parent node of this node.
388 *
389 * @return The Node object describing the containing element or root node.
390 */
391
392 /*@Nullable*/
393 public final NodeImpl getParent() {
394 if (parent instanceof DocumentImpl && ((DocumentImpl) parent).isImaginary()) {
395 return null;
396 }
397 return parent;
398 }
399
400 /**
401 * Get the raw value of the parent pointer. This will usually be the same as the parent node
402 * in the XDM model, but in the case of a parentless element it will be a pointer to the "imaginary"
403 * document node which is not properly part of the tree.
404 *
405 * @return either the real parent of this node, or the "imaginary" parent present in the tree
406 * implementation to provide a root object for the tree
407 */
408
409 /*@Nullable*/
410 protected final ParentNodeImpl getRawParent() {
411 return parent;
412 }
413
414 /**
415 * Set the raw parent pointer
416 *
417 * @param parent the "raw" parent pointer: either the real parent, or a dummy parent
418 * added to ensure that the tree is properly rooted.
419 */
420
421 protected final void setRawParent(/*@Nullable*/ ParentNodeImpl parent) {
422 this.parent = parent;
423 }
424
425 /**
426 * Get the previous sibling of the node
427 *
428 * @return The previous sibling node. Returns null if the current node is the first
429 * child of its parent.
430 */
431
432 /*@Nullable*/
433 public NodeImpl getPreviousSibling() {
434 if (parent == null) {
435 return null;
436 }
437 return parent.getNthChild(index - 1);
438 }
439
440
441 /**
442 * Get next sibling node
443 *
444 * @return The next sibling node of the required type. Returns null if the current node is the last
445 * child of its parent.
446 */
447
448 /*@Nullable*/
449 public NodeImpl getNextSibling() {
450 if (parent == null) {
451 return null;
452 }
453 return parent.getNthChild(index + 1);
454 }
455
456 /**
457 * Get first child - default implementation used for leaf nodes
458 *
459 * @return null
460 */
461
462 /*@Nullable*/
463 public NodeImpl getFirstChild() {
464 return null;
465 }
466
467 /**
468 * Get last child - default implementation used for leaf nodes
469 *
470 * @return null
471 */
472
473 /*@Nullable*/
474 public NodeInfo getLastChild() {
475 return null;
476 }
477
478 /**
479 * Return an enumeration over the nodes reached by the given axis from this node
480 *
481 * @param axisNumber The axis to be iterated over
482 * @return an AxisIterator that scans the nodes reached by the axis in turn.
483 */
484
485 public AxisIterator iterateAxis(byte axisNumber) {
486 // Fast path for child axis
487 if (axisNumber == AxisInfo.CHILD) {
488 if (this instanceof ParentNodeImpl) {
489 return ((ParentNodeImpl) this).iterateChildren(null);
490 } else {
491 return EmptyIterator.OfNodes.THE_INSTANCE;
492 }
493 } else {
494 return iterateAxis(axisNumber, AnyNodeTest.getInstance());
495 }
496 }
497
498 /**
499 * Return an enumeration over the nodes reached by the given axis from this node
500 *
501 * @param axisNumber The axis to be iterated over
502 * @param nodeTest A pattern to be matched by the returned nodes
503 * @return an AxisIterator that scans the nodes reached by the axis in turn.
504 */
505
506 public AxisIterator iterateAxis(byte axisNumber, /*@NotNull*/ NodeTest nodeTest) {
507
508 switch (axisNumber) {
509 case AxisInfo.ANCESTOR:
510 return new AncestorEnumeration(this, nodeTest, false);
511
512 case AxisInfo.ANCESTOR_OR_SELF:
513 return new AncestorEnumeration(this, nodeTest, true);
514
515 case AxisInfo.ATTRIBUTE:
516 if (getNodeKind() != Type.ELEMENT) {
517 return EmptyIterator.OfNodes.THE_INSTANCE;
518 }
519 return new AttributeEnumeration(this, nodeTest);
520
521 case AxisInfo.CHILD:
522 if (this instanceof ParentNodeImpl) {
523 return ((ParentNodeImpl) this).iterateChildren(nodeTest);
524 } else {
525 return EmptyIterator.OfNodes.THE_INSTANCE;
526 }
527
528 case AxisInfo.DESCENDANT:
529 if (getNodeKind() == Type.DOCUMENT &&
530 nodeTest instanceof NameTest &&
531 nodeTest.getPrimitiveType() == Type.ELEMENT) {
532 return ((DocumentImpl) this).getAllElements(nodeTest.getFingerprint());
533 } else if (hasChildNodes()) {
534 return new SteppingNavigator.DescendantAxisIterator<>(this, false, nodeTest);
535 } else {
536 return EmptyIterator.OfNodes.THE_INSTANCE;
537 }
538
539 case AxisInfo.DESCENDANT_OR_SELF:
540 return new SteppingNavigator.DescendantAxisIterator<>(this, true, nodeTest);
541
542 case AxisInfo.FOLLOWING:
543 return new FollowingEnumeration(this, nodeTest);
544
545 case AxisInfo.FOLLOWING_SIBLING:
546 return new FollowingSiblingEnumeration(this, nodeTest);
547
548 case AxisInfo.NAMESPACE:
549 if (getNodeKind() != Type.ELEMENT) {
550 return EmptyIterator.OfNodes.THE_INSTANCE;
551 }
552 return NamespaceNode.makeIterator(this, nodeTest);
553
554 case AxisInfo.PARENT:
555 NodeInfo parent = getParent();
556 if (parent == null) {
557 return EmptyIterator.OfNodes.THE_INSTANCE;
558 }
559 return Navigator.filteredSingleton(parent, nodeTest);
560
561 case AxisInfo.PRECEDING:
562 return new PrecedingEnumeration(this, nodeTest);
563
564 case AxisInfo.PRECEDING_SIBLING:
565 return new PrecedingSiblingEnumeration(this, nodeTest);
566
567 case AxisInfo.SELF:
568 return Navigator.filteredSingleton(this, nodeTest);
569
570 case AxisInfo.PRECEDING_OR_ANCESTOR:
571 return new PrecedingOrAncestorEnumeration(this, nodeTest);
572
573 default:
574 throw new IllegalArgumentException("Unknown axis number " + axisNumber);
575 }
576 }
577
578 /**
579 * Find the value of a given attribute of this node. <BR>
580 * This method is defined on all nodes to meet XSL requirements, but for nodes
581 * other than elements it will always return null.
582 *
583 * @param uri the namespace uri of an attribute
584 * @param localName the local name of an attribute
585 * @return the value of the attribute, if it exists, otherwise null
586 */
587
588 /*@Nullable*/
589 public String getAttributeValue( /*@NotNull*/ String uri, /*@NotNull*/ String localName) {
590 return null;
591 }
592
593 /**
594 * Get the root node
595 *
596 * @return the NodeInfo representing the logical root of the tree. For this tree implementation the
597 * root will either be a document node or an element node.
598 */
599
600 public NodeInfo getRoot() {
601 NodeInfo parent = getParent();
602 if (parent == null) {
603 return this;
604 } else {
605 return parent.getRoot();
606 }
607 }
608
609 /**
610 * Get the physical root of the tree. This may be an imaginary document node: this method
611 * should be used only when control information held at the physical root is required
612 *
613 * @return the document node, which may be imaginary. In the case of a node that has been detached
614 * from the tree by means of a delete() operation, this method returns null.
615 */
616
617 /*@Nullable*/
618 public DocumentImpl getPhysicalRoot() {
619 ParentNodeImpl up = parent;
620 while (up != null && !(up instanceof DocumentImpl)) {
621 up = up.getRawParent();
622 }
623 return (DocumentImpl) up;
624 }
625
626 /**
627 * Get the next node in document order
628 *
629 * @param anchor the scan stops when it reaches a node that is not a descendant of the specified
630 * anchor node
631 * @return the next node in the document, or null if there is no such node
632 */
633
634 /*@Nullable*/
635 public NodeImpl getNextInDocument(NodeImpl anchor) {
636 // find the first child node if there is one; otherwise the next sibling node
637 // if there is one; otherwise the next sibling of the parent, grandparent, etc, up to the anchor element.
638 // If this yields no result, return null.
639
640 NodeImpl next = getFirstChild();
641 if (next != null) {
642 return next;
643 }
644 if (this == anchor) {
645 return null;
646 }
647 next = getNextSibling();
648 if (next != null) {
649 return next;
650 }
651 NodeImpl parent = this;
652 while (true) {
653 parent = parent.getParent();
654 if (parent == null) {
655 return null;
656 }
657 if (parent == anchor) {
658 return null;
659 }
660 next = parent.getNextSibling();
661 if (next != null) {
662 return next;
663 }
664 }
665 }
666
667 public NodeImpl getSuccessorElement(NodeImpl anchor, String uri, String local) {
668 NodeImpl next = getNextInDocument(anchor);
669 while (next != null && !(next.getNodeKind() == Type.ELEMENT &&
670 (uri == null || uri.equals(next.getURI())) &&
671 (local == null || local.equals(next.getLocalPart())))) {
672 next = next.getNextInDocument(anchor);
673 }
674 return next;
675 }
676
677 /**
678 * Get the previous node in document order
679 *
680 * @return the previous node in the document, or null if there is no such node
681 */
682
683 /*@Nullable*/
684 public NodeImpl getPreviousInDocument() {
685
686 // finds the last child of the previous sibling if there is one;
687 // otherwise the previous sibling element if there is one;
688 // otherwise the parent, up to the anchor element.
689 // If this reaches the document root, return null.
690
691 NodeImpl prev = getPreviousSibling();
692 if (prev != null) {
693 return prev.getLastDescendantOrSelf();
694 }
695 return getParent();
696 }
697
698 /*@NotNull*/
699 private NodeImpl getLastDescendantOrSelf() {
700 NodeImpl last = (NodeImpl) getLastChild();
701 if (last == null) {
702 return this;
703 }
704 return last.getLastDescendantOrSelf();
705 }
706
707 /**
708 * Get all namespace undeclarations and undeclarations defined on this element.
709 *
710 * @param buffer If this is non-null, and the result array fits in this buffer, then the result
711 * may overwrite the contents of this array, to avoid the cost of allocating a new array on the heap.
712 * @return An array of integers representing the namespace declarations and undeclarations present on
713 * this element. For a node other than an element, return null. Otherwise, the returned array is a
714 * sequence of namespace codes, whose meaning may be interpreted by reference to the name pool. The
715 * top half word of each namespace code represents the prefix, the bottom half represents the URI.
716 * If the bottom half is zero, then this is a namespace undeclaration rather than a declaration.
717 * The XML namespace is never included in the list. If the supplied array is larger than required,
718 * then the first unused entry will be set to -1.
719 * <p>For a node other than an element, the method returns null.</p>
720 */
721
722 /*@Nullable*/
723 public NamespaceBinding[] getDeclaredNamespaces(NamespaceBinding[] buffer) {
724 return null;
725 }
726
727 /**
728 * Determine whether the node has any children.
729 *
730 * @return <code>true</code> if the node has any children,
731 * <code>false</code> if the node has no children.
732 */
733
734 public boolean hasChildNodes() {
735 return getFirstChild() != null;
736 }
737
738 /**
739 * Set the type annotation on a node. This must only be called when the caller has verified (by validation)
740 * that the node is a valid instance of the specified type. The call is ignored if the node is not an element
741 * or attribute node.
742 *
743 * @param type the type annotation (possibly including high bits set to indicate the isID, isIDREF, and
744 * isNilled properties)
745 */
746
747 public void setTypeAnnotation(SchemaType type) {
748 // no action
749 }
750
751 /**
752 * Delete this node (that is, detach it from its parent)
753 */
754
755 public void delete() {
756 // Overridden for attribute nodes
757 if (parent != null) {
758 parent.removeChild(this);
759 DocumentImpl newRoot = new DocumentImpl();
760 newRoot.setConfiguration(getConfiguration());
761 newRoot.setImaginary(true);
762 parent = newRoot;
763 }
764 index = -1;
765 }
766
767 /**
768 * Test whether this MutableNodeInfo object represents a node that has been deleted.
769 * Generally, such a node is unusable, and any attempt to use it will result in an exception
770 * being thrown
771 *
772 * @return true if this node has been deleted
773 */
774
775 public boolean isDeleted() {
776 return index == -1 || (parent != null && parent.isDeleted());
777 }
778
779 /**
780 * Remove an attribute from this element node
781 * <p>If this node is not an element, or if the specified node is not an attribute
782 * of this element, this method takes no action.</p>
783 * <p>The attribute object itself becomes unusable; any attempt to use this attribute object,
784 * or any other object representing the same node, is likely to result in an exception.</p>
785 *
786 * @param attribute the attribute node to be removed
787 */
788
789 public void removeAttribute(NodeInfo attribute) {
790 // no action (overridden in subclasses)
791 }
792
793 /**
794 * Add an attribute to this element node.
795 * <p>If this node is not an element, or if the supplied node is not an attribute, the method
796 * takes no action. If the element already has an attribute with this name, the method
797 * throws an exception.</p>
798 * <p>This method does not perform any namespace fixup. It is the caller's responsibility
799 * to ensure that any namespace prefix used in the name of the attribute (or in its value
800 * if it has a namespace-sensitive type) is declared on this element.</p>
801 *
802 * @param name the name of the new attribute
803 * @param attType the type annotation of the new attribute
804 * @param value the string value of the new attribute
805 * @param properties properties including IS_ID and IS_IDREF properties
806 * @throws IllegalStateException if the element already has an attribute with the given name.
807 */
808
809 public void addAttribute(NodeName name, SimpleType attType, CharSequence value, int properties) {
810 // No action, unless this is an element node
811 }
812
813 /**
814 * Rename this node
815 *
816 * @param newNameCode the NamePool code of the new name
817 */
818
819 public void rename(NodeName newNameCode) {
820 // implemented for node kinds that have a name
821 }
822
823
824 public void addNamespace(NamespaceBinding nscode, boolean unused) {
825 // implemented for element nodes only
826 }
827
828 /**
829 * Replace this node with a given sequence of nodes. This node is effectively deleted, and the replacement
830 * nodes are attached to the parent of this node in its place.
831 * <p>The supplied nodes will become children of this node's parent. Adjacent text nodes will be merged, and
832 * zero-length text nodes removed. The supplied nodes may be modified in situ, for example to change their
833 * parent property and to add namespace bindings, or they may be copied, at the discretion of
834 * the implementation.</p>
835 *
836 * @param replacement the replacement nodes. If this node is an attribute, the replacements
837 * must also be attributes; if this node is not an attribute, the replacements must not be attributes.
838 * source the nodes to be inserted. The implementation determines what implementation classes
839 * of node it will accept; this implementation will accept attribute, text, comment, and processing instruction
840 * nodes belonging to any implementation, but elements must be instances of {@link net.sf.saxon.tree.linked.ElementImpl}.
841 * The supplied nodes will be modified in situ, for example
842 * to change their parent property and to add namespace bindings, if they are instances of
843 * {@link net.sf.saxon.tree.linked.ElementImpl}; otherwise they will be copied. If the nodes are copied, then on return
844 * the supplied source array will contain the copy rather than the original.
845 * @param inherit true if the replacement nodes are to inherit the namespaces of their new parent; false
846 * if such namespaces are to be undeclared
847 * @throws IllegalArgumentException if any of the replacement nodes is of the wrong kind. When replacing
848 * a child node, the replacement nodes must all be elements, text, comment, or PI nodes; when replacing
849 * an attribute, the replacement nodes must all be attributes.
850 * @throws IllegalStateException if this node is deleted or if it has no parent node.
851 * or if two replacement attributes have the same name.
852 */
853
854 public void replace(NodeInfo[] replacement, boolean inherit) {
855 if (isDeleted()) {
856 throw new IllegalStateException("Cannot replace a deleted node");
857 }
858 if (getParent() == null) {
859 throw new IllegalStateException("Cannot replace a parentless node");
860 }
861 assert parent != null;
862 parent.replaceChildrenAt(replacement, index, inherit);
863 parent = null;
864 index = -1; // mark the node as deleted
865 }
866
867 /**
868 * Insert a sequence of nodes as children of this node.
869 * <p>This method takes no action unless the target node is a document node or element node. It also
870 * takes no action in respect of any supplied nodes that are not elements, text nodes, comments, or
871 * processing instructions.</p>
872 * <p>The supplied nodes will form the new children. Adjacent text nodes will be merged, and
873 * zero-length text nodes removed. The supplied nodes may be modified in situ, for example to change their
874 * parent property and to add namespace bindings, or they may be copied, at the discretion of
875 * the implementation.</p>
876 *
877 * @param source the nodes to be inserted. The implementation determines what implementation classes
878 * of node it will accept; all implementations must accept nodes constructed using the Builder supplied
879 * by the {@link #newBuilder} method on this object. The supplied nodes may be modified in situ, for example
880 * to change their parent property and to add namespace bindings, but this depends on the implementation.
881 * The argument array may be modified as a result of the call.
882 * @param atStart true if the new nodes are to be inserted before existing children; false if they are
883 * to be inserted after existing children
884 * @param inherit true if the inserted nodes are to inherit the namespaces of their new parent; false
885 * if such namespaces are to be undeclared
886 * @throws IllegalArgumentException if the supplied nodes use a node implementation that this
887 * implementation does not accept.
888 */
889
890 public void insertChildren(NodeInfo[] source, boolean atStart, boolean inherit) {
891 // No action: node is not a document or element node
892 }
893
894 /**
895 * Insert copies of a sequence of nodes as siblings of this node.
896 * <p>This method takes no action unless the target node is an element, text node, comment, or
897 * processing instruction, and one that has a parent node. It also
898 * takes no action in respect of any supplied nodes that are not elements, text nodes, comments, or
899 * processing instructions.</p>
900 * <p>The supplied nodes must use the same data model implementation as the tree into which they
901 * will be inserted.</p>
902 *
903 * @param source the nodes to be inserted
904 * @param before true if the new nodes are to be inserted before the target node; false if they are
905 * @param inherit true if the inserted nodes are to inherit the namespaces of their new parent; false
906 * if such namespaces are to be undeclared
907 */
908
909 public void insertSiblings(NodeInfo[] source, boolean before, boolean inherit) {
910 if (parent == null) {
911 throw new IllegalStateException("Cannot add siblings if there is no parent");
912 }
913 parent.insertChildrenAt(source, before ? index : index + 1, inherit);
914 }
915
916
917 /**
918 * Remove type information from this node (and its ancestors, recursively).
919 * This method implements the upd:removeType() primitive defined in the XQuery Update specification
920 */
921
922 public void removeTypeAnnotation() {
923 // no action
924 }
925
926 /**
927 * Get a Builder suitable for building nodes that can be attached to this document.
928 *
929 * @return a new Builder that constructs nodes using the same object model implementation
930 * as this one, suitable for attachment to this tree
931 */
932
933 /*@NotNull*/
934 public Builder newBuilder() {
935 return getPhysicalRoot().newBuilder();
936 }
937
938 /**
939 * Get the effective boolean value of this sequence
940 *
941 * @return the effective boolean value
942 * @throws XPathException if the sequence has no effective boolean value (for example a sequence of two integers)
943 */
944 @Override
945 public boolean effectiveBooleanValue() throws XPathException {
946 return true;
947 }
948 }
949