| 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.om.FocusIterator; |
| 11 | import net.sf.saxon.om.Item; |
| 12 | import net.sf.saxon.om.SequenceIterator; |
| 13 | import net.sf.saxon.trans.XPathException; |
| 14 | |
| 15 | /** |
| 16 | * ContextMappingIterator merges a sequence of sequences into a single flat |
| 17 | * sequence. It takes as inputs an iteration, and a mapping function to be |
| 18 | * applied to each Item returned by that iteration. The mapping function itself |
| 19 | * returns another iteration. The result is an iteration of the concatenation of all |
| 20 | * the iterations returned by the mapping function: often referred to as a flat-map operation. |
| 21 | * <p>This is related to the {@link MappingIterator} class: it differs in that it |
| 22 | * sets each item being processed as the context item</p> |
| 23 | * @param <T> the type of items returned by the mapping iterator (that is, the same |
| 24 | * as the type of items returned by the mapping function). |
| 25 | */ |
| 26 | |
| 27 | public final class ContextMappingIterator<T extends Item<?>> implements SequenceIterator<T> { |
| 28 | |
| 29 | private FocusIterator<?> base; |
| 30 | private ContextMappingFunction<T> action; |
| 31 | private XPathContext context; |
| 32 | private SequenceIterator<? extends T> stepIterator = null; |
| 33 | |
| 34 | /** |
| 35 | * Construct a ContextMappingIterator that will apply a specified ContextMappingFunction to |
| 36 | * each Item returned by the base iterator. |
| 37 | * |
| 38 | * @param action the mapping function to be applied |
| 39 | * @param context the processing context. The mapping function is applied to each item returned |
| 40 | * by context.getCurrentIterator() in turn. |
| 41 | */ |
| 42 | |
| 43 | public ContextMappingIterator(ContextMappingFunction<T> action, XPathContext context) { |
| 44 | base = context.getCurrentIterator(); |
| 45 | this.action = action; |
| 46 | this.context = context; |
| 47 | } |
| 48 | |
| 49 | public T next() throws XPathException { |
| 50 | T nextItem; |
| 51 | while (true) { |
| 52 | if (stepIterator != null) { |
| 53 | nextItem = stepIterator.next(); |
| 54 | if (nextItem != null) { |
| 55 | break; |
| 56 | } else { |
| 57 | stepIterator = null; |
| 58 | } |
| 59 | } |
| 60 | if (base.next() != null) { |
| 61 | // Call the supplied mapping function |
| 62 | stepIterator = action.map(context); |
| 63 | nextItem = stepIterator.next(); |
| 64 | if (nextItem == null) { |
| 65 | stepIterator = null; |
| 66 | } else { |
| 67 | break; |
| 68 | } |
| 69 | |
| 70 | } else { |
| 71 | stepIterator = null; |
| 72 | return null; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | return nextItem; |
| 77 | } |
| 78 | |
| 79 | public void close() { |
| 80 | base.close(); |
| 81 | if (stepIterator != null) { |
| 82 | stepIterator.close(); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Get properties of this iterator, as a bit-significant integer. |
| 88 | * |
| 89 | * @return the properties of this iterator. This will be some combination of |
| 90 | * properties such as {@link net.sf.saxon.om.SequenceIterator#GROUNDED}, {@link net.sf.saxon.om.SequenceIterator#LAST_POSITION_FINDER}, |
| 91 | * and {@link net.sf.saxon.om.SequenceIterator#LOOKAHEAD}. It is always |
| 92 | * acceptable to return the value zero, indicating that there are no known special properties. |
| 93 | * It is acceptable for the properties of the iterator to change depending on its state. |
| 94 | */ |
| 95 | |
| 96 | public int getProperties() { |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | } |
| 101 |