Search in sources :

Example 1 with NodeReadTrx

use of org.sirix.api.NodeReadTrx in project sirix by sirixdb.

the class TextView method serialize.

/**
 * Serialize a tree.
 */
private void serialize() {
    final NodeReadTrx rtx = mRtx;
    // Style document.
    final StyledDocument doc = (StyledDocument) mText.getDocument();
    final Style styleElements = doc.addStyle("elements", null);
    StyleConstants.setForeground(styleElements, ELEMENT_COLOR);
    final Style styleNamespaces = doc.addStyle("attributes", null);
    StyleConstants.setForeground(styleNamespaces, NAMESPACE_COLOR);
    final Style styleAttributes = doc.addStyle("attributes", null);
    StyleConstants.setForeground(styleAttributes, ATTRIBUTE_COLOR);
    final Style styleText = doc.addStyle("text", null);
    StyleConstants.setForeground(styleText, TEXT_COLOR);
    final long nodeKey = rtx.getNodeKey();
    try {
        switch(rtx.getKind()) {
            case DOCUMENT:
            case ELEMENT:
                mText.setText("");
                if (mAxis == null) {
                    mSerializer = new StAXSerializer(rtx);
                } else {
                    mSerializer = new StAXDiffSerializer(new DiffAxis(IncludeSelf.YES, mDb.getSession().beginNodeReadTrx(mDb.getCompareRevisionNumber()), rtx, mAxis));
                }
                processStAX(State.INITIAL);
                break;
            case TEXT:
                rtx.moveTo(nodeKey);
                mText.setText("");
                doc.insertString(doc.getLength(), new String(rtx.getRawValue()), styleText);
                break;
            case NAMESPACE:
                // Move transaction to parent of given namespace node.
                rtx.moveToParent();
                mText.setText("");
                final long nNodeKey = rtx.getNodeKey();
                for (int i = 0, namespCount = rtx.getNamespaceCount(); i < namespCount; i++) {
                    rtx.moveToNamespace(i);
                    if (rtx.getNodeKey() == nodeKey) {
                        break;
                    }
                    rtx.moveTo(nNodeKey);
                }
                if (rtx.nameForKey(rtx.getPrefixKey()).length() == 0) {
                    doc.insertString(doc.getLength(), new StringBuilder().append("xmlns='").append(rtx.nameForKey(rtx.getURIKey())).append("'").toString(), styleNamespaces);
                } else {
                    doc.insertString(doc.getLength(), new StringBuilder().append("xmlns:").append(rtx.nameForKey(rtx.getPrefixKey())).append("='").append(rtx.nameForKey(rtx.getURIKey())).append("'").toString(), styleNamespaces);
                }
                break;
            case ATTRIBUTE:
                // Move transaction to parent of given attribute node.
                rtx.moveToParent();
                mText.setText("");
                final long aNodeKey = rtx.getNodeKey();
                for (int i = 0, attsCount = rtx.getAttributeCount(); i < attsCount; i++) {
                    rtx.moveToAttribute(i);
                    if (rtx.getNodeKey() == nodeKey) {
                        break;
                    }
                    rtx.moveTo(aNodeKey);
                }
                // Display value.
                final String attPrefix = rtx.getName().getPrefix();
                final QNm attQName = rtx.getName();
                if (attPrefix == null || attPrefix.isEmpty()) {
                    doc.insertString(doc.getLength(), new StringBuilder().append(attQName.getLocalName()).append("='").append(rtx.getValue()).append("'").toString(), styleAttributes);
                } else {
                    doc.insertString(doc.getLength(), new StringBuilder().append(attPrefix).append(":").append(attQName.getLocalName()).append("='").append(rtx.getValue()).append("'").toString(), styleAttributes);
                }
                break;
            default:
                throw new IllegalStateException("Node kind not known!");
        }
    } catch (final SirixException | BadLocationException | XMLStreamException e) {
        LOGWRAPPER.error(e.getMessage(), e);
    }
}
Also used : StAXSerializer(org.sirix.service.xml.serialize.StAXSerializer) StyledDocument(javax.swing.text.StyledDocument) DiffAxis(org.sirix.gui.view.DiffAxis) QNm(org.brackit.xquery.atomic.QNm) NodeReadTrx(org.sirix.api.NodeReadTrx) XMLStreamException(javax.xml.stream.XMLStreamException) Style(javax.swing.text.Style) SirixException(org.sirix.exception.SirixException) BadLocationException(javax.swing.text.BadLocationException)

Example 2 with NodeReadTrx

use of org.sirix.api.NodeReadTrx in project sirix by sirixdb.

the class DatabaseRepresentation method getLastRevision.

/**
 * This method reads the existing database, and offers the last revision id of
 * the database
 *
 * @param resourceName
 *          The name of the existing database.
 * @return The {@link OutputStream} containing the result
 * @throws WebApplicationException
 *           The Exception occurred.
 * @throws SirixException
 */
public long getLastRevision(final String resourceName) throws JaxRxException, SirixException {
    long lastRevision;
    if (WorkerHelper.checkExistingResource(mStoragePath, resourceName)) {
        Database database = Databases.openDatabase(mStoragePath);
        NodeReadTrx rtx = null;
        Session session = null;
        try {
            session = database.getSession(new SessionConfiguration.Builder(resourceName).build());
            lastRevision = session.getMostRecentRevisionNumber();
        } catch (final Exception globExcep) {
            throw new JaxRxException(globExcep);
        } finally {
            WorkerHelper.closeRTX(rtx, session, database);
        }
    } else {
        throw new JaxRxException(404, "Resource not found");
    }
    return lastRevision;
}
Also used : NodeReadTrx(org.sirix.api.NodeReadTrx) 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 NodeReadTrx

use of org.sirix.api.NodeReadTrx 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 NodeReadTrx

use of org.sirix.api.NodeReadTrx in project sirix by sirixdb.

the class DocumentWrapper method selectID.

@Override
public NodeInfo selectID(final String ID, final boolean getParent) {
    try {
        final NodeReadTrx rtx = mSession.beginNodeReadTrx();
        final Axis axis = new DescendantAxis(rtx, IncludeSelf.YES);
        while (axis.hasNext()) {
            if (rtx.getKind() == Kind.ELEMENT) {
                final int attCount = rtx.getAttributeCount();
                if (attCount > 0) {
                    final long nodeKey = rtx.getNodeKey();
                    for (int index = 0; index < attCount; index++) {
                        rtx.moveToAttribute(index);
                        if ("xml:id".equalsIgnoreCase(rtx.getName().getLocalName()) && ID.equals(rtx.getValue())) {
                            if (getParent) {
                                rtx.moveToParent();
                            }
                            return new NodeWrapper(this, rtx.getNodeKey());
                        }
                        rtx.moveTo(nodeKey);
                    }
                }
            }
            axis.next();
        }
        rtx.close();
    } catch (final SirixException e) {
        LOGWRAPPER.error(e.getMessage(), e);
    }
    return null;
}
Also used : NodeReadTrx(org.sirix.api.NodeReadTrx) SirixException(org.sirix.exception.SirixException) DescendantAxis(org.sirix.axis.DescendantAxis) Axis(org.sirix.api.Axis) DescendantAxis(org.sirix.axis.DescendantAxis)

Example 5 with NodeReadTrx

use of org.sirix.api.NodeReadTrx in project sirix by sirixdb.

the class NodeWrapper method getParent.

@Override
public NodeInfo getParent() {
    try {
        NodeInfo parent = null;
        final NodeReadTrx rtx = createRtxAndMove();
        if (rtx.hasParent()) {
            // Parent transaction.
            parent = new NodeWrapper(mDocWrapper, rtx.getParentKey());
        }
        rtx.close();
        return parent;
    } catch (final SirixException e) {
        LOGGER.error(e.getMessage(), e);
        return null;
    }
}
Also used : NodeReadTrx(org.sirix.api.NodeReadTrx) NodeInfo(net.sf.saxon.om.NodeInfo) SirixException(org.sirix.exception.SirixException)

Aggregations

NodeReadTrx (org.sirix.api.NodeReadTrx)21 SirixException (org.sirix.exception.SirixException)13 IOException (java.io.IOException)6 SessionConfiguration (org.sirix.access.conf.SessionConfiguration)6 Database (org.sirix.api.Database)6 Session (org.sirix.api.Session)6 WebApplicationException (javax.ws.rs.WebApplicationException)4 Axis (org.sirix.api.Axis)4 XMLStreamException (javax.xml.stream.XMLStreamException)3 NodeInfo (net.sf.saxon.om.NodeInfo)3 JaxRxException (org.jaxrx.core.JaxRxException)3 Test (org.junit.Test)3 DescendantAxis (org.sirix.axis.DescendantAxis)3 XPathAxis (org.sirix.service.xml.xpath.XPathAxis)3 ArrayList (java.util.ArrayList)2 XPathExpression (javax.xml.xpath.XPathExpression)2 QNm (org.brackit.xquery.atomic.QNm)2 FilterAxis (org.sirix.axis.filter.FilterAxis)2 TextFilter (org.sirix.axis.filter.TextFilter)2 TransactionTuple (org.sirix.gui.view.TransactionTuple)2