Search in sources :

Example 56 with Item

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

the class FunSort method eval.

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    Sequence seq = args[0];
    Collator collator = collator(args, 1);
    ArrayList<Sequence> keys = new ArrayList<>(seq.getItemCount());
    try (FunctionReference ref = function(args, 2)) {
        final Sequence[] refArgs = new Sequence[1];
        Item item;
        Sequence value;
        for (final SequenceIterator i = seq.iterate(); i.hasNext(); ) {
            item = i.nextItem();
            if (ref != null) {
                refArgs[0] = item.toSequence();
                value = ref.evalFunction(null, null, refArgs);
            } else {
                value = item.toSequence();
            }
            keys.add(Atomize.atomize(value));
        }
    }
    return sort(seq, keys, collator);
}
Also used : Item(org.exist.xquery.value.Item) SequenceIterator(org.exist.xquery.value.SequenceIterator) ArrayList(java.util.ArrayList) FunctionReference(org.exist.xquery.value.FunctionReference) ValueSequence(org.exist.xquery.value.ValueSequence) Sequence(org.exist.xquery.value.Sequence) Collator(com.ibm.icu.text.Collator)

Example 57 with Item

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

the class FunStringJoin 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);
        }
    }
    String sep = null;
    if (getArgumentCount() == 2) {
        sep = args[1].getStringValue();
        if (sep.isEmpty()) {
            sep = null;
        }
    }
    final StringBuilder out = new StringBuilder();
    Item next;
    boolean gotOne = false;
    for (final SequenceIterator i = args[0].iterate(); i.hasNext(); ) {
        next = i.nextItem();
        if (gotOne && sep != null) {
            out.append(sep);
        }
        out.append(next.getStringValue());
        gotOne = true;
    }
    final Sequence result = new StringValue(out.toString());
    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) Sequence(org.exist.xquery.value.Sequence) StringValue(org.exist.xquery.value.StringValue)

Example 58 with Item

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

the class FunSum 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 inner = getArgument(0).eval(contextSequence, contextItem);
    if (inner.isEmpty()) {
        // If $zero is not specified, then the value returned for an empty sequence is the xs:integer value 0
        Sequence zero = IntegerValue.ZERO;
        if (getSignature().getArgumentCount() == 2) {
            zero = getArgument(1).eval(contextSequence, contextItem);
        }
        result = zero;
    } else {
        final SequenceIterator iter = inner.iterate();
        Item item = iter.nextItem();
        AtomicValue value = item.atomize();
        value = check(value, null);
        // Set the first value
        ComputableValue sum = (ComputableValue) value;
        while (iter.hasNext()) {
            item = iter.nextItem();
            value = item.atomize();
            value = check(value, sum);
            if (Type.subTypeOfUnion(value.getType(), Type.NUMBER)) {
                if (((NumericValue) value).isInfinite()) {
                    gotInfinity = true;
                }
                if (((NumericValue) value).isNaN()) {
                    sum = DoubleValue.NaN;
                    break;
                }
            }
            sum = (ComputableValue) sum.promote(value);
            // Aggregate next values
            sum = sum.plus((ComputableValue) value);
        }
        result = sum;
    }
    if (!gotInfinity) {
        if (Type.subTypeOfUnion(result.getItemType(), Type.NUMBER) && ((NumericValue) result).isInfinite()) {
        // Throw an overflow eception here since we get an infinity
        // whereas is hasn't been provided by the sequence
        }
    }
    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) AtomicValue(org.exist.xquery.value.AtomicValue) Sequence(org.exist.xquery.value.Sequence) NumericValue(org.exist.xquery.value.NumericValue)

Example 59 with Item

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

the class FunMax 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 arg = getArgument(0).eval(contextSequence, contextItem);
    if (arg.isEmpty()) {
        result = Sequence.EMPTY_SEQUENCE;
    } else {
        boolean computableProcessing = false;
        // TODO : test if a range index is defined *iff* it is compatible with the collator
        final Collator collator = getCollator(contextSequence, contextItem, 2);
        final SequenceIterator iter = arg.unorderedIterator();
        AtomicValue max = null;
        while (iter.hasNext()) {
            final Item item = iter.nextItem();
            if (item instanceof QNameValue) {
                throw new XPathException(this, ErrorCodes.FORG0006, "Cannot compare " + Type.getTypeName(item.getType()), arg);
            }
            AtomicValue value = item.atomize();
            // Duration values must either all be xs:yearMonthDuration values or must all be xs:dayTimeDuration values.
            if (Type.subTypeOf(value.getType(), Type.DURATION)) {
                value = ((DurationValue) value).wrap();
                if (value.getType() == Type.YEAR_MONTH_DURATION) {
                    if (max != null && max.getType() != Type.YEAR_MONTH_DURATION) {
                        throw new XPathException(this, ErrorCodes.FORG0006, "Cannot compare " + Type.getTypeName(max.getType()) + " and " + Type.getTypeName(value.getType()), value);
                    }
                } else if (value.getType() == Type.DAY_TIME_DURATION) {
                    if (max != null && max.getType() != Type.DAY_TIME_DURATION) {
                        throw new XPathException(this, ErrorCodes.FORG0006, "Cannot compare " + Type.getTypeName(max.getType()) + " and " + Type.getTypeName(value.getType()), value);
                    }
                } else {
                    throw new XPathException(this, ErrorCodes.FORG0006, "Cannot compare " + Type.getTypeName(value.getType()), value);
                }
            // Any value of type xdt:untypedAtomic is cast to xs:double
            } else if (value.getType() == Type.UNTYPED_ATOMIC) {
                value = value.convertTo(Type.DOUBLE);
            }
            if (max == null) {
                max = value;
            } else {
                if (Type.getCommonSuperType(max.getType(), value.getType()) == Type.ATOMIC) {
                    throw new XPathException(this, ErrorCodes.FORG0006, "Cannot compare " + Type.getTypeName(max.getType()) + " and " + Type.getTypeName(value.getType()), max);
                }
                // Any value of type xdt:untypedAtomic is cast to xs:double
                if (value.getType() == Type.UNTYPED_ATOMIC) {
                    value = value.convertTo(Type.DOUBLE);
                }
                // Numeric tests
                if (Type.subTypeOfUnion(value.getType(), Type.NUMBER)) {
                    // Don't mix comparisons
                    if (!Type.subTypeOfUnion(max.getType(), Type.NUMBER)) {
                        throw new XPathException(this, ErrorCodes.FORG0006, "Cannot compare " + Type.getTypeName(max.getType()) + " and " + Type.getTypeName(value.getType()), max);
                    }
                    if (((NumericValue) value).isNaN()) {
                        // Type NaN correctly
                        value = value.promote(max);
                        if (value.getType() == Type.FLOAT) {
                            max = FloatValue.NaN;
                        } else {
                            max = DoubleValue.NaN;
                        }
                        // although result will be NaN, we need to continue on order to type correctly
                        continue;
                    } else {
                        max = max.promote(value);
                    }
                }
                // Ugly test
                if (max instanceof ComputableValue && value instanceof ComputableValue) {
                    // Type value correctly
                    value = value.promote(max);
                    max = (ComputableValue) max.max(collator, value);
                    computableProcessing = true;
                } else {
                    if (computableProcessing) {
                        throw new XPathException(this, ErrorCodes.FORG0006, "Cannot compare " + Type.getTypeName(max.getType()) + " and " + Type.getTypeName(value.getType()), max);
                    }
                    if (Collations.compare(collator, value.getStringValue(), max.getStringValue()) > 0) {
                        max = value;
                    }
                }
            }
        }
        result = max;
    }
    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) QNameValue(org.exist.xquery.value.QNameValue) AtomicValue(org.exist.xquery.value.AtomicValue) Sequence(org.exist.xquery.value.Sequence) NumericValue(org.exist.xquery.value.NumericValue) Collator(com.ibm.icu.text.Collator)

Example 60 with Item

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

the class FunNodeName method eval.

@Override
public Sequence eval(Sequence contextSequence, final 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();
    }
    // If we have one argument, we take it into account
    final Sequence seq;
    if (getSignature().getArgumentCount() > 0) {
        seq = getArgument(0).eval(contextSequence, contextItem);
    } else {
        // Otherwise, we take the context sequence and we iterate over it
        seq = contextSequence;
    }
    if (seq == null) {
        throw new XPathException(this, ErrorCodes.XPDY0002, "Undefined context item");
    }
    final Sequence result;
    if (seq.isEmpty()) {
        result = Sequence.EMPTY_SEQUENCE;
    } else {
        final Item item = seq.itemAt(0);
        if (!Type.subTypeOf(item.getType(), Type.NODE)) {
            throw new XPathException(this, ErrorCodes.XPTY0004, "item is not a node; got '" + Type.getTypeName(item.getType()) + "'");
        }
        // TODO : how to improve performance ?
        final Node n = ((NodeValue) item).getNode();
        // Returns an expanded-QName for node kinds that can have names.
        if (n instanceof INode) {
            final QName qn = ((INode) n).getQName();
            if (qn.equals(QName.EMPTY_QNAME)) {
                result = Sequence.EMPTY_SEQUENCE;
            } else {
                result = new QNameValue(context, qn);
            }
        // For other kinds of nodes it returns the empty sequence.
        } else {
            result = Sequence.EMPTY_SEQUENCE;
        }
    }
    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) INode(org.exist.dom.INode) XPathException(org.exist.xquery.XPathException) QName(org.exist.dom.QName) INode(org.exist.dom.INode) Node(org.w3c.dom.Node) QNameValue(org.exist.xquery.value.QNameValue) 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