Search in sources :

Example 1 with Module

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

the class PrologFunctions method importModule.

private void importModule(Sequence[] args) throws XPathException {
    context.saveState();
    final String uri = args[0].getStringValue();
    final String prefix = args[1].getStringValue();
    final Sequence seq = args[2];
    final AnyURIValue[] locationHints = new AnyURIValue[seq.getItemCount()];
    for (int i = 0; i < locationHints.length; i++) {
        locationHints[i] = (AnyURIValue) seq.itemAt(i);
    }
    final Module[] modules = context.importModule(uri, prefix, locationHints);
    context.getRootContext().resolveForwardReferences();
    for (final Module module : modules) {
        if (!module.isInternalModule()) {
            // ensure variable declarations in the imported module are analyzed.
            // unlike when using a normal import statement, this is not done automatically
            ((ExternalModule) module).analyzeGlobalVars();
        }
    }
// context.getRootContext().analyzeAndOptimizeIfModulesChanged((PathExpr) context.getRootExpression());
}
Also used : Module(org.exist.xquery.Module)

Example 2 with Module

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

the class ModuleInfo method eval.

/* (non-Javadoc)
	 * @see org.exist.xquery.BasicFunction#eval(org.exist.xquery.value.Sequence[], org.exist.xquery.value.Sequence)
	 */
@SuppressWarnings("unchecked")
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    if ("get-module-description".equals(getSignature().getName().getLocalPart())) {
        final String uri = args[0].getStringValue();
        final Module[] modules = context.getModules(uri);
        if (isEmpty(modules)) {
            throw new XPathException(this, "No module found matching namespace URI: " + uri);
        }
        final Sequence result = new ValueSequence();
        for (final Module module : modules) {
            result.add(new StringValue(module.getDescription()));
        }
        return result;
    } else if ("is-module-registered".equals(getSignature().getName().getLocalPart())) {
        final String uri = args[0].getStringValue();
        final Module[] modules = context.getModules(uri);
        return new BooleanValue(modules != null && modules.length > 0);
    } else if ("mapped-modules".equals(getSignature().getName().getLocalPart())) {
        final ValueSequence resultSeq = new ValueSequence();
        for (final Iterator<String> i = context.getMappedModuleURIs(); i.hasNext(); ) {
            resultSeq.add(new StringValue(i.next()));
        }
        return resultSeq;
    } else if ("is-module-mapped".equals(getSignature().getName().getLocalPart())) {
        final String uri = args[0].getStringValue();
        return new BooleanValue(((Map<String, String>) context.getBroker().getConfiguration().getProperty(XQueryContext.PROPERTY_STATIC_MODULE_MAP)).get(uri) != null);
    } else if ("map-module".equals(getSignature().getName().getLocalPart())) {
        if (!context.getSubject().hasDbaRole()) {
            final XPathException xPathException = new XPathException(this, "Permission denied, calling user '" + context.getSubject().getName() + "' must be a DBA to call this function.");
            logger.error("Invalid user", xPathException);
            throw xPathException;
        }
        final String namespace = args[0].getStringValue();
        final String location = args[1].getStringValue();
        final Map<String, String> moduleMap = (Map<String, String>) context.getBroker().getConfiguration().getProperty(XQueryContext.PROPERTY_STATIC_MODULE_MAP);
        moduleMap.put(namespace, location);
        return Sequence.EMPTY_SEQUENCE;
    } else if ("unmap-module".equals(getSignature().getName().getLocalPart())) {
        if (!context.getSubject().hasDbaRole()) {
            final XPathException xPathException = new XPathException(this, "Permission denied, calling user '" + context.getSubject().getName() + "' must be a DBA to call this function.");
            logger.error("Invalid user", xPathException);
            throw xPathException;
        }
        final String namespace = args[0].getStringValue();
        final Map<String, String> moduleMap = (Map<String, String>) context.getBroker().getConfiguration().getProperty(XQueryContext.PROPERTY_STATIC_MODULE_MAP);
        moduleMap.remove(namespace);
        return Sequence.EMPTY_SEQUENCE;
    } else if ("get-module-info".equals(getSignature().getName().getLocalPart())) {
        context.pushDocumentContext();
        try {
            final MemTreeBuilder builder = context.getDocumentBuilder();
            builder.startElement(MODULES_QNAME, null);
            if (getArgumentCount() == 1) {
                final Module[] modules = context.getModules(args[0].getStringValue());
                if (modules != null) {
                    outputModules(builder, modules);
                }
            } else {
                for (final Iterator<Module> i = context.getRootModules(); i.hasNext(); ) {
                    final Module module = i.next();
                    outputModule(builder, module);
                }
            }
            return builder.getDocument().getNode(1);
        } finally {
            context.popDocumentContext();
        }
    } else {
        final ValueSequence resultSeq = new ValueSequence();
        final XQueryContext tempContext = new XQueryContext(context.getBroker().getBrokerPool());
        for (final Iterator<Module> i = tempContext.getRootModules(); i.hasNext(); ) {
            final Module module = i.next();
            resultSeq.add(new StringValue(module.getNamespaceURI()));
        }
        if (tempContext.getRepository().isPresent()) {
            for (final URI uri : tempContext.getRepository().get().getJavaModules()) {
                resultSeq.add(new StringValue(uri.toString()));
            }
        }
        return resultSeq;
    }
}
Also used : XPathException(org.exist.xquery.XPathException) XQueryContext(org.exist.xquery.XQueryContext) ValueSequence(org.exist.xquery.value.ValueSequence) Sequence(org.exist.xquery.value.Sequence) URI(java.net.URI) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) BooleanValue(org.exist.xquery.value.BooleanValue) ValueSequence(org.exist.xquery.value.ValueSequence) Module(org.exist.xquery.Module) ExternalModule(org.exist.xquery.ExternalModule) StringValue(org.exist.xquery.value.StringValue) Map(java.util.Map)

Example 3 with Module

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

the class XQueryInspector method getDependencies.

private static void getDependencies(final XQueryContext xqyCtx, final Map<String, Set<String>> dependencies) {
    final String xqueryUri = getDbUri(xqyCtx.getSource());
    Set<String> depSet = dependencies.get(xqueryUri);
    if (depSet == null) {
        final Iterator<Module> itModule = xqyCtx.getModules();
        while (itModule.hasNext()) {
            final Module module = itModule.next();
            if (module instanceof ExternalModule) {
                final ExternalModule extModule = (ExternalModule) module;
                final Source source = extModule.getSource();
                if (source instanceof DBSource) {
                    final String moduleUri = getDbUri(source);
                    if (depSet == null) {
                        depSet = new HashSet<>();
                    }
                    depSet.add(moduleUri);
                    /*
                         * must merge map here as recursive function
                         * can cause problems with recursive
                         * module imports m1 -> m2 -> m2 -> m1
                         */
                    dependencies.put(xqueryUri, depSet);
                }
                getDependencies(extModule.getContext(), dependencies);
            }
        }
    }
}
Also used : DBSource(org.exist.source.DBSource) Module(org.exist.xquery.Module) ExternalModule(org.exist.xquery.ExternalModule) ExternalModule(org.exist.xquery.ExternalModule) Source(org.exist.source.Source) DBSource(org.exist.source.DBSource)

Example 4 with Module

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

the class ProcessMonitor method getRequestURI.

/**
 * Try to figure out the HTTP request URI by which a query was called.
 * Request tracking is not enabled unless {@link #setTrackRequestURI(boolean)}
 * is called.
 *
 * @param watchdog XQuery WatchDog
 * @return HTTP request URI by which a query was called
 */
public static String getRequestURI(final XQueryWatchDog watchdog) {
    final Module[] modules = watchdog.getContext().getModules(RequestModule.NAMESPACE_URI);
    if (isEmpty(modules)) {
        return null;
    }
    final Optional<RequestWrapper> maybeRequest = Optional.ofNullable(watchdog.getContext()).map(XQueryContext::getHttpContext).map(XQueryContext.HttpContext::getRequest);
    if (!maybeRequest.isPresent()) {
        return null;
    }
    final RequestWrapper request = maybeRequest.get();
    final Object attr = request.getAttribute(XQueryURLRewrite.RQ_ATTR_REQUEST_URI);
    String uri;
    if (attr == null) {
        uri = request.getRequestURI();
    } else {
        uri = attr.toString();
    }
    String queryString = request.getQueryString();
    if (queryString != null) {
        uri += "?" + queryString;
    }
    return uri;
}
Also used : RequestWrapper(org.exist.http.servlets.RequestWrapper) Module(org.exist.xquery.Module) RequestModule(org.exist.xquery.functions.request.RequestModule)

Example 5 with Module

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

the class FunOnFunctions method lookupFunction.

public static FunctionCall lookupFunction(final Expression parent, final QName qname, final int arity) {
    // check if the function is from a module
    final Module[] modules = parent.getContext().getModules(qname.getNamespaceURI());
    try {
        UserDefinedFunction func = null;
        if (modules == null || modules.length == 0) {
            func = parent.getContext().resolveFunction(qname, arity);
        } else {
            for (final Module module : modules) {
                func = ((ExternalModule) module).getFunction(qname, arity, parent.getContext());
                if (func != null) {
                    if (module.isInternalModule()) {
                        throw new XPathException(parent, ErrorCodes.XPST0017, "Cannot create a reference to an internal Java function");
                    }
                    break;
                }
            }
        }
        if (func == null) {
            return null;
        }
        final FunctionCall funcCall = new FunctionCall(parent.getContext(), func);
        funcCall.setLocation(parent.getLine(), parent.getColumn());
        return funcCall;
    } catch (final XPathException e) {
        // function not found: return empty sequence
        return null;
    }
}
Also used : Module(org.exist.xquery.Module)

Aggregations

Module (org.exist.xquery.Module)12 QName (org.exist.dom.QName)5 MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)3 URI (java.net.URI)2 Map (java.util.Map)2 Source (org.exist.source.Source)2 ExternalModule (org.exist.xquery.ExternalModule)2 XPathException (org.exist.xquery.XPathException)2 AttributesImpl (org.xml.sax.helpers.AttributesImpl)2 ConsumerE (com.evolvedbinary.j8fu.function.ConsumerE)1 IEntry (io.lacuna.bifurcan.IEntry)1 IMap (io.lacuna.bifurcan.IMap)1 Map (io.lacuna.bifurcan.Map)1 Maps (io.lacuna.bifurcan.Maps)1 IOException (java.io.IOException)1 java.util (java.util)1 TreeSet (java.util.TreeSet)1 DocumentImpl (org.exist.dom.memtree.DocumentImpl)1 RequestWrapper (org.exist.http.servlets.RequestWrapper)1 PermissionDeniedException (org.exist.security.PermissionDeniedException)1