Search in sources :

Example 36 with StructNode

use of org.sirix.node.interfaces.StructNode in project sirix by sirixdb.

the class XdmNodeWriterTrxImpl method comment.

/**
 * Comment node.
 *
 * @param value value of comment
 * @param insert insertion location
 * @throws SirixException if any unexpected error occurs
 */
private XdmNodeWriteTrx comment(final String value, final Insert insert) throws SirixException {
    // Produces a NPE if value is null (what we want).
    if (value.contains("--")) {
        throw new SirixUsageException("Character sequence \"--\" is not allowed in comment content!");
    }
    if (value.endsWith("-")) {
        throw new SirixUsageException("Comment content must not end with \"-\"!");
    }
    acquireLock();
    try {
        if (getCurrentNode() instanceof StructNode && (getCurrentNode().getKind() != Kind.DOCUMENT || (getCurrentNode().getKind() == Kind.DOCUMENT && insert == Insert.ASFIRSTCHILD))) {
            checkAccessAndCommit();
            // Insert new comment node.
            final byte[] commentValue = getBytes(value);
            long parentKey = 0;
            long leftSibKey = 0;
            long rightSibKey = 0;
            InsertPos pos = InsertPos.ASFIRSTCHILD;
            Optional<SirixDeweyID> id = Optional.<SirixDeweyID>empty();
            switch(insert) {
                case ASFIRSTCHILD:
                    parentKey = getCurrentNode().getNodeKey();
                    leftSibKey = Fixed.NULL_NODE_KEY.getStandardProperty();
                    rightSibKey = ((StructNode) getCurrentNode()).getFirstChildKey();
                    id = newFirstChildID();
                    break;
                case ASRIGHTSIBLING:
                    parentKey = getCurrentNode().getParentKey();
                    leftSibKey = getCurrentNode().getNodeKey();
                    rightSibKey = ((StructNode) getCurrentNode()).getRightSiblingKey();
                    pos = InsertPos.ASRIGHTSIBLING;
                    id = newRightSiblingID();
                    break;
                case ASLEFTSIBLING:
                    parentKey = getCurrentNode().getParentKey();
                    leftSibKey = ((StructNode) getCurrentNode()).getLeftSiblingKey();
                    rightSibKey = getCurrentNode().getNodeKey();
                    pos = InsertPos.ASLEFTSIBLING;
                    id = newLeftSiblingID();
                    break;
                default:
                    throw new IllegalStateException("Insert location not known!");
            }
            final CommentNode node = mNodeFactory.createCommentNode(parentKey, leftSibKey, rightSibKey, commentValue, mCompression, id);
            // Adapt local nodes and hashes.
            mNodeReader.setCurrentNode(node);
            adaptForInsert(node, pos, PageKind.RECORDPAGE);
            mNodeReader.setCurrentNode(node);
            adaptHashesWithAdd();
            return this;
        } else {
            throw new SirixUsageException("Current node must be a structural node!");
        }
    } finally {
        unLock();
    }
}
Also used : CommentNode(org.sirix.node.CommentNode) SirixUsageException(org.sirix.exception.SirixUsageException) SirixDeweyID(org.sirix.node.SirixDeweyID) StructNode(org.sirix.node.interfaces.StructNode)

Example 37 with StructNode

use of org.sirix.node.interfaces.StructNode in project sirix by sirixdb.

the class XdmNodeWriterTrxImpl method replace.

/**
 * Replace a node.
 *
 * @param rtx the transaction which is located at the node to replace
 * @return
 * @throws SirixException
 */
private ImmutableNode replace(final XdmNodeReadTrx rtx) throws SirixException {
    assert rtx != null;
    final StructNode currentNode = mNodeReader.getStructuralNode();
    long key = currentNode.getNodeKey();
    if (currentNode.hasLeftSibling()) {
        moveToLeftSibling();
        key = copySubtreeAsRightSibling(rtx).getNodeKey();
    } else {
        moveToParent();
        key = copySubtreeAsFirstChild(rtx).getNodeKey();
        moveTo(key);
    }
    removeReplaced(currentNode, key);
    return mNodeReader.getCurrentNode();
}
Also used : StructNode(org.sirix.node.interfaces.StructNode)

Example 38 with StructNode

use of org.sirix.node.interfaces.StructNode in project sirix by sirixdb.

the class XdmNodeWriterTrxImpl method replaceNode.

@Override
public XdmNodeWriteTrx replaceNode(final String xml) throws SirixException, IOException, XMLStreamException {
    checkNotNull(xml);
    acquireLock();
    try {
        checkAccessAndCommit();
        final XMLEventReader reader = XMLShredder.createStringReader(checkNotNull(xml));
        ImmutableNode insertedRootNode = null;
        if (getCurrentNode() instanceof StructNode) {
            final StructNode currentNode = mNodeReader.getStructuralNode();
            if (xml.startsWith("<")) {
                while (reader.hasNext()) {
                    final XMLEvent event = reader.peek();
                    if (event.isStartDocument()) {
                        reader.nextEvent();
                        continue;
                    }
                    switch(event.getEventType()) {
                        case XMLStreamConstants.START_ELEMENT:
                            Insert pos = Insert.ASFIRSTCHILD;
                            if (currentNode.hasLeftSibling()) {
                                moveToLeftSibling();
                                pos = Insert.ASRIGHTSIBLING;
                            } else {
                                moveToParent();
                            }
                            final XMLShredder shredder = new XMLShredder.Builder(this, reader, pos).build();
                            shredder.call();
                            if (reader.hasNext()) {
                                // End document.
                                reader.nextEvent();
                            }
                            insertedRootNode = mNodeReader.getCurrentNode();
                            moveTo(currentNode.getNodeKey());
                            remove();
                            moveTo(insertedRootNode.getNodeKey());
                            break;
                    }
                }
            } else {
                insertedRootNode = replaceWithTextNode(xml);
            }
            if (insertedRootNode != null) {
                moveTo(insertedRootNode.getNodeKey());
            }
        } else {
        }
    } finally {
        unLock();
    }
    return this;
}
Also used : ImmutableNode(org.sirix.node.interfaces.immutable.ImmutableNode) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) XMLShredder(org.sirix.service.xml.shredder.XMLShredder) Insert(org.sirix.service.xml.shredder.Insert) StructNode(org.sirix.node.interfaces.StructNode)

Example 39 with StructNode

use of org.sirix.node.interfaces.StructNode in project sirix by sirixdb.

the class XdmNodeWriterTrxImpl method insertElementAsRightSibling.

@Override
public XdmNodeWriteTrx insertElementAsRightSibling(final QNm name) throws SirixException {
    if (!XMLToken.isValidQName(checkNotNull(name))) {
        throw new IllegalArgumentException("The QName is not valid!");
    }
    acquireLock();
    try {
        if (getCurrentNode() instanceof StructNode && !isDocumentRoot()) {
            checkAccessAndCommit();
            final long key = getCurrentNode().getNodeKey();
            moveToParent();
            final long pathNodeKey = mBuildPathSummary ? mPathSummaryWriter.getPathNodeKey(name, Kind.ELEMENT) : 0;
            moveTo(key);
            final long parentKey = getCurrentNode().getParentKey();
            final long leftSibKey = getCurrentNode().getNodeKey();
            final long rightSibKey = ((StructNode) getCurrentNode()).getRightSiblingKey();
            final Optional<SirixDeweyID> id = newRightSiblingID();
            final ElementNode node = mNodeFactory.createElementNode(parentKey, leftSibKey, rightSibKey, 0, name, pathNodeKey, id);
            mNodeReader.setCurrentNode(node);
            adaptForInsert(node, InsertPos.ASRIGHTSIBLING, PageKind.RECORDPAGE);
            mNodeReader.setCurrentNode(node);
            adaptHashesWithAdd();
            return this;
        } else {
            throw new SirixUsageException("Insert is not allowed if current node is not an StructuralNode (either Text or Element)!");
        }
    } finally {
        unLock();
    }
}
Also used : SirixDeweyID(org.sirix.node.SirixDeweyID) ElementNode(org.sirix.node.ElementNode) SirixUsageException(org.sirix.exception.SirixUsageException) StructNode(org.sirix.node.interfaces.StructNode)

Example 40 with StructNode

use of org.sirix.node.interfaces.StructNode in project sirix by sirixdb.

the class XdmNodeWriterTrxImpl method adaptForInsert.

// ////////////////////////////////////////////////////////////
// insert operation
// ////////////////////////////////////////////////////////////
/**
 * Adapting everything for insert operations.
 *
 * @param newNode pointer of the new node to be inserted
 * @param insertPos determines the position where to insert
 * @param pageKind kind of subtree root page
 * @throws SirixIOException if anything weird happens
 */
private void adaptForInsert(final Node newNode, final InsertPos insertPos, final PageKind pageKind) throws SirixIOException {
    assert newNode != null;
    assert insertPos != null;
    assert pageKind != null;
    if (newNode instanceof StructNode) {
        final StructNode strucNode = (StructNode) newNode;
        final StructNode parent = (StructNode) getPageTransaction().prepareEntryForModification(newNode.getParentKey(), pageKind, -1, Optional.<UnorderedKeyValuePage>empty());
        parent.incrementChildCount();
        if (!((StructNode) newNode).hasLeftSibling()) {
            parent.setFirstChildKey(newNode.getNodeKey());
        }
        if (strucNode.hasRightSibling()) {
            final StructNode rightSiblingNode = (StructNode) getPageTransaction().prepareEntryForModification(strucNode.getRightSiblingKey(), pageKind, -1, Optional.<UnorderedKeyValuePage>empty());
            rightSiblingNode.setLeftSiblingKey(newNode.getNodeKey());
        }
        if (strucNode.hasLeftSibling()) {
            final StructNode leftSiblingNode = (StructNode) getPageTransaction().prepareEntryForModification(strucNode.getLeftSiblingKey(), pageKind, -1, Optional.<UnorderedKeyValuePage>empty());
            leftSiblingNode.setRightSiblingKey(newNode.getNodeKey());
        }
    }
}
Also used : UnorderedKeyValuePage(org.sirix.page.UnorderedKeyValuePage) StructNode(org.sirix.node.interfaces.StructNode)

Aggregations

StructNode (org.sirix.node.interfaces.StructNode)41 SirixDeweyID (org.sirix.node.SirixDeweyID)12 SirixUsageException (org.sirix.exception.SirixUsageException)11 ElementNode (org.sirix.node.ElementNode)10 TextNode (org.sirix.node.TextNode)8 ImmutableNode (org.sirix.node.interfaces.immutable.ImmutableNode)8 UnorderedKeyValuePage (org.sirix.page.UnorderedKeyValuePage)8 CommentNode (org.sirix.node.CommentNode)6 PINode (org.sirix.node.PINode)6 AttributeNode (org.sirix.node.AttributeNode)5 Kind (org.sirix.node.Kind)5 NamespaceNode (org.sirix.node.NamespaceNode)5 NameNode (org.sirix.node.interfaces.NameNode)5 Node (org.sirix.node.interfaces.Node)5 ValueNode (org.sirix.node.interfaces.ValueNode)5 PageKind (org.sirix.page.PageKind)5 Optional (java.util.Optional)3 PostOrderAxis (org.sirix.axis.PostOrderAxis)3 Axis (org.sirix.api.Axis)2 DescendantAxis (org.sirix.axis.DescendantAxis)2