Search in sources :

Example 1 with Insert

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;
}
Also used : ImmutableNode(org.sirix.node.interfaces.immutable.ImmutableNode) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) XMLShredder(org.sirix.service.xml.shredder.XMLShredder) Insert(org.sirix.service.xml.shredder.Insert) StructNode(org.sirix.node.interfaces.StructNode)

Aggregations

XMLEventReader (javax.xml.stream.XMLEventReader)1 XMLEvent (javax.xml.stream.events.XMLEvent)1 StructNode (org.sirix.node.interfaces.StructNode)1 ImmutableNode (org.sirix.node.interfaces.immutable.ImmutableNode)1 Insert (org.sirix.service.xml.shredder.Insert)1 XMLShredder (org.sirix.service.xml.shredder.XMLShredder)1