use of org.sirix.exception.SirixUsageException in project sirix by sirixdb.
the class XdmNodeWriterTrxImpl method pi.
/**
* Processing instruction.
*
* @param target target PI
* @param content content of PI
* @param insert insertion location
* @throws SirixException if any unexpected error occurs
*/
private XdmNodeWriteTrx pi(final String target, final String content, final Insert insert) throws SirixException {
final byte[] targetBytes = getBytes(target);
if (!XMLToken.isNCName(checkNotNull(targetBytes))) {
throw new IllegalArgumentException("The target is not valid!");
}
if (content.contains("?>-")) {
throw new SirixUsageException("The content must not contain '?>-'");
}
acquireLock();
try {
if (getCurrentNode() instanceof StructNode) {
checkAccessAndCommit();
// Insert new processing instruction node.
final byte[] processingContent = getBytes(content);
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 QNm targetName = new QNm(target);
final long pathNodeKey = mBuildPathSummary ? mPathSummaryWriter.getPathNodeKey(targetName, Kind.PROCESSING_INSTRUCTION) : 0;
final PINode node = mNodeFactory.createPINode(parentKey, leftSibKey, rightSibKey, targetName, processingContent, mCompression, pathNodeKey, 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.exception.SirixUsageException 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.exception.SirixUsageException 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.exception.SirixUsageException in project sirix by sirixdb.
the class XdmNodeWriterTrxImpl method moveSubtreeToRightSibling.
@Override
public XdmNodeWriteTrx moveSubtreeToRightSibling(@Nonnegative final long fromKey) throws SirixException {
acquireLock();
try {
if (fromKey < 0 || fromKey > getMaxNodeKey()) {
throw new IllegalArgumentException("Argument must be a valid node key!");
}
if (fromKey == getCurrentNode().getNodeKey()) {
throw new IllegalArgumentException("Can't move itself to first child of itself!");
}
// Save: Every node in the "usual" node page is of type Node.
@SuppressWarnings("unchecked") final Optional<? extends Node> node = (Optional<? extends Node>) getPageTransaction().getRecord(fromKey, PageKind.RECORDPAGE, -1);
if (!node.isPresent()) {
throw new IllegalStateException("Node to move must exist!");
}
final Node nodeToMove = node.get();
if (nodeToMove instanceof StructNode && getCurrentNode() instanceof StructNode) {
final StructNode toMove = (StructNode) nodeToMove;
checkAncestors(toMove);
checkAccessAndCommit();
final StructNode nodeAnchor = (StructNode) getCurrentNode();
if (nodeAnchor.getRightSiblingKey() != nodeToMove.getNodeKey()) {
final long parentKey = toMove.getParentKey();
// Adapt hashes.
adaptHashesForMove(toMove);
// Adapt pointers and merge sibling text nodes.
adaptForMove(toMove, nodeAnchor, InsertPos.ASRIGHTSIBLING);
mNodeReader.moveTo(toMove.getNodeKey());
adaptHashesWithAdd();
// Adapt path summary.
if (mBuildPathSummary && toMove instanceof NameNode) {
final NameNode moved = (NameNode) toMove;
final OPType type = moved.getParentKey() == parentKey ? OPType.MOVEDSAMELEVEL : OPType.MOVED;
mPathSummaryWriter.adaptPathForChangedNode(moved, getName(), moved.getURIKey(), moved.getPrefixKey(), moved.getLocalNameKey(), type);
}
// Recompute DeweyIDs if they are used.
if (mDeweyIDsStored) {
computeNewDeweyIDs();
}
}
return this;
} else {
throw new SirixUsageException("Move is not allowed if moved node is not an ElementNode or TextNode and the node isn't inserted at an ElementNode or TextNode!");
}
} finally {
unLock();
}
}
use of org.sirix.exception.SirixUsageException in project sirix by sirixdb.
the class XdmNodeWriterTrxImpl method close.
@Override
public void close() {
acquireLock();
try {
if (!isClosed()) {
// Make sure to commit all dirty data.
if (mModificationCount > 0) {
throw new SirixUsageException("Must commit/rollback transaction first!");
}
// Release all state immediately.
mNodeReader.mResourceManager.closeWriteTransaction(getId());
mNodeReader.close();
removeCommitFile();
mPathSummaryWriter = null;
mNodeFactory = null;
// Shutdown pool.
mPool.shutdown();
try {
mPool.awaitTermination(2, TimeUnit.SECONDS);
} catch (final InterruptedException e) {
throw new SirixThreadedException(e);
}
}
} finally {
unLock();
}
}
Aggregations