Search in sources :

Example 16 with QNm

use of org.brackit.xquery.atomic.QNm in project sirix by sirixdb.

the class Store method execute.

@Override
public Sequence execute(final StaticContext sctx, final QueryContext ctx, final Sequence[] args) throws QueryException {
    try {
        final String collName = FunUtil.getString(args, 0, "collName", "collection", null, true);
        final Sequence nodes = args[2];
        if (nodes == null)
            throw new QueryException(new QNm("No sequence of nodes specified!"));
        final boolean createNew = args.length == 4 ? args[3].booleanValue() : true;
        final String resName = FunUtil.getString(args, 1, "resName", "resource", null, createNew ? false : true);
        final DBStore store = (DBStore) ctx.getStore();
        if (createNew) {
            create(store, collName, resName, nodes);
        } else {
            try {
                final DBCollection coll = (DBCollection) store.lookup(collName);
                add(store, coll, resName, nodes);
            } catch (DocumentException e) {
                // collection does not exist
                create(store, collName, resName, nodes);
            }
        }
        return null;
    } catch (final Exception e) {
        throw new QueryException(new QNm(e.getMessage()), e);
    }
}
Also used : QNm(org.brackit.xquery.atomic.QNm) DBCollection(org.sirix.xquery.node.DBCollection) QueryException(org.brackit.xquery.QueryException) DocumentException(org.brackit.xquery.xdm.DocumentException) Sequence(org.brackit.xquery.xdm.Sequence) DBStore(org.sirix.xquery.node.DBStore) QueryException(org.brackit.xquery.QueryException) DocumentException(org.brackit.xquery.xdm.DocumentException) IOException(java.io.IOException)

Example 17 with QNm

use of org.brackit.xquery.atomic.QNm in project sirix by sirixdb.

the class Rollback method execute.

@Override
public Sequence execute(StaticContext sctx, QueryContext ctx, Sequence[] args) throws QueryException {
    final DBNode doc = ((DBNode) args[0]);
    if (doc.getTrx() instanceof XdmNodeWriteTrx) {
        final XdmNodeWriteTrx wtx = (XdmNodeWriteTrx) doc.getTrx();
        final long revision = wtx.getRevisionNumber();
        wtx.rollback();
        return new Int64(revision);
    } else {
        throw new QueryException(new QNm("The transaction is not a write transaction!"));
    }
}
Also used : DBNode(org.sirix.xquery.node.DBNode) XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) QNm(org.brackit.xquery.atomic.QNm) QueryException(org.brackit.xquery.QueryException) Int64(org.brackit.xquery.atomic.Int64)

Example 18 with QNm

use of org.brackit.xquery.atomic.QNm in project sirix by sirixdb.

the class TestNodeWrapperXPath method test.

/**
 * XPath tests.
 *
 * @param expressions
 *          Expressions, which are used.
 * @param doc
 *          The test document.
 * @param expectedResults
 *          Expected result for each expression.
 * @param result
 *          Array with the result types for each expression.
 * @param xpathConstants
 *          XPathConstants for each expression.
 * @param namespaces
 *          Array of boolean values for each expression.
 * @param namespace
 *          The namespace Context, which is used.
 * @throws Exception
 *           Any Exception which maybe occurs.
 */
@SuppressWarnings("unchecked")
@Ignore("Not a test, utility method only")
public void test(final String[] expressions, final Object doc, final Object[] expectedResults, Object[] result, final QName[] xpathConstants, final boolean[] namespaces, final Object namespace) throws Exception {
    // For every expected result.
    for (int i = 0; i < expectedResults.length; i++) {
        // If namespace is required.
        if (namespaces[i]) {
            xpe.setNamespaceContext((NamespaceContext) namespace);
        }
        final XPathExpression findLine = xpe.compile(expressions[i]);
        // Cast the evaluated value.
        if (xpathConstants[i].equals(XPathConstants.NODESET)) {
            result[i] = findLine.evaluate(doc, xpathConstants[i]);
        } else if (xpathConstants[i].equals(XPathConstants.STRING)) {
            result[i] = (String) findLine.evaluate(doc, xpathConstants[i]);
        } else if (xpathConstants[i].equals(XPathConstants.NUMBER)) {
            result[i] = Double.parseDouble(findLine.evaluate(doc, xpathConstants[i]).toString());
        } else {
            throw new IllegalStateException("Unknown XPathConstant!");
        }
        assertNotNull(result);
        if (xpathConstants[i].equals(XPathConstants.NODESET)) {
            final ArrayList<NodeWrapper> test = (ArrayList<NodeWrapper>) result[i];
            final String res = (String) expectedResults[i];
            final String[] expRes = res.split(" ");
            // compare it.
            for (int j = 0; j < test.size(); j++) {
                final NodeWrapper item = test.get(j);
                mHolder.getRtx().moveTo(item.getKey());
                final QNm qName = mHolder.getRtx().getName();
                if (mHolder.getRtx().getKind() == Kind.ELEMENT) {
                    assertEquals(expRes[j], qName.getPrefix() + ":" + qName.getLocalName());
                } else if (mHolder.getRtx().getKind() == Kind.TEXT) {
                    assertEquals(expRes[j], mHolder.getRtx().getValue());
                }
            }
        } else {
            assertEquals(expectedResults[i], result[i]);
        }
    }
}
Also used : XPathExpression(javax.xml.xpath.XPathExpression) QNm(org.brackit.xquery.atomic.QNm) ArrayList(java.util.ArrayList) Ignore(org.junit.Ignore)

Example 19 with QNm

use of org.brackit.xquery.atomic.QNm in project sirix by sirixdb.

the class PathSummaryTest method testSetQNmSecond.

/**
 * Test setQNm on test document (finds a corresponding path summary after rename).
 *
 * @throws SirixException if Sirix fails
 */
@Test
public void testSetQNmSecond() throws SirixException {
    wtx.moveTo(9);
    wtx.setName(new QNm("d"));
    wtx.setName(new QNm("b"));
    PathSummaryReader pathSummary = wtx.getPathSummary();
    pathSummary.moveToDocumentRoot();
    testSetQNmSecondHelper(pathSummary);
    wtx.commit();
    wtx.close();
    pathSummary = holder.getResourceManager().openPathSummary();
    testSetQNmSecondHelper(pathSummary);
    pathSummary.close();
}
Also used : QNm(org.brackit.xquery.atomic.QNm) PathSummaryReader(org.sirix.index.path.summary.PathSummaryReader) Test(org.junit.Test)

Example 20 with QNm

use of org.brackit.xquery.atomic.QNm in project sirix by sirixdb.

the class PathSummaryTest method testSetQNmThird.

/**
 * Test setQNm on test document (finds no corresponding path summary after rename -- after
 * references dropped to 0).
 *
 * @throws SirixException if Sirix fails
 */
@Test
public void testSetQNmThird() throws SirixException {
    wtx.moveTo(9);
    wtx.setName(new QNm("d"));
    wtx.moveTo(5);
    wtx.setName(new QNm("t"));
    PathSummaryReader pathSummary = wtx.getPathSummary();
    pathSummary.moveToDocumentRoot();
    testSetQNmThirdHelper(pathSummary);
    wtx.commit();
    wtx.close();
    pathSummary = holder.getResourceManager().openPathSummary();
    testSetQNmThirdHelper(pathSummary);
    pathSummary.close();
}
Also used : QNm(org.brackit.xquery.atomic.QNm) PathSummaryReader(org.sirix.index.path.summary.PathSummaryReader) Test(org.junit.Test)

Aggregations

QNm (org.brackit.xquery.atomic.QNm)89 XdmNodeReadTrx (org.sirix.api.XdmNodeReadTrx)20 Test (org.junit.Test)18 QueryException (org.brackit.xquery.QueryException)15 XdmNodeWriteTrx (org.sirix.api.XdmNodeWriteTrx)15 DBNode (org.sirix.xquery.node.DBNode)12 QName (javax.xml.namespace.QName)11 PathSummaryReader (org.sirix.index.path.summary.PathSummaryReader)11 IndexController (org.sirix.access.IndexController)10 IndexDef (org.sirix.index.IndexDef)10 Item (org.brackit.xquery.xdm.Item)7 Axis (org.sirix.api.Axis)7 DescendantAxis (org.sirix.axis.DescendantAxis)7 SirixException (org.sirix.exception.SirixException)7 Attribute (javax.xml.stream.events.Attribute)5 Namespace (javax.xml.stream.events.Namespace)5 DocumentException (org.brackit.xquery.xdm.DocumentException)5 SirixIOException (org.sirix.exception.SirixIOException)5 Ignore (org.junit.Ignore)4 SirixUsageException (org.sirix.exception.SirixUsageException)4