Search in sources :

Example 1 with INodeIterator

use of org.exist.storage.dom.INodeIterator in project exist by eXist-db.

the class ElementImpl method getFirstChild.

@Override
public Node getFirstChild() {
    if (!hasChildNodes()) {
        return null;
    }
    try (final DBBroker broker = ownerDocument.getBrokerPool().getBroker();
        final INodeIterator iterator = broker.getNodeIterator(this)) {
        iterator.next();
        IStoredNode next;
        for (int i = 0; i < getChildCount(); i++) {
            next = iterator.next();
            if (next.getNodeType() != Node.ATTRIBUTE_NODE) {
                return next;
            }
        }
    } catch (final EXistException | IOException e) {
        LOG.warn("Exception while retrieving child node: {}", e.getMessage(), e);
    }
    return null;
}
Also used : INodeIterator(org.exist.storage.dom.INodeIterator) EXistException(org.exist.EXistException) IOException(java.io.IOException)

Example 2 with INodeIterator

use of org.exist.storage.dom.INodeIterator in project exist by eXist-db.

the class VirtualNodeSet method getNodes.

/**
 * Realize the node set by recursively scanning the
 * DOM.
 */
private NodeSet getNodes() {
    final NewArrayNodeSet result = new NewArrayNodeSet();
    for (final NodeProxy proxy : context) {
        if (proxy.getNodeId() == NodeId.DOCUMENT_NODE) {
            if (proxy.getOwnerDocument().getResourceType() == DocumentImpl.BINARY_FILE) {
                // skip binary resources
                continue;
            }
            // Add root node if axis is either self, ancestor-self or descendant-self /ljo
            if ((axis == Constants.SELF_AXIS || axis == Constants.ANCESTOR_SELF_AXIS || axis == Constants.DESCENDANT_SELF_AXIS) && test.matches(proxy)) {
                result.add(proxy);
            }
            if ((axis == Constants.CHILD_AXIS || axis == Constants.ATTRIBUTE_AXIS) && proxy.getOwnerDocument().getChildCount() == 1) {
                // Optimization: if the document has just 1 child node, we know that
                // it has to be an element. Instead of calling Document.getChildNodes(),
                // we just create a NodeProxy for the first child and return it if the
                // test matches
                final NodeProxy p = proxy.getOwnerDocument().getFirstChildProxy();
                if (test.matches(p)) {
                    if (useSelfAsContext && inPredicate) {
                        p.addContextNode(contextId, p);
                    }
                    p.addMatches(proxy);
                    result.add(p);
                }
            } else {
                final NodeList cl = proxy.getOwnerDocument().getChildNodes();
                for (int j = 0; j < cl.getLength(); j++) {
                    final IStoredNode<?> node = (IStoredNode<?>) cl.item(j);
                    final NodeProxy p = new NodeProxy(node);
                    if (test.matches(p)) {
                        // p.deepCopyContext(proxy);
                        if (useSelfAsContext && inPredicate) {
                            p.addContextNode(contextId, p);
                        }
                        result.add(p);
                    }
                    if (node.getNodeType() == Node.ELEMENT_NODE && (axis == Constants.DESCENDANT_AXIS || axis == Constants.DESCENDANT_SELF_AXIS || axis == Constants.DESCENDANT_ATTRIBUTE_AXIS)) {
                        // note: we create a copy of the docElemProxy here to
                        // be used as context when traversing the tree.
                        final NodeProxy contextNode = new NodeProxy(p);
                        contextNode.deepCopyContext(proxy);
                        // TODO : is this StoredNode construction necessary ?
                        try (final INodeIterator domIter = broker.getNodeIterator(contextNode.asStoredNode())) {
                            domIter.next();
                            contextNode.setMatches(proxy.getMatches());
                            addChildren(contextNode, result, node, domIter, 0);
                        } catch (final IOException ioe) {
                            LOG.warn("Unable to close iterator", ioe);
                        }
                    }
                    if (node.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE && (axis == Constants.CHILD_AXIS || axis == Constants.DESCENDANT_AXIS || axis == Constants.DESCENDANT_SELF_AXIS || // fixme! self axis probably not needed /ljo
                    axis == Constants.SELF_AXIS || axis == Constants.PRECEDING_AXIS || axis == Constants.FOLLOWING_AXIS) && test.matches(node)) {
                        result.add(p);
                    }
                }
            }
            continue;
        }
        if ((axis == Constants.SELF_AXIS || axis == Constants.ANCESTOR_SELF_AXIS || axis == Constants.DESCENDANT_SELF_AXIS) && test.matches(proxy)) {
            if (useSelfAsContext && inPredicate) {
                proxy.addContextNode(contextId, proxy);
            }
            result.add(proxy);
        }
        if (test.getType() == Type.PROCESSING_INSTRUCTION || test.getType() == Type.COMMENT || test.getType() == Type.CDATA_SECTION) {
            final DocumentImpl doc = proxy.getOwnerDocument();
            if (axis == Constants.PRECEDING_AXIS) {
                IStoredNode<?> ps = (IStoredNode<?>) doc.getFirstChild();
                final IStoredNode<?> pe = (IStoredNode<?>) doc.getDocumentElement();
                while (ps != null && !ps.equals(pe)) {
                    if (test.matches(ps)) {
                        result.add(new NodeProxy(ps));
                    }
                    ps = (IStoredNode<?>) doc.getFollowingSibling(ps);
                }
            }
            if (axis == Constants.FOLLOWING_AXIS) {
                final IStoredNode<?> pe = (IStoredNode<?>) doc.getDocumentElement();
                IStoredNode<?> pf = (IStoredNode<?>) doc.getFollowingSibling(pe);
                while (pf != null) {
                    if (test.matches(pf)) {
                        result.add(new NodeProxy(pf));
                    }
                    pf = (IStoredNode<?>) doc.getFollowingSibling(pf);
                }
            }
            if (axis == Constants.SELF_AXIS || axis == Constants.ANCESTOR_SELF_AXIS || axis == Constants.DESCENDANT_SELF_AXIS) {
                result.add(proxy);
            }
        }
        if (axis != Constants.SELF_AXIS) {
            addChildren(proxy, result);
        }
    }
    realDocumentSet = result.getDocumentSet();
    return result;
}
Also used : INodeIterator(org.exist.storage.dom.INodeIterator) NodeList(org.w3c.dom.NodeList) IOException(java.io.IOException)

Example 3 with INodeIterator

use of org.exist.storage.dom.INodeIterator in project exist by eXist-db.

the class IndexUtils method scanNode.

public static void scanNode(DBBroker broker, Txn transaction, IStoredNode node, StreamListener listener) {
    try (final INodeIterator iterator = broker.getNodeIterator(node)) {
        iterator.next();
        final NodePath path = node.getPath();
        scanNode(transaction, iterator, node, listener, path);
    } catch (final IOException ioe) {
        LOG.warn("Unable to close iterator", ioe);
    }
}
Also used : INodeIterator(org.exist.storage.dom.INodeIterator) IOException(java.io.IOException) NodePath(org.exist.storage.NodePath)

Example 4 with INodeIterator

use of org.exist.storage.dom.INodeIterator in project exist by eXist-db.

the class NativeBroker method reindexXMLResource.

/**
 * Reindex the nodes in the document. This method will either reindex all
 * descendant nodes of the passed node, or all nodes below some level of
 * the document if node is null.
 */
@Override
public void reindexXMLResource(final Txn transaction, final DocumentImpl doc, final IndexMode mode) {
    final StreamListener listener = getIndexController().getStreamListener(doc, ReindexMode.STORE);
    getIndexController().startIndexDocument(transaction, listener);
    try {
        final NodeList nodes = doc.getChildNodes();
        for (int i = 0; i < nodes.getLength(); i++) {
            final IStoredNode<?> node = (IStoredNode<?>) nodes.item(i);
            try (final INodeIterator iterator = getNodeIterator(node)) {
                iterator.next();
                scanNodes(transaction, iterator, node, new NodePath2(), mode, listener);
            } catch (final IOException ioe) {
                LOG.error("Unable to close node iterator", ioe);
            }
        }
    } finally {
        getIndexController().endIndexDocument(transaction, listener);
    }
    flush();
}
Also used : INodeIterator(org.exist.storage.dom.INodeIterator) NodeList(org.w3c.dom.NodeList) StreamListener(org.exist.indexing.StreamListener)

Example 5 with INodeIterator

use of org.exist.storage.dom.INodeIterator in project exist by eXist-db.

the class NativeBroker method removeAllNodes.

@Override
public void removeAllNodes(final Txn transaction, final IStoredNode node, final NodePath currentPath, final StreamListener listener) {
    try (final INodeIterator iterator = getNodeIterator(node)) {
        iterator.next();
        final Deque<RemovedNode> stack = new ArrayDeque<>();
        collectNodesForRemoval(transaction, stack, iterator, listener, node, currentPath);
        while (!stack.isEmpty()) {
            final RemovedNode next = stack.pop();
            removeNode(transaction, next.node, next.path, next.content);
        }
    } catch (final IOException ioe) {
        LOG.error("Unable to close node iterator", ioe);
    }
}
Also used : INodeIterator(org.exist.storage.dom.INodeIterator)

Aggregations

INodeIterator (org.exist.storage.dom.INodeIterator)12 NodeList (org.w3c.dom.NodeList)7 IOException (java.io.IOException)6 StreamListener (org.exist.indexing.StreamListener)4 PooledObject (org.apache.commons.pool2.PooledObject)2 DefaultPooledObject (org.apache.commons.pool2.impl.DefaultPooledObject)2 EXistException (org.exist.EXistException)2 DOMTransaction (org.exist.storage.dom.DOMTransaction)2 NamedNodeMapImpl (org.exist.dom.NamedNodeMapImpl)1 QName (org.exist.dom.QName)1 DocumentTypeImpl (org.exist.dom.persistent.DocumentTypeImpl)1 IStoredNode (org.exist.dom.persistent.IStoredNode)1 NodeProxy (org.exist.dom.persistent.NodeProxy)1 NodePath (org.exist.storage.NodePath)1 TerminatedException (org.exist.xquery.TerminatedException)1 org.w3c.dom (org.w3c.dom)1