| 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.expr; |
| 9 | |
| 10 | import net.sf.saxon.expr.sort.ItemOrderComparer; |
| 11 | import net.sf.saxon.om.NodeInfo; |
| 12 | import net.sf.saxon.om.SequenceIterator; |
| 13 | import net.sf.saxon.trans.XPathException; |
| 14 | |
| 15 | |
| 16 | /** |
| 17 | * An enumeration representing a nodeset that is teh difference of two other NodeSets. |
| 18 | * There is an "except" operator in XPath 2.0 to create such an expression. |
| 19 | */ |
| 20 | |
| 21 | |
| 22 | public class DifferenceEnumeration implements SequenceIterator<NodeInfo> { |
| 23 | |
| 24 | |
| 25 | private SequenceIterator<? extends NodeInfo> p1; |
| 26 | private SequenceIterator<? extends NodeInfo> p2; |
| 27 | |
| 28 | /*@Nullable*/ private NodeInfo nextNode1 = null; |
| 29 | private NodeInfo nextNode2 = null; |
| 30 | private ItemOrderComparer comparer; |
| 31 | |
| 32 | /** |
| 33 | * Form an enumeration of the difference of two nodesets, that is, the nodes |
| 34 | * that are in p1 and that are not in p2. |
| 35 | * |
| 36 | * @param p1 the first operand, with nodes delivered in document order |
| 37 | * @param p2 the second operand, with nodes delivered in document order |
| 38 | * @param comparer the comparer |
| 39 | * @throws XPathException if a failure occurs reading from either input sequence |
| 40 | */ |
| 41 | |
| 42 | public DifferenceEnumeration(SequenceIterator<? extends NodeInfo> p1, SequenceIterator<? extends NodeInfo> p2, |
| 43 | ItemOrderComparer comparer) throws XPathException { |
| 44 | this.p1 = p1; |
| 45 | this.p2 = p2; |
| 46 | this.comparer = comparer; |
| 47 | |
| 48 | // move to the first node in each input nodeset |
| 49 | |
| 50 | nextNode1 = next(p1); |
| 51 | nextNode2 = next(p2); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Get the next item from one of the input sequences, |
| 56 | * checking that it is a node. |
| 57 | * |
| 58 | * @param iter the iterator from which the next node is to be read |
| 59 | * @return the node that was read, or null if the stream is exhausted |
| 60 | * @throws XPathException if a failure occurs reading the input |
| 61 | */ |
| 62 | |
| 63 | private NodeInfo next(SequenceIterator iter) throws XPathException { |
| 64 | return (NodeInfo)iter.next(); |
| 65 | // rely on type-checking to prevent a ClassCastException |
| 66 | } |
| 67 | |
| 68 | public NodeInfo next() throws XPathException { |
| 69 | // main merge loop: if the node in p1 has a lower key value that that in p2, return it; |
| 70 | // if they are equal, advance both nodesets; if p1 is higher, advance p2. |
| 71 | |
| 72 | while (true) { |
| 73 | |
| 74 | if (nextNode1 == null) { |
| 75 | return null; |
| 76 | } |
| 77 | |
| 78 | if (nextNode2 == null) { |
| 79 | // second node-set is exhausted; return the next node from the first node-set |
| 80 | return deliver(); |
| 81 | } |
| 82 | |
| 83 | int c = comparer.compare(nextNode1, nextNode2); |
| 84 | if (c < 0) { // p1 is lower |
| 85 | return deliver(); |
| 86 | |
| 87 | } else if (c > 0) { // p1 is higher |
| 88 | nextNode2 = next(p2); |
| 89 | if (nextNode2 == null) { |
| 90 | return deliver(); |
| 91 | } |
| 92 | |
| 93 | } else { // keys are equal |
| 94 | nextNode2 = next(p2); |
| 95 | nextNode1 = next(p1); |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Deliver the next node from the first node-set, advancing the iterator to |
| 102 | * look-ahead for the next item, and setting the current and position variables. |
| 103 | * |
| 104 | * @return the next node from the first node-set |
| 105 | * @throws XPathException if a failure occurs reading from the input |
| 106 | */ |
| 107 | private NodeInfo deliver() throws XPathException { |
| 108 | NodeInfo current = nextNode1; |
| 109 | nextNode1 = next(p1); |
| 110 | return current; |
| 111 | } |
| 112 | |
| 113 | public void close() { |
| 114 | p1.close(); |
| 115 | p2.close(); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Get properties of this iterator, as a bit-significant integer. |
| 120 | * |
| 121 | * @return the properties of this iterator. This will be some combination of |
| 122 | * properties such as {@link SequenceIterator#GROUNDED}, {@link SequenceIterator#LAST_POSITION_FINDER}, |
| 123 | * and {@link SequenceIterator#LOOKAHEAD}. It is always |
| 124 | * acceptable to return the value zero, indicating that there are no known special properties. |
| 125 | * It is acceptable for the properties of the iterator to change depending on its state. |
| 126 | */ |
| 127 | |
| 128 | public int getProperties() { |
| 129 | return 0; |
| 130 | } |
| 131 | } |
| 132 |