Search in sources :

Example 6 with AnyURIValue

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

the class FunNamespaceURI method eval.

@Override
public Sequence eval(Sequence contextSequence, final 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();
    }
    // If we have one argument, we take it into account
    final Sequence seq;
    if (getSignature().getArgumentCount() > 0) {
        seq = getArgument(0).eval(contextSequence, contextItem);
    } else {
        // Otherwise, we take the context sequence and we iterate over it
        seq = contextSequence;
    }
    if (seq == null) {
        throw new XPathException(this, ErrorCodes.XPDY0002, "Undefined context item");
    }
    final Sequence result;
    if (seq.isEmpty()) {
        result = AnyURIValue.EMPTY_URI;
    } else {
        final Item item = seq.itemAt(0);
        if (!Type.subTypeOf(item.getType(), Type.NODE)) {
            throw new XPathException(this, ErrorCodes.XPTY0004, "Context item is not a node; got: " + Type.getTypeName(item.getType()));
        }
        // TODO : how to improve performance ?
        final Node n = ((NodeValue) item).getNode();
        String ns = n.getNamespaceURI();
        if (ns == null) {
            ns = XMLConstants.NULL_NS_URI;
        }
        result = new AnyURIValue(ns);
    }
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return result;
}
Also used : Item(org.exist.xquery.value.Item) NodeValue(org.exist.xquery.value.NodeValue) XPathException(org.exist.xquery.XPathException) Node(org.w3c.dom.Node) AnyURIValue(org.exist.xquery.value.AnyURIValue) Sequence(org.exist.xquery.value.Sequence)

Example 7 with AnyURIValue

use of org.exist.xquery.value.AnyURIValue 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 8 with AnyURIValue

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

the class XQueryTestRunner method run.

@Override
public void run(final RunNotifier notifier) {
    try {
        final String pkgName = getClass().getPackage().getName().replace('.', '/');
        final Source query = new ClassLoaderSource(pkgName + "/xquery-test-runner.xq");
        final URI testModuleUri = path.toAbsolutePath().toUri();
        final String suiteName = getSuiteName();
        final List<java.util.function.Function<XQueryContext, Tuple2<String, Object>>> externalVariableDeclarations = Arrays.asList(context -> new Tuple2<>("test-module-uri", new AnyURIValue(testModuleUri)), // set callback functions for notifying junit!
        context -> new Tuple2<>("test-ignored-function", new FunctionReference(new FunctionCall(context, new ExtTestIgnoredFunction(context, suiteName, notifier)))), context -> new Tuple2<>("test-started-function", new FunctionReference(new FunctionCall(context, new ExtTestStartedFunction(context, suiteName, notifier)))), context -> new Tuple2<>("test-failure-function", new FunctionReference(new FunctionCall(context, new ExtTestFailureFunction(context, suiteName, notifier)))), context -> new Tuple2<>("test-assumption-failed-function", new FunctionReference(new FunctionCall(context, new ExtTestAssumptionFailedFunction(context, suiteName, notifier)))), context -> new Tuple2<>("test-error-function", new FunctionReference(new FunctionCall(context, new ExtTestErrorFunction(context, suiteName, notifier)))), context -> new Tuple2<>("test-finished-function", new FunctionReference(new FunctionCall(context, new ExtTestFinishedFunction(context, suiteName, notifier)))));
        // NOTE: at this stage EXIST_EMBEDDED_SERVER_CLASS_INSTANCE in XSuite will be usable
        final BrokerPool brokerPool = XSuite.EXIST_EMBEDDED_SERVER_CLASS_INSTANCE.getBrokerPool();
        executeQuery(brokerPool, query, externalVariableDeclarations);
    } catch (final DatabaseConfigurationException | IOException | EXistException | PermissionDeniedException | XPathException e) {
        // TODO(AR) what to do here?
        throw new RuntimeException(e);
    }
}
Also used : URI(java.net.URI) ClassLoaderSource(org.exist.source.ClassLoaderSource) Source(org.exist.source.Source) FileSource(org.exist.source.FileSource) DatabaseConfigurationException(org.exist.util.DatabaseConfigurationException) FunctionReference(org.exist.xquery.value.FunctionReference) ClassLoaderSource(org.exist.source.ClassLoaderSource) AnyURIValue(org.exist.xquery.value.AnyURIValue) IOException(java.io.IOException) EXistException(org.exist.EXistException) java.util(java.util) PermissionDeniedException(org.exist.security.PermissionDeniedException) BrokerPool(org.exist.storage.BrokerPool)

Example 9 with AnyURIValue

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

the class DocUtils method getFromDynamicallyAvailableDocuments.

@Nullable
private static Sequence getFromDynamicallyAvailableDocuments(final XQueryContext context, final String path) throws XPathException {
    try {
        URI uri = new URI(path);
        if (!uri.isAbsolute()) {
            final AnyURIValue baseXdmUri = context.getBaseURI();
            if (baseXdmUri != null && !baseXdmUri.equals(AnyURIValue.EMPTY_URI)) {
                URI baseUri = baseXdmUri.toURI();
                if (!baseUri.toString().endsWith("/")) {
                    baseUri = new URI(baseUri.toString() + '/');
                }
                uri = baseUri.resolve(uri);
            }
        }
        return context.getDynamicallyAvailableDocument(uri.toString());
    } catch (final URISyntaxException e) {
        throw new XPathException(context.getRootExpression(), ErrorCodes.FODC0005, e);
    }
}
Also used : XPathException(org.exist.xquery.XPathException) AnyURIValue(org.exist.xquery.value.AnyURIValue) XmldbURI(org.exist.xmldb.XmldbURI) Nullable(javax.annotation.Nullable)

Example 10 with AnyURIValue

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

the class ZipEntryFunctions method eval.

@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
    final XmldbURI uri = ((AnyURIValue) args[0].itemAt(0)).toXmldbURI();
    final String entryName = args[1].itemAt(0).getStringValue();
    final ZipFileSource zipFileSource = new ZipFileFromDb(uri);
    ZipInputStream zis = null;
    boolean mustClose = true;
    Sequence result = Sequence.EMPTY_SEQUENCE;
    try {
        zis = zipFileSource.getStream(context.getBroker());
        ZipEntry zipEntry;
        while ((zipEntry = zis.getNextEntry()) != null) {
            try {
                if (zipEntry.getName().equals(entryName)) {
                    // process
                    if (isCalledAs(BINARY_ENTRY_NAME)) {
                        result = extractBinaryEntry(zis);
                        mustClose = false;
                    } else if (isCalledAs(HTML_ENTRY_NAME)) {
                        result = extractHtmlEntry(zis);
                    } else if (isCalledAs(TEXT_ENTRY_NAME)) {
                        result = extractStringEntry(zis);
                    } else if (isCalledAs(XML_ENTRY_NAME)) {
                        result = extractXmlEntry(zis);
                    }
                    break;
                }
            } finally {
            // DONT need to close as the extract functions
            // close the stream on the zip entry
            /*if(mustClose) {
                        zis.closeEntry();
                    }*/
            }
        }
    } catch (final IOException | PermissionDeniedException ioe) {
        LOG.error(ioe.getMessage(), ioe);
        throw new XPathException(this, ioe.getMessage(), ioe);
    } finally {
        if (zis != null && mustClose) {
            try {
                zis.close();
            } catch (final IOException ioe) {
                LOG.warn(ioe.getMessage(), ioe);
            }
        }
        zipFileSource.close();
    }
    return result;
}
Also used : XPathException(org.exist.xquery.XPathException) AnyURIValue(org.exist.xquery.value.AnyURIValue) ZipEntry(java.util.zip.ZipEntry) Sequence(org.exist.xquery.value.Sequence) IOException(java.io.IOException) ZipInputStream(java.util.zip.ZipInputStream) PermissionDeniedException(org.exist.security.PermissionDeniedException) XmldbURI(org.exist.xmldb.XmldbURI)

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