use of org.exist.xquery.value.IntegerValue in project exist by eXist-db.
the class RandomFunction method eval.
@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
final Sequence result;
final Random rnd = new Random();
if (getArgumentCount() == 0) {
if (isCalledAs("random")) {
result = new DoubleValue(rnd.nextDouble());
} else {
final BigInteger rndInt = new BigInteger(64, rnd);
result = new IntegerValue(rndInt, Type.UNSIGNED_LONG);
}
} else {
final IntegerValue upper = (IntegerValue) args[0].convertTo(Type.INTEGER);
result = new IntegerValue(rnd.nextInt(upper.getInt()));
}
return result;
}
use of org.exist.xquery.value.IntegerValue in project exist by eXist-db.
the class FunctionAvailable method eval.
@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
final QName functionName = ((QNameValue) args[0].itemAt(0)).getQName();
final int arity = ((IntegerValue) args[1].itemAt(0)).getInt();
final org.exist.xquery.Module[] modules = context.getModules(functionName.getNamespaceURI());
boolean found = false;
if (isEmpty(modules)) {
found = context.resolveFunction(functionName, arity) != null;
} else {
for (final org.exist.xquery.Module module : modules) {
if (module instanceof InternalModule) {
found = ((InternalModule) module).getFunctionDef(functionName, arity) != null;
} else if (module instanceof ExternalModule) {
found = ((ExternalModule) module).getFunction(functionName, arity, context) != null;
}
if (found) {
break;
}
}
}
return BooleanValue.valueOf(found);
}
use of org.exist.xquery.value.IntegerValue in project exist by eXist-db.
the class RangeExpression method eval.
/* (non-Javadoc)
* @see org.exist.xquery.Expression#eval(org.exist.dom.persistent.DocumentSet, org.exist.xquery.value.Sequence, org.exist.xquery.value.Item)
*/
public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
Sequence result = null;
final Sequence startSeq = start.eval(contextSequence, contextItem);
final Sequence endSeq = end.eval(contextSequence, contextItem);
if (startSeq.isEmpty()) {
result = Sequence.EMPTY_SEQUENCE;
} else if (endSeq.isEmpty()) {
result = Sequence.EMPTY_SEQUENCE;
} else if (startSeq.hasMany()) {
throw new XPathException(this, ErrorCodes.XPTY0004, "The first operand must have at most one item", startSeq);
} else if (endSeq.hasMany()) {
throw new XPathException(this, ErrorCodes.XPTY0004, "The second operand must have at most one item", endSeq);
} else {
if (context.isBackwardsCompatible()) {
NumericValue valueStart;
try {
// Currently breaks 1e3 to 3
valueStart = (NumericValue) startSeq.itemAt(0).convertTo(Type.NUMBER);
} catch (final XPathException e) {
throw new XPathException(this, ErrorCodes.FORG0006, "Required type is " + Type.getTypeName(Type.INTEGER) + " but got '" + Type.getTypeName(startSeq.itemAt(0).getType()) + "(" + startSeq.itemAt(0).getStringValue() + ")'", startSeq);
}
NumericValue valueEnd;
try {
// Currently breaks 3 to 1e3
valueEnd = (NumericValue) endSeq.itemAt(0).convertTo(Type.NUMBER);
} catch (final XPathException e) {
throw new XPathException(this, ErrorCodes.FORG0006, "Required type is " + Type.getTypeName(Type.INTEGER) + " but got '" + Type.getTypeName(endSeq.itemAt(0).getType()) + "(" + endSeq.itemAt(0).getStringValue() + ")'", endSeq);
}
// Implied by previous conversion
if (valueStart.hasFractionalPart()) {
throw new XPathException(this, ErrorCodes.FORG0006, "Required type is " + Type.getTypeName(Type.INTEGER) + " but got '" + Type.getTypeName(startSeq.itemAt(0).getType()) + "(" + startSeq.itemAt(0).getStringValue() + ")'", startSeq);
}
// Implied by previous conversion
if (valueEnd.hasFractionalPart()) {
throw new XPathException(this, ErrorCodes.FORG0006, "Required type is " + Type.getTypeName(Type.INTEGER) + " but got '" + Type.getTypeName(endSeq.itemAt(0).getType()) + "(" + startSeq.itemAt(0).getStringValue() + ")'", endSeq);
}
// result = new ValueSequence();
// for(long i = ((IntegerValue)valueStart.convertTo(Type.INTEGER)).getLong();
// i <= ((IntegerValue)valueEnd.convertTo(Type.INTEGER)).getLong(); i++) {
// result.add(new IntegerValue(i));
// }
result = new RangeSequence((IntegerValue) valueStart.convertTo(Type.INTEGER), (IntegerValue) valueEnd.convertTo(Type.INTEGER));
} else {
// Quite unusual test : we accept integers but no other *typed* type
if (!Type.subTypeOf(startSeq.itemAt(0).atomize().getType(), Type.INTEGER) && !Type.subTypeOf(startSeq.itemAt(0).atomize().getType(), Type.UNTYPED_ATOMIC)) {
throw new XPathException(this, ErrorCodes.FORG0006, "Required type is " + Type.getTypeName(Type.INTEGER) + " but got '" + Type.getTypeName(startSeq.itemAt(0).getType()) + "(" + startSeq.itemAt(0).getStringValue() + ")'", startSeq);
}
// Quite unusual test : we accept integers but no other *typed* type
if (!Type.subTypeOf(endSeq.itemAt(0).atomize().getType(), Type.INTEGER) && !Type.subTypeOf(endSeq.itemAt(0).atomize().getType(), Type.UNTYPED_ATOMIC)) {
throw new XPathException(this, ErrorCodes.FORG0006, "Required type is " + Type.getTypeName(Type.INTEGER) + " but got '" + Type.getTypeName(endSeq.itemAt(0).getType()) + "(" + endSeq.itemAt(0).getStringValue() + ")'", endSeq);
}
final IntegerValue valueStart = (IntegerValue) startSeq.itemAt(0).convertTo(Type.INTEGER);
final IntegerValue valueEnd = (IntegerValue) endSeq.itemAt(0).convertTo(Type.INTEGER);
// result = new ValueSequence();
// for (long i = valueStart.getLong(); i <= valueEnd.getLong(); i++) {
// result.add(new IntegerValue(i));
// }
result = new RangeSequence(valueStart, valueEnd);
}
}
return result;
}
Aggregations