Search in sources :

Example 1 with TransactionTuple

use of org.sirix.gui.view.TransactionTuple in project sirix by sirixdb.

the class StAXDiffSerializer method peek.

@Override
public XMLEvent peek() throws XMLStreamException {
    final long currNodeKey = mAxis.getTransaction().getNodeKey();
    final NodeReadTrx rtx = mAxis.getTransaction();
    try {
        if (!mHasNext && mEmitEndDocument) {
            mEvent = mFac.createEndDocument();
        } else if (!mHasNext) {
            return null;
        } else {
            if (mCloseElements && !mCloseElementsEmitted) {
                final TransactionTuple trxTuple = mStack.peek();
                final NodeReadTrx trx = trxTuple.getRtx();
                final long nodeKey = trx.getNodeKey();
                trx.moveTo(trxTuple.getKey());
                emitEndTag(trxTuple.getRtx());
                trx.moveTo(nodeKey);
            } else {
                if (mFirst && mAxis.isSelfIncluded() == IncludeSelf.YES) {
                    emitNode(rtx);
                } else {
                    if (rtx.hasFirstChild()) {
                        rtx.moveToFirstChild();
                        emitNode(rtx);
                    } else if (rtx.hasRightSibling()) {
                        rtx.moveToRightSibling();
                        final Kind nodeKind = rtx.getKind();
                        processNode(nodeKind);
                    } else if (rtx.hasParent()) {
                        rtx.moveToParent();
                        emitEndTag(rtx);
                    }
                }
            }
        }
    } catch (final IOException e) {
        throw new XMLStreamException(e);
    }
    rtx.moveTo(currNodeKey);
    return mEvent;
}
Also used : NodeReadTrx(org.sirix.api.NodeReadTrx) XMLStreamException(javax.xml.stream.XMLStreamException) Kind(org.sirix.node.Kind) TransactionTuple(org.sirix.gui.view.TransactionTuple) IOException(java.io.IOException)

Example 2 with TransactionTuple

use of org.sirix.gui.view.TransactionTuple in project sirix by sirixdb.

the class StAXDiffSerializer method emit.

/**
 * Move to node and emit it.
 *
 * @param pRtx
 *          Read Transaction.
 * @throws IOException
 *           if any I/O error occurred
 */
private void emit(final NodeReadTrx pRtx) throws IOException {
    assert pRtx != null;
    // Emit pending update elements.
    if (!mUpdatedStack.isEmpty()) {
        mDiff = DiffType.UPDATED;
        final TransactionTuple tuple = mUpdatedStack.peek();
        final NodeReadTrx trx = tuple.getRtx();
        final long nodeKey = trx.getNodeKey();
        trx.moveTo(tuple.getKey());
        if (mFirstUpdate && mDepth <= tuple.getDepth() && trx.getKind() == Kind.ELEMENT) {
            mFirstUpdate = false;
            mStack.pop();
            emitEndTag(trx);
        } else {
            mUpdatedStack.pop();
            emitNode(trx);
            if (trx.getKind() == Kind.ELEMENT) {
                mStack.push(tuple);
            }
            trx.moveTo(nodeKey);
            assert mUpdatedStack.isEmpty();
        }
    } else // Emit pending end elements.
    if (mCloseElements) {
        final long pNodeKey = pRtx.getNodeKey();
        final TransactionTuple tuple = mStack.peek();
        NodeReadTrx rtx = tuple.getRtx();
        final long nodeKey = rtx.getNodeKey();
        int depth = tuple.getDepth();
        mDiff = tuple.getDiff();
        if (mDepth < depth || (mDiff == DiffType.UPDATED && mFirstUpdate)) {
            if (mDiff == DiffType.UPDATED) {
                mFirstUpdate = false;
            }
            rtx.moveTo(mStack.pop().getKey());
            emitEndTag(rtx);
            rtx.moveTo(nodeKey);
        } else {
            mFirstUpdate = true;
            rtx.moveTo(mStack.pop().getKey());
            emitEndTag(rtx);
            rtx.moveTo(nodeKey);
            mCloseElementsEmitted = true;
            mCloseElements = false;
        }
        pRtx.moveTo(pNodeKey);
    } else {
        mCloseElementsEmitted = false;
        // Emit node.
        emitNode(pRtx);
        mLastKey = pRtx.getNodeKey();
        if (mLastKey == 2878) {
            System.out.println();
        }
        mDiff = mAxis.getDiff();
        final int depth = mAxis.getDepth();
        if (mDiff == DiffType.UPDATED) {
            mFirstUpdate = true;
            mUpdatedStack.push(new TransactionTuple(mAxis.getOldRtx().getNodeKey(), mAxis.getOldRtx(), mAxis.getDiff(), mAxis.getDepth()));
        }
        // Push end element to stack if we are a start element.
        if (pRtx.getKind() == Kind.ELEMENT) {
            mStack.push(new TransactionTuple(mLastKey, pRtx, mAxis.getDiff(), mAxis.getDepth()));
        }
        final Kind nodeKind = pRtx.getKind();
        // required.
        if (mLastKey != Fixed.DOCUMENT_NODE_KEY.getStandardProperty()) {
            if (mAxis.hasNext()) {
                final long peekKey = mAxis.peek();
                mDepth = mAxis.getDepth();
                mAxis.getTransaction().moveTo(peekKey);
                if ((depth > mDepth) || (nodeKind == Kind.ELEMENT && mLastKey != mAxis.getTransaction().getParentKey())) {
                    moveToNextNode();
                } else {
                    mAxis.getTransaction().moveTo(mLastKey);
                }
            } else {
                mToLastKey = true;
                mCloseElements = true;
                mAxis.getTransaction().moveToDocumentRoot();
                mDepth = 0;
            }
        }
    }
}
Also used : NodeReadTrx(org.sirix.api.NodeReadTrx) Kind(org.sirix.node.Kind) TransactionTuple(org.sirix.gui.view.TransactionTuple)

Aggregations

NodeReadTrx (org.sirix.api.NodeReadTrx)2 TransactionTuple (org.sirix.gui.view.TransactionTuple)2 Kind (org.sirix.node.Kind)2 IOException (java.io.IOException)1 XMLStreamException (javax.xml.stream.XMLStreamException)1