Search in sources :

Example 16 with AnyURIValue

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

the class CollectionTest method doc_dynamicallyAvailableCollection_relativeUri.

@Test
public void doc_dynamicallyAvailableCollection_relativeUri() throws XPathException, EXistException, PermissionDeniedException, URISyntaxException {
    final BrokerPool pool = BrokerPool.getInstance();
    final String doc = "<timestamp>" + System.currentTimeMillis() + "</timestamp>";
    final String baseUri = "http://from-dynamic-context/";
    final String collectionRelativeUri = "collection1";
    final String query = "fn:collection('" + collectionRelativeUri + "')";
    try (final DBBroker broker = pool.getBroker()) {
        final XQueryContext context = new XQueryContext(pool);
        context.setBaseURI(new AnyURIValue(new URI(baseUri)));
        context.addDynamicallyAvailableCollection(baseUri + collectionRelativeUri, (broker2, transaction, uri) -> asInMemoryDocument(doc));
        final XQuery xqueryService = pool.getXQueryService();
        final CompiledXQuery compiled = xqueryService.compile(context, query);
        final Sequence result = xqueryService.execute(broker, compiled, null);
        assertFalse(result.isEmpty());
        assertEquals(1, result.getItemCount());
        assertTrue(result.itemAt(0) instanceof Node);
        final Source expectedSource = Input.fromString(doc).build();
        final Source actualSource = Input.fromNode((Node) result.itemAt(0)).build();
        final Diff diff = DiffBuilder.compare(expectedSource).withTest(actualSource).checkForIdentical().checkForSimilar().build();
        assertFalse(diff.toString(), diff.hasDifferences());
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) Diff(org.xmlunit.diff.Diff) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) AnyURIValue(org.exist.xquery.value.AnyURIValue) Node(org.w3c.dom.Node) XQueryContext(org.exist.xquery.XQueryContext) Sequence(org.exist.xquery.value.Sequence) URI(java.net.URI) BrokerPool(org.exist.storage.BrokerPool) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) Test(org.junit.Test)

Example 17 with AnyURIValue

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

the class GetPrincipalMetadataFunction method getPrincipalMetadataKeys.

private Sequence getPrincipalMetadataKeys(final Principal principal) throws XPathException {
    final Set<SchemaType> metadataKeys = principal.getMetadataKeys();
    final Sequence seq = new ValueSequence(metadataKeys.size());
    for (final SchemaType schemaType : metadataKeys) {
        seq.add(new AnyURIValue(schemaType.getNamespace()));
    }
    return seq;
}
Also used : AnyURIValue(org.exist.xquery.value.AnyURIValue) ValueSequence(org.exist.xquery.value.ValueSequence) ValueSequence(org.exist.xquery.value.ValueSequence) Sequence(org.exist.xquery.value.Sequence) AXSchemaType(org.exist.security.AXSchemaType) EXistSchemaType(org.exist.security.EXistSchemaType) SchemaType(org.exist.security.SchemaType)

Example 18 with AnyURIValue

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

the class XQueryTrigger method execute.

private void execute(boolean isBefore, DBBroker broker, Txn transaction, QName functionName, XmldbURI src, XmldbURI dst) throws TriggerException {
    final CompiledXQuery compiledQuery = getScript(isBefore, broker, transaction, src);
    if (compiledQuery == null) {
        return;
    }
    ProcessMonitor pm = null;
    final XQueryContext context = compiledQuery.getContext();
    // execute the XQuery
    try {
        int nParams = 1;
        if (dst != null)
            nParams = 2;
        final UserDefinedFunction function = context.resolveFunction(functionName, nParams);
        if (function != null) {
            final List<Expression> args = new ArrayList<>(nParams);
            if (isBefore) {
                args.add(new LiteralValue(context, new AnyURIValue(src)));
                if (dst != null)
                    args.add(new LiteralValue(context, new AnyURIValue(dst)));
            } else {
                if (dst != null)
                    args.add(new LiteralValue(context, new AnyURIValue(dst)));
                args.add(new LiteralValue(context, new AnyURIValue(src)));
            }
            pm = broker.getBrokerPool().getProcessMonitor();
            context.getProfiler().traceQueryStart();
            pm.queryStarted(context.getWatchDog());
            final FunctionCall call = new FunctionCall(context, function);
            call.setArguments(args);
            call.analyze(new AnalyzeContextInfo());
            final Sequence contextSequence;
            final ContextItemDeclaration cid = call.getContext().getContextItemDeclartion();
            if (cid != null) {
                contextSequence = cid.eval(null);
            } else {
                contextSequence = NodeSet.EMPTY_SET;
            }
            call.eval(contextSequence);
        }
    } catch (final XPathException e) {
        TriggerStatePerThread.setTriggerRunningState(TriggerStatePerThread.NO_TRIGGER_RUNNING, this, null);
        TriggerStatePerThread.setTransaction(null);
        throw new TriggerException(PREPARE_EXCEPTION_MESSAGE, e);
    } finally {
        if (pm != null) {
            context.getProfiler().traceQueryEnd(context);
            pm.queryCompleted(context.getWatchDog());
        }
        compiledQuery.reset();
        context.reset();
    }
    if (!isBefore) {
        TriggerStatePerThread.setTriggerRunningState(TriggerStatePerThread.NO_TRIGGER_RUNNING, this, null);
        TriggerStatePerThread.setTransaction(null);
        LOG.debug("Trigger fired 'after'");
    } else {
        LOG.debug("Trigger fired 'before'");
    }
}
Also used : AnyURIValue(org.exist.xquery.value.AnyURIValue) ArrayList(java.util.ArrayList) Sequence(org.exist.xquery.value.Sequence) ProcessMonitor(org.exist.storage.ProcessMonitor)

Example 19 with AnyURIValue

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

the class FunResolveURI method eval.

public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().start(this);
        context.getProfiler().message(this, Profiler.DEPENDENCIES, "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies()));
        if (contextSequence != null) {
            context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
        }
        if (contextItem != null) {
            context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());
        }
    }
    if (contextItem != null) {
        contextSequence = contextItem.toSequence();
    }
    AnyURIValue base;
    if (getArgumentCount() == 1) {
        if (!context.isBaseURIDeclared()) {
            throw new XPathException(this, ErrorCodes.FONS0005, "base URI of the static context has not been assigned a value.");
        }
        base = context.getBaseURI();
    } else {
        try {
            final Item item = getArgument(1).eval(contextSequence).itemAt(0).convertTo(Type.ANY_URI);
            base = (AnyURIValue) item;
        } catch (final XPathException e) {
            throw new XPathException(this, ErrorCodes.FORG0002, "invalid argument to fn:resolve-uri(): " + e.getMessage(), null, e);
        }
    }
    Sequence result;
    final Sequence seq = getArgument(0).eval(contextSequence);
    if (seq.isEmpty()) {
        result = Sequence.EMPTY_SEQUENCE;
    } else {
        AnyURIValue relative;
        try {
            final Item item = seq.itemAt(0).convertTo(Type.ANY_URI);
            relative = (AnyURIValue) item;
        } catch (final XPathException e) {
            throw new XPathException(this, ErrorCodes.FORG0002, "invalid argument to fn:resolve-uri(): " + e.getMessage(), seq, e);
        }
        URI relativeURI;
        URI baseURI;
        try {
            relativeURI = new URI(relative.getStringValue());
            baseURI = new URI(base.getStringValue());
        } catch (final URISyntaxException e) {
            throw new XPathException(this, ErrorCodes.FORG0009, "unable to resolve a relative URI against a base URI in fn:resolve-uri(): " + e.getMessage(), null, e);
        }
        if (relativeURI.isAbsolute()) {
            result = relative;
        } else {
            result = new AnyURIValue(baseURI.resolve(relativeURI));
        }
    }
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return result;
}
Also used : Item(org.exist.xquery.value.Item) XPathException(org.exist.xquery.XPathException) AnyURIValue(org.exist.xquery.value.AnyURIValue) Sequence(org.exist.xquery.value.Sequence) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 20 with AnyURIValue

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

the class QNameFunctions method eval.

/* (non-Javadoc)
	 * @see org.exist.xquery.BasicFunction#eval(org.exist.xquery.value.Sequence[], org.exist.xquery.value.Sequence)
	 */
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().start(this);
        context.getProfiler().message(this, Profiler.DEPENDENCIES, "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies()));
        if (contextSequence != null) {
            context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
        }
    }
    Sequence result;
    if (args[0].isEmpty()) {
        result = Sequence.EMPTY_SEQUENCE;
    } else {
        final QNameValue value = (QNameValue) args[0].itemAt(0);
        final QName qname = value.getQName();
        if (isCalledAs("prefix-from-QName")) {
            final String prefix = qname.getPrefix();
            if (prefix == null || prefix.isEmpty()) {
                result = Sequence.EMPTY_SEQUENCE;
            } else {
                result = new StringValue(prefix, Type.NCNAME);
            }
        } else if (isCalledAs("local-name-from-QName")) {
            result = new StringValue(qname.getLocalPart(), Type.NCNAME);
        } else {
            // fn:namespace-uri-from-QName
            String uri = qname.getNamespaceURI();
            if (uri == null) {
                uri = "";
            }
            result = new AnyURIValue(uri);
        }
    }
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return result;
}
Also used : QName(org.exist.dom.QName) QNameValue(org.exist.xquery.value.QNameValue) AnyURIValue(org.exist.xquery.value.AnyURIValue) Sequence(org.exist.xquery.value.Sequence) StringValue(org.exist.xquery.value.StringValue)

Aggregations

AnyURIValue (org.exist.xquery.value.AnyURIValue)27 Sequence (org.exist.xquery.value.Sequence)14 XPathException (org.exist.xquery.XPathException)12 BrokerPool (org.exist.storage.BrokerPool)9 DBBroker (org.exist.storage.DBBroker)9 URI (java.net.URI)8 XmldbURI (org.exist.xmldb.XmldbURI)8 XQueryContext (org.exist.xquery.XQueryContext)8 CompiledXQuery (org.exist.xquery.CompiledXQuery)7 XQuery (org.exist.xquery.XQuery)7 StringValue (org.exist.xquery.value.StringValue)7 IOException (java.io.IOException)6 URISyntaxException (java.net.URISyntaxException)6 PermissionDeniedException (org.exist.security.PermissionDeniedException)6 Source (org.exist.source.Source)4 MimeType (org.exist.util.MimeType)4 Resource (org.xmldb.api.base.Resource)4 XMLDBException (org.xmldb.api.base.XMLDBException)4 Path (java.nio.file.Path)3 DBSource (org.exist.source.DBSource)3