Search in sources :

Example 1 with XMLSerializer

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

the class WorkerHelper method serializeXML.

/**
 * This method creates a new XMLSerializer reference
 *
 * @param session
 *          Associated session.
 * @param out
 *          OutputStream
 *
 * @param serializeXMLDec
 *          specifies whether XML declaration should be shown
 * @param serializeRest
 *          specifies whether node id should be shown
 *
 * @return new XMLSerializer reference
 */
public static XMLSerializer serializeXML(final Session session, final OutputStream out, final boolean serializeXMLDec, final boolean serializeRest, final Integer revision) {
    final XMLSerializerBuilder builder;
    if (revision == null)
        builder = new XMLSerializerBuilder(session, out);
    else
        builder = new XMLSerializerBuilder(session, out, revision);
    if (serializeRest) {
        builder.emitRESTful().emitIDs();
    }
    if (serializeXMLDec) {
        builder.emitXMLDeclaration();
    }
    final XMLSerializer serializer = builder.build();
    return serializer;
}
Also used : XMLSerializerBuilder(org.sirix.service.xml.serialize.XMLSerializer.XMLSerializerBuilder) XMLSerializer(org.sirix.service.xml.serialize.XMLSerializer)

Example 2 with XMLSerializer

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

the class DatabaseRepresentation method serializIt.

/**
 * The XML serializer to a given tnk file.
 *
 * @param resource
 *          The resource that has to be serialized.
 * @param revision
 *          The revision of the document.
 * @param output
 *          The output stream where we write the XML file.
 * @param nodeid
 *          <code>true</code> when you want the result nodes with node id's.
 *          <code>false</code> otherwise.
 * @throws WebApplicationException
 *           The exception occurred.
 * @throws SirixException
 */
private void serializIt(final String resource, final Integer revision, final OutputStream output, final boolean nodeid) throws JaxRxException, SirixException {
    // Connection to sirix, creating a session
    Database database = null;
    Session session = null;
    // INodeReadTrx rtx = null;
    try {
        database = Databases.openDatabase(mStoragePath);
        session = database.getSession(new SessionConfiguration.Builder(resource).build());
        // and creating a transaction
        // if (revision == null) {
        // rtx = session.beginReadTransaction();
        // } else {
        // rtx = session.beginReadTransaction(revision);
        // }
        final XMLSerializerBuilder builder;
        if (revision == null)
            builder = new XMLSerializerBuilder(session, output);
        else
            builder = new XMLSerializerBuilder(session, output, revision);
        if (nodeid) {
            builder.emitRESTful().emitIDs();
        }
        final XMLSerializer serializer = builder.build();
        serializer.call();
    } catch (final Exception exce) {
        throw new JaxRxException(exce);
    } finally {
        // closing the sirix storage
        WorkerHelper.closeRTX(null, session, database);
    }
}
Also used : XMLSerializerBuilder(org.sirix.service.xml.serialize.XMLSerializer.XMLSerializerBuilder) XMLSerializer(org.sirix.service.xml.serialize.XMLSerializer) Database(org.sirix.api.Database) 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) JaxRxException(org.jaxrx.core.JaxRxException)

Example 3 with XMLSerializer

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

the class NodeIdRepresentation method serializeAT.

/**
 * This method serializes requested resource with an access type.
 *
 * @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.
 * @param accessType
 *          The {@link IDAccessType} which indicates the access to a special
 *          node.
 */
private void serializeAT(final String resource, final long nodeId, final Integer revision, final boolean doNodeId, final OutputStream output, final boolean wrapResult, final IDAccessType accessType) {
    if (WorkerHelper.checkExistingResource(mStoragePath, resource)) {
        Session session = null;
        Database database = null;
        NodeReadTrx rtx = null;
        try {
            database = Databases.openDatabase(mStoragePath);
            session = database.getSession(new SessionConfiguration.Builder(resource).build());
            if (revision == null) {
                rtx = session.beginNodeReadTrx();
            } else {
                rtx = session.beginNodeReadTrx(revision);
            }
            if (rtx.moveTo(nodeId).hasMoved()) {
                switch(accessType) {
                    case FIRSTCHILD:
                        if (!rtx.moveToFirstChild().hasMoved())
                            throw new JaxRxException(404, NOTFOUND);
                        break;
                    case LASTCHILD:
                        if (rtx.moveToFirstChild().hasMoved()) {
                            long last = rtx.getNodeKey();
                            while (rtx.moveToRightSibling().hasMoved()) {
                                last = rtx.getNodeKey();
                            }
                            rtx.moveTo(last);
                        } else {
                            throw new JaxRxException(404, NOTFOUND);
                        }
                        break;
                    case RIGHTSIBLING:
                        if (!rtx.moveToRightSibling().hasMoved())
                            throw new JaxRxException(404, NOTFOUND);
                        break;
                    case LEFTSIBLING:
                        if (!rtx.moveToLeftSibling().hasMoved())
                            throw new JaxRxException(404, NOTFOUND);
                        break;
                    // nothing to do;
                    default:
                }
                if (wrapResult) {
                    output.write(BEGINRESULT);
                    final XMLSerializerProperties props = new XMLSerializerProperties();
                    final XMLSerializerBuilder builder = new XMLSerializerBuilder(session, rtx.getNodeKey(), 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, rtx.getNodeKey(), output, props);
                    if (doNodeId) {
                        builder.emitRESTful().emitIDs();
                    }
                    final XMLSerializer serializer = builder.build();
                    serializer.call();
                }
            } else {
                throw new JaxRxException(404, NOTFOUND);
            }
        } 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(rtx, session, database);
            } catch (final SirixException exce) {
                throw new JaxRxException(exce);
            }
        }
    } else {
        throw new JaxRxException(404, "Resource does not exist");
    }
}
Also used : XMLSerializer(org.sirix.service.xml.serialize.XMLSerializer) IOException(java.io.IOException) SirixException(org.sirix.exception.SirixException) IOException(java.io.IOException) JaxRxException(org.jaxrx.core.JaxRxException) XMLSerializerBuilder(org.sirix.service.xml.serialize.XMLSerializer.XMLSerializerBuilder) XMLSerializerProperties(org.sirix.service.xml.serialize.XMLSerializerProperties) NodeReadTrx(org.sirix.api.NodeReadTrx) Database(org.sirix.api.Database) SirixException(org.sirix.exception.SirixException) SessionConfiguration(org.sirix.access.conf.SessionConfiguration) Session(org.sirix.api.Session) JaxRxException(org.jaxrx.core.JaxRxException)

Example 4 with XMLSerializer

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

the class PICommentTest method testPI.

@Test
public void testPI() throws SirixException {
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    final XMLSerializer serializer = new XMLSerializer.XMLSerializerBuilder(mHolder.getResourceManager(), out).emitXMLDeclaration().build();
    serializer.call();
    Assert.assertEquals(DocumentCreater.COMMENTPIXML, out.toString());
}
Also used : XMLSerializer(org.sirix.service.xml.serialize.XMLSerializer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 5 with XMLSerializer

use of org.sirix.service.xml.serialize.XMLSerializer 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)

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