Search in sources :

Example 26 with SequenceIterator

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

the class FunIndexOf method eval.

/* (non-Javadoc)
	 * @see org.exist.xquery.BasicFunction#eval(org.exist.xquery.value.Sequence[], org.exist.xquery.value.Sequence)
	 */
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()) {
        return Sequence.EMPTY_SEQUENCE;
    } else {
        final AtomicValue srch = args[1].itemAt(0).atomize();
        Collator collator;
        if (getSignature().getArgumentCount() == 3) {
            final String collation = args[2].getStringValue();
            collator = context.getCollator(collation);
        } else {
            collator = context.getDefaultCollator();
        }
        result = new ValueSequence();
        int j = 1;
        for (final SequenceIterator i = args[0].iterate(); i.hasNext(); j++) {
            final AtomicValue next = i.nextItem().atomize();
            try {
                if (ValueComparison.compareAtomic(collator, next, srch, StringTruncationOperator.NONE, Comparison.EQ)) {
                    result.add(new IntegerValue(j));
                }
            } catch (final XPathException e) {
            // Ignore me : values can not be compared
            }
        }
    }
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return result;
}
Also used : SequenceIterator(org.exist.xquery.value.SequenceIterator) XPathException(org.exist.xquery.XPathException) IntegerValue(org.exist.xquery.value.IntegerValue) ValueSequence(org.exist.xquery.value.ValueSequence) AtomicValue(org.exist.xquery.value.AtomicValue) ValueSequence(org.exist.xquery.value.ValueSequence) Sequence(org.exist.xquery.value.Sequence) Collator(com.ibm.icu.text.Collator)

Example 27 with SequenceIterator

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

the class FunAvg 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 {
    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 inner = getArgument(0).eval(contextSequence, contextItem);
    if (inner.isEmpty()) {
        result = Sequence.EMPTY_SEQUENCE;
    } else {
        final SequenceIterator iter = inner.iterate();
        Item item = iter.nextItem();
        AtomicValue value = item.atomize();
        // Any values of type xdt:untypedAtomic are cast to xs:double
        if (value.getType() == Type.UNTYPED_ATOMIC) {
            value = value.convertTo(Type.DOUBLE);
        }
        if (!(value instanceof ComputableValue)) {
            throw new XPathException(this, ErrorCodes.FORG0006, Type.getTypeName(value.getType()) + "(" + value + ") " + "can not be an operand in a sum", value);
        }
        // Set the first value
        ComputableValue sum = (ComputableValue) value;
        while (iter.hasNext()) {
            item = iter.nextItem();
            value = item.atomize();
            // Any value of type xdt:untypedAtomic are cast to xs:double
            if (value.getType() == Type.UNTYPED_ATOMIC) {
                value = value.convertTo(Type.DOUBLE);
            }
            if (!(value instanceof ComputableValue)) {
                throw new XPathException(this, ErrorCodes.FORG0006, "" + Type.getTypeName(value.getType()) + "(" + value + ") can not be an operand in a sum", value);
            }
            if (Type.subTypeOfUnion(value.getType(), Type.NUMBER)) {
                if (((NumericValue) value).isInfinite()) {
                    gotInfinity = true;
                }
                if (((NumericValue) value).isNaN()) {
                    sum = DoubleValue.NaN;
                    break;
                }
            }
            try {
                sum = (ComputableValue) sum.promote(value);
                // Aggregate next values
                sum = sum.plus((ComputableValue) value);
            } catch (final XPathException e) {
                throw new XPathException(this, ErrorCodes.FORG0006, e.getMessage());
            }
        }
        result = sum.div(new IntegerValue(inner.getItemCount()));
    }
    if (!gotInfinity) {
        if (Type.subTypeOfUnion(result.getItemType(), Type.NUMBER) && ((NumericValue) result).isInfinite()) {
        // Throw an overflow exception here since we get an infinity
        // whereas is hasn't been provided by the sequence
        // TODO ? -pb
        }
    }
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return result;
}
Also used : Item(org.exist.xquery.value.Item) ComputableValue(org.exist.xquery.value.ComputableValue) SequenceIterator(org.exist.xquery.value.SequenceIterator) XPathException(org.exist.xquery.XPathException) IntegerValue(org.exist.xquery.value.IntegerValue) AtomicValue(org.exist.xquery.value.AtomicValue) Sequence(org.exist.xquery.value.Sequence) NumericValue(org.exist.xquery.value.NumericValue)

Example 28 with SequenceIterator

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

the class FunDistinctValues method eval.

/* (non-Javadoc)
     * @see org.exist.xquery.Expression#eval(org.exist.xquery.StaticContext, org.exist.dom.persistent.DocumentSet, org.exist.xquery.value.Sequence, org.exist.xquery.value.Item)
     */
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());
        }
    }
    final Sequence seq = getArgument(0).eval(contextSequence, contextItem);
    final Collator collator = getCollator(contextSequence, contextItem, 2);
    final TreeSet<AtomicValue> set = new TreeSet<>(new ValueComparator(collator));
    final ValueSequence result = new ValueSequence();
    Item item;
    AtomicValue value;
    boolean hasAlreadyNaN = false;
    for (final SequenceIterator i = seq.iterate(); i.hasNext(); ) {
        item = i.nextItem();
        value = item.atomize();
        if (!set.contains(value)) {
            if (Type.subTypeOfUnion(value.getType(), Type.NUMBER)) {
                if (((NumericValue) value).isNaN()) {
                    // contains multiple NaN values a single NaN is returned.
                    if (!hasAlreadyNaN) {
                        set.add(value);
                        result.add(value);
                        hasAlreadyNaN = true;
                    }
                } else {
                    set.add(value);
                    result.add(value);
                }
            } else {
                set.add(value);
                result.add(value);
            }
        }
    }
    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) TreeSet(java.util.TreeSet) ValueSequence(org.exist.xquery.value.ValueSequence) AtomicValue(org.exist.xquery.value.AtomicValue) ValueSequence(org.exist.xquery.value.ValueSequence) Sequence(org.exist.xquery.value.Sequence) NumericValue(org.exist.xquery.value.NumericValue) Collator(com.ibm.icu.text.Collator)

Example 29 with SequenceIterator

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

the class FunIdRef method getIdRef.

private void getIdRef(Sequence result, Sequence seq, String id) throws XPathException {
    final Set<org.exist.dom.memtree.DocumentImpl> visitedDocs = new TreeSet<>();
    for (final SequenceIterator i = seq.iterate(); i.hasNext(); ) {
        final org.exist.dom.memtree.NodeImpl v = (org.exist.dom.memtree.NodeImpl) i.nextItem();
        final org.exist.dom.memtree.DocumentImpl doc = v.getOwnerDocument();
        if (!visitedDocs.contains(doc)) {
            final org.exist.dom.memtree.NodeImpl node = doc.selectByIdref(id);
            if (node != null) {
                result.add(node);
            }
            visitedDocs.add(doc);
        }
    }
}
Also used : SequenceIterator(org.exist.xquery.value.SequenceIterator) TreeSet(java.util.TreeSet)

Example 30 with SequenceIterator

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

the class NodeProxyTest method iterate_loop.

@Test
public void iterate_loop() {
    final NodeProxy mockNodeProxy = new NodeProxy(null, null, Node.ELEMENT_NODE, -1);
    final SequenceIterator it = mockNodeProxy.iterate();
    int count = 0;
    while (it.hasNext()) {
        it.nextItem();
        count++;
    }
    assertEquals(1, count);
}
Also used : SequenceIterator(org.exist.xquery.value.SequenceIterator) Test(org.junit.Test)

Aggregations

SequenceIterator (org.exist.xquery.value.SequenceIterator)75 Sequence (org.exist.xquery.value.Sequence)40 Item (org.exist.xquery.value.Item)35 XPathException (org.exist.xquery.XPathException)19 ValueSequence (org.exist.xquery.value.ValueSequence)16 Test (org.junit.Test)16 SAXException (org.xml.sax.SAXException)11 XQuery (org.exist.xquery.XQuery)10 Collator (com.ibm.icu.text.Collator)9 AtomicValue (org.exist.xquery.value.AtomicValue)9 MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)8 DBBroker (org.exist.storage.DBBroker)8 NumericValue (org.exist.xquery.value.NumericValue)8 Properties (java.util.Properties)7 StringWriter (java.io.StringWriter)6 EXistException (org.exist.EXistException)6 NodeProxy (org.exist.dom.persistent.NodeProxy)6 NodeSet (org.exist.dom.persistent.NodeSet)6 BrokerPool (org.exist.storage.BrokerPool)6 IOException (java.io.IOException)5