Search in sources :

Example 11 with NodeReadTrx

use of org.sirix.api.NodeReadTrx in project sirix by sirixdb.

the class StAXDiffSerializer method getElementText.

@Override
public String getElementText() throws XMLStreamException {
    final NodeReadTrx rtx = mAxis.getTransaction();
    final long nodeKey = rtx.getNodeKey();
    /*
		 * The cursor has to move back (once) after determining, that a closing tag
		 * would be the next event (precond: closeElement and either goBack or goUp
		 * is true).
		 */
    if (mCloseElements && mToLastKey) {
        rtx.moveTo(mLastKey);
    }
    if (mEvent.getEventType() != XMLStreamConstants.START_ELEMENT) {
        rtx.moveTo(nodeKey);
        throw new XMLStreamException("getElementText() only can be called on a start element");
    }
    final FilterAxis textFilterAxis = new FilterAxis(new DescendantAxis(rtx), new TextFilter(rtx));
    final StringBuilder strBuilder = new StringBuilder();
    while (textFilterAxis.hasNext()) {
        textFilterAxis.next();
        strBuilder.append(mAxis.getTransaction().getValue());
    }
    rtx.moveTo(nodeKey);
    return XMLToken.escapeContent(strBuilder.toString());
}
Also used : TextFilter(org.sirix.axis.filter.TextFilter) NodeReadTrx(org.sirix.api.NodeReadTrx) XMLStreamException(javax.xml.stream.XMLStreamException) DescendantAxis(org.sirix.axis.DescendantAxis) FilterAxis(org.sirix.axis.filter.FilterAxis)

Example 12 with NodeReadTrx

use of org.sirix.api.NodeReadTrx 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)

Example 13 with NodeReadTrx

use of org.sirix.api.NodeReadTrx in project sirix by sirixdb.

the class NodeWrapper method expandString.

/**
 * Filter text nodes.
 *
 * @return concatenated String of text node values
 */
private String expandString() {
    final FastStringBuffer fsb = new FastStringBuffer(FastStringBuffer.SMALL);
    try {
        final NodeReadTrx rtx = createRtxAndMove();
        final FilterAxis axis = new FilterAxis(new DescendantAxis(rtx), new TextFilter(rtx));
        while (axis.hasNext()) {
            axis.next();
            fsb.append(rtx.getValue());
        }
        rtx.close();
    } catch (final SirixException exc) {
        LOGGER.error(exc.toString());
    }
    return fsb.condense().toString();
}
Also used : TextFilter(org.sirix.axis.filter.TextFilter) FastStringBuffer(net.sf.saxon.tree.util.FastStringBuffer) NodeReadTrx(org.sirix.api.NodeReadTrx) SirixException(org.sirix.exception.SirixException) DescendantAxis(org.sirix.axis.DescendantAxis) FilterAxis(org.sirix.axis.filter.FilterAxis)

Example 14 with NodeReadTrx

use of org.sirix.api.NodeReadTrx in project sirix by sirixdb.

the class NodeWrapper method getDeclaredNamespaces.

@Override
public int[] getDeclaredNamespaces(final int[] buffer) {
    int[] retVal = null;
    if (mNodeKind == Kind.ELEMENT) {
        try {
            final NodeReadTrx rtx = createRtxAndMove();
            final int count = rtx.getNamespaceCount();
            if (count == 0) {
                retVal = EMPTY_NAMESPACE_LIST;
            } else {
                retVal = (buffer == null || count > buffer.length ? new int[count] : buffer);
                final NamePool pool = getNamePool();
                int n = 0;
                try {
                    for (int i = 0; i < count; i++) {
                        rtx.moveTo(i);
                        final String prefix = getPrefix();
                        final String uri = getURI();
                        rtx.moveTo(mKey);
                        retVal[n++] = pool.allocateNamespaceCode(prefix, uri);
                    }
                    rtx.close();
                } catch (final SirixException exc) {
                    LOGGER.error(exc.toString());
                }
                /*
					 * If the supplied array is larger than required, then the first
					 * unused entry will be set to -1.
					 */
                if (count < retVal.length) {
                    retVal[count] = -1;
                }
            }
        } catch (final SirixException e) {
            throw new IllegalStateException(e.getCause());
        }
    }
    return retVal;
}
Also used : NodeReadTrx(org.sirix.api.NodeReadTrx) SirixException(org.sirix.exception.SirixException) NamePool(net.sf.saxon.om.NamePool)

Example 15 with NodeReadTrx

use of org.sirix.api.NodeReadTrx in project sirix by sirixdb.

the class NodeWrapper method hasChildNodes.

@Override
public boolean hasChildNodes() {
    boolean hasChildNodes = false;
    try {
        final NodeReadTrx rtx = createRtxAndMove();
        if (rtx.getChildCount() > 0) {
            hasChildNodes = true;
        }
        rtx.close();
    } catch (final SirixException exc) {
        LOGGER.error(exc.toString());
    }
    return hasChildNodes;
}
Also used : NodeReadTrx(org.sirix.api.NodeReadTrx) SirixException(org.sirix.exception.SirixException)

Aggregations

NodeReadTrx (org.sirix.api.NodeReadTrx)21 SirixException (org.sirix.exception.SirixException)13 IOException (java.io.IOException)6 SessionConfiguration (org.sirix.access.conf.SessionConfiguration)6 Database (org.sirix.api.Database)6 Session (org.sirix.api.Session)6 WebApplicationException (javax.ws.rs.WebApplicationException)4 Axis (org.sirix.api.Axis)4 XMLStreamException (javax.xml.stream.XMLStreamException)3 NodeInfo (net.sf.saxon.om.NodeInfo)3 JaxRxException (org.jaxrx.core.JaxRxException)3 Test (org.junit.Test)3 DescendantAxis (org.sirix.axis.DescendantAxis)3 XPathAxis (org.sirix.service.xml.xpath.XPathAxis)3 ArrayList (java.util.ArrayList)2 XPathExpression (javax.xml.xpath.XPathExpression)2 QNm (org.brackit.xquery.atomic.QNm)2 FilterAxis (org.sirix.axis.filter.FilterAxis)2 TextFilter (org.sirix.axis.filter.TextFilter)2 TransactionTuple (org.sirix.gui.view.TransactionTuple)2