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