diff --git a/openide.filesystems/apichanges.xml b/openide.filesystems/apichanges.xml --- a/openide.filesystems/apichanges.xml +++ b/openide.filesystems/apichanges.xml @@ -46,6 +46,24 @@ Filesystems API + + + Added FileUtil.getConfigFile and getConfigRoot for simpler access to default filesystem + + + + + +

+ Rather than having to call + Respository.getDefault().getDefaultFileSystem().getRoot().getFileObject("foo/bar"), + you can simply call + FileUtil.getConfigFile("foo/bar"). +

+
+ + +
FileChooserBuilder added diff --git a/openide.filesystems/src/org/openide/filesystems/FileUtil.java b/openide.filesystems/src/org/openide/filesystems/FileUtil.java --- a/openide.filesystems/src/org/openide/filesystems/FileUtil.java +++ b/openide.filesystems/src/org/openide/filesystems/FileUtil.java @@ -1771,6 +1771,32 @@ return Ordering.affectsOrder(event); } + /** + * Returns {@code FileObject} from the NetBeans default (system, configuration) + * filesystems or {@code null} if does not exist. + * @param path the path from the root of the NetBeans default (system, configuration) + * filesystem delimited by '/' or empty string to get root folder. + * @throws NullPointerException if the path is {@code null} + * @return a {@code FileObject} for given path in the NetBeans default (system, configuration) + * filesystem or {@code null} if does not exist + * @since org.openide.filesystems 7.18 + */ + public static FileObject getConfigFile(String path) { + Parameters.notNull("path", path); //NOI18N + return Repository.getDefault().getDefaultFileSystem().findResource(path); + } + + /** + * Returns the root of the NetBeans default (system, configuration) + * filesystems. + * @return a {@code FileObject} for the root of the NetBeans default (system, configuration) + * filesystem + * @since org.openide.filesystems 7.18 + */ + public static FileObject getConfigRoot() { + return getConfigFile(""); //NOI18N + } + private static File wrapFileNoCanonicalize(File f) { if (f instanceof NonCanonicalizingFile) { return f; diff --git a/openide.filesystems/src/org/openide/filesystems/Repository.java b/openide.filesystems/src/org/openide/filesystems/Repository.java --- a/openide.filesystems/src/org/openide/filesystems/Repository.java +++ b/openide.filesystems/src/org/openide/filesystems/Repository.java @@ -60,12 +60,14 @@ import org.openide.util.io.NbMarshalledObject; /** - * Holder for system filesystem, used for most of NetBeans' runtime configuration. - * There is only one useful thing to do with this class: + * + * Holder for NetBeans default (system, configuration) filesystem, used for most + * of NetBeans' runtime configuration. The default implementation is a merge + * of all of the XML layer files provided by enabled modules, and + * the {@code config} subfolder of the userdir on the user's disk. + * If you just want to modify configuration data use e.g. *
- * FileSystem sfs = Repository.getDefault().getDefaultFileSystem();
- * // now use somehow, e.g.
- * FileObject menus = sfs.findResource("Menu");
+ * FileObject menus = FileUtil.getConfigFile("Menu");
  * // ...
  * 
* Formerly (NB 3.x) contained a list of mounted filesystems. This functionality @@ -169,7 +171,10 @@ /** * Gets the NetBeans default (system, configuration) filesystem. * @return the default filesystem + * @deprecated Please use {@link FileUtil#getConfigFile(String)} or + * {@link FileUtil#getConfigRoot()} instead. */ + @Deprecated public final FileSystem getDefaultFileSystem() { return system; } diff --git a/openide.filesystems/test/unit/src/org/openide/filesystems/FileUtilTest.java b/openide.filesystems/test/unit/src/org/openide/filesystems/FileUtilTest.java --- a/openide.filesystems/test/unit/src/org/openide/filesystems/FileUtilTest.java +++ b/openide.filesystems/test/unit/src/org/openide/filesystems/FileUtilTest.java @@ -319,4 +319,20 @@ logger.removeHandler(handler); } } + + /** Tests getConfigFile method (see #91534). */ + public void testGetConfigFile() throws IOException { + FileObject rootDFS = Repository.getDefault().getDefaultFileSystem().getRoot(); + assertNotNull("Sample FileObject not created.", rootDFS.createFolder("folder1").createFolder("folder2").createData("file.ext")); + assertNotNull("Existing FileObject not found.", FileUtil.getConfigFile("folder1/folder2/file.ext")); + assertNull("Path with backslashes is not valid.", FileUtil.getConfigFile("folder1\\folder2\\file.ext")); + assertEquals("Root should be returned for empty path.", rootDFS, FileUtil.getConfigFile("")); + assertEquals("Root should be returned from getConfigRoot", rootDFS, FileUtil.getConfigRoot()); + try { + FileUtil.getConfigFile(null); + fail("NullPointerException should be thrown for null path."); + } catch (NullPointerException npe) { + // OK + } + } }