Search in sources :

Example 6 with NodeId

use of org.exist.numbering.NodeId in project exist by eXist-db.

the class ElementImpl method appendChild.

@Override
public Node appendChild(final Node newChild) throws DOMException {
    if (newChild.getNodeType() != Node.DOCUMENT_NODE && newChild.getOwnerDocument() != null && newChild.getOwnerDocument() != ownerDocument) {
        throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, "Owning document IDs do not match");
    }
    if (newChild == this) {
        throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, "Cannot append an element to itself");
    }
    if (newChild.getNodeType() == DOCUMENT_NODE) {
        throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, "A Document Node may not be appended to an element");
    }
    if (newChild.getNodeType() == DOCUMENT_TYPE_NODE) {
        throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, "A Document Type Node may not be appended to an element");
    }
    if (newChild instanceof IStoredNode) {
        final NodeId newChildId = ((IStoredNode) newChild).getNodeId();
        if (newChildId != null && getNodeId().isDescendantOf(newChildId)) {
            throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, "The node to append is one of this node's ancestors");
        }
    }
    final TransactionManager transact = ownerDocument.getBrokerPool().getTransactionManager();
    final org.exist.dom.NodeListImpl nl = new org.exist.dom.NodeListImpl();
    nl.add(newChild);
    try (final DBBroker broker = ownerDocument.getBrokerPool().getBroker();
        final Txn transaction = transact.beginTransaction()) {
        appendChildren(transaction, nl, 0);
        broker.storeXMLResource(transaction, getOwnerDocument());
        // bugID 3419602
        transact.commit(transaction);
        return getLastChild();
    } catch (final Exception e) {
        throw new DOMException(DOMException.INVALID_STATE_ERR, e.getMessage());
    }
}
Also used : org.w3c.dom(org.w3c.dom) TransactionManager(org.exist.storage.txn.TransactionManager) NodeId(org.exist.numbering.NodeId) Txn(org.exist.storage.txn.Txn) XMLStreamException(javax.xml.stream.XMLStreamException) EXistException(org.exist.EXistException) IllegalQNameException(org.exist.dom.QName.IllegalQNameException) IOException(java.io.IOException) TransactionException(org.exist.storage.txn.TransactionException)

Example 7 with NodeId

use of org.exist.numbering.NodeId in project exist by eXist-db.

the class NewArrayNodeSet method selectFollowingSiblings.

/**
 * The method <code>selectFollowingSiblings</code>
 *
 * @param contextSet a <code>NodeSet</code> value
 * @param contextId  an <code>int</code> value
 * @return a <code>NodeSet</code> value
 */
@Override
public NodeSet selectFollowingSiblings(final NodeSet contextSet, final int contextId) {
    sort();
    final NodeSet result = new NewArrayNodeSet();
    for (final NodeProxy reference : contextSet) {
        final NodeId parentId = reference.getNodeId().getParentId();
        final int docIdx = findDoc(reference.getOwnerDocument());
        if (docIdx < 0) {
            continue;
        }
        // BUG: can't be null, make trouble @LocationStep line 388 -shabanovd
        // do a binary search to pick some node in the range of valid
        // child ids
        int low = documentNodesOffset[docIdx];
        int high = low + (documentNodesCount[docIdx] - 1);
        final int end = low + documentNodesCount[docIdx];
        int mid = low;
        int cmp;
        NodeProxy p = null;
        while (low <= high) {
            mid = (low + high) / 2;
            p = nodes[mid];
            if (p.getNodeId().isDescendantOf(parentId) || (parentId.equals(NodeId.DOCUMENT_NODE) && p.getNodeId().getTreeLevel() == 1)) {
                // found a child node, break out.
                break;
            }
            cmp = p.getNodeId().compareTo(parentId);
            if (cmp > 0) {
                high = mid - 1;
            } else {
                low = mid + 1;
            }
        }
        if (low > high) {
            // no node found
            continue;
        }
        // find the first child node in the range
        while (mid > documentNodesOffset[docIdx] && nodes[mid - 1].getNodeId().compareTo(parentId) > -1) {
            --mid;
        }
        final NodeId refId = reference.getNodeId();
        for (int i = mid; i < end; i++) {
            final NodeId currentId = nodes[i].getNodeId();
            if (!(currentId.isDescendantOf(parentId) || (p != null && parentId.equals(NodeId.DOCUMENT_NODE) && p.getNodeId().getTreeLevel() == 1))) {
                continue;
            }
            if (currentId.getTreeLevel() == refId.getTreeLevel() && currentId.compareTo(refId) > 0) {
                if (contextId != Expression.IGNORE_CONTEXT && nodes[i].getContext() != null && reference.getContext() != null && nodes[i].getContext().getContextId() == reference.getContext().getContextId()) {
                    continue;
                }
                if (Expression.IGNORE_CONTEXT != contextId) {
                    if (Expression.NO_CONTEXT_ID == contextId) {
                        nodes[i].copyContext(reference);
                    } else {
                        nodes[i].addContextNode(contextId, reference);
                    }
                }
                result.add(nodes[i]);
            }
        }
    }
    return result;
}
Also used : NodeId(org.exist.numbering.NodeId)

Example 8 with NodeId

use of org.exist.numbering.NodeId in project exist by eXist-db.

the class NodeProxy method getParents.

@Override
public NodeSet getParents(final int contextId) {
    final NodeId pid = nodeId.getParentId();
    if (pid == null || pid == NodeId.DOCUMENT_NODE) {
        return NodeSet.EMPTY_SET;
    }
    final NodeProxy parent = new NodeProxy(doc, pid, Node.ELEMENT_NODE);
    if (contextId != Expression.NO_CONTEXT_ID) {
        parent.addContextNode(contextId, this);
    } else {
        parent.copyContext(this);
    }
    parent.addMatches(this);
    return parent;
}
Also used : NodeId(org.exist.numbering.NodeId)

Example 9 with NodeId

use of org.exist.numbering.NodeId in project exist by eXist-db.

the class NodeProxy method getAncestors.

@Override
public NodeSet getAncestors(final int contextId, final boolean includeSelf) {
    final NodeSet ancestors = new NewArrayNodeSet();
    if (includeSelf) {
        ancestors.add(this);
    }
    NodeId parentID = nodeId.getParentId();
    while (parentID != null) {
        final NodeProxy parent = new NodeProxy(getOwnerDocument(), parentID, Node.ELEMENT_NODE);
        if (contextId != Expression.NO_CONTEXT_ID) {
            parent.addContextNode(contextId, this);
        } else {
            parent.copyContext(this);
        }
        parent.addMatches(this);
        ancestors.add(parent);
        parentID = parentID.getParentId();
    }
    return ancestors;
}
Also used : NodeId(org.exist.numbering.NodeId)

Example 10 with NodeId

use of org.exist.numbering.NodeId in project exist by eXist-db.

the class NewArrayNodeSet method selectPrecedingSiblings.

@Override
public NodeSet selectPrecedingSiblings(final NodeSet contextSet, final int contextId) {
    sort();
    final NodeSet result = new NewArrayNodeSet();
    for (final NodeProxy reference : contextSet) {
        final NodeId parentId = reference.getNodeId().getParentId();
        final int docIdx = findDoc(reference.getOwnerDocument());
        if (docIdx < 0) {
            continue;
        }
        // do a binary search to pick some node in the range of valid
        // child ids
        int low = documentNodesOffset[docIdx];
        int high = low + (documentNodesCount[docIdx] - 1);
        final int end = low + documentNodesCount[docIdx];
        int mid = low;
        int cmp;
        NodeProxy p = null;
        while (low <= high) {
            mid = (low + high) / 2;
            p = nodes[mid];
            if (p.getNodeId().isDescendantOf(parentId) || (parentId.equals(NodeId.DOCUMENT_NODE) && p.getNodeId().getTreeLevel() == 1)) {
                // found a child node, break out.
                break;
            }
            cmp = p.getNodeId().compareTo(parentId);
            if (cmp > 0) {
                high = mid - 1;
            } else {
                low = mid + 1;
            }
        }
        if (low > high) {
            // no node found
            continue;
        }
        // find the first child node in the range
        while (mid < end && nodes[mid].getNodeId().isDescendantOf(parentId)) {
            ++mid;
        }
        if (mid == 0 && parentId.equals(NodeId.DOCUMENT_NODE)) {
            mid = getLength();
        }
        --mid;
        final NodeId refId = reference.getNodeId();
        for (int i = mid; i >= documentNodesOffset[docIdx]; i--) {
            final NodeId currentId = nodes[i].getNodeId();
            if (!(currentId.isDescendantOf(parentId) || (p != null && parentId.equals(NodeId.DOCUMENT_NODE) && p.getNodeId().getTreeLevel() == 1))) {
                break;
            }
            if (currentId.getTreeLevel() == refId.getTreeLevel() && currentId.compareTo(refId) < 0) {
                if (contextId != Expression.IGNORE_CONTEXT && nodes[i].getContext() != null && reference.getContext() != null && nodes[i].getContext().getContextId() == reference.getContext().getContextId()) {
                    continue;
                }
                if (Expression.IGNORE_CONTEXT != contextId) {
                    if (Expression.NO_CONTEXT_ID == contextId) {
                        nodes[i].copyContext(reference);
                    } else {
                        nodes[i].addContextNode(contextId, reference);
                    }
                }
                result.add(nodes[i]);
            }
        }
    }
    return result;
}
Also used : NodeId(org.exist.numbering.NodeId)

Aggregations

NodeId (org.exist.numbering.NodeId)86 IOException (java.io.IOException)21 XMLStreamException (javax.xml.stream.XMLStreamException)17 EXistException (org.exist.EXistException)15 PermissionDeniedException (org.exist.security.PermissionDeniedException)13 NodeProxy (org.exist.dom.persistent.NodeProxy)12 ReentrantLock (java.util.concurrent.locks.ReentrantLock)11 DocumentImpl (org.exist.dom.persistent.DocumentImpl)10 Value (org.exist.storage.btree.Value)8 QName (org.exist.dom.QName)7 ExtendedXMLStreamReader (org.exist.stax.ExtendedXMLStreamReader)6 IEmbeddedXMLStreamReader (org.exist.stax.IEmbeddedXMLStreamReader)6 NodeImpl (org.exist.dom.memtree.NodeImpl)5 Txn (org.exist.storage.txn.Txn)5 DatabaseConfigurationException (org.exist.util.DatabaseConfigurationException)5 LockException (org.exist.util.LockException)5 URISyntaxException (java.net.URISyntaxException)4 IndexController (org.exist.indexing.IndexController)4 StreamListener (org.exist.indexing.StreamListener)4 DBBroker (org.exist.storage.DBBroker)4