Search in sources :

Example 1 with UserDefinedFunction

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

the class XQueryInspector method findServices.

public static List<RestXqService> findServices(final CompiledXQuery compiled) throws ExQueryException {
    final List<RestXqService> services = new ArrayList<>();
    try {
        // look at each function
        final Iterator<UserDefinedFunction> itFunctions = compiled.getContext().localFunctions();
        final Set<URI> xqueryLocations = new HashSet<>();
        while (itFunctions.hasNext()) {
            final UserDefinedFunction function = itFunctions.next();
            final Annotation[] annotations = function.getSignature().getAnnotations();
            Set<org.exquery.xquery3.Annotation> functionRestAnnotations = null;
            // process the function annotations
            for (final Annotation annotation : annotations) {
                if (RestAnnotationFactory.isRestXqAnnotation(annotation.getName().toJavaQName())) {
                    final org.exquery.xquery3.Annotation restAnnotation = RestAnnotationFactory.getAnnotation(new AnnotationAdapter(annotation));
                    if (functionRestAnnotations == null) {
                        functionRestAnnotations = new HashSet<>();
                    }
                    functionRestAnnotations.add(restAnnotation);
                }
            }
            if (functionRestAnnotations != null) {
                final ResourceFunction resourceFunction = ResourceFunctionFactory.create(new URI(compiled.getSource().path()), functionRestAnnotations);
                final RestXqService service = new RestXqServiceImpl(resourceFunction, compiled.getContext().getBroker().getBrokerPool());
                // record the xquerylocation
                xqueryLocations.add(resourceFunction.getXQueryLocation());
                // add the service to the list of services for this query
                services.add(service);
            }
        }
        for (final URI xqueryLocation : xqueryLocations) {
            // add service location and compiled query to the cache
            RestXqServiceCompiledXQueryCacheImpl.getInstance().returnCompiledQuery(xqueryLocation, compiled);
        }
    } catch (final URISyntaxException | AnnotationException use) {
        throw new ExQueryException(use.getMessage(), use);
    }
    return services;
}
Also used : AnnotationAdapter(org.exist.extensions.exquery.restxq.impl.adapters.AnnotationAdapter) UserDefinedFunction(org.exist.xquery.UserDefinedFunction) ExQueryException(org.exquery.ExQueryException) ArrayList(java.util.ArrayList) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) Annotation(org.exist.xquery.Annotation) RestXqService(org.exquery.restxq.RestXqService) ResourceFunction(org.exquery.restxq.ResourceFunction) AnnotationException(org.exquery.annotation.AnnotationException) HashSet(java.util.HashSet)

Aggregations

URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 AnnotationAdapter (org.exist.extensions.exquery.restxq.impl.adapters.AnnotationAdapter)1 Annotation (org.exist.xquery.Annotation)1 UserDefinedFunction (org.exist.xquery.UserDefinedFunction)1 ExQueryException (org.exquery.ExQueryException)1 AnnotationException (org.exquery.annotation.AnnotationException)1 ResourceFunction (org.exquery.restxq.ResourceFunction)1 RestXqService (org.exquery.restxq.RestXqService)1