Search in sources :

Example 1 with XMLSerializerProperties

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

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

use of org.sirix.service.xml.serialize.XMLSerializerProperties 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)3 XMLSerializerProperties (org.sirix.service.xml.serialize.XMLSerializerProperties)3 IOException (java.io.IOException)2 JaxRxException (org.jaxrx.core.JaxRxException)2 SessionConfiguration (org.sirix.access.conf.SessionConfiguration)2 Database (org.sirix.api.Database)2 Session (org.sirix.api.Session)2 SirixException (org.sirix.exception.SirixException)2 XMLSerializer (org.sirix.service.xml.serialize.XMLSerializer)2 NodeReadTrx (org.sirix.api.NodeReadTrx)1