Search in sources :

Example 1 with XMLShredder

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

the class WorkerHelper method shredInputStream.

/**
 * Shreds a given InputStream
 *
 * @param wtx
 *          current write transaction reference
 * @param value
 *          InputStream to be shred
 */
public static void shredInputStream(final NodeWriteTrx wtx, final InputStream value, final Insert child) {
    final XMLInputFactory factory = XMLInputFactory.newInstance();
    factory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
    XMLEventReader parser;
    try {
        parser = factory.createXMLEventReader(value);
    } catch (final XMLStreamException xmlse) {
        throw new WebApplicationException(xmlse);
    }
    try {
        final XMLShredder shredder = new XMLShredder.Builder(wtx, parser, child).commitAfterwards().build();
        shredder.call();
    } catch (final Exception exce) {
        throw new WebApplicationException(exce);
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) WebApplicationException(javax.ws.rs.WebApplicationException) XMLSerializerBuilder(org.sirix.service.xml.serialize.XMLSerializer.XMLSerializerBuilder) XMLEventReader(javax.xml.stream.XMLEventReader) XMLShredder(org.sirix.service.xml.shredder.XMLShredder) XMLInputFactory(javax.xml.stream.XMLInputFactory) SirixException(org.sirix.exception.SirixException) XMLStreamException(javax.xml.stream.XMLStreamException) WebApplicationException(javax.ws.rs.WebApplicationException)

Example 2 with XMLShredder

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

the class DatabaseRepresentation method shred.

/**
 * This method is responsible to save the XML file, which is in an
 * {@link InputStream}, as a sirix object.
 *
 * @param xmlInput
 *          XML file wrapped in an {@link InputStream}
 * @param resource
 *          name of the resource
 * @return {@code true} if shredding process has been successful,
 *         {@code false} otherwise.
 * @throws SirixException
 *           if any sirix related exception occurs
 */
public final boolean shred(final InputStream xmlInput, final String resource) throws SirixException {
    boolean allOk = false;
    NodeWriteTrx wtx = null;
    Database database = null;
    Session session = null;
    boolean abort = false;
    try {
        final DatabaseConfiguration dbConf = new DatabaseConfiguration(mStoragePath);
        Databases.createDatabase(dbConf);
        database = Databases.openDatabase(dbConf.getFile());
        // Shredding the database to the file as XML
        final ResourceConfiguration resConf = new ResourceConfiguration.Builder(resource, dbConf).revisionsToRestore(1).build();
        if (database.createResource(resConf)) {
            session = database.getSession(new SessionConfiguration.Builder(resource).build());
            wtx = session.beginNodeWriteTrx();
            final XMLShredder shredder = new XMLShredder.Builder(wtx, RESTXMLShredder.createReader(xmlInput), Insert.ASFIRSTCHILD).commitAfterwards().build();
            shredder.call();
            allOk = true;
        }
    } catch (final Exception exce) {
        abort = true;
        throw new JaxRxException(exce);
    } finally {
        WorkerHelper.closeWTX(abort, wtx, session, database);
    }
    return allOk;
}
Also used : DatabaseConfiguration(org.sirix.access.conf.DatabaseConfiguration) XMLSerializerBuilder(org.sirix.service.xml.serialize.XMLSerializer.XMLSerializerBuilder) NodeWriteTrx(org.sirix.api.NodeWriteTrx) Database(org.sirix.api.Database) XMLShredder(org.sirix.service.xml.shredder.XMLShredder) RESTXMLShredder(org.sirix.service.jaxrx.util.RESTXMLShredder) SessionConfiguration(org.sirix.access.conf.SessionConfiguration) JaxRxException(org.jaxrx.core.JaxRxException) SirixException(org.sirix.exception.SirixException) IOException(java.io.IOException) WebApplicationException(javax.ws.rs.WebApplicationException) Session(org.sirix.api.Session) ResourceConfiguration(org.sirix.access.conf.ResourceConfiguration) JaxRxException(org.jaxrx.core.JaxRxException)

Example 3 with XMLShredder

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

the class TestNodeWrapper method testGetBaseURI.

@Test
public void testGetBaseURI() throws Exception {
    // Test with xml:base specified.
    final File source = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "data" + File.separator + "testBaseURI.xml");
    final Session session = generateSession();
    final NodeWriteTrx wtx = session.beginNodeWriteTrx();
    final XMLEventReader reader = XMLShredder.createFileReader(source);
    final XMLShredder shredder = new XMLShredder.Builder(wtx, reader, Insert.ASFIRSTCHILD).commitAfterwards().build();
    shredder.call();
    wtx.close();
    final Processor proc = new Processor(false);
    final NodeInfo doc = new DocumentWrapper(session, proc.getUnderlyingConfiguration());
    doc.getNamePool().allocate("xml", "http://www.w3.org/XML/1998/namespace", "base");
    doc.getNamePool().allocate("", "", "baz");
    final NameTest test = new NameTest(Type.ELEMENT, "", "baz", doc.getNamePool());
    final AxisIterator iterator = doc.iterateAxis(Axis.DESCENDANT, test);
    final NodeInfo baz = (NodeInfo) iterator.next();
    assertEquals("http://example.org", baz.getBaseURI());
    session.close();
    mDatabase.close();
}
Also used : NameTest(net.sf.saxon.pattern.NameTest) Processor(net.sf.saxon.s9api.Processor) NodeInfo(net.sf.saxon.om.NodeInfo) NodeWriteTrx(org.sirix.api.NodeWriteTrx) XMLEventReader(javax.xml.stream.XMLEventReader) XMLShredder(org.sirix.service.xml.shredder.XMLShredder) File(java.io.File) AxisIterator(net.sf.saxon.tree.iter.AxisIterator) Session(org.sirix.api.Session) NameTest(net.sf.saxon.pattern.NameTest) Test(org.junit.Test)

Example 4 with XMLShredder

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

the class FMSETest method test.

/**
 * Test a folder of XML files.
 *
 * @param FOLDER path string
 * @throws Exception if any exception occurs
 */
private void test(final String FOLDER) throws Exception {
    Database database = TestHelper.getDatabase(PATHS.PATH1.getFile());
    ResourceManager resource = database.getResourceManager(new ResourceManagerConfiguration.Builder(TestHelper.RESOURCE).build());
    final Path folder = Paths.get(FOLDER);
    final List<Path> list = Files.list(folder).filter(path -> path.getFileName().endsWith(".xml")).collect(toList());
    // Sort files list according to file names.
    list.sort((first, second) -> {
        final String firstName = first.getFileName().toString().substring(0, first.getFileName().toString().indexOf('.'));
        final String secondName = second.getFileName().toString().substring(0, second.getFileName().toString().indexOf('.'));
        if (Integer.parseInt(firstName) < Integer.parseInt(secondName)) {
            return -1;
        } else if (Integer.parseInt(firstName) > Integer.parseInt(secondName)) {
            return +1;
        } else {
            return 0;
        }
    });
    boolean first = true;
    // Shredder files.
    for (final Path file : list) {
        if (file.getFileName().endsWith(".xml")) {
            if (first) {
                first = false;
                try (final XdmNodeWriteTrx wtx = resource.beginNodeWriteTrx()) {
                    final XMLShredder shredder = new XMLShredder.Builder(wtx, XMLShredder.createFileReader(file), Insert.ASFIRSTCHILD).commitAfterwards().build();
                    shredder.call();
                }
            } else {
                FMSEImport.main(new String[] { PATHS.PATH1.getFile().toAbsolutePath().toString(), file.toAbsolutePath().toString() });
            }
            resource.close();
            resource = database.getResourceManager(new ResourceManagerConfiguration.Builder(TestHelper.RESOURCE).build());
            final OutputStream out = new ByteArrayOutputStream();
            final XMLSerializer serializer = new XMLSerializerBuilder(resource, out).build();
            serializer.call();
            final StringBuilder sBuilder = TestHelper.readFile(file, false);
            final Diff diff = new Diff(sBuilder.toString(), out.toString());
            final DetailedDiff detDiff = new DetailedDiff(diff);
            @SuppressWarnings("unchecked") final List<Difference> differences = detDiff.getAllDifferences();
            for (final Difference difference : differences) {
                System.err.println("***********************");
                System.err.println(difference);
                System.err.println("***********************");
            }
            assertTrue("pieces of XML are similar " + diff, diff.similar());
            assertTrue("but are they identical? " + diff, diff.identical());
        }
    }
    database.close();
}
Also used : Path(java.nio.file.Path) XMLUnit(org.custommonkey.xmlunit.XMLUnit) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Difference(org.custommonkey.xmlunit.Difference) XMLSerializer(org.sirix.service.xml.serialize.XMLSerializer) XMLTestCase(org.custommonkey.xmlunit.XMLTestCase) PATHS(org.sirix.TestHelper.PATHS) ResourceManagerConfiguration(org.sirix.access.conf.ResourceManagerConfiguration) TestHelper(org.sirix.TestHelper) After(org.junit.After) XMLSerializerBuilder(org.sirix.service.xml.serialize.XMLSerializer.XMLSerializerBuilder) DetailedDiff(org.custommonkey.xmlunit.DetailedDiff) Path(java.nio.file.Path) Before(org.junit.Before) OutputStream(java.io.OutputStream) SirixException(org.sirix.exception.SirixException) Files(java.nio.file.Files) IOException(java.io.IOException) Test(org.junit.Test) FMSEImport(org.sirix.diff.service.FMSEImport) File(java.io.File) XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) Insert(org.sirix.service.xml.shredder.Insert) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) XMLShredder(org.sirix.service.xml.shredder.XMLShredder) ResourceManager(org.sirix.api.ResourceManager) Paths(java.nio.file.Paths) Database(org.sirix.api.Database) Diff(org.custommonkey.xmlunit.Diff) XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) XMLSerializer(org.sirix.service.xml.serialize.XMLSerializer) DetailedDiff(org.custommonkey.xmlunit.DetailedDiff) DetailedDiff(org.custommonkey.xmlunit.DetailedDiff) Diff(org.custommonkey.xmlunit.Diff) XMLSerializerBuilder(org.sirix.service.xml.serialize.XMLSerializer.XMLSerializerBuilder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) ResourceManagerConfiguration(org.sirix.access.conf.ResourceManagerConfiguration) ResourceManager(org.sirix.api.ResourceManager) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Difference(org.custommonkey.xmlunit.Difference) XMLSerializerBuilder(org.sirix.service.xml.serialize.XMLSerializer.XMLSerializerBuilder) Database(org.sirix.api.Database) XMLShredder(org.sirix.service.xml.shredder.XMLShredder)

Example 5 with XMLShredder

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

the class DocumentCreater method createRevisioned.

/**
 * Create revisioned document.
 *
 * @throws SirixException if shredding fails
 * @throws XMLStreamException if StAX reader couldn't be created
 * @throws IOException if reading XML string fails
 */
public static void createRevisioned(final Database database) throws SirixException, IOException, XMLStreamException {
    try (final XdmNodeWriteTrx firstWtx = database.getResourceManager(new ResourceManagerConfiguration.Builder(TestHelper.RESOURCE).build()).beginNodeWriteTrx()) {
        final XMLShredder shredder = new XMLShredder.Builder(firstWtx, XMLShredder.createStringReader(REVXML), Insert.ASFIRSTCHILD).commitAfterwards().build();
        shredder.call();
    }
    try (final XdmNodeWriteTrx secondWtx = database.getResourceManager(new ResourceManagerConfiguration.Builder(TestHelper.RESOURCE).build()).beginNodeWriteTrx()) {
        secondWtx.moveToFirstChild();
        secondWtx.moveToFirstChild();
        secondWtx.moveToFirstChild();
        secondWtx.setValue("A Contrived Test Document");
        secondWtx.moveToParent();
        secondWtx.moveToRightSibling();
        secondWtx.moveToRightSibling();
        secondWtx.moveToFirstChild();
        secondWtx.moveToRightSibling();
        final long key = secondWtx.getNodeKey();
        secondWtx.insertAttribute(new QNm("role"), "bold");
        secondWtx.moveTo(key);
        secondWtx.moveToRightSibling();
        secondWtx.setValue("changed in it.");
        secondWtx.moveToParent();
        secondWtx.insertElementAsRightSibling(new QNm("para"));
        secondWtx.insertTextAsFirstChild("This is a new para 2b.");
        secondWtx.moveToParent();
        secondWtx.moveToRightSibling();
        secondWtx.moveToRightSibling();
        secondWtx.moveToFirstChild();
        secondWtx.setValue("This is a different para 4.");
        secondWtx.moveToParent();
        secondWtx.insertElementAsRightSibling(new QNm("para"));
        secondWtx.insertTextAsFirstChild("This is a new para 4b.");
        secondWtx.moveToParent();
        secondWtx.moveToRightSibling();
        secondWtx.moveToRightSibling();
        secondWtx.remove();
        secondWtx.remove();
        secondWtx.commit();
        secondWtx.moveToDocumentRoot();
        secondWtx.moveToFirstChild();
        secondWtx.moveToFirstChild();
        secondWtx.remove();
        secondWtx.commit();
    }
}
Also used : XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) QNm(org.brackit.xquery.atomic.QNm) ResourceManagerConfiguration(org.sirix.access.conf.ResourceManagerConfiguration) XMLShredder(org.sirix.service.xml.shredder.XMLShredder)

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