Index: src/org/openide/filesystems/FileUtil.java =================================================================== RCS file: /cvs/openide/src/org/openide/filesystems/FileUtil.java,v retrieving revision 1.75 diff -u -r1.75 FileUtil.java --- src/org/openide/filesystems/FileUtil.java 30 Jan 2004 10:10:43 -0000 1.75 +++ src/org/openide/filesystems/FileUtil.java 30 Jan 2004 12:46:55 -0000 @@ -272,6 +272,7 @@ * @param file File whose coresponding FileObjects will be looked for * @return corresponding FileObjects or empty array if no * corresponding FileObject exists. + * @see #findBestFileObject * @since 1.29 */ public static FileObject[] fromFile (File file) { @@ -316,6 +317,28 @@ return results; } + /** + * Finds the best FileObject for the given File. If there is multiple + * FileObjects available for the File (use {@link #fromFile} method + * to list them) the FileObject with minimal number of path components + * will be choosen. + * + * @param file File for which the FileObject will be searched + * @return the most suitable FileObject or null if there is not any + * @since X.XX + */ + public static FileObject findBestFileObject(File file) { + // XXX: consider replacing fromFile() call + // with more efficient algorithm + FileObject fos[] = fromFile(file); + FileObject shortest = fos.length > 0 ? fos[0] : null; + for (int i=1; i findURL(FileObject fo, int type) . * @param url to wanted FileObjects * @return a suitable arry of FileObjects, or empty array if not successful + * @see #findBestFileObject * @since 2.22*/ public static FileObject[] findFileObjects (URL url) { /** first basic implementation */ @@ -121,6 +122,29 @@ return retVal; } + /** + * Finds the best FileObject for the given URL. If there is multiple + * FileObjects available for the URL (use {@link #findFileObjects} method + * to list them) the FileObject with minimal number of path components + * will be choosen. + * + * @param url URL for which the FileObject will be searched + * @return the most suitable FileObject or null if there is not any + * @since X.XX + */ + public static FileObject findBestFileObject(URL url) { + // XXX: consider replacing findFileObjects() call + // with more efficient algorithm + FileObject fos[] = findFileObjects(url); + FileObject shortest = fos.length > 0 ? fos[0] : null; + for (int i=1; i URL for " + textPath, textU, URLMapper.findURL(textFO, URLMapper.EXTERNAL)); assertEquals("correct URL -> FO for root", Collections.singletonList(rootFO), Arrays.asList(URLMapper.findFileObjects(rootU))); assertEquals("correct URL -> FO for " + textPath, Collections.singletonList(textFO), Arrays.asList(URLMapper.findFileObjects(textU))); + } + + public void testFindBestFileObject() throws Exception { + // This tests creates three subfolders in working directory and a file + // in each subfolder. Then it mounts all subfolders (including the working + // dir) into repository as individual (overlapping) filesystems and + // tests that "best" fileobject is returned for misc URLs. + File workdir = getWorkDir(); + String path = workdir.getAbsolutePath() + "/folder1/folder2/folder3"; + File f = new File(path); + f.mkdirs(); + + path = workdir.getAbsolutePath() + "/folder1/file1.txt"; + f = new File(path); + f.createNewFile(); + path = workdir.getAbsolutePath() + "/folder1/folder2/file2.txt"; + f = new File(path); + f.createNewFile(); + path = workdir.getAbsolutePath() + "/folder1/folder2/folder3/file3.txt"; + f = new File(path); + f.createNewFile(); + + LocalFileSystem lfs = new LocalFileSystem(); + File folder = workdir; + lfs.setRootDirectory(folder); + Repository.getDefault().addFileSystem(lfs); + lfs = new LocalFileSystem(); + folder = new File(folder, "folder1"); + lfs.setRootDirectory(folder); + Repository.getDefault().addFileSystem(lfs); + lfs = new LocalFileSystem(); + folder = new File(folder, "folder2"); + lfs.setRootDirectory(folder); + Repository.getDefault().addFileSystem(lfs); + lfs = new LocalFileSystem(); + folder = new File(folder, "folder3"); + lfs.setRootDirectory(folder); + Repository.getDefault().addFileSystem(lfs); + + String base = workdir.toURI().toURL().toExternalForm(); + FileObject fos[] = URLMapper.findFileObjects(new URL(base+"/folder1/folder2/folder3/file3.txt")); + ArrayList list = new ArrayList(); + for (int i=0; i