Search in sources :

Example 6 with XMLShredder

use of org.sirix.service.xml.shredder.XMLShredder in project sirix by sirixdb.

the class DiffTestHelper method initializeData.

private static void initializeData(final Holder holder, final Path... files) throws SirixException, IOException, XMLStreamException {
    int i = 0;
    for (final Path file : files) {
        if (i == 0) {
            final XMLShredder init = new XMLShredder.Builder(holder.getWriter(), XMLShredder.createFileReader(file), Insert.ASFIRSTCHILD).commitAfterwards().build();
            init.call();
        } else {
            final XMLUpdateShredder init = new XMLUpdateShredder(holder.getWriter(), XMLShredder.createFileReader(file), Insert.ASFIRSTCHILD, file, ShredderCommit.COMMIT);
            init.call();
        }
        i++;
    }
}
Also used : Path(java.nio.file.Path) XMLUpdateShredder(org.sirix.service.xml.shredder.XMLUpdateShredder) XMLShredder(org.sirix.service.xml.shredder.XMLShredder)

Example 7 with XMLShredder

use of org.sirix.service.xml.shredder.XMLShredder in project sirix by sirixdb.

the class FMSEImport method shredder.

/**
 * Shredder new revision as temporal resource.
 *
 * @param resNewRev {@link File} reference for new revision (XML resource)
 * @param newRev {@link File} reference for shreddered new revision (sirix resource)
 * @throws SirixException if sirix fails to shredder the file
 * @throws IOException if file couldn't be read
 * @throws XMLStreamException if XML document isn't well formed
 * @throws NullPointerException if {@code resNewRev} or {@code newRev} is {@code null}
 */
private void shredder(final Path resNewRev, @Nonnull Path newRev) throws SirixException, IOException, XMLStreamException {
    assert resNewRev != null;
    assert newRev != null;
    final DatabaseConfiguration conf = new DatabaseConfiguration(newRev);
    Databases.truncateDatabase(conf);
    Databases.createDatabase(conf);
    try (final Database db = Databases.openDatabase(newRev)) {
        db.createResource(new ResourceConfiguration.Builder("shredded", conf).build());
        try (final ResourceManager resMgr = db.getResourceManager(new ResourceManagerConfiguration.Builder("shredded").build());
            final XdmNodeWriteTrx wtx = resMgr.beginNodeWriteTrx()) {
            final XMLEventReader fileReader = XMLShredder.createFileReader(resNewRev);
            final XMLShredder shredder = new XMLShredder.Builder(wtx, fileReader, Insert.ASFIRSTCHILD).commitAfterwards().build();
            shredder.call();
        }
    }
}
Also used : XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) DatabaseConfiguration(org.sirix.access.conf.DatabaseConfiguration) Database(org.sirix.api.Database) XMLEventReader(javax.xml.stream.XMLEventReader) XMLShredder(org.sirix.service.xml.shredder.XMLShredder) ResourceManager(org.sirix.api.ResourceManager)

Example 8 with XMLShredder

use of org.sirix.service.xml.shredder.XMLShredder in project sirix by sirixdb.

the class BookShredding method shredder.

private static void shredder(final File pBooks) throws Exception {
    final DatabaseConfiguration config = new DatabaseConfiguration(TestHelper.PATHS.PATH1.getFile());
    Databases.truncateDatabase(config);
    Databases.createDatabase(config);
    final Database database = Databases.openDatabase(config.getFile());
    database.createResource(new ResourceConfiguration.Builder(TestHelper.RESOURCE, PATHS.PATH1.getConfig()).build());
    final Session session = database.getSession(new SessionConfiguration.Builder(TestHelper.RESOURCE).build());
    final NodeWriteTrx wtx = session.beginNodeWriteTrx();
    final XMLEventReader reader = XMLShredder.createFileReader(pBooks);
    final XMLShredder shredder = new XMLShredder.Builder(wtx, reader, Insert.ASFIRSTCHILD).commitAfterwards().build();
    shredder.call();
    wtx.close();
    session.close();
}
Also used : DatabaseConfiguration(org.sirix.access.conf.DatabaseConfiguration) Database(org.sirix.api.Database) NodeWriteTrx(org.sirix.api.NodeWriteTrx) XMLEventReader(javax.xml.stream.XMLEventReader) XMLShredder(org.sirix.service.xml.shredder.XMLShredder) Session(org.sirix.api.Session)

Example 9 with XMLShredder

use of org.sirix.service.xml.shredder.XMLShredder in project sirix by sirixdb.

the class XdmNodeWriterTrxImpl method insertSubtree.

private XdmNodeWriteTrx insertSubtree(final XMLEventReader reader, final Insert insert) throws SirixException {
    checkNotNull(reader);
    assert insert != null;
    acquireLock();
    try {
        if (getCurrentNode() instanceof StructNode) {
            checkAccessAndCommit();
            mBulkInsert = true;
            long nodeKey = getCurrentNode().getNodeKey();
            final XMLShredder shredder = new XMLShredder.Builder(this, reader, insert).commitAfterwards().build();
            shredder.call();
            moveTo(nodeKey);
            switch(insert) {
                case ASFIRSTCHILD:
                    moveToFirstChild();
                    break;
                case ASRIGHTSIBLING:
                    moveToRightSibling();
                    break;
                case ASLEFTSIBLING:
                    moveToLeftSibling();
                    break;
            }
            nodeKey = getCurrentNode().getNodeKey();
            postOrderTraversalHashes();
            final ImmutableNode startNode = getCurrentNode();
            moveToParent();
            while (getCurrentNode().hasParent()) {
                moveToParent();
                addParentHash(startNode);
            }
            moveTo(nodeKey);
            mBulkInsert = false;
        }
    } finally {
        unLock();
    }
    return this;
}
Also used : ImmutableNode(org.sirix.node.interfaces.immutable.ImmutableNode) XMLShredder(org.sirix.service.xml.shredder.XMLShredder) StructNode(org.sirix.node.interfaces.StructNode)

Example 10 with XMLShredder

use of org.sirix.service.xml.shredder.XMLShredder 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

XMLShredder (org.sirix.service.xml.shredder.XMLShredder)10 XMLEventReader (javax.xml.stream.XMLEventReader)5 Database (org.sirix.api.Database)4 DatabaseConfiguration (org.sirix.access.conf.DatabaseConfiguration)3 NodeWriteTrx (org.sirix.api.NodeWriteTrx)3 Session (org.sirix.api.Session)3 XdmNodeWriteTrx (org.sirix.api.XdmNodeWriteTrx)3 SirixException (org.sirix.exception.SirixException)3 XMLSerializerBuilder (org.sirix.service.xml.serialize.XMLSerializer.XMLSerializerBuilder)3 File (java.io.File)2 IOException (java.io.IOException)2 Path (java.nio.file.Path)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 Test (org.junit.Test)2 ResourceManagerConfiguration (org.sirix.access.conf.ResourceManagerConfiguration)2 StructNode (org.sirix.node.interfaces.StructNode)2 ImmutableNode (org.sirix.node.interfaces.immutable.ImmutableNode)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStream (java.io.OutputStream)1 Files (java.nio.file.Files)1