use of org.exist.dom.memtree.NodeImpl in project exist by eXist-db.
the class ValueSequence method getSelf.
@Override
public Sequence getSelf(final NodeTest test) throws XPathException {
sortInDocumentOrder();
final ValueSequence nodes = new ValueSequence(true);
nodes.keepUnOrdered(keepUnOrdered);
for (int i = 0; i <= size; i++) {
final NodeImpl node = (NodeImpl) values[i];
if ((test.getType() == Type.NODE && node.getNodeType() == Node.ATTRIBUTE_NODE) || test.matches(node)) {
nodes.add(node);
}
}
return nodes;
}
use of org.exist.dom.memtree.NodeImpl in project exist by eXist-db.
the class Search method eval.
@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
NodeImpl report = null;
try {
// Only match documents that match these URLs
List<String> toBeMatchedURIs = new ArrayList<>();
Sequence pathSeq = getArgumentCount() > 1 ? args[0] : contextSequence;
if (pathSeq == null)
return Sequence.EMPTY_SEQUENCE;
// Get first agument, these are the documents / collections to search in
for (SequenceIterator i = pathSeq.iterate(); i.hasNext(); ) {
String path;
Item item = i.nextItem();
if (Type.subTypeOf(item.getType(), Type.NODE)) {
if (((NodeValue) item).isPersistentSet()) {
path = ((NodeProxy) item).getOwnerDocument().getURI().toString();
} else {
path = item.getStringValue();
}
} else {
path = item.getStringValue();
}
toBeMatchedURIs.add(path);
}
// Get second argument, this is the query
String query;
if (getArgumentCount() == 1)
query = args[0].itemAt(0).getStringValue();
else
query = args[1].itemAt(0).getStringValue();
String[] fields = null;
if (getArgumentCount() == 3) {
fields = new String[args[2].getItemCount()];
int j = 0;
for (SequenceIterator i = args[2].iterate(); i.hasNext(); ) {
fields[j++] = i.nextItem().getStringValue();
}
}
// Get the lucene worker
LuceneIndexWorker index = (LuceneIndexWorker) context.getBroker().getIndexController().getWorkerByIndexId(LuceneIndex.ID);
QueryOptions options = Query.parseOptions(this, contextSequence, null, 4);
// Perform search
report = index.search(context, toBeMatchedURIs, query, fields, options);
} catch (IOException e) {
throw new XPathException(this, e.getMessage(), e);
} catch (XPathException ex) {
// Log and rethrow
logger.error(ex.getMessage(), ex);
throw ex;
}
// Return list of matching files.
return report;
}
use of org.exist.dom.memtree.NodeImpl in project exist by eXist-db.
the class ArrayListValueSequence method getChildren.
@Override
public Sequence getChildren(final NodeTest test) throws XPathException {
final ArrayListValueSequence nodes = new ArrayListValueSequence();
for (final Item value : values) {
final NodeImpl node = (NodeImpl) value;
node.selectChildren(test, nodes);
}
return nodes;
}
use of org.exist.dom.memtree.NodeImpl in project exist by eXist-db.
the class ArrayListValueSequence method toNodeSet.
@Override
public NodeSet toNodeSet() throws XPathException {
if (isEmpty) {
return NodeSet.EMPTY_SET;
}
// for this method to work, all items have to be nodes
if (itemType != Type.ANY_TYPE && Type.subTypeOf(itemType, Type.NODE)) {
final NodeSet set = new NewArrayNodeSet();
for (int i = 0; i <= values.size(); i++) {
NodeValue v = (NodeValue) values.get(i);
if (v.getImplementationType() != NodeValue.PERSISTENT_NODE) {
// found an in-memory document
final DocumentImpl doc;
if (v.getType() == Type.DOCUMENT) {
doc = (DocumentImpl) v;
} else {
doc = ((NodeImpl) v).getOwnerDocument();
}
if (doc == null) {
continue;
}
// make this document persistent: doc.makePersistent()
// returns a map of all root node ids mapped to the corresponding
// persistent node. We scan the current sequence and replace all
// in-memory nodes with their new persistent node objects.
final DocumentImpl expandedDoc = doc.expandRefs(null);
final org.exist.dom.persistent.DocumentImpl newDoc = expandedDoc.makePersistent();
if (newDoc != null) {
NodeId rootId = newDoc.getBrokerPool().getNodeFactory().createInstance();
for (int j = i; j <= values.size(); j++) {
v = (NodeValue) values.get(j);
if (v.getImplementationType() != NodeValue.PERSISTENT_NODE) {
NodeImpl node = (NodeImpl) v;
final Document nodeOwnerDoc;
if (node.getNodeType() == Node.DOCUMENT_NODE) {
nodeOwnerDoc = (Document) node;
} else {
nodeOwnerDoc = node.getOwnerDocument();
}
if (nodeOwnerDoc == doc) {
if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
node = expandedDoc.getAttribute(node.getNodeNumber());
} else {
node = expandedDoc.getNode(node.getNodeNumber());
}
NodeId nodeId = node.getNodeId();
if (nodeId == null) {
throw new XPathException("Internal error: nodeId == null");
}
if (node.getNodeType() == Node.DOCUMENT_NODE) {
nodeId = rootId;
} else {
nodeId = rootId.append(nodeId);
}
final NodeProxy p = new NodeProxy(newDoc, nodeId, node.getNodeType());
// replace the node by the NodeProxy
values.set(j, p);
setHasChanged();
}
}
}
set.add((NodeProxy) values.get(i));
}
} else {
set.add((NodeProxy) v);
}
}
// }
return set;
} else {
throw new XPathException("Type error: the sequence cannot be converted into" + " a node set. Item type is " + Type.getTypeName(itemType));
}
}
use of org.exist.dom.memtree.NodeImpl in project exist by eXist-db.
the class ArrayListValueSequence method getDescendantAttributes.
@Override
public Sequence getDescendantAttributes(final NodeTest test) throws XPathException {
final ArrayListValueSequence nodes = new ArrayListValueSequence();
for (final Item value : values) {
final NodeImpl node = (NodeImpl) value;
node.selectDescendantAttributes(test, nodes);
}
return nodes;
}
Aggregations