| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 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 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 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 |
|
| 66 |
|
| 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 |
|
| 97 |
|
| 98 |
|
| 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 |
|
| 111 |
|
| 112 |
throw new AssertionError("base of removeIterator is not a LastPositionFinder"); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 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 |
|