final SequenceBuilder sb = new SequenceBuilder();
final SequencePointable seq = (SequencePointable) SequencePointable.FACTORY.createPointable();
final VoidPointable p = (VoidPointable) VoidPointable.FACTORY.createPointable();
final DoublePointable doublep = (DoublePointable) DoublePointable.FACTORY.createPointable();
final LongPointable longp = (LongPointable) LongPointable.FACTORY.createPointable();
final XSDecimalPointable decp = (XSDecimalPointable) XSDecimalPointable.FACTORY.createPointable();
final ArrayBackedValueStorage abvsRound = new ArrayBackedValueStorage();
final FnRoundOperation round = new FnRoundOperation();
return new AbstractTaggedValueArgumentScalarEvaluator(args) {
@Override
protected void evaluate(TaggedValuePointable[] args, IPointable result) throws SystemException {
try {
long startingLoc;
TaggedValuePointable tvp2 = args[1];
startingLoc = getLongFromArgument(tvp2);
if (startingLoc < 1) {
startingLoc = 1;
}
// Get length.
long endingLoc = Long.MAX_VALUE;
if (args.length > 2) {
TaggedValuePointable tvp3 = args[2];
endingLoc = getLongFromArgument(tvp3) + startingLoc;
}
abvs.reset();
sb.reset(abvs);
TaggedValuePointable tvp1 = args[0];
if (tvp1.getTag() == ValueTag.SEQUENCE_TAG) {
tvp1.getValue(seq);
int seqLen = seq.getEntryCount();
if (endingLoc < startingLoc) {
// Empty sequence.
} else if (startingLoc == 1 && endingLoc > seqLen) {
// Includes whole sequence.
result.set(tvp1);
return;
} else {
for (int j = 0; j < seqLen; ++j) {
if (startingLoc <= j + 1 && j + 1 < endingLoc) {
seq.getEntry(j, p);
sb.addItem(p);
}
}
}
} else if (startingLoc == 1 && endingLoc > 1) {
// Includes item.
result.set(tvp1);
return;
}
sb.finish();
result.set(abvs);
} catch (IOException e) {
throw new SystemException(ErrorCode.SYSE0001);
}
}
/**
* XQuery Specification calls for double value. Integer and Decimal are allowed to cut down on casting.
*
* @param tvp
* @return
* @throws SystemException
* @throws IOException
*/
public long getLongFromArgument(TaggedValuePointable tvp) throws SystemException, IOException {
if (tvp.getTag() == ValueTag.XS_DOUBLE_TAG) {
tvp.getValue(doublep);
abvsRound.reset();
round.operateDouble(doublep, abvsRound.getDataOutput());
doublep.set(abvsRound.getByteArray(), abvsRound.getStartOffset() + 1,
DoublePointable.TYPE_TRAITS.getFixedLength());
return doublep.longValue();
} else if (tvp.getTag() == ValueTag.XS_INTEGER_TAG) {
tvp.getValue(longp);
return longp.longValue();
} else if (tvp.getTag() == ValueTag.XS_DECIMAL_TAG) {
tvp.getValue(decp);
return decp.longValue();
} else {
throw new SystemException(ErrorCode.FORG0006);
}
}
};