Search in sources :

Example 6 with ExtArrayNodeSet

use of org.exist.dom.persistent.ExtArrayNodeSet in project exist by eXist-db.

the class FunDoctype 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());
        }
    }
    final MutableDocumentSet docs = new DefaultDocumentSet();
    for (int i = 0; i < getArgumentCount(); i++) {
        final Sequence seq = getArgument(i).eval(contextSequence, contextItem);
        for (final SequenceIterator j = seq.iterate(); j.hasNext(); ) {
            final String next = j.nextItem().getStringValue();
            try {
                context.getBroker().getXMLResourcesByDoctype(next, docs);
            } catch (final PermissionDeniedException | LockException e) {
                LOG.error(e.getMessage(), e);
                throw new XPathException(this, e);
            }
        }
    }
    final NodeSet result = new ExtArrayNodeSet(1);
    for (final Iterator<DocumentImpl> i = docs.getDocumentIterator(); i.hasNext(); ) {
        result.add(new NodeProxy(i.next(), NodeId.DOCUMENT_NODE));
    }
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return result;
}
Also used : ExtArrayNodeSet(org.exist.dom.persistent.ExtArrayNodeSet) NodeSet(org.exist.dom.persistent.NodeSet) MutableDocumentSet(org.exist.dom.persistent.MutableDocumentSet) DefaultDocumentSet(org.exist.dom.persistent.DefaultDocumentSet) DocumentImpl(org.exist.dom.persistent.DocumentImpl) NodeProxy(org.exist.dom.persistent.NodeProxy) ExtArrayNodeSet(org.exist.dom.persistent.ExtArrayNodeSet) LockException(org.exist.util.LockException) PermissionDeniedException(org.exist.security.PermissionDeniedException)

Example 7 with ExtArrayNodeSet

use of org.exist.dom.persistent.ExtArrayNodeSet in project exist by eXist-db.

the class NodeSets method transformNodes.

/**
 * Builds a new NodeSet by applying a function to all NodeProxys of the supplied NodeSet and returning all non-null
 * results.
 *
 * @param nodes
 *            the NodeSet containig the NodeProys to be transformed
 * @param transform
 *            the function to be applied to all NodeProxys in nodes
 * @return a new NodeSet containing the non-null results of f applied to the NodeProxys in nodes
 *
 * @throws XPathException if an error occurs with the query.
 */
public static NodeSet transformNodes(final NodeSet nodes, final Function<NodeProxy, NodeProxy> transform) throws XPathException {
    final NodeSet result = new ExtArrayNodeSet();
    for (NodeProxy nodeProxy : nodes) {
        final NodeProxy node = transform.apply(nodeProxy);
        if (node != null) {
            result.add(node);
        }
    }
    // ensure result is ready to use
    result.iterate();
    return result;
}
Also used : ExtArrayNodeSet(org.exist.dom.persistent.ExtArrayNodeSet) NodeSet(org.exist.dom.persistent.NodeSet) ExtArrayNodeSet(org.exist.dom.persistent.ExtArrayNodeSet) NodeProxy(org.exist.dom.persistent.NodeProxy)

Example 8 with ExtArrayNodeSet

use of org.exist.dom.persistent.ExtArrayNodeSet in project exist by eXist-db.

the class FunIdRef method eval.

/**
 * @see org.exist.xquery.Expression#eval(Sequence, 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());
        }
    }
    if (getArgumentCount() < 1) {
        throw new XPathException(this, ErrorCodes.XPST0017, "function id requires one argument");
    }
    if (contextItem != null) {
        contextSequence = contextItem.toSequence();
    }
    Sequence result;
    boolean processInMem = false;
    final Expression arg = getArgument(0);
    final Sequence idrefval = arg.eval(contextSequence);
    if (idrefval.isEmpty()) {
        result = Sequence.EMPTY_SEQUENCE;
    } else {
        String nextId;
        DocumentSet docs = null;
        if (getArgumentCount() == 2) {
            // second argument should be a node, whose owner document will be
            // searched for the id
            final Sequence nodes = getArgument(1).eval(contextSequence);
            if (nodes.isEmpty()) {
                throw new XPathException(this, ErrorCodes.XPDY0002, "no node or context item for fn:idref");
            }
            if (!Type.subTypeOf(nodes.itemAt(0).getType(), Type.NODE)) {
                throw new XPathException(this, ErrorCodes.XPTY0004, "fn:idref() argument is not a node");
            }
            NodeValue node = (NodeValue) nodes.itemAt(0);
            if (node.getImplementationType() == NodeValue.IN_MEMORY_NODE) // TODO : how to enforce this ?
            // If $node, or the context item if the second argument is omitted,
            // is a node in a tree whose root is not a document node [err:FODC0001] is raised                    processInMem = true;
            {
                processInMem = true;
            } else {
                MutableDocumentSet ndocs = new DefaultDocumentSet();
                ndocs.add(((NodeProxy) node).getOwnerDocument());
                docs = ndocs;
            }
            contextSequence = node;
        } else if (contextSequence == null) {
            throw new XPathException(this, ErrorCodes.XPDY0002, "no context item specified");
        } else if (!Type.subTypeOf(contextSequence.getItemType(), Type.NODE)) {
            throw new XPathException(this, ErrorCodes.XPTY0004, "context item is not a node");
        } else {
            if (contextSequence.isPersistentSet()) {
                docs = contextSequence.toNodeSet().getDocumentSet();
            } else {
                processInMem = true;
            }
        }
        if (processInMem) {
            result = new ValueSequence();
        } else {
            result = new ExtArrayNodeSet();
        }
        for (final SequenceIterator i = idrefval.iterate(); i.hasNext(); ) {
            nextId = i.nextItem().getStringValue();
            if (nextId.isEmpty()) {
                continue;
            }
            if (XMLNames.isNCName(nextId)) {
                if (processInMem) {
                    getIdRef(result, contextSequence, nextId);
                } else {
                    getIdRef((NodeSet) result, docs, nextId);
                }
            }
        }
    }
    result.removeDuplicates();
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return result;
}
Also used : NodeValue(org.exist.xquery.value.NodeValue) MutableDocumentSet(org.exist.dom.persistent.MutableDocumentSet) DefaultDocumentSet(org.exist.dom.persistent.DefaultDocumentSet) ExtArrayNodeSet(org.exist.dom.persistent.ExtArrayNodeSet) SequenceIterator(org.exist.xquery.value.SequenceIterator) XPathException(org.exist.xquery.XPathException) Expression(org.exist.xquery.Expression) ValueSequence(org.exist.xquery.value.ValueSequence) ValueSequence(org.exist.xquery.value.ValueSequence) Sequence(org.exist.xquery.value.Sequence) DefaultDocumentSet(org.exist.dom.persistent.DefaultDocumentSet) DocumentSet(org.exist.dom.persistent.DocumentSet) MutableDocumentSet(org.exist.dom.persistent.MutableDocumentSet)

Example 9 with ExtArrayNodeSet

use of org.exist.dom.persistent.ExtArrayNodeSet in project exist by eXist-db.

the class LocalXPathQueryService method query.

@Override
public ResourceSet query(final XMLResource res, final String query, final String sortBy) throws XMLDBException {
    final Node n = ((LocalXMLResource) res).root;
    return withDb((broker, transaction) -> {
        if (n != null && n instanceof org.exist.dom.memtree.NodeImpl) {
            final XmldbURI[] docs = new XmldbURI[] { getCollectionUri(broker, transaction, res.getParentCollection()) };
            return doQuery(broker, transaction, query, docs, (org.exist.dom.memtree.NodeImpl) n, sortBy);
        }
        final NodeProxy node = ((LocalXMLResource) res).getNode(broker, transaction);
        if (node == null) {
            // resource is a document
            // TODO : use dedicated function in XmldbURI
            final XmldbURI[] docs = new XmldbURI[] { getCollectionUri(broker, transaction, res.getParentCollection()).append(res.getDocumentId()) };
            return doQuery(broker, transaction, query, docs, null, sortBy);
        } else {
            final NodeSet set = new ExtArrayNodeSet(1);
            set.add(node);
            final XmldbURI[] docs = new XmldbURI[] { node.getOwnerDocument().getURI() };
            return doQuery(broker, transaction, query, docs, set, sortBy);
        }
    });
}
Also used : ExtArrayNodeSet(org.exist.dom.persistent.ExtArrayNodeSet) NodeSet(org.exist.dom.persistent.NodeSet) ExtArrayNodeSet(org.exist.dom.persistent.ExtArrayNodeSet) Node(org.w3c.dom.Node) NodeProxy(org.exist.dom.persistent.NodeProxy)

Example 10 with ExtArrayNodeSet

use of org.exist.dom.persistent.ExtArrayNodeSet in project exist by eXist-db.

the class LocalXPathQueryService method execute.

@Override
public ResourceSet execute(final XMLResource res, final CompiledExpression expression) throws XMLDBException {
    return withDb((broker, transaction) -> {
        final NodeProxy node = ((LocalXMLResource) res).getNode(broker, transaction);
        if (node == null) {
            // resource is a document
            final XmldbURI[] docs = new XmldbURI[] { getCollectionUri(broker, transaction, res.getParentCollection()).append(res.getDocumentId()) };
            return execute(broker, transaction, docs, null, expression, null);
        } else {
            final NodeSet set = new ExtArrayNodeSet(1);
            set.add(node);
            final XmldbURI[] docs = new XmldbURI[] { node.getOwnerDocument().getURI() };
            return execute(broker, transaction, docs, set, expression, null);
        }
    });
}
Also used : ExtArrayNodeSet(org.exist.dom.persistent.ExtArrayNodeSet) NodeSet(org.exist.dom.persistent.NodeSet) ExtArrayNodeSet(org.exist.dom.persistent.ExtArrayNodeSet) NodeProxy(org.exist.dom.persistent.NodeProxy)

Aggregations

ExtArrayNodeSet (org.exist.dom.persistent.ExtArrayNodeSet)10 NodeSet (org.exist.dom.persistent.NodeSet)8 NodeProxy (org.exist.dom.persistent.NodeProxy)6 Sequence (org.exist.xquery.value.Sequence)4 ValueSequence (org.exist.xquery.value.ValueSequence)3 DefaultDocumentSet (org.exist.dom.persistent.DefaultDocumentSet)2 MutableDocumentSet (org.exist.dom.persistent.MutableDocumentSet)2 XPathException (org.exist.xquery.XPathException)2 SequenceIterator (org.exist.xquery.value.SequenceIterator)2 Collator (com.ibm.icu.text.Collator)1 IOException (java.io.IOException)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 Collection (org.exist.collections.Collection)1 QName (org.exist.dom.QName)1 DocumentImpl (org.exist.dom.memtree.DocumentImpl)1 NodeImpl (org.exist.dom.memtree.NodeImpl)1 ContextItem (org.exist.dom.persistent.ContextItem)1 DocumentImpl (org.exist.dom.persistent.DocumentImpl)1 DocumentSet (org.exist.dom.persistent.DocumentSet)1 PermissionDeniedException (org.exist.security.PermissionDeniedException)1