| 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.om; |
| 9 | |
| 10 | import net.sf.saxon.expr.LastPositionFinder; |
| 11 | import net.sf.saxon.pattern.AnyNodeTest; |
| 12 | import net.sf.saxon.pattern.NodeTest; |
| 13 | import net.sf.saxon.trans.XPathException; |
| 14 | import net.sf.saxon.tree.iter.AxisIterator; |
| 15 | import net.sf.saxon.tree.iter.GroundedIterator; |
| 16 | import net.sf.saxon.tree.iter.LookaheadIterator; |
| 17 | import net.sf.saxon.tree.wrapper.SiblingCountingNode; |
| 18 | import net.sf.saxon.value.SequenceExtent; |
| 19 | |
| 20 | /** |
| 21 | * An iterator that maintains the values of position() and current(), as a wrapper |
| 22 | * over an iterator which does not maintain these values itself. |
| 23 | * |
| 24 | * <p>Note that when a FocusTrackingIterator is used to wrap a SequenceIterator |
| 25 | * in order to track the values of position() and current(), it is important to ensure |
| 26 | * (a) that the SequenceIterator is initially positioned at the start of the sequence, |
| 27 | * and (b) that all calls on next() to advance the iterator are directed at the |
| 28 | * FocusTrackingIterator, and not at the wrapped SequenceIterator.</p> |
| 29 | * |
| 30 | * @since 9.6 |
| 31 | */ |
| 32 | public class FocusTrackingIterator<T extends Item<?>> |
| 33 | implements FocusIterator<T>, LookaheadIterator<T>, GroundedIterator<T>, LastPositionFinder { |
| 34 | |
| 35 | private SequenceIterator<T> base; |
| 36 | private T curr; |
| 37 | private int pos = 0; |
| 38 | private int last = -1; |
| 39 | private SiblingMemory siblingMemory; |
| 40 | |
| 41 | |
| 42 | public static <T extends Item<?>> FocusTrackingIterator<T> track(SequenceIterator<T> base) { |
| 43 | return new FocusTrackingIterator<>(base); |
| 44 | } |
| 45 | |
| 46 | public FocusTrackingIterator(SequenceIterator<T> base) { |
| 47 | this.base = base; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Get the underlying iterator |
| 52 | * @return the iterator underlying this FocusIterator |
| 53 | */ |
| 54 | |
| 55 | public SequenceIterator<T> getUnderlyingIterator() { |
| 56 | return base; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Get the next item in the sequence. This method changes the state of the |
| 61 | * iterator, in particular it affects the result of subsequent calls of |
| 62 | * position() and current(). |
| 63 | * |
| 64 | * @return the next item, or null if there are no more items. Once a call |
| 65 | * on next() has returned null, no further calls should be made. The preferred |
| 66 | * action for an iterator if subsequent calls on next() are made is to return |
| 67 | * null again, and all implementations within Saxon follow this rule. |
| 68 | * @throws XPathException |
| 69 | * if an error occurs retrieving the next item |
| 70 | * @since 8.4 |
| 71 | */ |
| 72 | public T next() throws XPathException { |
| 73 | curr = base.next(); |
| 74 | if (curr == null) { |
| 75 | pos = -1; |
| 76 | } else { |
| 77 | pos++; |
| 78 | } |
| 79 | return curr; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Get the current value in the sequence (the one returned by the |
| 84 | * most recent call on next()). This will be null before the first |
| 85 | * call of next(). This method does not change the state of the iterator. |
| 86 | * |
| 87 | * @return the current item, the one most recently returned by a call on |
| 88 | * next(). Returns null if next() has not been called, or if the end |
| 89 | * of the sequence has been reached. |
| 90 | * @since 8.4 |
| 91 | */ |
| 92 | public T current() { |
| 93 | return curr; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Get the current position. This will usually be zero before the first call |
| 98 | * on next(), otherwise it will be the number of times that next() has |
| 99 | * been called. Once next() has returned null, the preferred action is |
| 100 | * for subsequent calls on position() to return -1, but not all existing |
| 101 | * implementations follow this practice. (In particular, the EmptyIterator |
| 102 | * is stateless, and always returns 0 as the value of position(), whether |
| 103 | * or not next() has been called.) |
| 104 | * <p>This method does not change the state of the iterator.</p> |
| 105 | * |
| 106 | * @return the current position, the position of the item returned by the |
| 107 | * most recent call of next(). This is 1 after next() has been successfully |
| 108 | * called once, 2 after it has been called twice, and so on. If next() has |
| 109 | * never been called, the method returns zero. If the end of the sequence |
| 110 | * has been reached, the value returned will always be <= 0; the preferred |
| 111 | * value is -1. |
| 112 | * @since 8.4 |
| 113 | */ |
| 114 | public int position() { |
| 115 | return pos; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Get the position of the last item in the sequence. The method is stateless in its external effect: |
| 120 | * that is, it does not change the values returned by position(), next(), hasNext(), etc. However, |
| 121 | * it creates a new base iterator which means that the result of calling getUnderlyingIterator() |
| 122 | * may change. |
| 123 | * @return the position of the last item |
| 124 | * @throws XPathException if a failure occurs reading the sequence |
| 125 | */ |
| 126 | |
| 127 | public int getLength() throws XPathException { |
| 128 | if (last == -1) { |
| 129 | if ((base.getProperties() & SequenceIterator.LAST_POSITION_FINDER) != 0) { |
| 130 | last = ((LastPositionFinder)base).getLength(); |
| 131 | } |
| 132 | if (last == -1) { |
| 133 | GroundedValue<T> residue = SequenceExtent.makeResidue(base); |
| 134 | last = pos + residue.getLength(); |
| 135 | base = residue.iterate(); |
| 136 | } |
| 137 | } |
| 138 | return last; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Determine whether there are more items to come. Note that this operation |
| 143 | * is stateless and it is not necessary (or usual) to call it before calling |
| 144 | * next(). It is used only when there is an explicit need to tell if we |
| 145 | * are at the last element. |
| 146 | * <p>This method must not be called unless the result of getProperties() on the iterator |
| 147 | * includes the bit setting {@link net.sf.saxon.om.SequenceIterator#LOOKAHEAD}</p> |
| 148 | * |
| 149 | * @return true if there are more items in the sequence |
| 150 | * @throws ClassCastException if the base iterator does not support lookahead processing |
| 151 | */ |
| 152 | public boolean hasNext() { |
| 153 | return ((LookaheadIterator)base).hasNext(); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Return a GroundedValue containing all the items in the sequence returned by this |
| 158 | * SequenceIterator. This should be an "in-memory" value, not a Closure. |
| 159 | * |
| 160 | * @return the corresponding Value |
| 161 | * @throws XPathException in the cases of subclasses (such as the iterator over a MemoClosure) |
| 162 | * which cause evaluation of expressions while materializing the value. |
| 163 | * @throws ClassCastException if the iterator does not have the {@link #GROUNDED} property. |
| 164 | */ |
| 165 | @Override |
| 166 | public GroundedValue<T> materialize() throws XPathException { |
| 167 | return base.materialize(); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Return a GroundedValue containing all the remaining items in the sequence returned by this |
| 172 | * SequenceIterator, starting at the current position. This should be an "in-memory" value, not a Closure. |
| 173 | * |
| 174 | * @return the corresponding Value |
| 175 | * @throws XPathException in the cases of subclasses (such as the iterator over a MemoClosure) |
| 176 | * which cause evaluation of expressions while materializing the value. |
| 177 | * @throws ClassCastException if the iterator does not have the {@link #GROUNDED} property. |
| 178 | */ |
| 179 | @Override |
| 180 | public GroundedValue<T> getResidue() throws XPathException { |
| 181 | return new SequenceExtent<>(this); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Close the iterator. This indicates to the supplier of the data that the client |
| 186 | * does not require any more items to be delivered by the iterator. This may enable the |
| 187 | * supplier to release resources. After calling close(), no further calls on the |
| 188 | * iterator should be made; if further calls are made, the effect of such calls is undefined. |
| 189 | * <p>(Currently, closing an iterator is important only when the data is being "pushed" in |
| 190 | * another thread. Closing the iterator terminates that thread and means that it needs to do |
| 191 | * no additional work. Indeed, failing to close the iterator may cause the push thread to hang |
| 192 | * waiting for the buffer to be emptied.)</p> |
| 193 | * |
| 194 | * @since 9.1 |
| 195 | */ |
| 196 | public void close() { |
| 197 | base.close(); |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Get properties of this iterator, as a bit-significant integer. |
| 202 | * |
| 203 | * @return the properties of this iterator. This will be some combination of |
| 204 | * properties such as {@link #GROUNDED}, {@link #LAST_POSITION_FINDER}, |
| 205 | * and {@link #LOOKAHEAD}. It is always |
| 206 | * acceptable to return the value zero, indicating that there are no known special properties. |
| 207 | * It is acceptable for the properties of the iterator to change depending on its state. |
| 208 | * @since 8.6 |
| 209 | */ |
| 210 | public int getProperties() { |
| 211 | return base.getProperties(); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Cached data to support optimization of the getSiblingPosition() method |
| 216 | */ |
| 217 | |
| 218 | private static class SiblingMemory { |
| 219 | public NodeTest mostRecentNodeTest = null; |
| 220 | public NodeInfo mostRecentNode = null; |
| 221 | public int mostRecentPosition = -1; |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Get the sibling position of a node: specifically, count how many preceding siblings |
| 226 | * of a node satisfy the nodetest. This method appears here because it can potentially |
| 227 | * make use of cached information. When an instruction such as {@code xsl:apply-templates |
| 228 | * select="*"} (which selects a set of sibling nodes) is used in conjunction with patterns |
| 229 | * such as {@code match="*[position() mod 2 = 1]}, then calculation of the position of one |
| 230 | * node in the sequence of siblings can take advantage of the fact that the position of the |
| 231 | * immediately preceding sibling is already known. |
| 232 | * |
| 233 | * <p>This optimization was suggested by the late Sebastian Rahtz, one of Saxon's earliest |
| 234 | * power users, and it is dedicated to his memory.</p> |
| 235 | * |
| 236 | * @param node the starting node, which is assumed to satisfy the node test |
| 237 | * @param nodeTest the node test |
| 238 | * @param max the maximum number of nodes to be counted |
| 239 | * @return the number of preceding siblings that satisfy the node test, plus one, unless the |
| 240 | * number exceeds max, in which case return some number greater than or equal to max. |
| 241 | */ |
| 242 | |
| 243 | public int getSiblingPosition(NodeInfo node, NodeTest nodeTest, int max) { |
| 244 | if (node instanceof SiblingCountingNode && nodeTest instanceof AnyNodeTest) { |
| 245 | return ((SiblingCountingNode) node).getSiblingPosition(); |
| 246 | } |
| 247 | if (siblingMemory == null) { |
| 248 | siblingMemory = new SiblingMemory(); |
| 249 | } else if (siblingMemory.mostRecentNodeTest.equals(nodeTest) && node.equals(siblingMemory.mostRecentNode)) { |
| 250 | return siblingMemory.mostRecentPosition; |
| 251 | } |
| 252 | SiblingMemory s = siblingMemory; |
| 253 | AxisIterator prev = node.iterateAxis(AxisInfo.PRECEDING_SIBLING, nodeTest); |
| 254 | NodeInfo prior; |
| 255 | int count = 1; |
| 256 | while ((prior = prev.next()) != null) { |
| 257 | if (prior.equals(s.mostRecentNode) && nodeTest.equals(s.mostRecentNodeTest)) { |
| 258 | int result = count + s.mostRecentPosition; |
| 259 | s.mostRecentNode = node; |
| 260 | s.mostRecentPosition = result; |
| 261 | return result; |
| 262 | } |
| 263 | if (++count > max) { |
| 264 | return count; |
| 265 | } |
| 266 | } |
| 267 | s.mostRecentNode = node; |
| 268 | s.mostRecentPosition = count; |
| 269 | s.mostRecentNodeTest = nodeTest; |
| 270 | return count; |
| 271 | } |
| 272 | } |