use of org.sirix.service.xml.shredder.Insert 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;
}
Aggregations