| 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.tree.iter.EmptyIterator; |
| 17 |
import net.sf.saxon.value.Int64Value; |
| 18 |
import net.sf.saxon.value.NumericValue; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
public class Subsequence_3 extends SystemFunction implements Callable { |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public int getSpecialProperties(Expression[] arguments) { |
| 35 |
return arguments[0].getSpecialProperties(); |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public Sequence<?> call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 57 |
return SequenceTool.toLazySequence(subSequence( |
| 58 |
arguments[0].iterate(), |
| 59 |
(NumericValue) arguments[1].head(), |
| 60 |
(NumericValue) arguments[2].head(), |
| 61 |
context)); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
public static SequenceIterator<?> subSequence( |
| 66 |
SequenceIterator seq, NumericValue startVal, NumericValue lengthVal, XPathContext context) |
| 67 |
throws XPathException { |
| 68 |
|
| 69 |
if (startVal instanceof Int64Value && lengthVal instanceof Int64Value) { |
| 70 |
|
| 71 |
long lstart = startVal.longValue(); |
| 72 |
if (lstart > Integer.MAX_VALUE) { |
| 73 |
return EmptyIterator.emptyIterator(); |
| 74 |
} |
| 75 |
long llength = lengthVal.longValue(); |
| 76 |
if (llength > Integer.MAX_VALUE) { |
| 77 |
llength = Integer.MAX_VALUE; |
| 78 |
} |
| 79 |
if (llength < 1) { |
| 80 |
return EmptyIterator.emptyIterator(); |
| 81 |
} |
| 82 |
long lend = lstart + llength - 1; |
| 83 |
if (lend < 1) { |
| 84 |
return EmptyIterator.emptyIterator(); |
| 85 |
} |
| 86 |
int start = lstart < 1 ? 1 : (int) lstart; |
| 87 |
return SubsequenceIterator.make(seq, start, (int) lend); |
| 88 |
} else { |
| 89 |
if (startVal.isNaN()) { |
| 90 |
return EmptyIterator.emptyIterator(); |
| 91 |
} |
| 92 |
if (startVal.compareTo(Int64Value.MAX_LONG) > 0) { |
| 93 |
return EmptyIterator.emptyIterator(); |
| 94 |
} |
| 95 |
startVal = startVal.round(0); |
| 96 |
|
| 97 |
if (lengthVal.isNaN()) { |
| 98 |
return EmptyIterator.emptyIterator(); |
| 99 |
} |
| 100 |
lengthVal = lengthVal.round(0); |
| 101 |
|
| 102 |
if (lengthVal.compareTo(Int64Value.ZERO) <= 0) { |
| 103 |
return EmptyIterator.emptyIterator(); |
| 104 |
} |
| 105 |
NumericValue rend = (NumericValue) ArithmeticExpression.compute( |
| 106 |
startVal, Calculator.PLUS, lengthVal, context); |
| 107 |
if (rend.isNaN()) { |
| 108 |
|
| 109 |
return EmptyIterator.emptyIterator(); |
| 110 |
} |
| 111 |
rend = (NumericValue) ArithmeticExpression.compute( |
| 112 |
rend, Calculator.MINUS, Int64Value.PLUS_ONE, context); |
| 113 |
if (rend.compareTo(Int64Value.ZERO) <= 0) { |
| 114 |
return EmptyIterator.emptyIterator(); |
| 115 |
} |
| 116 |
|
| 117 |
long lstart; |
| 118 |
if (startVal.compareTo(Int64Value.PLUS_ONE) <= 0) { |
| 119 |
lstart = 1; |
| 120 |
} else { |
| 121 |
lstart = startVal.longValue(); |
| 122 |
} |
| 123 |
if (lstart > Integer.MAX_VALUE) { |
| 124 |
return EmptyIterator.emptyIterator(); |
| 125 |
} |
| 126 |
|
| 127 |
long lend; |
| 128 |
if (rend.compareTo(Int64Value.MAX_LONG) >= 0) { |
| 129 |
lend = Integer.MAX_VALUE; |
| 130 |
} else { |
| 131 |
lend = rend.longValue(); |
| 132 |
} |
| 133 |
return SubsequenceIterator.make(seq, (int) lstart, (int) lend); |
| 134 |
|
| 135 |
} |
| 136 |
|
| 137 |
} |
| 138 |
|
| 139 |
public String getStreamerName() { |
| 140 |
return "Subsequence"; |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
} |
| 145 |
|