Search in sources :

Example 36 with XPathAxis

use of org.sirix.service.xml.xpath.XPathAxis in project sirix by sirixdb.

the class PredicateFilterAxisTest method testPredicates.

@Test
public void testPredicates() throws SirixException {
    // Find descendants starting from nodeKey 0L (root).
    holder.getReader().moveToDocumentRoot();
    AbsAxisTest.testIAxisConventions(new XPathAxis(holder.getReader(), "/p:a[@i]"), new long[] { 1L });
    AbsAxisTest.testIAxisConventions(new XPathAxis(holder.getReader(), "p:a/b[@p:x]"), new long[] { 9L });
    AbsAxisTest.testIAxisConventions(new XPathAxis(holder.getReader(), "p:a[text()]"), new long[] { 1L });
    AbsAxisTest.testIAxisConventions(new XPathAxis(holder.getReader(), "p:a[element()]"), new long[] { 1L });
    AbsAxisTest.testIAxisConventions(new XPathAxis(holder.getReader(), "p:a[node()/text()]"), new long[] { 1L });
    AbsAxisTest.testIAxisConventions(new XPathAxis(holder.getReader(), "p:a[./node()]"), new long[] { 1L });
    AbsAxisTest.testIAxisConventions(new XPathAxis(holder.getReader(), "p:a[./node()/node()/node()]"), new long[] {});
    AbsAxisTest.testIAxisConventions(new XPathAxis(holder.getReader(), "p:a[//element()]"), new long[] { 1L });
    AbsAxisTest.testIAxisConventions(new XPathAxis(holder.getReader(), "p:a[/text()]"), new long[] {});
    AbsAxisTest.testIAxisConventions(new XPathAxis(holder.getReader(), "p:a[3<4]"), new long[] { 1L });
    AbsAxisTest.testIAxisConventions(new XPathAxis(holder.getReader(), "p:a[13>=4]"), new long[] { 1L });
    AbsAxisTest.testIAxisConventions(new XPathAxis(holder.getReader(), "p:a[13.0>=4]"), new long[] { 1L });
    AbsAxisTest.testIAxisConventions(new XPathAxis(holder.getReader(), "p:a[4 = 4]"), new long[] { 1L });
    AbsAxisTest.testIAxisConventions(new XPathAxis(holder.getReader(), "p:a[3=4]"), new long[] {});
    AbsAxisTest.testIAxisConventions(new XPathAxis(holder.getReader(), "p:a[3.2 = 3.22]"), new long[] {});
    holder.getReader().moveTo(1L);
    AbsAxisTest.testIAxisConventions(new XPathAxis(holder.getReader(), "child::b[child::c]"), new long[] { 5L, 9L });
    AbsAxisTest.testIAxisConventions(new XPathAxis(holder.getReader(), "child::*[text() or c]"), new long[] { 5l, 9L });
    AbsAxisTest.testIAxisConventions(new XPathAxis(holder.getReader(), "child::*[text() or c], /node(), //c"), new long[] { 5l, 9L, 1L, 7L, 11L });
}
Also used : XPathAxis(org.sirix.service.xml.xpath.XPathAxis) Test(org.junit.Test) AbsAxisTest(org.sirix.axis.AbsAxisTest)

Example 37 with XPathAxis

use of org.sirix.service.xml.xpath.XPathAxis in project sirix by sirixdb.

the class WikipediaImport method parseStartTag.

/**
 * Parses a start tag.
 *
 * @param startTagEvent current StAX {@link XMLEvent}
 * @param timestamp timestamp start tag {@link StartElement}
 * @param wikiPage wikipedia page start tag {@link StartElement}
 * @param revision revision start tag {@link StartElement}
 * @param pageID page-ID start tag {@link StartElement}
 * @param dateRange date range, the following values are possible:
 *        <dl>
 *        <dt>h</dt>
 *        <dd>hourly revisions</dd>
 *        <dt>d</dt>
 *        <dd>daily revisions</dd>
 *        <dt>w</dt>
 *        <dd>weekly revisions (currently unsupported)</dd>
 *        <dt>m</dt>
 *        <dd>monthly revisions</dd>
 *        </dl>
 * @throws XMLStreamException In case of any XML parsing errors.
 * @throws SirixException In case of any sirix errors.
 */
private void parseStartTag(final XMLEvent startTagEvent, final StartElement timestamp, final StartElement wikiPage, final StartElement revision, final StartElement pageID, final DateBy dateRange) throws XMLStreamException, SirixException {
    XMLEvent event = startTagEvent;
    if (checkStAXStartElement(event.asStartElement(), pageID)) {
        event = mReader.nextEvent();
        mPageEvents.add(event);
        if (!mIsRev) {
            mIdText = event.asCharacters().getData();
        }
    } else if (checkStAXStartElement(event.asStartElement(), timestamp)) {
        // Timestamp start tag found.
        event = mReader.nextEvent();
        mPageEvents.add(event);
        final String currTimestamp = event.asCharacters().getData();
        // Timestamp.
        if (mTimestamp == null) {
            mTimestamp = parseTimestamp(dateRange, currTimestamp);
        } else if (!parseTimestamp(dateRange, currTimestamp).equals(mTimestamp)) {
            mTimestamp = parseTimestamp(dateRange, currTimestamp);
            mWtx.commit();
            mWtx.close();
            mWtx = mResourceManager.beginNodeWriteTrx();
        }
        assert mIdText != null;
        // Search for existing page.
        final QName page = wikiPage.getName();
        final QName id = pageID.getName();
        final String query = "//" + qNameToString(page) + "[fn:string(" + qNameToString(id) + ") = '" + mIdText + "']";
        mWtx.moveToDocumentRoot();
        final Axis axis = new XPathAxis(mWtx, query);
        // Determines if page is found in shreddered file.
        mFound = false;
        // Counts found page.
        int resCounter = 0;
        long key = mWtx.getNodeKey();
        while (axis.hasNext()) {
            axis.next();
            // Page is found.
            mFound = true;
            // Make sure no more than one page with a unique id is found.
            resCounter++;
            assert resCounter == 1;
            // Make sure the transaction is on the page element found.
            assert mWtx.getName().getLocalName().equals(wikiPage.getName().getLocalPart());
            key = mWtx.getNodeKey();
        }
        mWtx.moveTo(key);
    }
}
Also used : XPathAxis(org.sirix.service.xml.xpath.XPathAxis) QName(javax.xml.namespace.QName) XMLEvent(javax.xml.stream.events.XMLEvent) Axis(org.sirix.api.Axis) XPathAxis(org.sirix.service.xml.xpath.XPathAxis)

Example 38 with XPathAxis

use of org.sirix.service.xml.xpath.XPathAxis 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 39 with XPathAxis

use of org.sirix.service.xml.xpath.XPathAxis 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 40 with XPathAxis

use of org.sirix.service.xml.xpath.XPathAxis in project sirix by sirixdb.

the class DatabaseRepresentation method getModificHistory.

/**
 * This method reads the existing database, and offers all modifications of
 * the two given revisions
 *
 * @param resourceName
 *          The name of the existing database.
 * @param revisionRange
 *          Contains the range of revisions
 * @param nodeid
 *          To response the resource with a restid for each node (
 *          <code>true</code>) or without ( <code>false</code>).
 * @param output
 *          The OutputStream reference which have to be modified and returned
 * @param wrap
 *          <code>true</code> if the results have to be wrapped.
 *          <code>false</code> otherwise.
 * @return The {@link OutputStream} containing the result
 * @throws SirixException
 * @throws WebApplicationException
 *           The Exception occurred.
 */
public OutputStream getModificHistory(// NOPMD this method needs alls these
final String resourceName, // functions
final String revisionRange, final boolean nodeid, final OutputStream output, final boolean wrap) throws JaxRxException, SirixException {
    // extract both revision from given String value
    final StringTokenizer tokenizer = new StringTokenizer(revisionRange, "-");
    final int revision1 = Integer.valueOf(tokenizer.nextToken());
    final int revision2 = Integer.valueOf(tokenizer.nextToken());
    if (revision1 < revision2 && revision2 <= getLastRevision(resourceName)) {
        // variables for highest rest-id in respectively revision
        long maxRestidRev1 = 1;
        long maxRestidRev2 = 1;
        // Connection to sirix, creating a session
        Database database = null;
        Axis axis = null;
        NodeReadTrx rtx = null;
        Session session = null;
        // List for all restIds of modifications
        final List<Long> modificRestids = new LinkedList<Long>();
        // List of all restIds of revision 1
        final List<Long> restIdsRev1 = new LinkedList<Long>();
        try {
            database = Databases.openDatabase(mStoragePath);
            session = database.getSession(new SessionConfiguration.Builder(resourceName).build());
            // get highest rest-id from given revision 1
            rtx = session.beginNodeReadTrx(revision1);
            axis = new XPathAxis(rtx, ".//*");
            while (axis.hasNext()) {
                axis.next();
                if (rtx.getNodeKey() > maxRestidRev1) {
                    maxRestidRev1 = rtx.getNodeKey();
                }
                // stores all restids from revision 1 into a list
                restIdsRev1.add(rtx.getNodeKey());
            }
            rtx.moveTo(Fixed.DOCUMENT_NODE_KEY.getStandardProperty());
            rtx.close();
            // get highest rest-id from given revision 2
            rtx = session.beginNodeReadTrx(revision2);
            axis = new XPathAxis(rtx, ".//*");
            while (axis.hasNext()) {
                axis.next();
                final Long nodeKey = rtx.getNodeKey();
                if (nodeKey > maxRestidRev2) {
                    maxRestidRev2 = rtx.getNodeKey();
                }
                if (nodeKey > maxRestidRev1) {
                    /*
						 * writes all restids of revision 2 higher than the highest restid
						 * of revision 1 into the list
						 */
                    modificRestids.add(nodeKey);
                }
                /*
					 * removes all restids from restIdsRev1 that appears in revision 2 all
					 * remaining restids in the list can be seen as deleted nodes
					 */
                restIdsRev1.remove(nodeKey);
            }
            rtx.moveTo(Fixed.DOCUMENT_NODE_KEY.getStandardProperty());
            rtx.close();
            rtx = session.beginNodeReadTrx(revision1);
            // linked list for holding unique restids from revision 1
            final List<Long> restIdsRev1New = new LinkedList<Long>();
            /*
				 * Checks if a deleted node has a parent node that was deleted too. If
				 * so, only the parent node is stored in new list to avoid double print
				 * of node modification
				 */
            for (Long nodeKey : restIdsRev1) {
                rtx.moveTo(nodeKey);
                final long parentKey = rtx.getParentKey();
                if (!restIdsRev1.contains(parentKey)) {
                    restIdsRev1New.add(nodeKey);
                }
            }
            rtx.moveTo(Fixed.DOCUMENT_NODE_KEY.getStandardProperty());
            rtx.close();
            if (wrap) {
                output.write(beginResult.getBytes());
            }
            /*
				 * Shred modified restids from revision 2 to xml fragment Just
				 * modifications done by post commands
				 */
            rtx = session.beginNodeReadTrx(revision2);
            for (Long nodeKey : modificRestids) {
                rtx.moveTo(nodeKey);
                WorkerHelper.serializeXML(session, output, false, nodeid, nodeKey, revision2).call();
            }
            rtx.moveTo(Fixed.DOCUMENT_NODE_KEY.getStandardProperty());
            rtx.close();
            /*
				 * Shred modified restids from revision 1 to xml fragment Just
				 * modifications done by put and deletes
				 */
            rtx = session.beginNodeReadTrx(revision1);
            for (Long nodeKey : restIdsRev1New) {
                rtx.moveTo(nodeKey);
                WorkerHelper.serializeXML(session, output, false, nodeid, nodeKey, revision1).call();
            }
            if (wrap) {
                output.write(endResult.getBytes());
            }
            rtx.moveTo(Fixed.DOCUMENT_NODE_KEY.getStandardProperty());
        } catch (final Exception globExcep) {
            throw new JaxRxException(globExcep);
        } finally {
            WorkerHelper.closeRTX(rtx, session, database);
        }
    } else {
        throw new JaxRxException(400, "Bad user request");
    }
    return output;
}
Also used : LinkedList(java.util.LinkedList) JaxRxException(org.jaxrx.core.JaxRxException) SirixException(org.sirix.exception.SirixException) IOException(java.io.IOException) WebApplicationException(javax.ws.rs.WebApplicationException) StringTokenizer(java.util.StringTokenizer) NodeReadTrx(org.sirix.api.NodeReadTrx) XPathAxis(org.sirix.service.xml.xpath.XPathAxis) Database(org.sirix.api.Database) Axis(org.sirix.api.Axis) XPathAxis(org.sirix.service.xml.xpath.XPathAxis) SessionConfiguration(org.sirix.access.conf.SessionConfiguration) Session(org.sirix.api.Session) JaxRxException(org.jaxrx.core.JaxRxException)

Aggregations

XPathAxis (org.sirix.service.xml.xpath.XPathAxis)67 Test (org.junit.Test)62 Ignore (org.junit.Ignore)27 AbsAxisTest (org.sirix.axis.AbsAxisTest)18 SirixXPathException (org.sirix.exception.SirixXPathException)12 AbstractAxis (org.sirix.service.xml.xpath.AbstractAxis)12 Axis (org.sirix.api.Axis)6 IOException (java.io.IOException)3 WebApplicationException (javax.ws.rs.WebApplicationException)3 SessionConfiguration (org.sirix.access.conf.SessionConfiguration)3 Database (org.sirix.api.Database)3 NodeReadTrx (org.sirix.api.NodeReadTrx)3 Session (org.sirix.api.Session)3 SirixException (org.sirix.exception.SirixException)3 XPathError (org.sirix.service.xml.xpath.XPathError)3 XdmNodeReadTrx (org.sirix.api.XdmNodeReadTrx)2 LinkedList (java.util.LinkedList)1 StringTokenizer (java.util.StringTokenizer)1 QName (javax.xml.namespace.QName)1 XMLEvent (javax.xml.stream.events.XMLEvent)1