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());
}
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;
}
}
}
}
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();
}
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;
}
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;
}
Aggregations