Search in sources :

Example 6 with FileSource

use of org.exist.source.FileSource in project exist by eXist-db.

the class AbstractTestRunner method executeQuery.

protected static Sequence executeQuery(final BrokerPool brokerPool, final Source query, final List<Function<XQueryContext, Tuple2<String, Object>>> externalVariableBindings) throws EXistException, PermissionDeniedException, XPathException, IOException, DatabaseConfigurationException {
    final SecurityManager securityManager = requireNonNull(brokerPool.getSecurityManager(), "securityManager is null");
    try (final DBBroker broker = brokerPool.get(Optional.of(securityManager.getSystemSubject()))) {
        final XQueryPool queryPool = brokerPool.getXQueryPool();
        CompiledXQuery compiledQuery = queryPool.borrowCompiledXQuery(broker, query);
        try {
            XQueryContext context;
            if (compiledQuery == null) {
                context = new XQueryContext(broker.getBrokerPool());
            } else {
                context = compiledQuery.getContext();
                context.prepareForReuse();
            }
            // setup misc. context
            context.setBaseURI(new AnyURIValue("/db"));
            if (query instanceof FileSource) {
                final Path queryPath = Paths.get(((FileSource) query).getPath().toAbsolutePath().toString());
                if (Files.isDirectory(queryPath)) {
                    context.setModuleLoadPath(queryPath.toString());
                } else {
                    context.setModuleLoadPath(queryPath.getParent().toString());
                }
            }
            // declare variables for the query
            for (final Function<XQueryContext, Tuple2<String, Object>> externalVariableBinding : externalVariableBindings) {
                final Tuple2<String, Object> nameValue = externalVariableBinding.apply(context);
                context.declareVariable(nameValue._1, nameValue._2);
            }
            final XQuery xqueryService = brokerPool.getXQueryService();
            // compile or update the context
            if (compiledQuery == null) {
                compiledQuery = xqueryService.compile(context, query);
            } else {
                compiledQuery.getContext().updateContext(context);
                context.getWatchDog().reset();
            }
            return xqueryService.execute(broker, compiledQuery, null);
        } finally {
            queryPool.returnCompiledXQuery(query, compiledQuery);
        }
    }
}
Also used : Path(java.nio.file.Path) SecurityManager(org.exist.security.SecurityManager) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) AnyURIValue(org.exist.xquery.value.AnyURIValue) FileSource(org.exist.source.FileSource) XQueryContext(org.exist.xquery.XQueryContext) XQueryPool(org.exist.storage.XQueryPool) DBBroker(org.exist.storage.DBBroker) Tuple2(com.evolvedbinary.j8fu.tuple.Tuple2)

Example 7 with FileSource

use of org.exist.source.FileSource in project exist by eXist-db.

the class XQueryTestRunner method extractTestInfo.

private static XQueryTestInfo extractTestInfo(final Path path) throws InitializationError {
    try {
        final Configuration config = getConfiguration();
        final XQueryContext xqueryContext = new XQueryContext(config);
        final Source xquerySource = new FileSource(path, UTF_8, false);
        final XQuery xquery = new XQuery();
        final CompiledXQuery compiledXQuery = xquery.compile(xqueryContext, xquerySource);
        String moduleNsPrefix = null;
        String moduleNsUri = null;
        final List<XQueryTestInfo.TestFunctionDef> testFunctions = new ArrayList<>();
        final Iterator<UserDefinedFunction> localFunctions = compiledXQuery.getContext().localFunctions();
        while (localFunctions.hasNext()) {
            final UserDefinedFunction localFunction = localFunctions.next();
            final FunctionSignature localFunctionSignature = localFunction.getSignature();
            String testName = null;
            boolean isTest = false;
            final Annotation[] annotations = localFunctionSignature.getAnnotations();
            if (annotations != null) {
                for (final Annotation annotation : annotations) {
                    final QName annotationName = annotation.getName();
                    if (annotationName.getNamespaceURI().equals(XQSUITE_NAMESPACE)) {
                        if (annotationName.getLocalPart().startsWith("assert")) {
                            isTest = true;
                            if (testName != null) {
                                break;
                            }
                        } else if (annotationName.getLocalPart().equals("name")) {
                            final LiteralValue[] annotationValues = annotation.getValue();
                            if (annotationValues != null && annotationValues.length > 0) {
                                testName = annotationValues[0].getValue().getStringValue();
                                if (isTest) {
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            if (isTest) {
                if (testName == null) {
                    testName = localFunctionSignature.getName().getLocalPart();
                }
                if (moduleNsPrefix == null) {
                    moduleNsPrefix = localFunctionSignature.getName().getPrefix();
                }
                if (moduleNsUri == null) {
                    moduleNsUri = localFunctionSignature.getName().getNamespaceURI();
                }
                testFunctions.add(new XQueryTestInfo.TestFunctionDef(testName));
            }
        }
        return new XQueryTestInfo(moduleNsPrefix, moduleNsUri, testFunctions);
    } catch (final DatabaseConfigurationException | IOException | PermissionDeniedException | XPathException e) {
        throw new InitializationError(e);
    }
}
Also used : Configuration(org.exist.util.Configuration) InitializationError(org.junit.runners.model.InitializationError) ClassLoaderSource(org.exist.source.ClassLoaderSource) Source(org.exist.source.Source) FileSource(org.exist.source.FileSource) DatabaseConfigurationException(org.exist.util.DatabaseConfigurationException) QName(org.exist.dom.QName) FileSource(org.exist.source.FileSource) IOException(java.io.IOException) PermissionDeniedException(org.exist.security.PermissionDeniedException)

Example 8 with FileSource

use of org.exist.source.FileSource in project exist by eXist-db.

the class XQuery method execute.

public Sequence execute(final DBBroker broker, File file, Sequence contextSequence) throws XPathException, IOException, PermissionDeniedException {
    final XQueryContext context = new XQueryContext(broker.getBrokerPool());
    final CompiledXQuery compiled = compile(context, new FileSource(file.toPath(), true));
    return execute(broker, compiled, contextSequence);
}
Also used : FileSource(org.exist.source.FileSource)

Aggregations

FileSource (org.exist.source.FileSource)8 Path (java.nio.file.Path)4 PermissionDeniedException (org.exist.security.PermissionDeniedException)4 IOException (java.io.IOException)3 Source (org.exist.source.Source)3 XmldbURI (org.exist.xmldb.XmldbURI)2 Tuple2 (com.evolvedbinary.j8fu.tuple.Tuple2)1 StringWriter (java.io.StringWriter)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Properties (java.util.Properties)1 RequestDispatcher (javax.servlet.RequestDispatcher)1 ServletException (javax.servlet.ServletException)1 EXistException (org.exist.EXistException)1 QName (org.exist.dom.QName)1 DocumentSet (org.exist.dom.persistent.DocumentSet)1 SecurityManager (org.exist.security.SecurityManager)1 ClassLoaderSource (org.exist.source.ClassLoaderSource)1 DBSource (org.exist.source.DBSource)1 StringSource (org.exist.source.StringSource)1