Search in sources :

Example 61 with IntegerValue

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;
}
Also used : Random(java.util.Random) DoubleValue(org.exist.xquery.value.DoubleValue) IntegerValue(org.exist.xquery.value.IntegerValue) BigInteger(java.math.BigInteger) Sequence(org.exist.xquery.value.Sequence)

Example 62 with IntegerValue

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);
}
Also used : org.exist.xquery(org.exist.xquery) QName(org.exist.dom.QName) QNameValue(org.exist.xquery.value.QNameValue) IntegerValue(org.exist.xquery.value.IntegerValue)

Example 63 with IntegerValue

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;
}
Also used : IntegerValue(org.exist.xquery.value.IntegerValue) Sequence(org.exist.xquery.value.Sequence) NumericValue(org.exist.xquery.value.NumericValue)

Aggregations

IntegerValue (org.exist.xquery.value.IntegerValue)63 Sequence (org.exist.xquery.value.Sequence)33 XPathException (org.exist.xquery.XPathException)32 BrokerPool (org.exist.storage.BrokerPool)11 DBBroker (org.exist.storage.DBBroker)11 Test (org.junit.Test)11 Txn (org.exist.storage.txn.Txn)8 MessagingException (jakarta.mail.MessagingException)7 StringInputSource (org.exist.util.StringInputSource)7 NamingException (javax.naming.NamingException)6 DirContext (javax.naming.directory.DirContext)6 QName (org.exist.dom.QName)6 Source (org.exist.source.Source)6 StringSource (org.exist.source.StringSource)6 InputStream (java.io.InputStream)5 Map (java.util.Map)5 StringValue (org.exist.xquery.value.StringValue)5 ValueSequence (org.exist.xquery.value.ValueSequence)5 Folder (jakarta.mail.Folder)4 Image (java.awt.Image)4