Search in sources :

Example 11 with XMLSerializerBuilder

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

the class XMLSerializerTest method testIDSerializer.

@Test
public void testIDSerializer() throws Exception {
    final Database database = TestHelper.getDatabase(PATHS.PATH1.getFile());
    final ResourceManager manager = database.getResourceManager(new ResourceManagerConfiguration.Builder(TestHelper.RESOURCE).build());
    final XdmNodeWriteTrx wtx = manager.beginNodeWriteTrx();
    DocumentCreater.create(wtx);
    wtx.commit();
    wtx.close();
    // Generate from this session.
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    final XMLSerializer serializer = new XMLSerializerBuilder(manager, out).emitIDs().emitXMLDeclaration().build();
    serializer.call();
    assertEquals(DocumentCreater.ID, out.toString());
    manager.close();
}
Also used : XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) XMLSerializerBuilder(org.sirix.service.xml.serialize.XMLSerializer.XMLSerializerBuilder) XMLSerializerBuilder(org.sirix.service.xml.serialize.XMLSerializer.XMLSerializerBuilder) Database(org.sirix.api.Database) ResourceManager(org.sirix.api.ResourceManager) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 12 with XMLSerializerBuilder

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

XMLSerializerBuilder (org.sirix.service.xml.serialize.XMLSerializer.XMLSerializerBuilder)12 Database (org.sirix.api.Database)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 Test (org.junit.Test)7 ResourceManager (org.sirix.api.ResourceManager)6 XdmNodeWriteTrx (org.sirix.api.XdmNodeWriteTrx)6 SirixException (org.sirix.exception.SirixException)6 XMLSerializer (org.sirix.service.xml.serialize.XMLSerializer)6 IOException (java.io.IOException)4 JaxRxException (org.jaxrx.core.JaxRxException)3 SessionConfiguration (org.sirix.access.conf.SessionConfiguration)3 Session (org.sirix.api.Session)3 XMLSerializerProperties (org.sirix.service.xml.serialize.XMLSerializerProperties)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