Search in sources :

Example 21 with Sequence

use of org.exist.xquery.value.Sequence in project exist by eXist-db.

the class XMLDBAbstractCollectionManipulator method eval.

@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
    if (0 == args.length) {
        throw new XPathException(this, "Expected a collection as the argument " + (paramNumber + 1) + ".");
    }
    final boolean collectionNeedsClose = false;
    Collection collection = null;
    final Item item = args[paramNumber].itemAt(0);
    if (Type.subTypeOf(item.getType(), Type.NODE)) {
        final NodeValue node = (NodeValue) item;
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Found node");
        }
        if (node.getImplementationType() == NodeValue.PERSISTENT_NODE) {
            final org.exist.collections.Collection internalCol = ((NodeProxy) node).getOwnerDocument().getCollection();
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Found node");
            }
            try {
                // TODO: use xmldbURI
                collection = getLocalCollection(context, internalCol.getURI().toString());
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("Loaded collection {}", collection.getName());
                }
            } catch (final XMLDBException e) {
                throw new XPathException(this, "Failed to access collection: " + internalCol.getURI(), e);
            }
        } else {
            return Sequence.EMPTY_SEQUENCE;
        }
    }
    if (collection == null) {
        // Otherwise, just extract the name as a string:
        final String collectionURI = args[paramNumber].getStringValue();
        if (collectionURI != null) {
            try {
                collection = getCollection(context, collectionURI, Optional.empty(), Optional.empty());
            } catch (final XMLDBException xe) {
                if (errorIfAbsent) {
                    throw new XPathException(this, "Could not locate collection: " + collectionURI, xe);
                }
                collection = null;
            }
        }
        if (collection == null && errorIfAbsent) {
            throw new XPathException(this, "Unable to find collection: " + collectionURI);
        }
    }
    Sequence s = Sequence.EMPTY_SEQUENCE;
    try {
        s = evalWithCollection(collection, args, contextSequence);
    } finally {
        if (collectionNeedsClose && collection != null) {
            try {
                collection.close();
            } catch (final Exception e) {
                throw new XPathException(this, "Unable to close collection", e);
            }
        }
    }
    return s;
}
Also used : Item(org.exist.xquery.value.Item) NodeValue(org.exist.xquery.value.NodeValue) XPathException(org.exist.xquery.XPathException) InTxnLocalCollection(org.exist.xmldb.txn.bridge.InTxnLocalCollection) LocalCollection(org.exist.xmldb.LocalCollection) Collection(org.xmldb.api.base.Collection) XMLDBException(org.xmldb.api.base.XMLDBException) Sequence(org.exist.xquery.value.Sequence) XMLDBException(org.xmldb.api.base.XMLDBException) XPathException(org.exist.xquery.XPathException)

Example 22 with Sequence

use of org.exist.xquery.value.Sequence in project exist by eXist-db.

the class XMLDBMatchCollection method eval.

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    Sequence result = Sequence.EMPTY_SEQUENCE;
    final String regexp = args[0].getStringValue();
    final List<String> collectionNames = context.getBroker().findCollectionsMatching(regexp);
    if (!collectionNames.isEmpty()) {
        result = copyListToValueSequence(collectionNames);
    }
    return result;
}
Also used : ValueSequence(org.exist.xquery.value.ValueSequence) Sequence(org.exist.xquery.value.Sequence)

Example 23 with Sequence

use of org.exist.xquery.value.Sequence in project exist by eXist-db.

the class SecurityManagerTest method removedGroupExists.

private boolean removedGroupExists(final DBBroker broker, final String groupName) throws XPathException, PermissionDeniedException {
    final XQuery queryService = broker.getBrokerPool().getXQueryService();
    final Sequence result = queryService.execute(broker, "declare namespace config='http://exist-db.org/Configuration'; collection('" + REMOVED_GROUPS_URI + "')//config:group[config:name eq '" + groupName + "']", null);
    return result.getItemCount() == 1 && result.itemAt(0).toJavaObject(Boolean.class) == true;
}
Also used : XQuery(org.exist.xquery.XQuery) Sequence(org.exist.xquery.value.Sequence)

Example 24 with Sequence

use of org.exist.xquery.value.Sequence in project exist by eXist-db.

the class XqueryApiTest method createCol.

@Override
protected void createCol(final String collectionName, final String uid, final String pwd) throws ApiException {
    final Sequence result = executeQuery(uid, pwd, "xmldb:create-collection('/db', '" + collectionName + "')");
    assertEquals("/db/" + collectionName, serialize(result));
}
Also used : Sequence(org.exist.xquery.value.Sequence)

Example 25 with Sequence

use of org.exist.xquery.value.Sequence in project exist by eXist-db.

the class FnCollectionSecurityTest method cannotAccessCollectionInCollectionHierarchyWithDeniedExecute.

@Test(expected = PermissionDeniedException.class)
public void cannotAccessCollectionInCollectionHierarchyWithDeniedExecute() throws EXistException, AuthenticationException, PermissionDeniedException, XPathException {
    // as docTestUser1 user
    final String query = "fn:collection('" + TEST_SUB_COLLECTION_1_1 + "')";
    final BrokerPool pool = server.getBrokerPool();
    final SecurityManager securityManager = pool.getSecurityManager();
    final Subject testUser1 = securityManager.authenticate(TEST_USER_1, TEST_USER_1);
    try (final DBBroker broker = pool.get(Optional.of(testUser1));
        final Txn transaction = pool.getTransactionManager().beginTransaction()) {
        final XQuery xqueryService = pool.getXQueryService();
        final Sequence result = xqueryService.execute(broker, query, null);
        fail("Expected PermissionDeniedException via XPathException");
        transaction.commit();
    } catch (final XPathException e) {
        if (e.getCause() != null && e.getCause() instanceof PermissionDeniedException) {
            throw (PermissionDeniedException) e.getCause();
        } else {
            throw e;
        }
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) XPathException(org.exist.xquery.XPathException) XQuery(org.exist.xquery.XQuery) Txn(org.exist.storage.txn.Txn) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool) Test(org.junit.Test)

Aggregations

Sequence (org.exist.xquery.value.Sequence)427 DBBroker (org.exist.storage.DBBroker)179 BrokerPool (org.exist.storage.BrokerPool)158 Test (org.junit.Test)114 XQuery (org.exist.xquery.XQuery)108 XPathException (org.exist.xquery.XPathException)86 Txn (org.exist.storage.txn.Txn)81 Item (org.exist.xquery.value.Item)68 ValueSequence (org.exist.xquery.value.ValueSequence)55 StringValue (org.exist.xquery.value.StringValue)49 Source (org.exist.source.Source)45 QName (org.exist.dom.QName)42 StringSource (org.exist.source.StringSource)42 SequenceIterator (org.exist.xquery.value.SequenceIterator)40 StringInputSource (org.exist.util.StringInputSource)37 CompiledXQuery (org.exist.xquery.CompiledXQuery)36 XQueryContext (org.exist.xquery.XQueryContext)35 IntegerValue (org.exist.xquery.value.IntegerValue)33 InputSource (org.xml.sax.InputSource)23 Diff (org.xmlunit.diff.Diff)21