Index: openide/fs/apichanges.xml =================================================================== RCS file: /cvs/openide/fs/apichanges.xml,v --- openide/fs/apichanges.xml 14 Mar 2007 13:20:26 -0000 1.12 +++ openide/fs/apichanges.xml 27 Mar 2007 02:08:37 -0000 @@ -23,6 +23,28 @@ Filesystems API + + + Convenience system filesystem accessor methods on FileUtil + + + + + +

+ Convenience methods added for looking up files/folders in + the system filesystem. Rather than having to call + Respository.getDefault().getDefaultFileSystem().getRoot().getFileObject("foo/bar"), + you can simply call FileUtil.getConfigurationFolder ("foo/bar", false). + FileUtil.getConfigurationFolder(path)s + and + FileUtil.getConfigurationFile(path) + were added to the API. +

+
+ + +
Allow modules to dynamically add/remove layer content Index: openide/fs/manifest.mf =================================================================== RCS file: /cvs/openide/fs/manifest.mf,v --- openide/fs/manifest.mf 14 Mar 2007 13:20:27 -0000 1.9 +++ openide/fs/manifest.mf 27 Mar 2007 02:08:37 -0000 @@ -1,5 +1,5 @@ Manifest-Version: 1.0 OpenIDE-Module: org.openide.filesystems -OpenIDE-Module-Specification-Version: 7.1 +OpenIDE-Module-Specification-Version: 7.2 OpenIDE-Module-Localizing-Bundle: org/openide/filesystems/Bundle.properties Index: openide/fs/src/org/openide/filesystems/FileUtil.java =================================================================== RCS file: /cvs/openide/fs/src/org/openide/filesystems/FileUtil.java,v --- openide/fs/src/org/openide/filesystems/FileUtil.java 20 Mar 2007 20:12:29 -0000 1.38 +++ openide/fs/src/org/openide/filesystems/FileUtil.java 27 Mar 2007 02:08:40 -0000 @@ -1709,4 +1709,92 @@ return wrapFileNoCanonicalize(delegate.createFileObject(path)); } } + + /** + * Get a data file or folder from the System (configuration) Filesystem + * with the specified file path. + * + * @param path The path from the root of the System Filesystem to the file, + * including its name and extension. + * @return A FileObject for that file in the system filesystem + */ + public static FileObject getConfigurationFile (String path) { + if (path == null) { + throw new NullPointerException ("Null path to file"); //NOI18N + } + FileSystem sfs = Repository.getDefault().getDefaultFileSystem(); + FileObject root = sfs.getRoot(); + FileObject result = root.getFileObject(path); + assert result == null || result.isData() : "Not a data file: " + path; //NOI18N + return result; + } + + /** + * Get a data file or folder from the System (configuration) Filesystem + * with the specified file path, creating a new empty file if the specified + * file does not exist. + * + * @param path The path from the root of the System Filesystem to the file, + * including its name and extension. + * @return A FileObject for that file in the system filesystem + * @throws IOException if the file did not previously exist and could not be + * created for some reason + */ + public static FileObject getOrCreateConfigurationFile (String path) throws + IOException { + FileObject result = getConfigurationFile (path); + if (result == null) { + FileSystem sfs = Repository.getDefault().getDefaultFileSystem(); + FileObject root = sfs.getRoot(); + result = createData(root, path); + } + return result; + } + + /** + * Get a folder in the System (configuration) Filesystem with the + * specified file path. + * + * @param path The path from the root of the System Filesystem to the folder. + * If the path is the empty string, returns the root of the + * system filesystem (do not use "/" for the root). + * @return A FileObject representing a new file created with the specified + * path, or one representing the existing file if the file already + * was present, or null if an I/O error occurred when trying to + * create a non-existent folder + */ + public static FileObject getConfigurationFolder (String path) { + if (path == null) { + throw new NullPointerException ("Null path"); //NOI18N + } + FileSystem sfs = Repository.getDefault().getDefaultFileSystem(); + FileObject root = sfs.getRoot(); + FileObject result = path.length() == 0 ? //NOI18N + root : root.getFileObject(path); + assert result == null || result.isFolder() : "Not a folder: " + path; //NOI18N + return result; + } + + /** + * Get a folder in the System (configuration) Filesystem with the + * specified file path, creating it if necessary. + * + * @param path The path from the root of the System Filesystem to the folder. + * If the path is the empty string, returns the root of the + * system filesystem (do not use "/" for the root). + * @return A FileObject representing a new file created with the specified + * path, or one representing the existing file if the file already + * was present, or null if an I/O error occurred when trying to + * create a non-existent folder + * @throws IOException if an error occurs creating the folder + */ + public static FileObject getOrCreateConfigurationFolder (String path) throws IOException { + FileObject result = getConfigurationFolder(path); + if (result == null) { + FileSystem sfs = Repository.getDefault().getDefaultFileSystem(); + FileObject root = sfs.getRoot(); + result = createFolder (root, path); + } + return result; + } } Index: openide/fs/src/org/openide/filesystems/Repository.java =================================================================== RCS file: /cvs/openide/fs/src/org/openide/filesystems/Repository.java,v --- openide/fs/src/org/openide/filesystems/Repository.java 8 Sep 2006 08:00:32 -0000 1.8 +++ openide/fs/src/org/openide/filesystems/Repository.java 27 Mar 2007 02:08:41 -0000 @@ -144,7 +144,17 @@ /** * Gets the NetBeans default (system, configuration) filesystem. - * @return the default filesystem + * The System Filesystem contains runtime data that is a merge of + * all of the XML layer files provided by enabled modules, and the + * config/ subfolder of the userdir on the user's disk. + * + * If you want to simply get a file or folder from the system, + * it is simpler to call + * FileUtil.getConfigurationFile(path) or + * + * FileUtil.getConfigurationFolder(path). + * + * @return the system filesystem */ public final FileSystem getDefaultFileSystem() { return system; Index: openide/fs/test/unit/src/org/openide/filesystems/FileUtilTest.java =================================================================== RCS file: /cvs/openide/fs/test/unit/src/org/openide/filesystems/FileUtilTest.java,v --- openide/fs/test/unit/src/org/openide/filesystems/FileUtilTest.java 20 Mar 2007 20:12:29 -0000 1.1 +++ openide/fs/test/unit/src/org/openide/filesystems/FileUtilTest.java 27 Mar 2007 02:08:41 -0000 @@ -20,20 +20,26 @@ package org.openide.filesystems; import java.io.File; +import java.io.IOException; import java.net.URL; import org.netbeans.junit.MockServices; import org.netbeans.junit.NbTestCase; import org.openide.util.Utilities; +import org.netbeans.junit.MockServices; /** - * @author Jesse Glick + * @author Jesse Glick, Tim Boudreau */ public class FileUtilTest extends NbTestCase { public FileUtilTest(String n) { super(n); } - + + protected void setUp() throws java.lang.Exception { + MockServices.setServices(MyRepo.class); + } + public void testToFileObjectSlash() throws Exception { // #98388 if (!Utilities.isUnix()) { return; @@ -60,5 +66,56 @@ } } } + + protected String[] getResources(String testName) { + return new String[] { "somefolder/somefile.txt" }; + } + public void testGetConfigurationData() throws Exception { + System.out.println("testGetConfigurationData"); + FileObject f = FileUtil.getOrCreateConfigurationFolder("folder"); + assertNotNull (f); + FileObject f1 = FileUtil.getOrCreateConfigurationFile ("folder/file.txt"); + assertNotNull (f1); + assertEquals (f1, f.getFileObject ("file.txt")); + } + + public void testCreateConfigurationFile() throws Exception { + System.out.println("testCreateConfigurationFile"); + FileObject f = FileUtil.getOrCreateConfigurationFile("somewhere/file2.txt"); + assertNotNull (f); + FileObject f1 = FileUtil.getOrCreateConfigurationFile ("somewhere/file2.txt"); + assertNotNull (f1); + assertEquals (f, f1); + f = FileUtil.getOrCreateConfigurationFolder ("somewhere"); + assertEquals (f, f1.getParent()); + + f = FileUtil.getOrCreateConfigurationFolder("folder"); + FileObject nue = f.createData ("hello.txt"); + assertEquals (nue, FileUtil.getOrCreateConfigurationFile("folder/hello.txt")); + assertNotNull (f.getFileObject ("hello.txt")); + assertTrue (f.getFileObject ("hello.txt").isData()); + assertFalse (f.getFileObject ("hello.txt").isFolder()); + } + + public void testCreateConfigurationFolder() throws Exception { + System.out.println("testCreateConfigurationFolder"); + FileObject f = FileUtil.getOrCreateConfigurationFolder ("other"); + assertNotNull (f); + FileObject f1 = FileUtil.getOrCreateConfigurationFolder("other"); + assertEquals (f, f1); + } + + public static class MyRepo extends Repository { + static FileSystem sysfs; + public MyRepo () { + super(sysfs = FileUtil.createMemoryFileSystem()); + try { + FileObject fld = sysfs.getRoot().createFolder("folder"); + fld.createData("file.txt"); + } catch (IOException ioe) { + throw new Error (ioe); + } + } + } }