Search in sources :

Example 71 with Item

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

the class InspectModuleTest method xqDoc_withAtSignInline.

@Ignore("https://github.com/eXist-db/exist/issues/1386")
@Test
public void xqDoc_withAtSignInline() throws PermissionDeniedException, XPathException, EXistException {
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    final XQuery xqueryService = pool.getXQueryService();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
        final Txn transaction = pool.getTransactionManager().beginTransaction()) {
        final String query = "import module namespace inspect = \"http://exist-db.org/xquery/inspection\";\n" + "inspect:inspect-module(xs:anyURI(\"xmldb:exist://" + TEST_COLLECTION.append(TEST_MODULE).toCollectionPathURI() + "\"))\n" + "/function[@name eq \"x:fun1\"]";
        final Sequence result = xqueryService.execute(broker, query, null);
        assertNotNull(result);
        assertEquals(1, result.getItemCount());
        final Item item1 = result.itemAt(0);
        assertTrue(item1 instanceof ElementImpl);
        final Element function = (Element) item1;
        final NodeList descriptions = function.getElementsByTagName("description");
        assertEquals(1, descriptions.getLength());
        assertEquals("Some description.", descriptions.item(0).getFirstChild().getTextContent());
        final NodeList arguments = function.getElementsByTagName("argument");
        assertEquals(0, arguments.getLength());
        final NodeList returns = function.getElementsByTagName("returns");
        assertEquals(1, returns.getLength());
        assertEquals("taxonomy[@type = \"reign\"]", returns.item(0).getFirstChild().getTextContent());
        transaction.commit();
    }
}
Also used : Item(org.exist.xquery.value.Item) DBBroker(org.exist.storage.DBBroker) ElementImpl(org.exist.dom.memtree.ElementImpl) XQuery(org.exist.xquery.XQuery) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Txn(org.exist.storage.txn.Txn) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool)

Example 72 with Item

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

the class InspectModuleTest method xqDoc_withParamsAndReturn.

@Test
public void xqDoc_withParamsAndReturn() throws PermissionDeniedException, XPathException, EXistException {
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    final XQuery xqueryService = pool.getXQueryService();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
        final Txn transaction = pool.getTransactionManager().beginTransaction()) {
        final String query = "import module namespace inspect = \"http://exist-db.org/xquery/inspection\";\n" + "inspect:inspect-module(xs:anyURI(\"xmldb:exist://" + TEST_COLLECTION.append(TEST_MODULE).toCollectionPathURI() + "\"))\n" + "/function[@name eq \"x:fun2\"]";
        final Sequence result = xqueryService.execute(broker, query, null);
        assertNotNull(result);
        assertEquals(1, result.getItemCount());
        final Item item1 = result.itemAt(0);
        assertTrue(item1 instanceof ElementImpl);
        final Element function = (Element) item1;
        final NodeList descriptions = function.getElementsByTagName("description");
        assertEquals(1, descriptions.getLength());
        assertEquals("Some other description.", descriptions.item(0).getFirstChild().getNodeValue());
        final NodeList arguments = function.getElementsByTagName("argument");
        assertEquals(2, arguments.getLength());
        assertEquals("first parameter", arguments.item(0).getFirstChild().getNodeValue());
        assertEquals("second parameter", arguments.item(1).getFirstChild().getNodeValue());
        final NodeList returns = function.getElementsByTagName("returns");
        assertEquals(1, returns.getLength());
        assertEquals("our result", returns.item(0).getFirstChild().getNodeValue());
        transaction.commit();
    }
}
Also used : Item(org.exist.xquery.value.Item) DBBroker(org.exist.storage.DBBroker) ElementImpl(org.exist.dom.memtree.ElementImpl) XQuery(org.exist.xquery.XQuery) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Txn(org.exist.storage.txn.Txn) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool)

Example 73 with Item

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

the class CollectionName 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 (args[0].isEmpty()) {
        return Sequence.EMPTY_SEQUENCE;
    }
    final Item item = args[0].itemAt(0);
    if (item.getType() == Type.JAVA_OBJECT) {
        final Object o = ((JavaObjectValue) item).getObject();
        if (!(o instanceof Collection)) {
            throw new XPathException(this, "Passed Java object should be of type org.xmldb.api.base.Collection");
        }
        final Collection collection = (Collection) o;
        try {
            return new StringValue(collection.getName());
        } catch (final XMLDBException e) {
            throw new XPathException(this, "Failed to retrieve collection name", e);
        }
    } else if (Type.subTypeOf(item.getType(), Type.STRING)) {
        final String path = item.getStringValue();
        try {
            final XmldbURI uri = XmldbURI.xmldbUriFor(path).removeLastSegment();
            return new StringValue(uri.toString());
        } catch (final URISyntaxException e) {
            throw new XPathException(this, "Illegal URI for resource path: " + path);
        }
    } else if (Type.subTypeOf(item.getType(), Type.NODE)) {
        final NodeValue node = (NodeValue) item;
        if (node.getImplementationType() == NodeValue.PERSISTENT_NODE) {
            final NodeProxy p = (NodeProxy) node;
            // TODO: use xmldbUri
            return new StringValue(p.getOwnerDocument().getCollection().getURI().toString());
        }
    } else {
        throw new XPathException(this, "First argument to util:collection-name should be either " + "a Java object of type org.xmldb.api.base.Collection or a node; got: " + Type.getTypeName(item.getType()));
    }
    return Sequence.EMPTY_SEQUENCE;
}
Also used : Item(org.exist.xquery.value.Item) NodeValue(org.exist.xquery.value.NodeValue) XPathException(org.exist.xquery.XPathException) Collection(org.xmldb.api.base.Collection) XMLDBException(org.xmldb.api.base.XMLDBException) URISyntaxException(java.net.URISyntaxException) JavaObjectValue(org.exist.xquery.value.JavaObjectValue) StringValue(org.exist.xquery.value.StringValue) NodeProxy(org.exist.dom.persistent.NodeProxy) XmldbURI(org.exist.xmldb.XmldbURI)

Example 74 with Item

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

the class DeepCopyFunction method eval.

@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
    if (args[0].isEmpty()) {
        return Sequence.EMPTY_SEQUENCE;
    }
    final Item a = args[0].itemAt(0);
    context.pushDocumentContext();
    try {
        final MemTreeBuilder builder = context.getDocumentBuilder();
        builder.startDocument();
        final DocumentBuilderReceiver receiver = new DocumentBuilderReceiver(builder);
        try {
            final Properties props = new Properties();
            a.toSAX(context.getBroker(), receiver, props);
        } catch (final SAXException e) {
            throw new XPathException(this, "Cannot Deep-copy Item");
        }
        builder.endDocument();
        return (NodeValue) receiver.getDocument().getDocumentElement();
    } finally {
        context.popDocumentContext();
    }
}
Also used : Item(org.exist.xquery.value.Item) NodeValue(org.exist.xquery.value.NodeValue) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) XPathException(org.exist.xquery.XPathException) DocumentBuilderReceiver(org.exist.dom.memtree.DocumentBuilderReceiver) Properties(java.util.Properties) SAXException(org.xml.sax.SAXException)

Example 75 with Item

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

the class QNameIndexLookup method eval.

public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
    if (contextSequence == null || contextSequence.isEmpty()) {
        // if the context sequence is empty, we create a default context
        final RootNode rootNode = new RootNode(context);
        contextSequence = rootNode.eval(null, null);
    }
    final Sequence[] args = getArguments(null, null);
    final Item item = args[0].itemAt(0);
    final QNameValue qval;
    try {
        // attempt to convert the first argument to a QName
        qval = (QNameValue) item.convertTo(Type.QNAME);
    } catch (final XPathException e) {
        // wrong type: generate a diagnostic error
        throw new XPathException(this, Messages.formatMessage(Error.FUNC_PARAM_TYPE, new Object[] { "1", getSignature().toString(), null, Type.getTypeName(Type.QNAME), Type.getTypeName(item.getType()) }));
    }
    QName qname = qval.getQName();
    if (args.length == 3 && !(args[2].itemAt(0).toJavaObject(boolean.class))) {
        qname = new QName(qname.getLocalPart(), qname.getNamespaceURI(), qname.getPrefix(), ElementValue.ATTRIBUTE);
    }
    final AtomicValue comparisonCriterion = args[1].itemAt(0).atomize();
    final NativeValueIndex valueIndex = context.getBroker().getValueIndex();
    final Sequence result = valueIndex.find(context.getWatchDog(), Comparison.EQ, contextSequence.getDocumentSet(), null, NodeSet.ANCESTOR, qname, comparisonCriterion);
    return result;
}
Also used : RootNode(org.exist.xquery.RootNode) Item(org.exist.xquery.value.Item) XPathException(org.exist.xquery.XPathException) QName(org.exist.dom.QName) QNameValue(org.exist.xquery.value.QNameValue) AtomicValue(org.exist.xquery.value.AtomicValue) NativeValueIndex(org.exist.storage.NativeValueIndex) 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