Search in sources :

Example 66 with Item

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

the class AbstractUpdateTest method read.

private void read(final BrokerPool pool) throws EXistException, PermissionDeniedException, SAXException, XPathException {
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        final Serializer serializer = broker.borrowSerializer();
        try (final LockedDocument lockedDoc = broker.getXMLResource(TEST_COLLECTION_URI.append("test2/test.xml"), LockMode.READ_LOCK)) {
            assertNotNull("Document '" + TEST_COLLECTION_URI.append("test2/test.xml") + "' should not be null", lockedDoc);
            final String data = serializer.serialize(lockedDoc.getDocument());
        } finally {
            broker.returnSerializer(serializer);
        }
        final XQuery xquery = pool.getXQueryService();
        final Sequence seq = xquery.execute(broker, "/products/product[last()]", null);
        for (final SequenceIterator i = seq.iterate(); i.hasNext(); ) {
            Item next = i.nextItem();
        }
    }
}
Also used : Item(org.exist.xquery.value.Item) SequenceIterator(org.exist.xquery.value.SequenceIterator) XQuery(org.exist.xquery.XQuery) LockedDocument(org.exist.dom.persistent.LockedDocument) Sequence(org.exist.xquery.value.Sequence) Serializer(org.exist.storage.serializers.Serializer)

Example 67 with Item

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

the class XQueryTest method importExternalClasspathMainModule.

@Test
public void importExternalClasspathMainModule() throws EXistException, IOException, PermissionDeniedException, XPathException, QName.IllegalQNameException {
    final long timestamp = System.currentTimeMillis();
    final BrokerPool brokerPool = BrokerPool.getInstance();
    try (final DBBroker broker = brokerPool.getBroker()) {
        final org.exist.source.Source source = SourceFactory.getSource(broker, "/", "resource:org/exist/xquery/external-classpath-main-module.xq", false);
        final XQuery xquery = brokerPool.getXQueryService();
        final XQueryPool queryPool = brokerPool.getXQueryPool();
        CompiledXQuery compiled = null;
        XQueryContext context = null;
        try {
            compiled = queryPool.borrowCompiledXQuery(broker, source);
            if (compiled == null) {
                context = new XQueryContext(brokerPool);
            } else {
                context = compiled.getContext();
                context.prepareForReuse();
            }
            context.declareVariable(new QName("s"), new IntegerValue(timestamp));
            if (compiled == null) {
                compiled = xquery.compile(context, source);
            }
            final Sequence result = xquery.execute(broker, compiled, null, null);
            assertEquals(1, result.getItemCount());
            final Item item = result.itemAt(0);
            assertTrue(Type.subTypeOf(item.getType(), Type.NODE));
            final Source expected = Input.fromString("<echo>" + timestamp + "</echo>").build();
            final Source actual = Input.fromNode((Node) item).build();
            final Diff diff = DiffBuilder.compare(expected).withTest(actual).checkForSimilar().build();
            assertFalse(diff.toString(), diff.hasDifferences());
        } finally {
            if (compiled != null) {
                compiled.reset();
            }
            if (context != null) {
                context.reset();
            }
            if (compiled != null) {
                queryPool.returnCompiledXQuery(source, compiled);
            }
        }
    }
}
Also used : Diff(org.xmlunit.diff.Diff) DetailedDiff(org.custommonkey.xmlunit.DetailedDiff) QName(org.exist.dom.QName) IntegerValue(org.exist.xquery.value.IntegerValue) Node(org.w3c.dom.Node) XMLResource(org.xmldb.api.modules.XMLResource) EXistResource(org.exist.xmldb.EXistResource) Resource(org.xmldb.api.base.Resource) Sequence(org.exist.xquery.value.Sequence) Source(javax.xml.transform.Source) XQueryPool(org.exist.storage.XQueryPool) Item(org.exist.xquery.value.Item) DBBroker(org.exist.storage.DBBroker) BrokerPool(org.exist.storage.BrokerPool)

Example 68 with Item

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

the class FunCeiling 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());
        }
    }
    final Sequence seq = getArgument(0).eval(contextSequence, contextItem);
    Sequence result;
    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.ceiling();
    }
    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 69 with Item

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

the class TypeswitchExpression method checkType.

private boolean checkType(SequenceType type, Sequence seq) throws XPathException {
    final Cardinality requiredCardinality = type.getCardinality();
    Cardinality actualCardinality;
    if (seq.isEmpty()) {
        actualCardinality = Cardinality.EMPTY_SEQUENCE;
    } else if (seq.hasMany()) {
        actualCardinality = Cardinality._MANY;
    } else {
        actualCardinality = Cardinality.EXACTLY_ONE;
    }
    if (!requiredCardinality.isSuperCardinalityOrEqualOf(actualCardinality)) {
        return false;
    }
    for (final SequenceIterator i = seq.iterate(); i.hasNext(); ) {
        final Item next = i.nextItem();
        if (!type.checkType(next)) {
            return false;
        }
    }
    return true;
}
Also used : Item(org.exist.xquery.value.Item) SequenceIterator(org.exist.xquery.value.SequenceIterator)

Example 70 with Item

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

the class DOMIndexerTest method xQuery.

@Test
public void xQuery() throws EXistException, PermissionDeniedException, SAXException, XPathException, IOException {
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
        final StringWriter out = new StringWriter()) {
        final XQuery xquery = pool.getXQueryService();
        final Sequence result = xquery.execute(broker, XQUERY, null);
        final int count = result.getItemCount();
        final Properties props = new Properties();
        props.setProperty(OutputKeys.INDENT, "yes");
        final SAXSerializer serializer = new SAXSerializer(out, props);
        serializer.startDocument();
        for (final SequenceIterator i = result.iterate(); i.hasNext(); ) {
            final Item next = i.nextItem();
            next.toSAX(broker, serializer, props);
        }
        serializer.endDocument();
        out.toString();
    }
}
Also used : Item(org.exist.xquery.value.Item) DBBroker(org.exist.storage.DBBroker) StringWriter(java.io.StringWriter) SequenceIterator(org.exist.xquery.value.SequenceIterator) XQuery(org.exist.xquery.XQuery) Sequence(org.exist.xquery.value.Sequence) Properties(java.util.Properties) SAXSerializer(org.exist.util.serializer.SAXSerializer) BrokerPool(org.exist.storage.BrokerPool) Test(org.junit.Test)

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