Search in sources :

Example 61 with Item

use of org.exist.xquery.value.Item in project exist by eXist-db.

the class FunFloor method eval.

public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().start(this);
        context.getProfiler().message(this, Profiler.DEPENDENCIES, "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies()));
        if (contextSequence != null) {
            context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
        }
        if (contextItem != null) {
            context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());
        }
    }
    // if (contextItem != null)
    // contextSequence = contextItem.toSequence();
    Sequence result;
    final Sequence seq = getArgument(0).eval(contextSequence, contextItem);
    if (seq.isEmpty()) {
        result = Sequence.EMPTY_SEQUENCE;
    } else {
        final Item item = seq.itemAt(0);
        NumericValue value;
        if (item instanceof NumericValue) {
            value = (NumericValue) item;
        } else {
            value = (NumericValue) item.convertTo(Type.NUMBER);
        }
        result = value.floor();
    }
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return result;
}
Also used : Item(org.exist.xquery.value.Item) Sequence(org.exist.xquery.value.Sequence) NumericValue(org.exist.xquery.value.NumericValue)

Example 62 with Item

use of org.exist.xquery.value.Item in project exist by eXist-db.

the class FunHigherOrderFun method eval.

@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
    Sequence result = new ValueSequence();
    if (isCalledAs("for-each")) {
        try (final FunctionReference ref = (FunctionReference) args[1].itemAt(0)) {
            ref.analyze(cachedContextInfo);
            if (funcRefHasDifferentArity(ref, 1)) {
                throw new XPathException(this, ErrorCodes.XPTY0004, "The supplied function (" + ref.getStringValue() + ") has " + ref.getSignature().getArgumentCount() + " arguments - expected 1");
            }
            for (final SequenceIterator i = args[0].iterate(); i.hasNext(); ) {
                final Item item = i.nextItem();
                final Sequence r = ref.evalFunction(null, null, new Sequence[] { item.toSequence() });
                result.addAll(r);
            }
        }
    } else if (isCalledAs("filter")) {
        final FunctionReference refParam;
        final Sequence seq;
        // Hack: switch parameters for backwards compatibility
        if (Type.subTypeOf(args[1].getItemType(), Type.FUNCTION_REFERENCE)) {
            refParam = (FunctionReference) args[1].itemAt(0);
            seq = args[0];
        } else {
            refParam = (FunctionReference) args[0].itemAt(0);
            seq = args[1];
        }
        try (final FunctionReference ref = refParam) {
            ref.analyze(cachedContextInfo);
            if (funcRefHasDifferentArity(ref, 1)) {
                throw new XPathException(this, ErrorCodes.XPTY0004, "The supplied function (" + ref.getStringValue() + ") has " + ref.getSignature().getArgumentCount() + " arguments - expected 1");
            }
            for (final SequenceIterator i = seq.iterate(); i.hasNext(); ) {
                final Item item = i.nextItem();
                final Sequence r = ref.evalFunction(null, null, new Sequence[] { item.toSequence() });
                // two pending tests in exist-core/src/test/xquery/xquery3/fnHigherOrderFunctions.xql
                if (r.effectiveBooleanValue()) {
                    result.add(item);
                }
            }
        }
    } else if (isCalledAs("fold-left")) {
        try (final FunctionReference ref = (FunctionReference) args[2].itemAt(0)) {
            ref.analyze(cachedContextInfo);
            if (funcRefHasDifferentArity(ref, 2)) {
                throw new XPathException(this, ErrorCodes.XPTY0004, "The supplied function (" + ref.getStringValue() + ") has " + ref.getSignature().getArgumentCount() + " arguments - expected 2");
            }
            final Sequence seq = args[0];
            final Sequence zero = args[1];
            result = foldLeft(ref, zero, seq.iterate());
        }
    } else if (isCalledAs("fold-right")) {
        try (final FunctionReference ref = (FunctionReference) args[2].itemAt(0)) {
            ref.analyze(cachedContextInfo);
            if (funcRefHasDifferentArity(ref, 2)) {
                throw new XPathException(this, ErrorCodes.XPTY0004, "The supplied function (" + ref.getStringValue() + ") has " + ref.getSignature().getArgumentCount() + " arguments - expected 2");
            }
            final Sequence zero = args[1];
            final Sequence seq = args[0];
            if (seq instanceof ValueSequence) {
                result = foldRightNonRecursive(ref, zero, ((ValueSequence) seq).iterateInReverse());
            } else if (seq instanceof RangeSequence) {
                result = foldRightNonRecursive(ref, zero, ((RangeSequence) seq).iterateInReverse());
            } else {
                result = foldRight(ref, zero, seq);
            }
        }
    } else if (isCalledAs("for-each-pair")) {
        try (final FunctionReference ref = (FunctionReference) args[2].itemAt(0)) {
            ref.analyze(cachedContextInfo);
            if (funcRefHasDifferentArity(ref, 2)) {
                throw new XPathException(this, ErrorCodes.XPTY0004, "The supplied function (" + ref.getStringValue() + ") has " + ref.getSignature().getArgumentCount() + " arguments - expected 2");
            }
            final SequenceIterator i1 = args[0].iterate();
            final SequenceIterator i2 = args[1].iterate();
            while (i1.hasNext() && i2.hasNext()) {
                final Sequence r = ref.evalFunction(null, null, new Sequence[] { i1.nextItem().toSequence(), i2.nextItem().toSequence() });
                result.addAll(r);
            }
        }
    } else if (isCalledAs("apply")) {
        try (final FunctionReference ref = (FunctionReference) args[0].itemAt(0)) {
            ref.analyze(cachedContextInfo);
            final ArrayType array = (ArrayType) args[1].itemAt(0);
            if (funcRefHasDifferentArity(ref, array.getSize())) {
                throw new XPathException(this, ErrorCodes.FOAP0001, "Number of arguments supplied to fn:apply does not match function signature. Expected: " + ref.getSignature().getArgumentCount() + ", got: " + array.getSize());
            }
            final Sequence[] fargs = array.toArray();
            return ref.evalFunction(null, null, fargs);
        }
    }
    return result;
}
Also used : ArrayType(org.exist.xquery.functions.array.ArrayType) Item(org.exist.xquery.value.Item) SequenceIterator(org.exist.xquery.value.SequenceIterator) ValueSequence(org.exist.xquery.value.ValueSequence) FunctionReference(org.exist.xquery.value.FunctionReference) ValueSequence(org.exist.xquery.value.ValueSequence) Sequence(org.exist.xquery.value.Sequence)

Example 63 with Item

use of org.exist.xquery.value.Item in project exist by eXist-db.

the class FunReverse method eval.

public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().start(this);
        context.getProfiler().message(this, Profiler.DEPENDENCIES, "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies()));
        if (contextSequence != null) {
            context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
        }
        if (contextItem != null) {
            context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());
        }
    }
    Sequence result;
    final Sequence seq = getArguments(contextSequence, contextItem)[0];
    if (seq.isEmpty()) {
        result = Sequence.EMPTY_SEQUENCE;
    } else {
        final Sequence tmp = new ValueSequence();
        Item item;
        for (final SequenceIterator i = seq.iterate(); i.hasNext(); ) {
            item = i.nextItem();
            tmp.add(item);
        }
        result = new ValueSequence();
        for (int i = seq.getItemCount() - 1; i >= 0; i--) {
            result.add(tmp.itemAt(i));
        }
    }
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return result;
}
Also used : Item(org.exist.xquery.value.Item) SequenceIterator(org.exist.xquery.value.SequenceIterator) ValueSequence(org.exist.xquery.value.ValueSequence) ValueSequence(org.exist.xquery.value.ValueSequence) Sequence(org.exist.xquery.value.Sequence)

Example 64 with Item

use of org.exist.xquery.value.Item in project exist by eXist-db.

the class FunRound method eval.

public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().start(this);
        context.getProfiler().message(this, Profiler.DEPENDENCIES, "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies()));
        if (contextSequence != null) {
            context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
        }
        if (contextItem != null) {
            context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());
        }
    }
    if (contextItem != null) {
        contextSequence = contextItem.toSequence();
    }
    Sequence result;
    final Sequence seq = getArgument(0).eval(contextSequence, contextItem);
    if (seq.isEmpty()) {
        result = Sequence.EMPTY_SEQUENCE;
    } else {
        final Item item = seq.itemAt(0);
        NumericValue value;
        if (item instanceof NumericValue) {
            value = (NumericValue) item;
        } else {
            value = (NumericValue) item.convertTo(Type.NUMBER);
        }
        result = value.round();
    }
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return result;
}
Also used : Item(org.exist.xquery.value.Item) Sequence(org.exist.xquery.value.Sequence) NumericValue(org.exist.xquery.value.NumericValue)

Example 65 with Item

use of org.exist.xquery.value.Item in project exist by eXist-db.

the class FunNilled method eval.

public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().start(this);
        context.getProfiler().message(this, Profiler.DEPENDENCIES, "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies()));
        if (contextSequence != null) {
            context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
        }
    }
    Sequence result;
    if (args[0].isEmpty()) {
        result = Sequence.EMPTY_SEQUENCE;
    } else {
        final Item arg = args[0].itemAt(0);
        if (!Type.subTypeOf(arg.getType(), Type.ELEMENT)) {
            result = Sequence.EMPTY_SEQUENCE;
        } else {
            final Node n = ((NodeValue) arg).getNode();
            if (n.hasAttributes()) {
                final Node nilled = n.getAttributes().getNamedItemNS(Namespaces.SCHEMA_INSTANCE_NS, "nil");
                if (nilled != null) {
                    result = new BooleanValue(nilled.getNodeValue().equals("true"));
                } else {
                    result = BooleanValue.FALSE;
                }
            } else {
                result = BooleanValue.FALSE;
            }
        }
    }
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return result;
}
Also used : Item(org.exist.xquery.value.Item) NodeValue(org.exist.xquery.value.NodeValue) Node(org.w3c.dom.Node) BooleanValue(org.exist.xquery.value.BooleanValue) Sequence(org.exist.xquery.value.Sequence)

Aggregations

Item (org.exist.xquery.value.Item)88 Sequence (org.exist.xquery.value.Sequence)69 SequenceIterator (org.exist.xquery.value.SequenceIterator)36 XPathException (org.exist.xquery.XPathException)26 DBBroker (org.exist.storage.DBBroker)18 NodeValue (org.exist.xquery.value.NodeValue)17 XQuery (org.exist.xquery.XQuery)16 ValueSequence (org.exist.xquery.value.ValueSequence)16 BrokerPool (org.exist.storage.BrokerPool)14 NumericValue (org.exist.xquery.value.NumericValue)12 SAXException (org.xml.sax.SAXException)11 Properties (java.util.Properties)10 StringValue (org.exist.xquery.value.StringValue)10 Node (org.w3c.dom.Node)10 MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)9 AtomicValue (org.exist.xquery.value.AtomicValue)9 Txn (org.exist.storage.txn.Txn)8 IOException (java.io.IOException)7 StringWriter (java.io.StringWriter)7 EXistException (org.exist.EXistException)7