Search in sources :

Example 61 with SirixException

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

the class NodeWrapper method getStringValueCS.

@Override
public final CharSequence getStringValueCS() {
    String value = "";
    try {
        final NodeReadTrx rtx = createRtxAndMove();
        switch(mNodeKind) {
            case DOCUMENT:
            case ELEMENT:
                value = expandString();
                break;
            case ATTRIBUTE:
                value = emptyIfNull(rtx.getValue());
                break;
            case TEXT:
                value = rtx.getValue();
                break;
            case COMMENT:
            case PROCESSING_INSTRUCTION:
                value = emptyIfNull(rtx.getValue());
                break;
            default:
                value = "";
        }
        rtx.close();
    } catch (final SirixException exc) {
        LOGGER.error(exc.toString());
    }
    return value;
}
Also used : NodeReadTrx(org.sirix.api.NodeReadTrx) SirixException(org.sirix.exception.SirixException)

Example 62 with SirixException

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

the class NodeWrapper method getSiblingPosition.

@Override
public int getSiblingPosition() {
    int index = 0;
    try {
        final NodeReadTrx rtx = createRtxAndMove();
        while (rtx.hasLeftSibling()) {
            rtx.moveToLeftSibling();
            index++;
        }
        rtx.close();
    } catch (final SirixException e) {
        LOGGER.error(e.getMessage(), e);
    }
    return index;
}
Also used : NodeReadTrx(org.sirix.api.NodeReadTrx) SirixException(org.sirix.exception.SirixException)

Example 63 with SirixException

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

the class RestXPathProcessor method doXPathRes.

/**
 * This method performs an XPath evaluation and writes it to a given output
 * stream.
 *
 * @param resource
 *          The existing resource.
 * @param revision
 *          The revision of the requested document.
 * @param output
 *          The output stream where the results are written.
 * @param nodeid
 *          <code>true</code> if node id's have to be delivered.
 *          <code>false</code> otherwise.
 * @param xpath
 *          The XPath expression.
 * @throws SirixException
 */
private void doXPathRes(final String resource, final Integer revision, final OutputStream output, final boolean nodeid, final String xpath) throws SirixException {
    // Database connection to sirix
    Database database = null;
    Session session = null;
    NodeReadTrx rtx = null;
    try {
        database = Databases.openDatabase(mStoragePath);
        session = database.getSession(new SessionConfiguration.Builder(resource).build());
        // Creating a transaction
        if (revision == null) {
            rtx = session.beginNodeReadTrx();
        } else {
            rtx = session.beginNodeReadTrx(revision);
        }
        final Axis axis = new XPathAxis(rtx, xpath);
        for (final long key : axis) {
            WorkerHelper.serializeXML(session, output, false, nodeid, key, revision).call();
        }
    } catch (final Exception globExcep) {
        throw new WebApplicationException(globExcep, Response.Status.INTERNAL_SERVER_ERROR);
    } finally {
        if (rtx != null) {
            rtx.moveTo(Fixed.DOCUMENT_NODE_KEY.getStandardProperty());
            WorkerHelper.closeRTX(rtx, session, database);
        }
    }
}
Also used : NodeReadTrx(org.sirix.api.NodeReadTrx) WebApplicationException(javax.ws.rs.WebApplicationException) XPathAxis(org.sirix.service.xml.xpath.XPathAxis) Database(org.sirix.api.Database) SessionConfiguration(org.sirix.access.conf.SessionConfiguration) Axis(org.sirix.api.Axis) XPathAxis(org.sirix.service.xml.xpath.XPathAxis) SirixException(org.sirix.exception.SirixException) IOException(java.io.IOException) WebApplicationException(javax.ws.rs.WebApplicationException) Session(org.sirix.api.Session)

Example 64 with SirixException

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

the class RestXPathProcessor method getXpathResource.

/**
 * Getting part of the XML based on a XPath query
 *
 * @param dbFile
 *          where the content should be extracted
 *
 * @param query
 *          contains XPath query
 * @param rId
 *          To response the resource with a restid for each node (
 *          <code>true</code>) or without ( <code>false</code>).
 * @param doRevision
 *          The revision of the requested resource. If <code>null</code>, than
 *          response the latest revision.
 * @param output
 *          The OutputStream reference which have to be modified and returned
 * @param doNodeId
 *          specifies whether node id should be shown
 * @param doWrap
 *          output of result elements
 * @throws SirixException
 */
public void getXpathResource(final File dbFile, final long rId, final String query, final boolean doNodeId, final Integer doRevision, final OutputStream output, final boolean doWrap) throws SirixException {
    // work around because of query root char '/'
    String qQuery = query;
    if (query.charAt(0) == '/')
        qQuery = ".".concat(query);
    Database database = null;
    Session session = null;
    NodeReadTrx rtx = null;
    try {
        database = Databases.openDatabase(dbFile.getParentFile());
        session = database.getSession(new SessionConfiguration.Builder(dbFile.getName()).build());
        if (doRevision == null) {
            rtx = session.beginNodeReadTrx();
        } else {
            rtx = session.beginNodeReadTrx(doRevision);
        }
        final boolean exist = rtx.moveTo(rId).hasMoved();
        if (exist) {
            final Axis axis = new XPathAxis(rtx, qQuery);
            if (doWrap) {
                output.write(beginResult.getBytes());
                for (final long key : axis) {
                    WorkerHelper.serializeXML(session, output, false, doNodeId, key, doRevision).call();
                }
                output.write(endResult.getBytes());
            } else {
                for (final long key : axis) {
                    WorkerHelper.serializeXML(session, output, false, doNodeId, key, doRevision).call();
                }
            }
        } else {
            throw new WebApplicationException(404);
        }
    } catch (final Exception globExcep) {
        throw new WebApplicationException(globExcep, Response.Status.INTERNAL_SERVER_ERROR);
    } finally {
        WorkerHelper.closeRTX(rtx, session, database);
    }
}
Also used : NodeReadTrx(org.sirix.api.NodeReadTrx) WebApplicationException(javax.ws.rs.WebApplicationException) XPathAxis(org.sirix.service.xml.xpath.XPathAxis) Database(org.sirix.api.Database) SessionConfiguration(org.sirix.access.conf.SessionConfiguration) Axis(org.sirix.api.Axis) XPathAxis(org.sirix.service.xml.xpath.XPathAxis) SirixException(org.sirix.exception.SirixException) IOException(java.io.IOException) WebApplicationException(javax.ws.rs.WebApplicationException) Session(org.sirix.api.Session)

Example 65 with SirixException

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

the class DatabaseRepresentation method serialize.

/**
 * This method is responsible to build an {@link OutputStream} containing an
 * XML file out of the sirix file.
 *
 * @param resource
 *          The name of the resource that will be offered as XML.
 * @param nodeid
 *          To response the resource with a restid for each node (
 *          <code>true</code>) or without ( <code>false</code>).
 * @param revision
 *          The revision of the requested resource. If <code>null</code>, than
 *          response the latest revision.
 * @return The {@link OutputStream} containing the serialized XML file.
 * @throws IOException
 * @throws SirixException
 * @throws WebApplicationException
 */
private final OutputStream serialize(final String resource, final Integer revision, final boolean nodeid, final OutputStream output, final boolean wrapResult) throws IOException, JaxRxException, SirixException {
    if (WorkerHelper.checkExistingResource(mStoragePath, resource)) {
        try {
            if (wrapResult) {
                output.write(beginResult.getBytes());
                serializIt(resource, revision, output, nodeid);
                output.write(endResult.getBytes());
            } else {
                serializIt(resource, revision, output, nodeid);
            }
        } catch (final Exception exce) {
            throw new JaxRxException(exce);
        }
    } else {
        throw new JaxRxException(404, "Not found");
    }
    return output;
}
Also used : JaxRxException(org.jaxrx.core.JaxRxException) SirixException(org.sirix.exception.SirixException) IOException(java.io.IOException) WebApplicationException(javax.ws.rs.WebApplicationException) 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