Debian Codegraph @

net/sf/saxon/functions/Remove.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 package net.sf.saxon.functions;
9
10 import net.sf.saxon.expr.*;
11 import net.sf.saxon.om.Item;
12 import net.sf.saxon.om.Sequence;
13 import net.sf.saxon.om.SequenceIterator;
14 import net.sf.saxon.om.SequenceTool;
15 import net.sf.saxon.trans.XPathException;
16 import net.sf.saxon.value.IntegerValue;
17 import net.sf.saxon.value.NumericValue;
18
19 /**
20 * The XPath 2.0 remove() function
21 */
22
23 public class Remove extends SystemFunction {
24
25 @Override
26 public Expression makeFunctionCall(Expression[] arguments) {
27
28 if (Literal.isAtomic(arguments[1])) {
29 Sequence<?> index = ((Literal) arguments[1]).getValue();
30 if (index instanceof IntegerValue) {
31 try {
32 long value = ((IntegerValue) index).longValue();
33 if (value <= 0) {
34 return arguments[0];
35 } else if (value == 1) {
36 return new TailExpression(arguments[0], 2);
37 }
38 } catch (XPathException err) {
39 //
40 }
41 }
42 }
43
44 return super.makeFunctionCall(arguments);
45 }
46
47 /**
48 * Evaluate the expression as a general function call
49 *
50 * @param context the dynamic evaluation context
51 * @param arguments the values of the arguments, supplied as SequenceIterators
52 * @return the result of the evaluation, in the form of a SequenceIterator
53 * @throws net.sf.saxon.trans.XPathException if a dynamic error occurs during the evaluation of the expression
54 */
55 public Sequence<?> call(XPathContext context, Sequence[] arguments) throws XPathException {
56 NumericValue n = (NumericValue) arguments[1].head();
57 int pos = (int) n.longValue();
58 if (pos < 1) {
59 return arguments[0];
60 }
61 return SequenceTool.toLazySequence2(new RemoveIterator<>(arguments[0].iterate(), pos));
62 }
63
64 /**
65 * An implementation of SequenceIterator that returns all items except the one
66 * at a specified position.
67 */
68
69 public static class RemoveIterator<T extends Item<?>> implements SequenceIterator<T>, LastPositionFinder {
70
71 SequenceIterator<T> base;
72 int removePosition;
73 int basePosition = 0;
74 T current = null;
75
76 public RemoveIterator(SequenceIterator<T> base, int removePosition) {
77 this.base = base;
78 this.removePosition = removePosition;
79 }
80
81 public T next() throws XPathException {
82 current = base.next();
83 basePosition++;
84 if (current != null && basePosition == removePosition) {
85 current = base.next();
86 basePosition++;
87 }
88 return current;
89 }
90
91 public void close() {
92 base.close();
93 }
94
95 /**
96 * Get the last position (that is, the number of items in the sequence). This method is
97 * non-destructive: it does not change the state of the iterator.
98 * The result is undefined if the next() method of the iterator has already returned null.
99 */
100
101 public int getLength() throws XPathException {
102 if (base instanceof LastPositionFinder) {
103 int x = ((LastPositionFinder) base).getLength();
104 if (removePosition >= 1 && removePosition <= x) {
105 return x - 1;
106 } else {
107 return x;
108 }
109 } else {
110 // This shouldn't happen, because this iterator only has the LAST_POSITION_FINDER property
111 // if the base iterator has the LAST_POSITION_FINDER property
112 throw new AssertionError("base of removeIterator is not a LastPositionFinder");
113 }
114 }
115
116 /**
117 * Get properties of this iterator, as a bit-significant integer.
118 *
119 * @return the properties of this iterator. This will be some combination of
120 * properties such as {@link SequenceIterator#GROUNDED}, {@link SequenceIterator#LAST_POSITION_FINDER},
121 * and {@link SequenceIterator#LOOKAHEAD}. It is always
122 * acceptable to return the value zero, indicating that there are no known special properties.
123 * It is acceptable for the properties of the iterator to change depending on its state.
124 */
125
126 public int getProperties() {
127 return base.getProperties() & LAST_POSITION_FINDER;
128 }
129 }
130
131 public String getStreamerName() {
132 return "Remove";
133 }
134
135 }
136