Debian Codegraph @

net/sf/saxon/tree/wrapper/VirtualCopy.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.wrapper;
9
10 import net.sf.saxon.Configuration;
11 import net.sf.saxon.event.Receiver;
12 import net.sf.saxon.expr.parser.Location;
13 import net.sf.saxon.om.*;
14 import net.sf.saxon.pattern.NodeTest;
15 import net.sf.saxon.trans.XPathException;
16 import net.sf.saxon.tree.NamespaceNode;
17 import net.sf.saxon.tree.iter.AxisIterator;
18 import net.sf.saxon.tree.iter.EmptyIterator;
19 import net.sf.saxon.tree.tiny.GraftedElement;
20 import net.sf.saxon.tree.util.FastStringBuffer;
21 import net.sf.saxon.tree.util.NamespaceIterator;
22 import net.sf.saxon.tree.util.Navigator;
23 import net.sf.saxon.type.SchemaType;
24 import net.sf.saxon.type.Type;
25
26 import javax.xml.transform.SourceLocator;
27 import java.util.*;
28
29 /**
30 * This class represents a node that is a virtual copy of another node: that is, it behaves as a node that's the
31 * same as another node, but has different identity. Moreover, this class can create a virtual copy of a subtree,
32 * so that the parent of the virtual copy is null rather than being a virtual copy of the parent of the original.
33 * This means that none of the axes when applied to the virtual copy is allowed to stray outside the subtree.
34 * The virtual copy is implemented by means of a reference to the node of which
35 * it is a copy, but methods that are sensitive to node identity return a different result.
36 *
37 * (From 9.9:) The virtual copy of the subtree may either be parentless, or it may have a new parent
38 * node different from the parent in the original tree. In the latter case, navigation using the parent
39 * or ancestor axis reverts to the tree into which this copy is grafted. This node is referred to as the
40 * grafting host. Navigation using the preceding and following axes can also stray from the copied subtree
41 * into the grafting host tree, but this is handled automatically because the iterators for the preceding
42 * and following axes are not primitive, but are defined in terms of other axes.
43 */
44
45 public class VirtualCopy implements NodeInfo {
46
47 protected String systemId;
48 protected NodeInfo original;
49 protected VirtualCopy parent;
50 protected NodeInfo root; // the node forming the root of the subtree that was copied
51 protected VirtualTreeInfo tree;
52 private boolean dropNamespaces = false;
53 private GraftedElement graftingHost = null;
54
55 /**
56 * Protected constructor: create a virtual copy of a node
57 *
58 * @param base the node to be copied
59 * @param root the node in the source tree corresponding to the root of the virtual tree. This must be an ancestor
60 * of the base node
61 */
62
63 protected VirtualCopy(NodeInfo base, NodeInfo root) {
64 original = base;
65 systemId = base.getBaseURI();
66 this.root = root;
67 }
68
69 /**
70 * Public factory method: create a parentless virtual tree as a copy of an existing node
71 *
72 * @param original the node to be copied
73 * @return the virtual copy. If the original was already a virtual copy, this will be a virtual copy
74 * of the real underlying node.
75 */
76
77 public static VirtualCopy makeVirtualCopy(NodeInfo original) {
78
79 VirtualCopy vc;
80 // Don't allow copies of copies of copies: define the new copy in terms of the original
81 while (original instanceof VirtualCopy) {
82 original = ((VirtualCopy) original).original;
83 }
84
85 vc = new VirtualCopy(original, original);
86
87 Configuration config = original.getConfiguration();
88 VirtualTreeInfo doc = new VirtualTreeInfo(config, vc);
89 long docNr = config.getDocumentNumberAllocator().allocateDocumentNumber();
90 doc.setDocumentNumber(docNr);
91 vc.tree = doc;
92
93 return vc;
94 }
95
96 /**
97 * Public factory method: create a virtual tree as a copy of an existing node, grafted onto a new
98 * parent
99 *
100 * @param original the node to be copied
101 * @param graftingHost the node in the host tree that represents the root of the grafted tree
102 * @return the virtual copy. If the original was already a virtual copy, this will be a virtual copy
103 * of the real underlying node.
104 */
105
106 public static VirtualCopy makeVirtualCopy(NodeInfo original, GraftedElement graftingHost) {
107
108 VirtualCopy vc = new VirtualCopy(original, original);
109
110 Configuration config = original.getConfiguration();
111 VirtualTreeInfo doc = new VirtualTreeInfo(config, vc);
112 long docNr = config.getDocumentNumberAllocator().allocateDocumentNumber();
113 doc.setDocumentNumber(docNr);
114 vc.tree = doc;
115 vc.graftingHost = graftingHost;
116 vc.systemId = graftingHost.getSystemId();
117 return vc;
118 }
119
120 /**
121 * Wrap a node within an existing VirtualCopy.
122 *
123 * @param node the node to be wrapped
124 * @return a virtual copy of the node
125 */
126
127 /*@NotNull*/
128 protected VirtualCopy wrap(NodeInfo node) {
129 VirtualCopy vc = new VirtualCopy(node, root);
130 vc.tree = tree;
131 vc.graftingHost = graftingHost;
132 vc.systemId = systemId;
133 vc.dropNamespaces = dropNamespaces;
134 return vc;
135 }
136
137 /**
138 * Get the original (wrapped) node
139 * @return the node of which this one is a virtual copy
140 */
141
142 public NodeInfo getOriginalNode() {
143 return original;
144 }
145
146 /**
147 * Get the grafting host: the node in the host tree that represents the same node as
148 * the external element at the root of the grafted subtree
149 */
150
151 public GraftedElement getGraftingHost() {
152 return graftingHost;
153 }
154
155 /**
156 * Get information about the tree to which this NodeInfo belongs
157 *
158 * @return the TreeInfo
159 * @since 9.7
160 */
161 public VirtualTreeInfo getTreeInfo() {
162 return tree;
163 }
164
165 /**
166 * Say that namespaces in the virtual tree should not be copied from the underlying
167 * tree. The semantics follow the rules for xsl:copy-of with copy-namespaces="no": that
168 * is, the only namespaces that are retained are those explicitly used in element or
169 * attribute nodes.
170 * @param drop true if namespaces are to be dropped
171 */
172
173 public void setDropNamespaces(boolean drop) {
174 this.dropNamespaces = drop;
175 }
176
177 /**
178 * Get fingerprint. The fingerprint is a coded form of the expanded name
179 * of the node: two nodes
180 * with the same name code have the same namespace URI and the same local name.
181 * The fingerprint contains no information about the namespace prefix. For a name
182 * in the null namespace, the fingerprint is the same as the name code.
183 *
184 * @return an integer fingerprint; two nodes with the same fingerprint have
185 * the same expanded QName. For unnamed nodes (text nodes, comments, document nodes,
186 * and namespace nodes for the default namespace), returns -1.
187 * @throws UnsupportedOperationException if this kind of node does not hold
188 * namepool fingerprints (specifically, if {@link #hasFingerprint()} returns false).
189 * @since 8.4 (moved into FingerprintedNode at some stage; then back into NodeInfo at 9.8).
190 */
191 @Override
192 public int getFingerprint() {
193 return original.getFingerprint();
194 }
195
196 /**
197 * Ask whether this NodeInfo implementation holds a fingerprint identifying the name of the
198 * node in the NamePool. If the answer is true, then the {@link #getFingerprint} method must
199 * return the fingerprint of the node. If the answer is false, then the {@link #getFingerprint}
200 * method should throw an {@code UnsupportedOperationException}. In the case of unnamed nodes
201 * such as text nodes, the result can be either true (in which case getFingerprint() should
202 * return -1) or false (in which case getFingerprint may throw an exception).
203 *
204 * @return true if the implementation of this node provides fingerprints.
205 * @since 9.8; previously Saxon relied on using <code>FingerprintedNode</code> as a marker interface.
206 */
207 @Override
208 public boolean hasFingerprint() {
209 return original.hasFingerprint();
210 }
211
212 /**
213 * Get the kind of node. This will be a value such as Type.ELEMENT or Type.ATTRIBUTE
214 *
215 * @return an integer identifying the kind of node. These integer values are the
216 * same as those used in the DOM
217 * @see net.sf.saxon.type.Type
218 */
219
220 public int getNodeKind() {
221 return original.getNodeKind();
222 }
223
224 /**
225 * Determine whether this is the same node as another node.
226 * Note: a.isSameNodeInfo(b) if and only if generateId(a)==generateId(b).
227 * This method has the same semantics as isSameNode() in DOM Level 3, but
228 * works on Saxon NodeInfo objects rather than DOM Node objects.
229 *
230 * @param other the node to be compared with this node
231 * @return true if this NodeInfo object and the supplied NodeInfo object represent
232 * the same node in the tree.
233 */
234
235 public boolean equals(Object other) {
236 return other instanceof VirtualCopy &&
237 getTreeInfo() == ((VirtualCopy) other).getTreeInfo() &&
238 original.equals(((VirtualCopy) other).original);
239 }
240
241 /**
242 * The hashCode() method obeys the contract for hashCode(): that is, if two objects are equal
243 * (represent the same node) then they must have the same hashCode()
244 */
245
246 public int hashCode() {
247 return original.hashCode() ^ ((int) (getTreeInfo().getDocumentNumber() & 0x7fffffff) << 19);
248 }
249
250 /**
251 * Get the System ID for the node.
252 *
253 * @return the System Identifier of the entity in the source document
254 * containing the node, or null if not known. Note this is not the
255 * same as the base URI: the base URI can be modified by xml:base, but
256 * the system ID cannot.
257 */
258
259 public String getSystemId() {
260 return systemId;
261 }
262
263 /**
264 * Get the Base URI for the node, that is, the URI used for resolving a relative URI contained
265 * in the node. This will be the same as the System ID unless xml:base has been used.
266 *
267 * @return the base URI of the node
268 */
269
270 /*@Nullable*/
271 public String getBaseURI() {
272 return Navigator.getBaseURI(this);
273 }
274
275 /**
276 * Get line number
277 *
278 * @return the line number of the node in its original source document; or
279 * -1 if not available
280 */
281
282 public int getLineNumber() {
283 return original.getLineNumber();
284 }
285
286 /**
287 * Get column number
288 *
289 * @return the column number of the node in its original source document; or -1 if not available
290 */
291
292 public int getColumnNumber() {
293 return original.getColumnNumber();
294 }
295
296 /**
297 * Get an immutable copy of this Location object. By default Location objects may be mutable, so they
298 * should not be saved for later use. The result of this operation holds the same location information,
299 * but in an immutable form.
300 */
301 public Location saveLocation() {
302 return this;
303 }
304
305 /**
306 * Determine the relative position of this node and another node, in document order.
307 * The other node will always be in the same document.
308 *
309 * @param other The other node, whose position is to be compared with this
310 * node
311 * @return -1 if this node precedes the other node, +1 if it follows the
312 * other node, or 0 if they are the same node. (In this case,
313 * isSameNode() will always return true, and the two nodes will
314 * produce the same result for generateId())
315 */
316
317 public int compareOrder(/*@NotNull*/ NodeInfo other) {
318 if (other instanceof VirtualCopy) {
319 if (graftingHost != null && ((VirtualCopy)other).graftingHost != null) {
320 int c = graftingHost.compareOrder(((VirtualCopy) other).graftingHost);
321 if (c == 0) {
322 return original.compareOrder(((VirtualCopy) other).original);
323 } else {
324 return c;
325 }
326 } else {
327 int c = root.compareOrder(((VirtualCopy) other).root);
328 if (c == 0) {
329 return original.compareOrder(((VirtualCopy) other).original);
330 } else {
331 return c;
332 }
333 }
334 } else {
335 return other.compareOrder(original);
336 }
337 }
338
339 /**
340 * Return the string value of the node. The interpretation of this depends on the type
341 * of node. For an element it is the accumulated character content of the element,
342 * including descendant elements.
343 *
344 * @return the string value of the node
345 */
346
347 public String getStringValue() {
348 return getStringValueCS().toString();
349 }
350
351 /**
352 * Get the value of the item as a CharSequence. This is in some cases more efficient than
353 * the version of the method that returns a String.
354 */
355
356 public CharSequence getStringValueCS() {
357 return original.getStringValueCS();
358 }
359
360 /**
361 * Get the local part of the name of this node. This is the name after the ":" if any.
362 *
363 * @return the local part of the name. For an unnamed node, returns "". Unlike the DOM
364 * interface, this returns the full name in the case of a non-namespaced name.
365 */
366
367 public String getLocalPart() {
368 return original.getLocalPart();
369 }
370
371 /**
372 * Get the URI part of the name of this node. This is the URI corresponding to the
373 * prefix, or the URI of the default namespace if appropriate.
374 *
375 * @return The URI of the namespace of this node. For an unnamed node,
376 * or for a node with an empty prefix, return an empty
377 * string.
378 */
379
380 public String getURI() {
381 return original.getURI();
382 }
383
384 /**
385 * Get the prefix of the name of the node. This is defined only for elements and attributes.
386 * If the node has no prefix, or for other kinds of node, return a zero-length string.
387 *
388 * @return The prefix of the name of the node.
389 */
390
391 public String getPrefix() {
392 return original.getPrefix();
393 }
394
395 /**
396 * Get the display name of this node. For elements and attributes this is [prefix:]localname.
397 * For unnamed nodes, it is an empty string.
398 *
399 * @return The display name of this node. For a node with no name, return
400 * an empty string.
401 */
402
403 public String getDisplayName() {
404 return original.getDisplayName();
405 }
406
407 /**
408 * Get the configuration
409 */
410
411 public Configuration getConfiguration() {
412 return original.getConfiguration();
413 }
414
415
416 /**
417 * Get the type annotation of this node, if any. The type annotation is represented as
418 * SchemaType object.
419 * <p>Types derived from a DTD are not reflected in the result of this method.</p>
420 *
421 * @return For element and attribute nodes: the type annotation derived from schema
422 * validation (defaulting to xs:untyped and xs:untypedAtomic in the absence of schema
423 * validation). For comments, text nodes, processing instructions, and namespaces: null.
424 * For document nodes, either xs:untyped if the document has not been validated, or
425 * xs:anyType if it has.
426 * @since 9.4
427 */
428 public SchemaType getSchemaType() {
429 return original.getSchemaType();
430 }
431
432 /**
433 * Get the NodeInfo object representing the parent of this node
434 *
435 * @return the parent of this node; null if this node has no parent
436 */
437
438 /*@Nullable*/
439 public NodeInfo getParent() {
440 if (original.equals(root)) {
441 return graftingHost==null ? null : graftingHost.getParent();
442 }
443 if (parent == null) {
444 NodeInfo basep = original.getParent();
445 if (basep == null) {
446 return null;
447 }
448 parent = wrap(basep);
449 }
450 return parent;
451 }
452
453 /**
454 * Return an iteration over all the nodes reached by the given axis from this node
455 * that match a given NodeTest
456 *
457 * @param axisNumber an integer identifying the axis; one of the constants
458 * defined in class net.sf.saxon.om.Axis
459 * @param nodeTest A pattern to be matched by the returned nodes; nodes
460 * that do not match this pattern are not included in the result
461 * @return an AxisIterator that scans the nodes reached by the axis in
462 * turn.
463 * @throws UnsupportedOperationException if the namespace axis is
464 * requested and this axis is not supported for this implementation.
465 * @see net.sf.saxon.om.AxisInfo
466 */
467
468 public AxisIterator iterateAxis(byte axisNumber, NodeTest nodeTest) {
469 VirtualCopy newParent = null;
470 switch (axisNumber) {
471 case AxisInfo.CHILD:
472 case AxisInfo.ATTRIBUTE:
473 newParent = this;
474 break;
475 case AxisInfo.SELF:
476 case AxisInfo.PRECEDING_SIBLING:
477 case AxisInfo.FOLLOWING_SIBLING:
478 newParent = parent;
479 break;
480 // Ensure that the ancestor, ancestor-or-self, following, and preceding axes use an implementation
481 // that relies on getParent() to escape from the subtree
482 case AxisInfo.ANCESTOR:
483 return new Navigator.AxisFilter(
484 new Navigator.AncestorEnumeration(this, false), nodeTest);
485 case AxisInfo.ANCESTOR_OR_SELF:
486 return new Navigator.AxisFilter(
487 new Navigator.AncestorEnumeration(this, true), nodeTest);
488 case AxisInfo.NAMESPACE:
489 if (getNodeKind() != Type.ELEMENT) {
490 return EmptyIterator.OfNodes.THE_INSTANCE;
491 }
492 return NamespaceNode.makeIterator(this, nodeTest);
493 case AxisInfo.PARENT:
494 return Navigator.filteredSingleton(getParent(), nodeTest);
495 case AxisInfo.PRECEDING:
496 return new Navigator.AxisFilter(
497 new Navigator.PrecedingEnumeration(this, false), nodeTest);
498 case AxisInfo.FOLLOWING:
499 return new Navigator.AxisFilter(
500 new Navigator.FollowingEnumeration(this), nodeTest);
501 case AxisInfo.PRECEDING_OR_ANCESTOR:
502 return new Navigator.AxisFilter(
503 new Navigator.PrecedingEnumeration(this, true), nodeTest);
504
505 }
506 return makeCopier(original.iterateAxis(axisNumber, nodeTest), newParent, !AxisInfo.isSubtreeAxis[axisNumber]);
507 }
508
509 /**
510 * Get the string value of a given attribute of this node
511 *
512 * @param uri the namespace URI of the attribute name. Supply the empty string for an attribute
513 * that is in no namespace
514 * @param local the local part of the attribute name.
515 * @return the attribute value if it exists, or null if it does not exist. Always returns null
516 * if this node is not an element.
517 */
518 public String getAttributeValue(/*@NotNull*/ String uri, /*@NotNull*/ String local) {
519 return original.getAttributeValue(uri, local);
520 }
521
522 /**
523 * Get the root node of the tree containing this node
524 *
525 * @return the NodeInfo representing the top-level ancestor of this node.
526 * This will not necessarily be a document node
527 */
528
529 /*@Nullable*/
530 public NodeInfo getRoot() {
531 NodeInfo n = this;
532 while (true) {
533 NodeInfo p = n.getParent();
534 if (p == null) {
535 return n;
536 }
537 n = p;
538 }
539 }
540
541 /**
542 * Determine whether the node has any children.
543 * <p>Note: the result is equivalent to
544 * <code>getEnumeration(Axis.CHILD, AnyNodeTest.getInstance()).hasNext()</code></p>
545 *
546 * @return True if the node has one or more children
547 */
548
549 public boolean hasChildNodes() {
550 return original.hasChildNodes();
551 }
552
553 /**
554 * Get a character string that uniquely identifies this node.
555 * Note: a.isSameNode(b) if and only if generateId(a)==generateId(b)
556 *
557 * @param buffer a buffer, to which will be appended
558 * a string that uniquely identifies this node, across all
559 * documents.
560 */
561
562 public void generateId(/*@NotNull*/ FastStringBuffer buffer) {
563 buffer.append("d");
564 buffer.append(Long.toString(getTreeInfo().getDocumentNumber()));
565 original.generateId(buffer);
566 }
567
568 /**
569 * Copy this node to a given outputter
570 * @param out the Receiver to which the node should be copied
571 * @param copyOptions a selection of the options defined in {@link CopyOptions}
572 * @param locationId Identifies the location of the instruction
573 */
574
575 public void copy(Receiver out, int copyOptions, Location locationId) throws XPathException {
576 original.copy(out, copyOptions, locationId);
577 }
578
579 /**
580 * Get all namespace declarations and undeclarations defined on this element.
581 *
582 * @param buffer If this is non-null, and the result array fits in this buffer, then the result
583 * may overwrite the contents of this array, to avoid the cost of allocating a new array on the heap.
584 * @return An array representing the namespace declarations and undeclarations present on
585 * this element. For a node other than an element, return null. Otherwise, the returned array is a
586 * sequence of NamespaceBinding objects.
587 * The XML namespace is never included in the list. If the supplied array is larger than required,
588 * then the first unused entry will be set to null.
589 * <p>For a VirtualCopy, the method needs to return all namespaces that are in scope for
590 * this element, because the virtual copy is assumed to contain copies of all the in-scope
591 * namespaces of the original.</p>
592 */
593
594 public NamespaceBinding[] getDeclaredNamespaces(NamespaceBinding[] buffer) {
595 if (getNodeKind() == Type.ELEMENT) {
596 if (dropNamespaces) {
597 List<NamespaceBinding> allNamespaces = new ArrayList<>(5);
598 String ns = getURI();
599 if (ns.isEmpty()) {
600 if (getParent() != null && !getParent().getURI().isEmpty()) {
601 allNamespaces.add(new NamespaceBinding("", ""));
602 }
603 } else {
604 allNamespaces.add(new NamespaceBinding(getPrefix(), getURI()));
605 }
606 original.iterateAxis(AxisInfo.ATTRIBUTE).forEach(att -> {
607 if (att.getURI() != null) {
608 NamespaceBinding b = new NamespaceBinding(att.getPrefix(), att.getURI());
609 if (!allNamespaces.contains(b)) {
610 allNamespaces.add(b);
611 }
612 }
613 });
614 return allNamespaces.toArray(NamespaceBinding.EMPTY_ARRAY);
615 } else {
616 if (original == root) {
617 List<NamespaceBinding> bindings = new ArrayList<>();
618 Iterator<NamespaceBinding> iter = NamespaceIterator.iterateNamespaces(original);
619 boolean declaresDefaultNamespace = false;
620 while (iter.hasNext()) {
621 NamespaceBinding binding = iter.next();
622 bindings.add(binding);
623 if (binding.getPrefix().isEmpty()) {
624 declaresDefaultNamespace = true;
625 }
626 }
627 if (!declaresDefaultNamespace && graftingHost != null && graftingHost.hasDefaultNamespace()) {
628 bindings.add(NamespaceBinding.DEFAULT_UNDECLARATION);
629 }
630 return bindings.toArray(NamespaceBinding.EMPTY_ARRAY);
631 } else {
632 return original.getDeclaredNamespaces(buffer);
633 }
634 }
635 } else {
636 return null;
637 }
638 }
639
640 /**
641 * Set the system identifier for this Source.
642 * <p>The system identifier is optional if the source does not
643 * get its data from a URL, but it may still be useful to provide one.
644 * The application can use a system identifier, for example, to resolve
645 * relative URIs and to include in error messages and warnings.</p>
646 *
647 * @param systemId The system identifier as a URL string.
648 */
649 public void setSystemId(String systemId) {
650 this.systemId = systemId;
651 }
652
653 /**
654 * Get the typed value.
655 *
656 * @return the typed value. If requireSingleton is set to true, the result will always be an
657 * AtomicValue. In other cases it may be a Value representing a sequence whose items are atomic
658 * values.
659 * @since 8.5
660 */
661
662 public AtomicSequence atomize() throws XPathException {
663 return original.atomize();
664 }
665
666 /**
667 * Determine whether this node has the is-id property
668 *
669 * @return true if the node is an ID
670 */
671
672 public boolean isId() {
673 return original.isId();
674 }
675
676 /**
677 * Determine whether this node has the is-idref property
678 *
679 * @return true if the node is an IDREF or IDREFS element or attribute
680 */
681
682 public boolean isIdref() {
683 return original.isIdref();
684 }
685
686 /**
687 * Determine whether the node has the is-nilled property
688 *
689 * @return true if the node has the is-nilled property
690 */
691
692 public boolean isNilled() {
693 return original.isNilled();
694 }
695
696 /**
697 * Return the public identifier for the current document event.
698 * <p>The return value is the public identifier of the document
699 * entity or of the external parsed entity in which the markup that
700 * triggered the event appears.</p>
701 *
702 * @return A string containing the public identifier, or
703 * null if none is available.
704 * @see #getSystemId
705 */
706 /*@Nullable*/
707 public String getPublicId() {
708 return original instanceof SourceLocator ? original.getPublicId() : null;
709 }
710
711 /**
712 * Ask whether a node in the source tree is within the scope of this virtual copy
713 * @param sourceNode the node being tested
714 * @return true if the node is within the scope of the subtree
715 */
716
717 protected boolean isIncludedInCopy(NodeInfo sourceNode) {
718 return Navigator.isAncestorOrSelf(root, sourceNode);
719 }
720
721 /**
722 * Create an iterator that makes and returns virtual copies of nodes on the original tree
723 *
724 * @param axis the axis to be navigated
725 * @param newParent the parent of the nodes in the new virtual tree (may be null)
726 * @param testInclusion if true, it is necessary to test whether nodes found in the source tree are
727 * included in the copy. If false, this test is unnecessary.
728 * @return the iterator that does the copying
729 */
730
731 protected VirtualCopier makeCopier(AxisIterator axis, VirtualCopy newParent, boolean testInclusion) {
732 return new VirtualCopier(axis, newParent, testInclusion);
733 }
734
735 /**
736 * VirtualCopier implements the XPath axes as applied to a VirtualCopy node. It works by
737 * applying the requested axis to the node of which this is a copy. There are two
738 * complications: firstly, all nodes encountered must themselves be (virtually) copied
739 * to give them a new identity. Secondly, axes that stray outside the subtree rooted at
740 * the original copied node must be truncated.
741 */
742
743 protected class VirtualCopier implements AxisIterator {
744
745 protected AxisIterator base;
746 private VirtualCopy parent;
747 protected boolean testInclusion;
748
749 public VirtualCopier(AxisIterator base, VirtualCopy parent, boolean testInclusion) {
750 this.base = base;
751 this.parent = parent;
752 this.testInclusion = testInclusion;
753 }
754
755
756 /**
757 * Get the next item in the sequence. <BR>
758 *
759 * @return the next Item. If there are no more nodes, return null.
760 */
761
762 /*@Nullable*/
763 public NodeInfo next() {
764 NodeInfo next = base.next();
765 if (next != null) {
766 if (testInclusion && !isIncludedInCopy(next)) {
767 // we're only interested in nodes within the subtree that was copied.
768 // Assert: once we find a node outside this subtree, all further nodes will also be outside
769 // the subtree.
770 return null;
771 }
772 VirtualCopy vc = wrap(next);
773 vc.parent = parent;
774 vc.systemId = systemId;
775 next = vc;
776 }
777 return next;
778 }
779
780 public void close() {
781 base.close();
782 }
783
784 /**
785 * Get properties of this iterator, as a bit-significant integer.
786 *
787 * @return the properties of this iterator. This will be some combination of
788 * properties such as {@link #GROUNDED}, {@link #LAST_POSITION_FINDER},
789 * and {@link #LOOKAHEAD}. It is always
790 * acceptable to return the value zero, indicating that there are no known special properties.
791 * It is acceptable for the properties of the iterator to change depending on its state.
792 */
793
794 public int getProperties() {
795 return 0;
796 }
797
798 }
799 }
800