public void applyModuleImport(Import mImport) throws XPathException {
        boolean foundOne = false;
        List existingModules = null;
        // resolve the location URIs against the base URI
        for (int i=0; i<mImport.locationURIs.size(); i++) {
            try {
                String uri = (String)mImport.locationURIs.get(i);
                URI abs = ResolveURI.makeAbsolute(uri, env.getBaseURI());
                mImport.locationURIs.set(i, abs);
            } catch (URISyntaxException e) {
                grumble("Invalid URI " + mImport.locationURIs.get(i) + ": " + e.getMessage());
            }
        }

        // See if the URI is that of a separately-compiled query library
        QueryLibrary lib = ((QueryModule)env).getUserQueryContext().getCompiledLibrary(mImport.namespaceURI);
        if (lib != null) {
            executable.addQueryLibraryModule(lib);
            foundOne = true;
            existingModules = new ArrayList<QueryModule>();
            existingModules.add(lib);
            lib.link(((QueryModule)env));

        } else {

            // If any of the modules are already loaded, don't re-read them; but do check that none of them
            // references the current module namespace directly or indirectly
            existingModules = executable.getQueryLibraryModules(mImport.namespaceURI);
            if (existingModules != null) {
                //System.err.println("Number of existing modules: " + existingModules.size());
                for (int m = 0; m < existingModules.size(); m++) {
                    QueryModule importedModule = (QueryModule)existingModules.get(m);
                    //System.err.println("Existing module location URI =  " + importedModule.getLocationURI());
                    if (importedModule.getLocationURI()!=null &&
                            !importedModule.getLocationURI().equals(((QueryModule)env).getLocationURI())) {
                        //QueryReader.importModuleContents(importedModule, (QueryModule)env);
                        foundOne = true;
                    }
//                    if (importedModule instanceof QueryLibrary) {
//                        // found a separately-compiled library module
//                        foundOne = true;
//                        ((QueryLibrary)importedModule).link(((QueryModule)env));
//                    }
    //                if (!disableCycleChecks &&
    //                        ((QueryModule)env).getModuleNamespace() != null &&
    //                        !((QueryModule)env).getModuleNamespace().equals(importedModule.getModuleNamespace()) &&
    //                        importedModule.importsNamespaceIndirectly(((QueryModule)env).getModuleNamespace())) {
    //                    grumble("A cycle exists among the module imports, involving namespaces " +
    //                            ((QueryModule)env).getModuleNamespace() + " and " +
    //                            importedModule.getModuleNamespace());
    //                }
                    for (int h = mImport.locationURIs.size() - 1; h >= 0; h--) {
                        if (mImport.locationURIs.get(h).equals(importedModule.getLocationURI())) {
                            mImport.locationURIs.remove(h);
                        }
                    }
                }
            }
        }

        // If we've found at least one module, and there are no location URIs left, call it a day.

        if (mImport.locationURIs.isEmpty() && foundOne) {
            return;
        }

        // Call the module URI resolver to find the remaining modules

        ModuleURIResolver resolver = ((QueryModule)env).getUserQueryContext().getModuleURIResolver();

        String[] hints = new String[mImport.locationURIs.size()];
        for (int h=0; h<hints.length; h++) {
            hints[h] = mImport.locationURIs.get(h).toString();
        }
        StreamSource[] sources = null;
        if (resolver != null) {
            try {
                sources = resolver.resolve(mImport.namespaceURI, env.getBaseURI(), hints);
            } catch (XPathException err) {
                grumble("Failed to resolve URI of imported module: " + err.getMessage());
            }
        }
        if (sources == null) {
            if (hints.length == 0) {
                if (existingModules == null) {
                    grumble("Cannot locate module for namespace " + mImport.namespaceURI, "XQST0059");
                }
            }
            resolver = env.getConfiguration().getStandardModuleURIResolver();
            sources = resolver.resolve(mImport.namespaceURI, env.getBaseURI(), hints);
        }

        for (int m = 0; m < sources.length; m++) {
            StreamSource ss = sources[m];
            String baseURI = ss.getSystemId();
            if (baseURI == null) {
                if (m < hints.length) {
                    baseURI = hints[m];  
                    ss.setSystemId(hints[m]);
                } else {
                    grumble("No base URI available for imported module", "XQST0059");
                }
            }
            // Although the module hadn't been loaded when we started, it might have been loaded since, as
            // a result of a reference from another imported module. Note, we are careful here to use URI.equals()
            // rather that String.equals() to compare URIs, as this gives a slightly more intelligent comparison,
            // for example the scheme name is case-independent, and file:///x/y/z matches file:/x/y/z.
            // TODO: use similar logic when loading schema modules 
            existingModules = executable.getQueryLibraryModules(mImport.namespaceURI);
            boolean loaded = false;
            if (existingModules != null && m < hints.length) {
                for (int e=0; e<existingModules.size(); e++) {
                    if (((QueryModule)existingModules.get(e)).getLocationURI().equals(mImport.locationURIs.get(m))) {
                        loaded = true;
                        break;
                    }
                }
            }
            if (loaded) {
                break;
            }

            try {
                String queryText = QueryReader.readSourceQuery(ss, nameChecker);
                try {
                    if (ss.getInputStream() != null) {
                        ss.getInputStream().close();
                    } else if (ss.getReader() != null) {
                        ss.getReader().close();
                    }
                } catch (IOException e) {
                    throw new XPathException("Failure while closing file for imported query module");
                }
                QueryModule.makeQueryModule(
                        baseURI, executable, (QueryModule)env, queryText, mImport.namespaceURI,
                        disableCycleChecks);
            } catch (XPathException err) {
                err.maybeSetLocation(makeLocator());
                reportError(err);
            }
        }
    }