Debian Codegraph @

net/sf/saxon/s9api/streams/Steps.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
9 package net.sf.saxon.s9api.streams;
10
11 import net.sf.saxon.lib.ConversionRules;
12 import net.sf.saxon.ma.arrays.ArrayItem;
13 import net.sf.saxon.om.AtomicSequence;
14 import net.sf.saxon.om.NameChecker;
15 import net.sf.saxon.om.NodeInfo;
16 import net.sf.saxon.pattern.NodeTest;
17 import net.sf.saxon.s9api.*;
18 import net.sf.saxon.trans.XPathException;
19 import net.sf.saxon.tree.iter.UnfailingIterator;
20 import net.sf.saxon.type.AtomicType;
21 import net.sf.saxon.type.Converter;
22 import net.sf.saxon.type.Type;
23 import net.sf.saxon.type.ValidationException;
24 import net.sf.saxon.value.AtomicValue;
25 import net.sf.saxon.value.StringValue;
26 import net.sf.saxon.value.Whitespace;
27
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.List;
31 import java.util.function.Predicate;
32 import java.util.stream.Stream;
33
34 import static net.sf.saxon.s9api.streams.Predicates.*;
35
36 /**
37 * This non-instantiable class provides a number of useful implementations of the {@link Step}
38 * interface, used to navigate XDM trees, typically as an argument to {@link XdmValue#select}.
39 */
40
41 public class Steps {
42 /**
43 * Obtain a {@link Step} that selects the root node of the containing document (which may or may not
44 * be a document node)
45 */
46
47 public static Step<XdmNode> root() {
48 return new Step<XdmNode>() {
49 public Stream<XdmNode> apply(XdmItem origin) {
50 return origin instanceof XdmNode
51 ? Stream.of(((XdmNode)origin).getRoot())
52 : Stream.empty();
53 }
54 };
55 }
56 /**
57 * Obtain a {@link Step} that atomizes an item to produce a stream of atomic values. (Atomizing a node will
58 * usually produce a single atomic value, but in the case of schema-typed nodes using a list
59 * type, there may be more than one atomic value. Atomizing an array also returns multiple
60 * atomic values)
61 */
62
63 public static Step<XdmAtomicValue> atomize() {
64 return new Step<XdmAtomicValue>() {
65 @Override
66 public Stream<? extends XdmAtomicValue> apply(XdmItem item) {
67 if (item instanceof XdmAtomicValue) {
68 return Stream.of((XdmAtomicValue)item);
69 } else if (item instanceof XdmNode) {
70 try {
71 return (XdmStream<XdmAtomicValue>) ((XdmNode) item).getTypedValue().stream();
72 } catch (SaxonApiException e) {
73 throw new SaxonApiUncheckedException(e);
74 }
75 } else if (item instanceof XdmArray) {
76 try {
77 ArrayItem arrayItem = ((XdmArray)item).getUnderlyingValue();
78 AtomicSequence data = arrayItem.atomize();
79 return (XdmStream<XdmAtomicValue>)XdmValue.wrap(data).stream();
80 } catch (XPathException e) {
81 throw new SaxonApiUncheckedException(new SaxonApiException(e));
82 }
83 } else {
84 throw new SaxonApiUncheckedException(new SaxonApiException("Cannot atomize supplied value"));
85 }
86 }
87 };
88 }
89
90 /**
91 * A step equivalent to the XPath "cast as" operator: the supplied item is atomized
92 * if necessary, and the resulting atomic values are cast to the required type
93 */
94
95 public static Step<XdmAtomicValue> castAs(ItemType type) {
96 if (!ItemType.ANY_ATOMIC_VALUE.subsumes(type)) {
97 throw new IllegalArgumentException("Target of castAs must be an atomic type");
98 }
99 final net.sf.saxon.type.ItemType tType = type.getUnderlyingItemType().getPrimitiveItemType();
100 final ConversionRules rules = type.getConversionRules();
101
102 return atomize().then(new Step<XdmAtomicValue>() {
103 public Stream<? extends XdmAtomicValue> apply(XdmItem xdmItem) {
104 try {
105 AtomicValue source = ((XdmAtomicValue)xdmItem).getUnderlyingValue();
106 Converter converter = rules.getConverter(source.getItemType(), (AtomicType)tType);
107 AtomicValue result = converter.convert(source).asAtomic();
108 return Stream.of((XdmAtomicValue) XdmValue.wrap(result));
109 } catch (ValidationException e) {
110 throw new SaxonApiUncheckedException(new SaxonApiException(e));
111 }
112 }
113 });
114 }
115
116 /**
117 * A {@code Step} to navigate from a node to its ancestors, in reverse document
118 * order (that is, nearest ancestor first, root node last)
119 */
120
121 private final static Step<XdmNode> ANCESTOR = new AxisStep(Axis.ANCESTOR);
122 /**
123 * A {@code Step} to navigate from a node to its ancestors, in reverse document
124 * order, with the node itself returned at the start of the sequence (that is, origin node first, root node last)
125 */
126
127 private final static Step<XdmNode> ANCESTOR_OR_SELF = new AxisStep(Axis.ANCESTOR_OR_SELF);
128 /**
129 * A {@code Step} to navigate from a node to its attributes
130 */
131
132 private final static Step<XdmNode> ATTRIBUTE = new AxisStep(Axis.ATTRIBUTE);
133 /**
134 * A Step to navigate from a node to its children
135 */
136
137 private final static Step<XdmNode> CHILD = new AxisStep(Axis.CHILD);
138 /**
139 * A Step to navigate from a node to its descendants, which are returned in document order
140 */
141
142 private final static Step<XdmNode> DESCENDANT = new AxisStep(Axis.DESCENDANT);
143 /**
144 * A Step to navigate from a node to its descendants, which are returned in document order,
145 * preceded by the origin node itself
146 */
147
148 private final static Step<XdmNode> DESCENDANT_OR_SELF = new AxisStep(Axis.DESCENDANT_OR_SELF);
149 /**
150 * A Step to navigate from a node to its following nodes (excluding descendants), which are returned in document order
151 */
152
153 private final static Step<XdmNode> FOLLOWING = new AxisStep(Axis.FOLLOWING);
154 /**
155 * A Step to navigate from a node to its following siblings, which are returned in document order
156 */
157
158 private final static Step<XdmNode> FOLLOWING_SIBLING = new AxisStep(Axis.FOLLOWING_SIBLING);
159 /**
160 * A {@code Step} to navigate from a node to its namespace nodes
161 */
162
163 private final static Step<XdmNode> NAMESPACE = new AxisStep(Axis.NAMESPACE);
164 /**
165 * A Step to navigate from a node to its parent
166 */
167
168 private final static Step<XdmNode> PARENT = new AxisStep(Axis.PARENT);
169 /**
170 * A Step to navigate from a node to its preceding siblings, which are returned in reverse document order
171 */
172
173 private final static Step<XdmNode> PRECEDING_SIBLING = new AxisStep(Axis.PRECEDING_SIBLING);
174 /**
175 * A Step to navigate from a node to its preceding nodes (excluding ancestors), which are returned in reverse document order
176 */
177
178 private final static Step<XdmNode> PRECEDING = new AxisStep(Axis.PRECEDING);
179 /**
180 * A Step to navigate from a node to itself (useful only if applying a predicate)
181 */
182
183 private final static Step<XdmNode> SELF = new AxisStep(Axis.SELF);
184
185 /**
186 * Obtain a Step that always returns an empty sequence, whatever the input
187 * @return a Step that always returns an empty sequence
188 */
189
190 public static <U extends XdmItem> Step<U> nothing() {
191 return new Step<U>() {
192 public Stream<U> apply(XdmItem xdmItem) {
193 return Stream.empty();
194 }
195 };
196 }
197
198 private static Predicate<XdmNode> nodeTestPredicate (NodeTest test) {
199 return item -> test.matchesNode(item.getUnderlyingNode());
200 }
201
202 private static Predicate<? super XdmNode> localNamePredicate(String given) {
203 if ("*".equals(given)) {
204 return isElement();
205 } else {
206 return item -> {
207 NodeInfo node = item.getUnderlyingNode();
208 return node.getNodeKind() == Type.ELEMENT
209 && node.getLocalPart().equals(given);
210 };
211 }
212 }
213
214 private static Predicate<? super XdmNode> expandedNamePredicate(String ns, String local) {
215 return item -> {
216 NodeInfo node = item.getUnderlyingNode();
217 return node.getNodeKind() == Type.ELEMENT
218 && node.getLocalPart().equals(local) && node.getURI().equals(ns);
219 };
220 }
221
222 /**
223 * Obtain a {@code Step} to navigate from a node to its ancestors, in reverse document
224 * order (that is, nearest ancestor first, root node last)
225 * @return a Step that selects all nodes on the ancestor axis
226 */
227 public static Step<XdmNode> ancestor() {
228 return ANCESTOR;
229 }
230
231 /**
232 * Obtain a {@code Step} that navigates from a node to its ancestor elements having a specified
233 * local name, irrespective of the namespace. The nodes are returned in reverse document
234 * order (that is, nearest ancestor first, root node last)
235 *
236 * @param localName the local name of the ancestors to be selected by the {@code Step},
237 * or "*" to select all ancestors that are element nodes
238 * @return a {@code Step}, which selects the ancestors of a supplied node that have the
239 * required local name.
240 */
241
242 public static Step<XdmNode> ancestor(String localName) {
243 return ancestor().where(localNamePredicate(localName));
244 }
245
246 /**
247 * Return a {@code Step} that navigates from a node to its ancestors having a specified
248 * namespace URI and local name, in reverse document order (that is, nearest ancestor first,
249 * root node last)
250 *
251 * @param uri the namespace URI of the ancestors to be selected by the {@code Step}
252 * @param localName the local name of the ancestors to be selected by the {@code Step}:
253 * supply a zero-length string to indicate the null namespace
254 * @return a {@code Step}, which selects the ancestors (at most one) of a supplied node that have the
255 * required local name and namespace URI.
256 */
257
258 public static Step<XdmNode> ancestor(String uri, String localName) {
259 return ancestor().where(expandedNamePredicate(uri, localName));
260 }
261
262 /**
263 * Obtain a {@code Step} that filters the nodes found on the ancestor axis using a supplied {@code Predicate}.
264 * Nodes are returned in reverse document order (that is, nearest ancestor first, root node last)
265 * <p>The function call {@code ancestor(predicate)} is equivalent to {@code ANCESTOR.where(predicate)}.</p>
266 *
267 * @param filter the predicate to be applied
268 * @return a {@code Step} that filters the nodes found on the ancestor axis using a supplied {@code Predicate}.
269 */
270
271 public static Step<XdmNode> ancestor(Predicate<? super XdmNode> filter) {
272 return ancestor().where(filter);
273 }
274
275 /**
276 * Obtain a {@code Step} to navigate from a node to its ancestors, in reverse document
277 * order, with the node itself returned at the start of the sequence (that is, origin node first,
278 * root node last)
279 * @return a Step that selects all nodes on the ancestor-or-self axis
280 */
281 public static Step<XdmNode> ancestorOrSelf() {
282 return ANCESTOR_OR_SELF;
283 }
284
285 /**
286 * Obtain a {@code Step} that navigates from a node to its ancestor elements having a specified
287 * local name, irrespective of the namespace. The nodes are returned in reverse document
288 * order (that is, nearest ancestor first, root node last), and include the node itself
289 *
290 * @param localName the local name of the ancestors to be selected by the {@code Step},
291 * or "*" to select all ancestor-or-self nodes that are element nodes
292 * @return a {@code Step}, which selects the ancestors-or-self of a supplied node that have the
293 * required local name.
294 */
295
296 public static Step<XdmNode> ancestorOrSelf(String localName) {
297 return ancestorOrSelf().where(localNamePredicate(localName));
298 }
299
300 /**
301 * Obtain a {@code Step} that navigates from a node to its ancestors-or-self having a specified
302 * namespace URI and local name, in reverse document order (that is, nearest ancestor first,
303 * root node last)
304 *
305 * @param uri the namespace URI of the ancestors to be selected by the {@code Step}:
306 * supply a zero-length string to indicate the null namespace
307 * @param localName the local name of the ancestors to be selected by the {@code Step}
308 * @return a {@code Step}, which selects the ancestors-or-self of a supplied node that have the
309 * required local name and namespace URI.
310 */
311
312 public static Step<XdmNode> ancestorOrSelf(String uri, String localName) {
313 return ancestorOrSelf().where(expandedNamePredicate(uri, localName));
314 }
315
316 /**
317 * Obtain a {@code Step} that filters the nodes found on the ancestor-or-self axis using a supplied {@code Predicate}.
318 * Nodes are returned in reverse document order (that is, origin node first,
319 * root node last)
320 * <p>The function call {@code ancestorOrSelf(predicate)} is equivalent to {@code ANCESTOR_OR_SELF.where(predicate)}.</p>
321 *
322 * @param filter the predicate to be applied
323 * @return a {@code Step} that filters the nodes found on the ancestor-or-self axis using a supplied {@code Predicate}.
324 */
325
326 public static Step<XdmNode> ancestorOrSelf(Predicate<? super XdmNode> filter) {
327 return ancestorOrSelf().where(filter);
328 }
329
330 /**
331 * Obtain a {@code Step} to navigate from a node to its attributes
332 * @return a Step that selects all nodes on the ancestor axis
333 */
334 public static Step<XdmNode> attribute() {
335 return ATTRIBUTE;
336 }
337
338 /**
339 * Obtain a {@code Step} that navigates from a node to its attributes having a specified
340 * local name, irrespective of the namespace
341 *
342 * @param localName the local name of the attributes to be selected by the {@code Step}, or
343 * "*" to select all attributes
344 * @return a {@code Step}, which selects the attributes of a supplied node that have the
345 * required local name.
346 */
347
348 public static Step<XdmNode> attribute(String localName) {
349 return "*".equals(localName) ? attribute() : attribute().where(hasLocalName(localName));
350 }
351
352 /**
353 * Return a {@code Step} that navigates from a node to its attribute having a specified
354 * namespace URI and local name
355 *
356 * @param uri the namespace URI of the attributes to be selected by the {@code Step}:
357 * supply a zero-length string to indicate the null namespace
358 * @param localName the local name of the attributes to be selected by the {@code Step}
359 * @return a {@code Step}, which selects the attributes (at most one) of a supplied node that have the
360 * required local name and namespace URI.
361 */
362
363 public static Step<XdmNode> attribute(String uri, String localName) {
364 return attribute().where(hasName(uri, localName));
365 }
366
367 /**
368 * Obtain a {@code Step} that filters the nodes found on the attribute axis using a supplied {@code Predicate}.
369 * The function call {@code attribute(predicate)} is equivalent to {@code ATTRIBUTE.where(predicate)}.
370 *
371 * @param filter the predicate to be applied
372 * @return a {@code Step} that filters the nodes found on the attribute axis using a supplied {@code Predicate}.
373 */
374
375 public static Step<XdmNode> attribute(Predicate<? super XdmNode> filter) {
376 return attribute().where(filter);
377 }
378
379 /**
380 * Obtain a {@link Step} to navigate from a node to its children
381 * @return a Step that selects all nodes on the child axis
382 */
383 public static Step<XdmNode> child() {
384 return CHILD;
385 }
386
387 /**
388 * Obtain a {@code Step} that navigates from a node to the element children having a specified
389 * local name, irrespective of the namespace
390 * @param localName the local name of the child elements to be selected by the Step,
391 * or "*" to select all children that are element nodes
392 * @return a {@code Step}, which selects the element children of a supplied node that have the
393 * required local name.
394 */
395
396 public static Step<XdmNode> child(String localName) {
397 return child().where(localNamePredicate(localName));
398 }
399
400 /**
401 * Obtain a {@code Step} that navigates from a node to the element children having a specified
402 * namespace URI and local name
403 * @param uri the namespace URI of the child elements to be selected by the {@code Step}:
404 * supply a zero-length string to indicate the null namespace
405 * @param localName the local name of the child elements to be selected by the {@code Step}
406 * @return a {@code Step}, which selects the element children of a supplied node that have the
407 * required local name and namespace URI.
408 */
409
410 public static Step<XdmNode> child(String uri, String localName) {
411 return child().where(expandedNamePredicate(uri, localName));
412 }
413
414 /**
415 * Obtain a {@code Step} that filters the nodes found on the child axis using a supplied {@code Predicate}.
416 * The function call {@code child(predicate)} is equivalent to {@code CHILD.where(predicate)}.
417 * For example, {@code child(isElement())} returns a Step that selects the element node children
418 * of a given node.
419 * @param filter the predicate to be applied
420 * @return a Step that filters the nodes found on the child axis using a supplied {@code Predicate}.
421 */
422
423 public static Step<XdmNode> child(Predicate<? super XdmNode> filter) {
424 return child().where(filter);
425 }
426
427 /**
428 * Obtain a {@link Step} to navigate from a node to its descendants, which are returned in document order
429 * @return a Step that selects all nodes on the descendant axis
430 */
431 public static Step<XdmNode> descendant() {
432 return DESCENDANT;
433 }
434
435 /**
436 * Obtain a {@code Step} that navigates from a node to the descendant elements having a specified
437 * local name, irrespective of the namespace. These are returned in document order.
438 *
439 * @param localName the local name of the descendant elements to be selected by the {@code Step},
440 * or "*" to select all descendants that are element nodes
441 * @return a {@code Step}, which selects the element descendants of a supplied node that have the
442 * required local name.
443 */
444
445 public static Step<XdmNode> descendant(String localName) {
446 return descendant().where(localNamePredicate(localName));
447 }
448
449 /**
450 * Obtain a {@code Step} that navigates from a node to the element descendants having a specified
451 * namespace URI and local name. These are returned in document order.
452 *
453 * @param uri the namespace URI of the descendant elements to be selected by the {@code Step}:
454 * supply a zero-length string to indicate the null namespace
455 * @param localName the local name of the descendant elements to be selected by the {@code Step}
456 * @return a {@code Step}, which selects the element descendants of a supplied node that have the
457 * required local name and namespace URI.
458 */
459
460 public static Step<XdmNode> descendant(String uri, String localName) {
461 return descendant().where(expandedNamePredicate(uri, localName));
462 }
463
464 /**
465 * Obtain a {@link Step} to navigate from a node to its descendants, which are returned in document order,
466 * preceded by the origin node itself
467 * @return a Step that selects all nodes on the descendant-or-self axis
468 */
469 public static Step<XdmNode> descendantOrSelf() {
470 return DESCENDANT_OR_SELF;
471 }
472
473 /**
474 * Obtain a Step that filters the nodes found on the descendant axis using a supplied {@code Predicate}.
475 * The function call {@code descendant(predicate)} is equivalent to {@code DESCENDANT.where(predicate)}.
476 * For example, {@code descendant(isElement())} returns a Step that selects the element node descendants
477 * of a given node, while {@code descendant(exists(attribute("id")))} selects those that have an attribute
478 * named "id". These are returned in document order.
479 *
480 * @param filter the predicate to be applied
481 * @return a Step that filters the nodes found on the descendant axis using a supplied Predicate.
482 */
483
484 public static Step<XdmNode> descendant(Predicate<? super XdmNode> filter) {
485 return descendant().where(filter);
486 }
487
488 /**
489 * Obtain a {@code Step} that navigates from a node to the descendant-or-self elements having a specified
490 * local name, irrespective of the namespace. These are returned in document order, preceded by the origin node
491 * itself if it matches the conditions.
492 *
493 * @param localName the local name of the descendant-or-self elements to be selected by the {@code Step},
494 * or "*" to select all descendant-or-self nodes that are element nodes
495 * @return a {@code Step}, which selects the element children of a supplied node that have the
496 * required local name.
497 */
498
499 public static Step<XdmNode> descendantOrSelf(String localName) {
500 return descendantOrSelf().where(localNamePredicate(localName));
501 }
502
503 /**
504 * Obtain a {@code Step} that navigates from a node to the descendant-or-self elements having a specified
505 * namespace URI and local name. These are returned in document order, preceded by the origin node
506 * itself if it matches the conditions.
507 *
508 * @param uri the namespace URI of the descendant-or-self elements to be selected by the {@code Step}:
509 * supply a zero-length string to indicate the null namespace
510 * @param localName the local name of the descendant-or-self elements to be selected by the {@code Step}
511 * @return a {@code Step}, which selects the element descendants-or-self of a supplied node that have a
512 * given local name and namespace URI.
513 */
514
515 public static Step<XdmNode> descendantOrSelf(String uri, String localName) {
516 return descendantOrSelf().where(expandedNamePredicate(uri, localName));
517 }
518
519 /**
520 * Obtain a Step that filters the nodes found on the descendant-or-self axis using a supplied {@code Predicate}.
521 * The function call {@code descendant(predicate)} is equivalent to {@code DESCENDANT.where(predicate)}.
522 * For example, {@code descendant(isElement())} returns a Step that selects the element node descendants
523 * of a given node, while {@code descendant(exists(attribute("id")))} selects those that have an attribute
524 * named "id". These are returned in document order.
525 *
526 * @param filter the predicate to be applied
527 * @return a Step that filters the nodes found on the descendant-or-self axis using a supplied Predicate.
528 */
529
530 public static Step<XdmNode> descendantOrSelf(Predicate<? super XdmNode> filter) {
531 return descendantOrSelf().where(filter);
532 }
533
534 /**
535 * Obtain a {@link Step} to navigate from a node to its following nodes
536 * (excluding descendants), which are returned in document order
537 * @return a Step that selects all nodes on the following axis
538 */
539 public static Step<XdmNode> following() {
540 return FOLLOWING;
541 }
542
543 /**
544 * Obtain a {@code Step} that navigates from a node to the following elements having a specified
545 * local name, irrespective of the namespace. These are returned in document order.
546 *
547 * @param localName the local name of the following elements to be selected by the {@code Step},
548 * or "*" to select all following nodes that are elements
549 * @return a {@code Step}, which selects the following elements of a supplied node that have the
550 * required local name.
551 */
552
553 public static Step<XdmNode> following(String localName) {
554 return following().where(localNamePredicate(localName));
555 }
556
557 /**
558 * Obtain a {@code Step} that navigates from a node to the following elements having a specified
559 * namespace URI and local name. These are returned in document order.
560 *
561 * @param uri the namespace URI of the following elements to be selected by the {@code Step}:
562 * supply a zero-length string to indicate the null namespace
563 * @param localName the local name of the following elements to be selected by the {@code Step}
564 * @return a {@code Step}, which selects the following elements of a supplied node that have the
565 * required local name and namespace URI.
566 */
567
568 public static Step<XdmNode> following(String uri, String localName) {
569 return following().where(expandedNamePredicate(uri, localName));
570 }
571
572 /**
573 * Obtain a Step that filters the nodes found on the following axis using a supplied {@code Predicate}.
574 * The function call {@code followingSibling(predicate)} is equivalent to {@code FOLLOWING_SIBLING.where(predicate)}.
575 * For example, {@code followingSibling(isElement())} returns a {@code Step} that selects the following sibling elements
576 * of a given node, while {@code followingSibling(exists(attribute("id")))} selects those that have an attribute
577 * named "id". These are returned in document order.
578 *
579 * @param filter the predicate to be applied
580 * @return a {@code Step} that filters the nodes found on the following axis using a supplied {@code Predicate}.
581 */
582
583 public static Step<XdmNode> following(Predicate<? super XdmNode> filter) {
584 return following().where(filter);
585 }
586
587 /**
588 * Obtain a {@link Step} to navigate from a node to its following siblings, which are returned
589 * in document order
590 * @return a Step that selects all nodes on the following-sibling axis
591 */
592 public static Step<XdmNode> followingSibling() {
593 return FOLLOWING_SIBLING;
594 }
595
596 /**
597 * Obtain a {@code Step} that navigates from a node to the following sibling elements having a specified
598 * local name, irrespective of the namespace. These are returned in document order.
599 *
600 * @param localName the local name of the following sibling elements to be selected by the {@code Step},
601 * or "*" to select all following siblings that are element nodes
602 * @return a {@code Step}, which selects the following sibling elements of a supplied node that have the
603 * required local name.
604 */
605
606 public static Step<XdmNode> followingSibling(String localName) {
607 return followingSibling().where(localNamePredicate(localName));
608 }
609
610 /**
611 * Obtain a {@code Step} that navigates from a node to the following sibling elements having a specified
612 * namespace URI and local name. These are returned in document order.
613 *
614 * @param uri the namespace URI of the following sibling elements to be selected by the {@code Step}:
615 * supply a zero-length string to indicate the null namespace
616 * @param localName the local name of the following sibling elements to be selected by the {@code Step}
617 * @return a {@code Step}, which selects the following sibling elements of a supplied node that have the
618 * required local name and namespace URI.
619 */
620
621 public static Step<XdmNode> followingSibling(String uri, String localName) {
622 return followingSibling().where(expandedNamePredicate(uri, localName));
623 }
624
625 /**
626 * Obtain a Step that filters the nodes found on the following sibling axis using a supplied {@code Predicate}.
627 * The function call {@code followingSibling(predicate)} is equivalent to {@code FOLLOWING_SIBLING.where(predicate)}.
628 * For example, {@code followingSibling(isElement())} returns a {@code Step} that selects the following sibling elements
629 * of a given node, while {@code followingSibling(exists(attribute("id")))} selects those that have an attribute
630 * named "id". These are returned in document order.
631 *
632 * @param filter the predicate to be applied
633 * @return a {@code Step} that filters the nodes found on the following sibling axis using a supplied {@code Predicate}.
634 */
635
636 public static Step<XdmNode> followingSibling(Predicate<? super XdmNode> filter) {
637 return followingSibling().where(filter);
638 }
639
640 /**
641 * Obtain a {@link Step} to navigate from a node to its namespace nodes
642 * @return a Step that selects all nodes on the namespace axis
643 */
644 public static Step<XdmNode> namespace() {
645 return NAMESPACE;
646 }
647
648 /**
649 * Obtain a {@code Step} that navigates from a node to its namespaces having a specified
650 * local name. The local name of a namespace node corresponds to the prefix used in the
651 * namespace binding.
652 *
653 * @param localName the local name (representing the namespace prefix) of the namespace nodes
654 * to be selected by the {@code Step}, or "*" to select all namespaces
655 * @return a {@code Step}, which selects the namespaces of a supplied node that have a
656 * given local name (prefix).
657 */
658
659 public static Step<XdmNode> namespace(String localName) {
660 return "*".equals(localName) ? namespace() : namespace().where(hasLocalName(localName));
661 }
662
663 /**
664 * Obtain a {@code Step} that filters the nodes found on the namespace axis using a supplied {@code Predicate}.
665 * The function call {@code namespace(predicate)} is equivalent to {@code namespace().where(predicate)}.
666 * For example, {@code namespace(eq("http://www.w3.org/1999/XSL/Transform")} selects a namespace node
667 * that binds a prefix to the XSLT namespace.
668 *
669 * @param filter the predicate to be applied
670 * @return a {@code Step} that filters the nodes found on the namespace axis using a supplied
671 * {@code Predicate}.
672 */
673
674 public static Step<XdmNode> namespace(Predicate<? super XdmNode> filter) {
675 return namespace().where(filter);
676 }
677
678 /**
679 * Obtain a {@link Step} to navigate from a node to its parent
680 * @return a Step that selects all nodes on the parent axis (of which there is at most one)
681 */
682 public static Step<XdmNode> parent() {
683 return PARENT;
684 }
685
686 /**
687 * Obtain a {@code Step} that navigates from a node to the parent element provided it has a specified
688 * local name, irrespective of the namespace
689 *
690 * @param localName the local name of the parent element to be selected by the Step,
691 * or "*" to select the parent node provided it is an element
692 * @return a {@code Step}, which selects the parent of a supplied node provided it is an element with the
693 * required local name.
694 */
695
696 public static Step<XdmNode> parent(String localName) {
697 return parent().where(isElement()).where(localNamePredicate(localName));
698 }
699
700 /**
701 * Obtain a {@code Step} that navigates from a node to the parent element provided it has a specified
702 * namespace URI and local name
703 *
704 * @param uri the namespace URI of the parent element to be selected by the {@code Step}:
705 * supply a zero-length string to indicate the null namespace
706 * @param localName the local name of the parent element to be selected by the {@code Step}
707 * @return a {@code Step}, which selects the parent element of a supplied node provided it is an
708 * element with the required local name and namespace URI.
709 */
710
711 public static Step<XdmNode> parent(String uri, String localName) {
712 return parent().where(expandedNamePredicate(uri, localName));
713 }
714
715 /**
716 * Obtain a {@code Step} that filters the node found on the parent axis using a supplied {@code Predicate}.
717 * The function call {@code parent(predicate)} is equivalent to {@code parent().where(predicate)}.
718 * For example, {@code parent(isElement())} returns a Step that selects the parent node provided
719 * it is an element
720 *
721 * @param filter the predicate to be applied
722 * @return a Step that filters the nodes found on the parent axis using a supplied {@code Predicate}.
723 */
724
725 public static Step<XdmNode> parent(Predicate<? super XdmNode> filter) {
726 return parent().where(filter);
727 }
728
729 /**
730 * Obtain a {@link Step} to navigate from a node to its preceding siblings, which are returned
731 * in reverse document order
732 * @return a Step that selects all nodes on the preceding-sibling axis
733 */
734 public static Step<XdmNode> precedingSibling() {
735 return PRECEDING_SIBLING;
736 }
737
738 /**
739 * Obtain a {@code Step} that navigates from a node to the preceding sibling elements having a specified
740 * local name, irrespective of the namespace. These are returned in reverse document order.
741 *
742 * @param localName the local name of the preceding sibling elements to be selected by the {@code Step},
743 * or "*" to select all descendants that are element nodes
744 * @return a {@code Step}, which selects the preceding sibling elements of a supplied node that have the
745 * required local name.
746 */
747
748 public static Step<XdmNode> precedingSibling(String localName) {
749 return precedingSibling().where(localNamePredicate(localName));
750 }
751
752 /**
753 * Obtain a {@code Step} that navigates from a node to the preceding sibling elements having a specified
754 * namespace URI and local name. These are returned in reverse document order.
755 *
756 * @param uri the namespace URI of the preceding sibling elements to be selected by the {@code Step}:
757 * supply a zero-length string to indicate the null namespace
758 * @param localName the local name of the preceding sibling elements to be selected by the {@code Step}
759 * @return a {@code Step}, which selects the preceding sibling elements of a supplied node that have the
760 * required local name and namespace URI.
761 */
762
763 public static Step<XdmNode> precedingSibling(String uri, String localName) {
764 return precedingSibling().where(expandedNamePredicate(uri, localName));
765 }
766
767 /**
768 * Obtain a Step that filters the nodes found on the preceding sibling axis using a supplied {@code Predicate}.
769 * The function call {@code precedingSibling(predicate)} is equivalent to {@code precedingSibling().where(predicate)}.
770 * For example, {@code precedingSibling(isElement())} returns a {@code Step} that selects the preceding sibling elements
771 * of a given node, while {@code precedingSibling(exists(attribute("id")))} selects those that have an attribute
772 * named "id". These are returned in reverse document order.
773 *
774 * @param filter the predicate to be applied
775 * @return a {@code Step} that filters the nodes found on the following sibling axis using a supplied {@code Predicate}.
776 */
777
778 public static Step<XdmNode> precedingSibling(Predicate<? super XdmNode> filter) {
779 return precedingSibling().where(filter);
780 }
781
782 /**
783 * Obtain a {@link Step} to navigate from a node to its preceding nodes (excluding ancestors),
784 * which are returned in reverse document order
785 * @return a Step that selects all nodes on the preceding axis
786 */
787 public static Step<XdmNode> preceding() {
788 return PRECEDING;
789 }
790
791 /**
792 * Obtain a {@code Step} that navigates from a node to the preceding elements having a specified
793 * local name. These are returned in reverse document order.
794 *
795 * @param localName the local name of the preceding elements to be selected by the {@code Step},
796 * or "*" to select all descendants that are element nodes
797 * @return a {@code Step}, which selects the preceding elements of a supplied node that have the
798 * required local name.
799 */
800
801 public static Step<XdmNode> preceding(String localName) {
802 return preceding().where(localNamePredicate(localName));
803 }
804
805 /**
806 * Obtain a {@code Step} that navigates from a node to the preceding elements having a specified
807 * namespace URI and local name. These are returned in reverse document order.
808 *
809 * @param uri the namespace URI of the preceding elements to be selected by the {@code Step}:
810 * supply a zero-length string to indicate the null namespace
811 * @param localName the local name of the preceding sibling elements to be selected by the {@code Step}
812 * @return a {@code Step}, which selects the preceding sibling elements of a supplied node that have the
813 * required local name and namespace URI.
814 */
815
816 public static Step<XdmNode> preceding(String uri, String localName) {
817 return preceding().where(expandedNamePredicate(uri, localName));
818 }
819
820 /**
821 * Obtain a Step that filters the nodes found on the preceding axis using a supplied {@code Predicate}.
822 * The function call {@code preceding(predicate)} is equivalent to {@code PRECEDING.where(predicate)}.
823 * For example, {@code preceding(isElement())} returns a {@code Step} that selects the preceding elements
824 * of a given node, while {@code preceding(exists(attribute("id")))} selects those that have an attribute
825 * named "id". These are returned in reverse document order.
826 *
827 * @param filter the predicate to be applied
828 * @return a {@code Step} that filters the nodes found on the following sibling axis using a supplied {@code Predicate}.
829 */
830
831 public static Step<XdmNode> preceding(Predicate<? super XdmNode> filter) {
832 return preceding().where(filter);
833 }
834
835 /**
836 * Obtain a {@link Step} to navigate from a node to itself (useful only if applying a predicate)
837 * @return a Step that selects all nodes on the self axis (that is, the node itself)
838 */
839 public static Step<XdmNode> self() {
840 return SELF;
841 }
842
843 /**
844 * Obtain a {@code Step} that navigates from a node to itself provided it is an element with a specified
845 * local name, irrespective of the namespace
846 *
847 * @param localName the local name of the element to be selected by the Step,
848 * or "*" to select the node provided that it is an element node
849 * @return a {@code Step}, which selects the supplied node provided it has a
850 * given local name.
851 */
852
853 public static Step<XdmNode> self(String localName) {
854 return self().where(localNamePredicate(localName));
855 }
856
857 /**
858 * Obtain a {@code Step} that navigates from a node to itself provided it has a specified
859 * namespace URI and local name
860 *
861 * @param uri the namespace URI of the element to be selected by the {@code Step}:
862 * supply a zero-length string to indicate the null namespace
863 * @param localName the local name of the element to be selected by the {@code Step}
864 * @return a {@code Step}, which selects the supplied node provided it is an element with a
865 * given local name and namespace URI.
866 */
867
868 public static Step<XdmNode> self(String uri, String localName) {
869 return self().where(expandedNamePredicate(uri, localName));
870 }
871
872 /**
873 * Obtain a {@code Step} that filters the node found on the self axis using a supplied {@code Predicate}.
874 * The function call {@code self(predicate)} is equivalent to {@code SELF.where(predicate)}.
875 * For example, {@code self(isElement())} returns a Step that selects the supplied node provided
876 * it is an element
877 *
878 * @param filter the predicate to be applied
879 * @return a Step that filters the nodes found on the parent axis using a supplied {@code Predicate}.
880 */
881
882 public static Step<XdmNode> self(Predicate<? super XdmNode> filter) {
883 return self().where(filter);
884 }
885
886 /**
887 * Obtain a {@code Step} that returns text nodes found on the child axis.
888 * The function call {@code text()} is equivalent to {@code child().where(isText())}.
889 * For example, {@code self(isElement())} returns a Step that selects the supplied node provided
890 * it is an element
891 * @return a Step that returns the text nodes found on the child axis.
892 */
893
894 public static Step<XdmNode> text() {
895 return child().where(isText());
896 }
897
898 /**
899 * Construct a path as a composite {@link Step} from a sequence of steps composed together
900 * @param steps the constituent steps in the path
901 * @return a composite step
902 */
903
904 @SafeVarargs
905 public static Step<? extends XdmNode> path(Step<? extends XdmNode>... steps) {
906 return pathFromList(Arrays.asList(steps));
907 }
908
909 private static Step<? extends XdmNode> pathFromList(List<Step<? extends XdmNode>> steps) {
910 if (steps.isEmpty()) {
911 return nothing();
912 } else if (steps.size() == 1) {
913 return steps.get(0);
914 } else {
915 return steps.get(0).then(pathFromList(steps.subList(1, steps.size())));
916 }
917 }
918
919 /**
920 * Construct a simple path consisting solely of simple child, attribute, descendant, root,
921 * and parent steps. For example, {@code path("div3", "head", "@style")} selects
922 * the same nodes as the XPath 2.0 expression {@code child::*:div3/child::*:head/attribute::*:style}.
923 * @param steps a sequence of strings. Each string must be one of the following:
924 * <ul>
925 * <li>A plain NCName (for example "item") selects child nodes by matching
926 * local-name (the namespace is ignored)</li>
927 * <li>An NCName preceded by "@" (for example, "@code"), selects attribute nodes
928 * by matching local-name (again, ignoring any namespace)</li>
929 * <li>The string "*" selects all child elements, regardless of name</li>
930 * <li>The string "/" selects the root node of the tree, provided it
931 * is a document node</li>
932 * <li>The string ".." selects the parent node</li>
933 * <li>The string "//" selects all descendant-or-self nodes (note,
934 * this does not involve finding the root of the tree: it correspondings to a binary
935 * '//' operator in XPath, not to an initial '//')</li>
936 * </ul>.
937 * <p>For more complex paths, see {@link #path(Step...)}</p>
938 * @throws IllegalArgumentException if any of the strings is invalid according
939 * to these rules.
940 */
941
942 public static Step<? extends XdmNode> path(String... steps) {
943 List<Step<? extends XdmNode>> pathSteps = new ArrayList<>();
944 for (String step : steps) {
945 if (step.equals("/")) {
946 pathSteps.add(root().where(isDocument()));
947 } else if (step.equals("..")) {
948 pathSteps.add(parent());
949 } else if (step.equals("*")) {
950 pathSteps.add(child(isElement()));
951 } else if (step.equals("//")) {
952 pathSteps.add(descendantOrSelf());
953 } else if (step.startsWith("@")) {
954 String name = step.substring(1);
955 if (!NameChecker.isValidNCName(name)) {
956 throw new IllegalArgumentException("Invalid attribute name " + name);
957 }
958 pathSteps.add(attribute(name));
959 } else {
960 if (!NameChecker.isValidNCName(step)) {
961 throw new IllegalArgumentException("Invalid element name " + step);
962 }
963 pathSteps.add(child(step));
964 }
965 }
966 return pathFromList(pathSteps);
967 }
968
969 /**
970 * Obtain a Step whose effect is to tokenize the supplied item on whitespace
971 * boundaries, returning a sequence of strings as {@code XdmAtomicValue} instances.
972 *
973 * <p>Note: the tokenize step, when applied to a string with leading and trailing whitespace,
974 * has the effect of removing this whitespace. In addition to its primary role, the function
975 * can therefore be useful for trimming the content of a single string. </p>
976 *
977 * <p>Usage example: {@code child().where(some(attribute("id").then(tokenize())).eq("a123"))}
978 * selects child elements that have an attribute named "id" whose value contains the token
979 * "a123".</p>
980 *
981 * @return a Step whose effect is to take a supplied item and split its string
982 * value into a sequence of xs:string instances
983 */
984
985 public static Step<XdmAtomicValue> tokenize() {
986 return new Step<XdmAtomicValue>() {
987 @Override
988 public Stream<XdmAtomicValue> apply(XdmItem item) {
989 UnfailingIterator<StringValue> iter = new Whitespace.Tokenizer(item.getStringValue());
990 return XdmSequenceIterator.ofAtomicValues(iter).stream();
991 }
992 };
993 }
994
995 /**
996 * Obtain a Step whose effect is to interpret the supplied item as an xs:ID value
997 * and return the nodes (in a given document) that have that string as their ID.
998 * @param doc the root node (document node) of the document within which the ID
999 * value should be sought
1000 * @return a Step whose effect is to take a supplied item and split its string
1001 * value into a sequence of xs:string instances
1002 */
1003
1004 public static Step<XdmNode> id(XdmNode doc) {
1005 return new Step<XdmNode>() {
1006 @Override
1007 public Stream<XdmNode> apply(XdmItem item) {
1008 if (doc.getNodeKind() != XdmNodeKind.DOCUMENT) {
1009 throw new IllegalArgumentException("id() - argument is not a document node");
1010 }
1011 NodeInfo target = doc.getUnderlyingNode().getTreeInfo().selectID(item.getStringValue(), true);
1012 return target==null
1013 ? Stream.empty()
1014 : Stream.of((XdmNode)XdmNode.wrap(target));
1015 }
1016 };
1017 }
1018
1019
1020 }
1021
1022 // Copyright (c) 2018 Saxonica Ltd
1023