Search in sources :

Example 16 with Kind

use of org.sirix.node.Kind in project sirix by sirixdb.

the class XdmNodeReadTrxImpl method getParentKind.

@Override
public Kind getParentKind() {
    assertNotClosed();
    if (mCurrentNode.getParentKey() == Fixed.NULL_NODE_KEY.getStandardProperty()) {
        return Kind.UNKNOWN;
    }
    final long nodeKey = mCurrentNode.getNodeKey();
    moveToParent();
    final Kind parentKind = mCurrentNode.getKind();
    moveTo(nodeKey);
    return parentKind;
}
Also used : PageKind(org.sirix.page.PageKind) Kind(org.sirix.node.Kind)

Example 17 with Kind

use of org.sirix.node.Kind in project sirix by sirixdb.

the class XdmNodeWriterTrxImpl method copy.

/**
 * Helper method for copy-operations.
 *
 * @param rtx the source {@link XdmNodeReadTrx}
 * @param insert the insertion strategy
 * @throws SirixException if anything fails in sirix
 */
private void copy(final XdmNodeReadTrx trx, final Insert insert) throws SirixException {
    assert trx != null;
    assert insert != null;
    final XdmNodeReadTrx rtx = trx.getResourceManager().beginNodeReadTrx(trx.getRevisionNumber());
    assert rtx.getRevisionNumber() == trx.getRevisionNumber();
    rtx.moveTo(trx.getNodeKey());
    assert rtx.getNodeKey() == trx.getNodeKey();
    if (rtx.getKind() == Kind.DOCUMENT) {
        rtx.moveToFirstChild();
    }
    if (!(rtx.isStructuralNode())) {
        throw new IllegalStateException("Node to insert must be a structural node (Text, PI, Comment, Document root or Element)!");
    }
    final Kind kind = rtx.getKind();
    switch(kind) {
        case TEXT:
            final String textValue = rtx.getValue();
            switch(insert) {
                case ASFIRSTCHILD:
                    insertTextAsFirstChild(textValue);
                    break;
                case ASLEFTSIBLING:
                    insertTextAsLeftSibling(textValue);
                    break;
                case ASRIGHTSIBLING:
                    insertTextAsRightSibling(textValue);
                    break;
                default:
                    throw new IllegalStateException();
            }
            break;
        case PROCESSING_INSTRUCTION:
            switch(insert) {
                case ASFIRSTCHILD:
                    insertPIAsFirstChild(rtx.getName().getLocalName(), rtx.getValue());
                    break;
                case ASLEFTSIBLING:
                    insertPIAsLeftSibling(rtx.getName().getLocalName(), rtx.getValue());
                    break;
                case ASRIGHTSIBLING:
                    insertPIAsRightSibling(rtx.getName().getLocalName(), rtx.getValue());
                    break;
                default:
                    throw new IllegalStateException();
            }
            break;
        case COMMENT:
            final String commentValue = rtx.getValue();
            switch(insert) {
                case ASFIRSTCHILD:
                    insertCommentAsFirstChild(commentValue);
                    break;
                case ASLEFTSIBLING:
                    insertCommentAsLeftSibling(commentValue);
                    break;
                case ASRIGHTSIBLING:
                    insertCommentAsRightSibling(commentValue);
                    break;
                default:
                    throw new IllegalStateException();
            }
            break;
        default:
            new XMLShredder.Builder(this, new StAXSerializer(rtx), insert).build().call();
    }
    rtx.close();
}
Also used : XdmNodeReadTrx(org.sirix.api.XdmNodeReadTrx) PageKind(org.sirix.page.PageKind) Kind(org.sirix.node.Kind) StAXSerializer(org.sirix.service.xml.serialize.StAXSerializer)

Example 18 with Kind

use of org.sirix.node.Kind in project sirix by sirixdb.

the class XdmNodeWriterTrxImpl method insertElementAsFirstChild.

@Override
public XdmNodeWriteTrx insertElementAsFirstChild(final QNm name) throws SirixException {
    if (!XMLToken.isValidQName(checkNotNull(name))) {
        throw new IllegalArgumentException("The QName is not valid!");
    }
    acquireLock();
    try {
        final Kind kind = mNodeReader.getCurrentNode().getKind();
        if (kind == Kind.ELEMENT || kind == Kind.DOCUMENT) {
            checkAccessAndCommit();
            final long parentKey = mNodeReader.getCurrentNode().getNodeKey();
            final long leftSibKey = Fixed.NULL_NODE_KEY.getStandardProperty();
            final long rightSibKey = ((StructNode) mNodeReader.getCurrentNode()).getFirstChildKey();
            final long pathNodeKey = mBuildPathSummary ? mPathSummaryWriter.getPathNodeKey(name, Kind.ELEMENT) : 0;
            final Optional<SirixDeweyID> id = newFirstChildID();
            final ElementNode node = mNodeFactory.createElementNode(parentKey, leftSibKey, rightSibKey, 0, name, pathNodeKey, id);
            mNodeReader.setCurrentNode(node);
            adaptForInsert(node, InsertPos.ASFIRSTCHILD, PageKind.RECORDPAGE);
            mNodeReader.setCurrentNode(node);
            adaptHashesWithAdd();
            return this;
        } else {
            throw new SirixUsageException("Insert is not allowed if current node is not an ElementNode!");
        }
    } finally {
        unLock();
    }
}
Also used : PageKind(org.sirix.page.PageKind) Kind(org.sirix.node.Kind) SirixDeweyID(org.sirix.node.SirixDeweyID) ElementNode(org.sirix.node.ElementNode) SirixUsageException(org.sirix.exception.SirixUsageException) StructNode(org.sirix.node.interfaces.StructNode)

Example 19 with Kind

use of org.sirix.node.Kind in project sirix by sirixdb.

the class XdmNodeWriterTrxImpl method removeName.

/**
 * Remove a name from the {@link NamePage} reference and the path summary if needed.
 *
 * @throws SirixException if Sirix fails
 */
private void removeName() throws SirixException {
    if (getCurrentNode() instanceof NameNode) {
        final NameNode node = ((NameNode) getCurrentNode());
        final Kind nodeKind = node.getKind();
        final NamePage page = ((NamePage) getPageTransaction().getActualRevisionRootPage().getNamePageReference().getPage());
        page.removeName(node.getPrefixKey(), nodeKind);
        page.removeName(node.getLocalNameKey(), nodeKind);
        page.removeName(node.getURIKey(), Kind.NAMESPACE);
        assert nodeKind != Kind.DOCUMENT;
        if (mBuildPathSummary) {
            mPathSummaryWriter.remove(node, nodeKind, page);
        }
    }
}
Also used : NameNode(org.sirix.node.interfaces.NameNode) PageKind(org.sirix.page.PageKind) Kind(org.sirix.node.Kind) NamePage(org.sirix.page.NamePage)

Example 20 with Kind

use of org.sirix.node.Kind in project sirix by sirixdb.

the class PrecedingSiblingAxis method nextKey.

@Override
protected long nextKey() {
    final XdmNodeReadTrx rtx = getTrx();
    if (mIsFirst) {
        mIsFirst = false;
        /*
       * If the context node is an attribute or namespace node, the following-sibling axis is empty.
       */
        final Kind kind = rtx.getKind();
        if (kind == Kind.ATTRIBUTE || kind == Kind.NAMESPACE) {
            return done();
        } else {
            if (rtx.hasParent()) {
                final long startNodeKey = rtx.getNodeKey();
                rtx.moveToParent();
                rtx.moveToFirstChild();
                if (rtx.getNodeKey() == startNodeKey) {
                    return Fixed.NULL_NODE_KEY.getStandardProperty();
                } else {
                    final long key = rtx.getNodeKey();
                    rtx.moveTo(startNodeKey);
                    return key;
                }
            }
        }
    }
    if (rtx.hasRightSibling() && rtx.getRightSiblingKey() != getStartKey()) {
        return rtx.getRightSiblingKey();
    }
    return done();
}
Also used : XdmNodeReadTrx(org.sirix.api.XdmNodeReadTrx) Kind(org.sirix.node.Kind)

Aggregations

Kind (org.sirix.node.Kind)20 PageKind (org.sirix.page.PageKind)13 StructNode (org.sirix.node.interfaces.StructNode)5 XdmNodeReadTrx (org.sirix.api.XdmNodeReadTrx)3 IOException (java.io.IOException)2 XMLStreamException (javax.xml.stream.XMLStreamException)2 NodeReadTrx (org.sirix.api.NodeReadTrx)2 SirixUsageException (org.sirix.exception.SirixUsageException)2 TransactionTuple (org.sirix.gui.view.TransactionTuple)2 NameNode (org.sirix.node.interfaces.NameNode)2 NamePage (org.sirix.page.NamePage)2 HashMap (java.util.HashMap)1 Axis (org.sirix.api.Axis)1 ChildAxis (org.sirix.axis.ChildAxis)1 DescendantAxis (org.sirix.axis.DescendantAxis)1 LevelOrderAxis (org.sirix.axis.LevelOrderAxis)1 PostOrderAxis (org.sirix.axis.PostOrderAxis)1 FilterAxis (org.sirix.axis.filter.FilterAxis)1 NameFilter (org.sirix.axis.filter.NameFilter)1 PathKindFilter (org.sirix.axis.filter.PathKindFilter)1