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 21 Mar 2007 04:50:10 -0000 @@ -1709,4 +1709,63 @@ return wrapFileNoCanonicalize(delegate.createFileObject(path)); } } + + /** + * Get a data file or folder from the System (configuration) Filesystem + * with the specified file path, creating it if necessary. + * + * @param The path from the root of the System Filesystem to the file, + * including its name and extension. + * @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 when trying to create a non-existent folder + */ + public static FileObject getConfigurationFile (String path, boolean create) { + 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(); + if (result == null && create) { + try { + result = FileUtil.createData(root, path); + } catch (IOException ioe) { + Exceptions.printStackTrace(ioe); + } + } + return result; + } + + /** + * Get a folder in the System (configuration) Filesystem with the + * specified file path, creating it if necessary. + * + * @param The path from the root of the System Filesystem to the file, + * including its name and extension. If the path is the + * empty string, returns the root of the system filesystem. + * @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, boolean create) { + if (path == null) { + throw new NullPointerException ("Null path"); + } + FileSystem sfs = Repository.getDefault().getDefaultFileSystem(); + FileObject root = sfs.getRoot(); + FileObject result = "".equals(path) ? + root : root.getFileObject(path); + assert result == null || result.isFolder(); + if (result == null && create) { + try { + result = FileUtil.createFolder(root, path); + } catch (IOException ioe) { + Exceptions.printStackTrace(ioe); + } + } + 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 21 Mar 2007 04:50:10 -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 21 Mar 2007 04:50:11 -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,76 @@ } } } + + protected String[] getResources(String testName) { + return new String[] { "somefolder/somefile.txt" }; + } + public void testGetConfigurationData() throws Exception { + System.out.println("testGetConfigurationData"); + FileObject f = FileUtil.getConfigurationFolder("folder", true); + assertNotNull (f); + FileObject f1 = FileUtil.getConfigurationFile ("folder/file.txt", true); + assertNotNull (f1); + assertEquals (f1, f.getFileObject ("file.txt")); + } + + public void testCreateConfigurationFile() throws Exception { + System.out.println("testCreateConfigurationFile"); + FileObject f = FileUtil.getConfigurationFile("somewhere/file2.txt", true); + assertNotNull (f); + Exception ioe = null; + FileObject f1 = null; + try { + f1 = FileUtil.getConfigurationFile ("somewhere/file2.txt", true); + } catch (Exception e) { + ioe = e; + } + assertNull (ioe); + assertNotNull (f1); + assertEquals (f, f1); + ioe = null; + try { + f = FileUtil.getConfigurationFolder ("somewhere", true); + } catch (Exception e) { + ioe = e; + } + assertNull (ioe); + assertEquals (f, f1.getParent()); + + f = FileUtil.getConfigurationFolder("folder", true); + FileObject nue = f.createData ("hello.txt"); + assertEquals (nue, FileUtil.getConfigurationFile("folder/hello.txt", true)); + 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.getConfigurationFolder ("other", true); + assertNotNull (f); + Exception ioe = null; + FileObject f1 = null; + try { + f1 = FileUtil.getConfigurationFolder("other", true); + } catch (Exception e) { + ioe = e; + } + assertNull (ioe); + 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); + } + } + } }