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;
}
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;
}
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;
}
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);
}
});
}
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);
}
});
}
Aggregations