Search in sources :

Example 1 with SirixException

use of org.sirix.exception.SirixException in project sirix by sirixdb.

the class RESTResponseHelper method buildResponseOfDomLR.

/**
 * This method builds the overview for the resources and collection we offer
 * in our implementation.
 *
 * @param storagePath
 *          path to the storage
 * @param availableRes
 *          a list of available resources or collections
 * @return the streaming output for the HTTP response body
 */
public static StreamingOutput buildResponseOfDomLR(final File storagePath, final List<String> availableRes) {
    final StreamingOutput sOutput = new StreamingOutput() {

        @Override
        public void write(final OutputStream output) throws IOException, WebApplicationException {
            Document document;
            try {
                document = createSurroundingXMLResp();
                final Element resElement = RESTResponseHelper.createResultElement(document);
                List<Element> collections;
                try {
                    collections = RESTResponseHelper.createCollectionElementDBs(storagePath, availableRes, document);
                } catch (final SirixException exce) {
                    throw new WebApplicationException(exce);
                }
                for (final Element resource : collections) {
                    resElement.appendChild(resource);
                }
                document.appendChild(resElement);
                final DOMSource domSource = new DOMSource(document);
                final StreamResult streamResult = new StreamResult(output);
                final Transformer transformer = TransformerFactory.newInstance().newTransformer();
                transformer.transform(domSource, streamResult);
            } catch (ParserConfigurationException | TransformerFactoryConfigurationError | TransformerException e) {
                throw new WebApplicationException(e);
            }
        }
    };
    return sOutput;
}
Also used : TransformerFactoryConfigurationError(javax.xml.transform.TransformerFactoryConfigurationError) DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) WebApplicationException(javax.ws.rs.WebApplicationException) StreamResult(javax.xml.transform.stream.StreamResult) OutputStream(java.io.OutputStream) Element(org.w3c.dom.Element) StreamingOutput(javax.ws.rs.core.StreamingOutput) Document(org.w3c.dom.Document) SirixException(org.sirix.exception.SirixException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) TransformerException(javax.xml.transform.TransformerException)

Example 2 with SirixException

use of org.sirix.exception.SirixException 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 3 with SirixException

use of org.sirix.exception.SirixException in project sirix by sirixdb.

the class TextView method refreshUpdate.

@Override
public void refreshUpdate(final Optional<VisualItemAxis> pAxis) {
    checkNotNull(pAxis);
    dispose();
    mAxis = null;
    final Dimension mainFrame = mGUI.getSize();
    mText.setSize(new Dimension(mainFrame.width - 16, mainFrame.height - mGUI.getJMenuBar().getHeight() - 37));
    if (pAxis.isPresent()) {
        mAxis = pAxis.get();
    }
    mDb = checkNotNull(mGUI).getReadDB();
    try {
        if (mRtx != null) {
            mRtx.close();
        }
        mRtx = mDb.getSession().beginNodeReadTrx(mDb.getRevisionNumber());
        mRtx.moveTo(mDb.getNodeKey());
        mText.setText("");
        if (mDimension != null) {
            mText.setSize(mDimension);
        }
        mTempValue = 0;
        mLineChanges = 0;
        serialize();
        mText.setCaretPosition(0);
        repaint();
    } catch (final SirixException e) {
        LOGWRAPPER.error(e.getMessage(), e);
    }
    addAdjustmentListener();
}
Also used : SirixException(org.sirix.exception.SirixException) Dimension(java.awt.Dimension)

Example 4 with SirixException

use of org.sirix.exception.SirixException 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 5 with SirixException

use of org.sirix.exception.SirixException in project sirix by sirixdb.

the class DatabaseRepresentation method getResource.

/**
 * This method is responsible to deliver the whole database. Additional
 * parameters can be set (wrap, revision, output) which change the response
 * view.
 *
 * @param resourceName
 *          The name of the requested database.
 * @param queryParams
 *          The optional query parameters.
 * @return The XML database resource, depending on the query parameters.
 * @throws JaxRxException
 *           The exception occurred.
 */
public StreamingOutput getResource(final String resourceName, final Map<QueryParameter, String> queryParams) throws JaxRxException {
    final StreamingOutput streamingOutput = new StreamingOutput() {

        @Override
        public void write(final OutputStream output) throws IOException, JaxRxException {
            final String revision = queryParams.get(QueryParameter.REVISION);
            final String wrap = queryParams.get(QueryParameter.WRAP);
            final String nodeId = queryParams.get(QueryParameter.OUTPUT);
            final boolean wrapResult = (wrap == null) ? false : wrap.equalsIgnoreCase(YESSTRING);
            final boolean nodeid = (nodeId == null) ? false : nodeId.equalsIgnoreCase(YESSTRING);
            try {
                if (revision == null) {
                    serialize(resourceName, null, nodeid, output, wrapResult);
                } else {
                    // pattern which have to match against the input
                    final Pattern pattern = Pattern.compile("[0-9]+[-]{1}[1-9]+");
                    final Matcher matcher = pattern.matcher(revision);
                    if (matcher.matches()) {
                        getModificHistory(resourceName, revision, nodeid, output, wrapResult);
                    } else {
                        serialize(resourceName, Integer.valueOf(revision), nodeid, output, wrapResult);
                    }
                }
            } catch (final NumberFormatException exce) {
                throw new JaxRxException(400, exce.getMessage());
            } catch (final SirixException exce) {
                throw new JaxRxException(exce);
            }
        }
    };
    return streamingOutput;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) OutputStream(java.io.OutputStream) SirixException(org.sirix.exception.SirixException) StreamingOutput(javax.ws.rs.core.StreamingOutput) JaxRxException(org.jaxrx.core.JaxRxException)

Aggregations

SirixException (org.sirix.exception.SirixException)74 DocumentException (org.brackit.xquery.xdm.DocumentException)25 IOException (java.io.IOException)18 Database (org.sirix.api.Database)17 JaxRxException (org.jaxrx.core.JaxRxException)14 NodeReadTrx (org.sirix.api.NodeReadTrx)13 XdmNodeWriteTrx (org.sirix.api.XdmNodeWriteTrx)13 SessionConfiguration (org.sirix.access.conf.SessionConfiguration)12 Session (org.sirix.api.Session)12 ResourceManager (org.sirix.api.ResourceManager)10 WebApplicationException (javax.ws.rs.WebApplicationException)8 Path (java.nio.file.Path)7 XMLStreamException (javax.xml.stream.XMLStreamException)7 QNm (org.brackit.xquery.atomic.QNm)7 XdmNodeReadTrx (org.sirix.api.XdmNodeReadTrx)6 OutputStream (java.io.OutputStream)5 SubtreeListener (org.brackit.xquery.node.parser.SubtreeListener)5 AbstractTemporalNode (org.brackit.xquery.xdm.AbstractTemporalNode)5 StreamingOutput (javax.ws.rs.core.StreamingOutput)4 DatabaseConfiguration (org.sirix.access.conf.DatabaseConfiguration)4