Search in sources :

Example 36 with Item

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

the class FunName 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 = StringValue.EMPTY_STRING;
    } 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();
        if (n instanceof INode) {
            result = new StringValue(((INode) n).getQName().getStringValue());
        } else {
            result = StringValue.EMPTY_STRING;
        }
    }
    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) INode(org.exist.dom.INode) Node(org.w3c.dom.Node) Sequence(org.exist.xquery.value.Sequence) StringValue(org.exist.xquery.value.StringValue)

Example 37 with Item

use of org.exist.xquery.value.Item 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 38 with Item

use of org.exist.xquery.value.Item 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 39 with Item

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

the class BasicNodeSetTest method executeQuery.

private static Sequence executeQuery(final DBBroker broker, final String query, final int expected, final String expectedResult) throws XPathException, SAXException, PermissionDeniedException {
    final XQuery xquery = broker.getBrokerPool().getXQueryService();
    final Sequence seq = xquery.execute(broker, query, null);
    assertEquals(expected, seq.getItemCount());
    if (expectedResult != null) {
        final Item item = seq.itemAt(0);
        final String value = serialize(broker, item);
        assertEquals(expectedResult, value);
    }
    return seq;
}
Also used : Item(org.exist.xquery.value.Item) XQuery(org.exist.xquery.XQuery) Sequence(org.exist.xquery.value.Sequence)

Example 40 with Item

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

the class RecoveryTest method verify.

private void verify(final BrokerPool pool) throws EXistException, PermissionDeniedException, SAXException, XPathException, IOException, BTreeException, LockException {
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        final Serializer serializer = broker.borrowSerializer();
        try {
            try (final LockedDocument lockedDoc = broker.getXMLResource(XmldbURI.ROOT_COLLECTION_URI.append("test/test2/hamlet.xml"), LockMode.READ_LOCK)) {
                assertNotNull("Document '" + XmldbURI.ROOT_COLLECTION + "/test/test2/hamlet.xml' should not be null", lockedDoc);
                final String data = serializer.serialize(lockedDoc.getDocument());
                assertNotNull(data);
            }
            try (final LockedDocument lockedDoc = broker.getXMLResource(XmldbURI.ROOT_COLLECTION_URI.append("test/test2/test_string.xml"), LockMode.READ_LOCK)) {
                assertNotNull("Document '" + XmldbURI.ROOT_COLLECTION + "/test/test2/test_string.xml' should not be null", lockedDoc);
                final String data = serializer.serialize(lockedDoc.getDocument());
                assertNotNull(data);
            }
            final String lastSampleName = SAMPLES.getShakespeareXmlSampleNames()[SAMPLES.getShakespeareXmlSampleNames().length - 1];
            try (final LockedDocument lockedDoc = broker.getXMLResource(TestConstants.TEST_COLLECTION_URI2.append(lastSampleName), LockMode.READ_LOCK)) {
                assertNull("Document '" + XmldbURI.ROOT_COLLECTION + "/test/test2/'" + lastSampleName + " should not exist anymore", lockedDoc);
            }
            final XQuery xquery = pool.getXQueryService();
            assertNotNull(xquery);
            final Sequence seq = xquery.execute(broker, "//SPEECH[contains(LINE, 'king')]", null);
            assertNotNull(seq);
            for (final SequenceIterator i = seq.iterate(); i.hasNext(); ) {
                final Item next = i.nextItem();
                final String value = serializer.serialize((NodeValue) next);
            }
        } finally {
            broker.returnSerializer(serializer);
        }
        try (final LockedDocument lockedBinDoc = broker.getXMLResource(TestConstants.TEST_COLLECTION_URI2.append(TestConstants.TEST_BINARY_URI), LockMode.READ_LOCK)) {
            assertNotNull("Binary document is null", lockedBinDoc);
            final BinaryDocument binDoc = (BinaryDocument) lockedBinDoc.getDocument();
            try (final InputStream is = broker.getBinaryResource(binDoc)) {
                final byte[] bdata = new byte[(int) binDoc.getContentLength()];
                is.read(bdata);
                final String data = new String(bdata);
                assertNotNull(data);
            }
        }
        final DOMFile domDb = ((NativeBroker) broker).getDOMFile();
        assertNotNull(domDb);
        try (final Writer writer = new StringWriter()) {
            domDb.dump(writer);
        }
        final TransactionManager transact = pool.getTransactionManager();
        try (final Txn transaction = transact.beginTransaction()) {
            try (final Collection root = broker.openCollection(TestConstants.TEST_COLLECTION_URI, LockMode.WRITE_LOCK)) {
                assertNotNull(root);
                transaction.acquireCollectionLock(() -> broker.getBrokerPool().getLockManager().acquireCollectionWriteLock(root.getURI()));
                broker.removeCollection(transaction, root);
            }
            transact.commit(transaction);
        }
    }
}
Also used : XQuery(org.exist.xquery.XQuery) InputStream(java.io.InputStream) Sequence(org.exist.xquery.value.Sequence) DOMFile(org.exist.storage.dom.DOMFile) Txn(org.exist.storage.txn.Txn) BinaryDocument(org.exist.dom.persistent.BinaryDocument) Item(org.exist.xquery.value.Item) SequenceIterator(org.exist.xquery.value.SequenceIterator) StringWriter(java.io.StringWriter) TransactionManager(org.exist.storage.txn.TransactionManager) LockedDocument(org.exist.dom.persistent.LockedDocument) Collection(org.exist.collections.Collection) StringWriter(java.io.StringWriter) Writer(java.io.Writer) Serializer(org.exist.storage.serializers.Serializer)

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