/** * Search for a resource in a local path, or the main repository path. * * @param path the resource name * @param loaders optional list of module loaders * @param localRoot a repository to look first * @return the resource * @throws IOException if an I/O error occurred */ public Resource findResource(String path, ModuleLoader[] loaders, Repository localRoot) throws IOException { // Note: as an extension to the CommonJS modules API // we allow absolute module paths for resources File file = new File(path); if (file.isAbsolute()) { Resource res; outer: if (loaders != null) { // loaders must contain at least one loader assert loaders.length > 0 && loaders[0] != null; for (ModuleLoader loader : loaders) { res = new FileResource(path + loader.getExtension()); if (res.exists()) { break outer; } } res = new FileResource(path + loaders[0].getExtension()); } else { res = new FileResource(file); } res.setAbsolute(true); return res; } else if (localRoot != null && (path.startsWith("./") || path.startsWith("../"))) { String newpath = localRoot.getRelativePath() + path; return findResource(newpath, loaders, null); } else { return config.getResource(normalizePath(path), loaders); } }
/** * Search for a repository in the local path, or the main repository path. * * @param path the repository name * @param localPath a repository to look first * @return the repository * @throws IOException if an I/O error occurred */ public Repository findRepository(String path, Repository localPath) throws IOException { // To be consistent, always return absolute repository if path is absolute // if we make this dependent on whether files exist we introduce a lot of // vague and undetermined behaviour. File file = new File(path); if (file.isAbsolute()) { return new FileRepository(file); } if (localPath != null) { Repository repository = localPath.getChildRepository(path); if (repository != null && repository.exists()) { return repository; } } return config.getRepository(normalizePath(path)); }