Search in sources :

Example 6 with XMLSerializer

use of org.sirix.service.xml.serialize.XMLSerializer in project sirix by sirixdb.

the class XMLUpdateShredderTest method test.

// /** Not working anymore due to text merging on deletes. */
// @Ignore
// @Test
// public void testAllEighth() throws Exception {
// test(XMLALLEIGHTH);
// }
// 
// /** Not working anymore due to text merging on deletes. */
// @Ignore
// @Test
// public void testAllNineth() throws Exception {
// test(XMLALLNINETH);
// }
// @Test
// public void testLinguistics() throws Exception {
// test(XMLLINGUISTICS);
// }
private void test(final Path folder) throws Exception {
    final Database database = TestHelper.getDatabase(PATHS.PATH1.getFile());
    database.createResource(new ResourceConfiguration.Builder(TestHelper.RESOURCE, PATHS.PATH1.getConfig()).build());
    final ResourceManager manager = database.getResourceManager(new ResourceManagerConfiguration.Builder(TestHelper.RESOURCE).build());
    int i = 2;
    final List<Path> files = Files.list(folder).filter(file -> file.getFileName().endsWith(".xml")).collect(Collectors.toList());
    // Sort files array according to file names.
    files.sort((first, second) -> {
        final String firstName = first.getFileName().toString().substring(0, second.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 : files) {
        if (file.endsWith(".xml")) {
            final XdmNodeWriteTrx wtx = manager.beginNodeWriteTrx();
            if (first) {
                final XMLShredder shredder = new XMLShredder.Builder(wtx, XMLShredder.createFileReader(file), Insert.ASFIRSTCHILD).commitAfterwards().build();
                shredder.call();
                first = false;
            } else {
                @SuppressWarnings("deprecation") final XMLUpdateShredder shredder = new XMLUpdateShredder(wtx, XMLShredder.createFileReader(file), Insert.ASFIRSTCHILD, file, ShredderCommit.COMMIT);
                shredder.call();
            }
            assertEquals(i, wtx.getRevisionNumber());
            i++;
            final OutputStream out = new ByteArrayOutputStream();
            final XMLSerializer serializer = new XMLSerializerBuilder(manager, out).prettyPrint().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.out.println("***********************");
                System.out.println(difference);
                System.out.println("***********************");
            }
            assertTrue("pieces of XML are similar " + diff, diff.similar());
            assertTrue("but are they identical? " + diff, diff.identical());
            wtx.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) Test(org.junit.Test) Collectors(java.util.stream.Collectors) XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) List(java.util.List) ResourceConfiguration(org.sirix.access.conf.ResourceConfiguration) 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) 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)

Example 7 with XMLSerializer

use of org.sirix.service.xml.serialize.XMLSerializer in project sirix by sirixdb.

the class NodeIdRepresentation method serialize.

/**
 * This method serializes requested resource
 *
 * @param resource
 *          The requested resource
 * @param nodeId
 *          The node id of the requested resource.
 * @param revision
 *          The revision of the requested resource.
 * @param doNodeId
 *          Specifies whether the node id's have to be shown in the result.
 * @param output
 *          The output stream to be written.
 * @param wrapResult
 *          Specifies whether the result has to be wrapped with a result
 *          element.
 */
private void serialize(final String resource, final long nodeId, final Long revision, final boolean doNodeId, final OutputStream output, final boolean wrapResult) {
    if (WorkerHelper.checkExistingResource(mStoragePath, resource)) {
        Session session = null;
        Database database = null;
        try {
            database = Databases.openDatabase(mStoragePath);
            session = database.getSession(new SessionConfiguration.Builder(resource).build());
            if (wrapResult) {
                output.write(BEGINRESULT);
                final XMLSerializerProperties props = new XMLSerializerProperties();
                final XMLSerializerBuilder builder = new XMLSerializerBuilder(session, nodeId, output, props);
                if (doNodeId) {
                    builder.emitRESTful().emitIDs();
                }
                final XMLSerializer serializer = builder.build();
                serializer.call();
                output.write(ENDRESULT);
            } else {
                final XMLSerializerProperties props = new XMLSerializerProperties();
                final XMLSerializerBuilder builder = new XMLSerializerBuilder(session, nodeId, output, props);
                if (doNodeId) {
                    builder.emitRESTful().emitIDs();
                }
                final XMLSerializer serializer = builder.build();
                serializer.call();
            }
        } catch (final SirixException ttExcep) {
            throw new JaxRxException(ttExcep);
        } catch (final IOException ioExcep) {
            throw new JaxRxException(ioExcep);
        } catch (final Exception globExcep) {
            if (globExcep instanceof JaxRxException) {
                // types
                throw (JaxRxException) globExcep;
            } else {
                throw new JaxRxException(globExcep);
            }
        } finally {
            try {
                WorkerHelper.closeRTX(null, session, database);
            } catch (final SirixException exce) {
                throw new JaxRxException(exce);
            }
        }
    } else {
        throw new JaxRxException(404, "Resource does not exist");
    }
}
Also used : XMLSerializerBuilder(org.sirix.service.xml.serialize.XMLSerializer.XMLSerializerBuilder) XMLSerializerProperties(org.sirix.service.xml.serialize.XMLSerializerProperties) XMLSerializer(org.sirix.service.xml.serialize.XMLSerializer) Database(org.sirix.api.Database) SirixException(org.sirix.exception.SirixException) IOException(java.io.IOException) SessionConfiguration(org.sirix.access.conf.SessionConfiguration) SirixException(org.sirix.exception.SirixException) IOException(java.io.IOException) JaxRxException(org.jaxrx.core.JaxRxException) Session(org.sirix.api.Session) JaxRxException(org.jaxrx.core.JaxRxException)

Aggregations

XMLSerializer (org.sirix.service.xml.serialize.XMLSerializer)7 XMLSerializerBuilder (org.sirix.service.xml.serialize.XMLSerializer.XMLSerializerBuilder)6 Database (org.sirix.api.Database)5 SirixException (org.sirix.exception.SirixException)5 IOException (java.io.IOException)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 JaxRxException (org.jaxrx.core.JaxRxException)3 Test (org.junit.Test)3 SessionConfiguration (org.sirix.access.conf.SessionConfiguration)3 Session (org.sirix.api.Session)3 OutputStream (java.io.OutputStream)2 Files (java.nio.file.Files)2 Path (java.nio.file.Path)2 Paths (java.nio.file.Paths)2 List (java.util.List)2 DetailedDiff (org.custommonkey.xmlunit.DetailedDiff)2 Diff (org.custommonkey.xmlunit.Diff)2 Difference (org.custommonkey.xmlunit.Difference)2 XMLTestCase (org.custommonkey.xmlunit.XMLTestCase)2 XMLUnit (org.custommonkey.xmlunit.XMLUnit)2