| 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.s9api.streams; |
| 9 | |
| 10 | import net.sf.saxon.s9api.Axis; |
| 11 | import net.sf.saxon.s9api.XdmItem; |
| 12 | import net.sf.saxon.s9api.XdmNode; |
| 13 | |
| 14 | import java.util.stream.Stream; |
| 15 | |
| 16 | /** |
| 17 | * An AxisStep is a {@link Step} that implements one of the 13 XPath Axes. |
| 18 | */ |
| 19 | class AxisStep extends Step<XdmNode> { |
| 20 | |
| 21 | private Axis axis; |
| 22 | |
| 23 | public AxisStep(Axis axis) { |
| 24 | this.axis = axis; |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Apply this function to the given argument. In other words, return a stream of nodes |
| 29 | * selected by this XPath axis, starting at the supplied origin |
| 30 | * |
| 31 | * @param node the origin node for the navigation |
| 32 | * @return the stream of nodes selected by the axis. The nodes are returned in Axis order |
| 33 | * (for a reverse axis like preceding-sibling or ancestor, this is the reverse of document |
| 34 | * order). If the supplied argument is not a node, return an empty stream. |
| 35 | */ |
| 36 | @Override |
| 37 | public Stream<? extends XdmNode> apply(XdmItem node) { |
| 38 | return node instanceof XdmNode ? ((XdmNode)node).axisIterator(axis).stream() : Stream.empty(); |
| 39 | } |
| 40 | } |
| 41 |