Search in sources :

Example 1 with XMLSerializerBuilder

use of org.sirix.service.xml.serialize.XMLSerializer.XMLSerializerBuilder 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 Long nodekey, final Integer revision) {
    final XMLSerializerProperties props = new XMLSerializerProperties();
    final XMLSerializerBuilder builder;
    if (revision == null && nodekey == null) {
        builder = new XMLSerializerBuilder(session, out);
    } else if (revision != null && nodekey == null) {
        builder = new XMLSerializerBuilder(session, out, revision);
    } else if (revision == null && nodekey != null) {
        builder = new XMLSerializerBuilder(session, nodekey, out, props);
    } else {
        assert revision != null;
        builder = new XMLSerializerBuilder(session, nodekey, out, props, revision);
    }
    if (serializeRest) {
        builder.emitRESTful().emitIDs();
    }
    if (serializeXMLDec) {
        builder.emitXMLDeclaration();
    }
    return builder.build();
}
Also used : XMLSerializerBuilder(org.sirix.service.xml.serialize.XMLSerializer.XMLSerializerBuilder) XMLSerializerProperties(org.sirix.service.xml.serialize.XMLSerializerProperties)

Example 2 with XMLSerializerBuilder

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

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

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

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

the class StAXSerializerTest method testStAXSerializer.

@Test
public void testStAXSerializer() {
    try {
        final ByteArrayOutputStream out = new ByteArrayOutputStream();
        final XMLSerializer xmlSerializer = new XMLSerializerBuilder(holder.getResourceManager(), out).emitXMLDeclaration().build();
        xmlSerializer.call();
        final XdmNodeReadTrx rtx = holder.getResourceManager().beginNodeReadTrx();
        StAXSerializer serializer = new StAXSerializer(rtx);
        final StringBuilder strBuilder = new StringBuilder();
        boolean isEmptyElement = false;
        while (serializer.hasNext()) {
            XMLEvent event = serializer.nextEvent();
            switch(event.getEventType()) {
                case XMLStreamConstants.START_DOCUMENT:
                    strBuilder.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");
                    break;
                case XMLStreamConstants.START_ELEMENT:
                    emitElement(event, strBuilder);
                    if (serializer.peek().getEventType() == XMLStreamConstants.END_ELEMENT) {
                        strBuilder.append("/>");
                        isEmptyElement = true;
                    } else {
                        strBuilder.append('>');
                    }
                    break;
                case XMLStreamConstants.END_ELEMENT:
                    if (isEmptyElement) {
                        isEmptyElement = false;
                    } else {
                        emitQName(true, event, strBuilder);
                        strBuilder.append('>');
                    }
                    break;
                case XMLStreamConstants.CHARACTERS:
                    strBuilder.append(((Characters) event).getData());
                    break;
            }
        }
        assertEquals(out.toString(), strBuilder.toString());
        // Check getElementText().
        // ========================================================
        holder.getReader().moveToDocumentRoot();
        holder.getReader().moveToFirstChild();
        serializer = new StAXSerializer(holder.getReader());
        String elemText = null;
        // <p:a>
        if (serializer.hasNext()) {
            serializer.next();
            elemText = serializer.getElementText();
        }
        assertEquals("oops1foooops2baroops3", elemText);
        // oops1
        checkForException(serializer);
        // <b>
        if (serializer.hasNext()) {
            serializer.next();
            elemText = serializer.getElementText();
        }
        assertEquals("foo", elemText);
        // foo
        checkForException(serializer);
        // <c>
        if (serializer.hasNext()) {
            serializer.next();
            elemText = serializer.getElementText();
        }
        assertEquals("", elemText);
        // </c>
        checkForException(serializer);
        // </b>
        checkForException(serializer);
        // oops2
        checkForException(serializer);
        // <b p:x='y'>
        if (serializer.hasNext()) {
            serializer.next();
            elemText = serializer.getElementText();
        }
        assertEquals("bar", elemText);
        // <c>
        if (serializer.hasNext()) {
            serializer.next();
            elemText = serializer.getElementText();
        }
        assertEquals("", elemText);
        // </c>
        checkForException(serializer);
        // bar
        checkForException(serializer);
        // </b>
        checkForException(serializer);
        // oops3
        checkForException(serializer);
        // </p:a>
        checkForException(serializer);
        rtx.close();
    } catch (final XMLStreamException e) {
        fail("XML error while parsing: " + e.getMessage());
    } catch (final SirixException e) {
        fail("Sirix exception occured: " + e.getMessage());
    } catch (final Exception e) {
        fail("Any exception occured: " + e.getMessage());
    }
}
Also used : XMLSerializerBuilder(org.sirix.service.xml.serialize.XMLSerializer.XMLSerializerBuilder) XdmNodeReadTrx(org.sirix.api.XdmNodeReadTrx) XMLStreamException(javax.xml.stream.XMLStreamException) XMLEvent(javax.xml.stream.events.XMLEvent) SirixException(org.sirix.exception.SirixException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SirixException(org.sirix.exception.SirixException) XMLStreamException(javax.xml.stream.XMLStreamException) Test(org.junit.Test)

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