| 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 |
public class Subsequence_2 extends SystemFunction implements Callable { |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public int getSpecialProperties(Expression[] arguments) { |
| 34 |
return arguments[0].getSpecialProperties(); |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public int getCardinality(Expression[] arguments) { |
| 43 |
return arguments[0].getCardinality() | StaticProperty.ALLOWS_ZERO_OR_ONE; |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public Sequence<?> call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 66 |
return SequenceTool.toLazySequence( |
| 67 |
subSequence(arguments[0].iterate(), (NumericValue) arguments[1].head())); |
| 68 |
} |
| 69 |
|
| 70 |
public static SequenceIterator<?> subSequence( |
| 71 |
SequenceIterator<?> seq, NumericValue startVal) |
| 72 |
throws XPathException { |
| 73 |
|
| 74 |
long lstart; |
| 75 |
if (startVal instanceof Int64Value) { |
| 76 |
lstart = startVal.longValue(); |
| 77 |
if (lstart <= 1) { |
| 78 |
return seq; |
| 79 |
} |
| 80 |
} else if (startVal.isNaN()) { |
| 81 |
return EmptyIterator.emptyIterator(); |
| 82 |
} else { |
| 83 |
startVal = startVal.round(0); |
| 84 |
if (startVal.compareTo(Int64Value.PLUS_ONE) <= 0) { |
| 85 |
return seq; |
| 86 |
} else if (startVal.compareTo(Int64Value.MAX_LONG) > 0) { |
| 87 |
return EmptyIterator.emptyIterator(); |
| 88 |
} else { |
| 89 |
lstart = startVal.longValue(); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
if (lstart > Integer.MAX_VALUE) { |
| 94 |
|
| 95 |
return EmptyIterator.emptyIterator(); |
| 96 |
} |
| 97 |
|
| 98 |
return TailIterator.make(seq, (int) lstart); |
| 99 |
|
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
@Override |
| 112 |
public Expression makeFunctionCall(Expression[] arguments) { |
| 113 |
|
| 114 |
try { |
| 115 |
if (Literal.isAtomic(arguments[1]) && !(arguments[0] instanceof ErrorExpression)) { |
| 116 |
NumericValue start = (NumericValue) ((Literal) arguments[1]).getValue(); |
| 117 |
start = start.round(0); |
| 118 |
long intStart = start.longValue(); |
| 119 |
if (intStart > Integer.MAX_VALUE) { |
| 120 |
|
| 121 |
return super.makeFunctionCall(arguments); |
| 122 |
} |
| 123 |
if (intStart <= 0) { |
| 124 |
return arguments[0]; |
| 125 |
} |
| 126 |
return new TailExpression(arguments[0], (int) intStart); |
| 127 |
} |
| 128 |
} catch (Exception e) { |
| 129 |
|
| 130 |
} |
| 131 |
return super.makeFunctionCall(arguments); |
| 132 |
} |
| 133 |
|
| 134 |
public String getStreamerName() { |
| 135 |
return "Subsequence"; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
} |
| 140 |
|