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 10 Nov 2006 14:19:11 -0000 1.32 +++ openide/fs/src/org/openide/filesystems/FileUtil.java 24 Dec 2006 18:57:03 -0000 @@ -1668,4 +1668,57 @@ return wrapFileNoCanonicalize(delegate.createFileObject(path)); } } + + /** + * Get a data file 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 if an I/O error occurred + */ + public static FileObject getConfigurationFile (String path) { + if (path == null) { + throw new NullPointerException ("null path to file"); + } + FileSystem sfs = Repository.getDefault().getDefaultFileSystem(); + FileObject root = sfs.getRoot(); + FileObject result = sfs.findResource(path); + if (result == null) { + 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 "/", null + * or 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 + */ + public static FileObject getConfigurationFolder (String path) { + FileSystem sfs = Repository.getDefault().getDefaultFileSystem(); + FileObject root = sfs.getRoot(); + FileObject result = path == null || "/".equals(path) || "".equals(path) ? + root : sfs.findResource(path); + if (result == null) { + 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 24 Dec 2006 18:57:03 -0000 @@ -145,7 +145,10 @@ /** * Gets the NetBeans default (system, configuration) filesystem. * @return the default filesystem + * @deprecated Use FileUtil.getConfigurationFile() or + * FileUtil.getConfigurationFolder() instead */ + @Deprecated public final FileSystem getDefaultFileSystem() { return system; } Index: openide/fs/test/unit/src/org/openide/filesystems/FileUtilSfsMethodsTest.java =================================================================== RCS file: openide/fs/test/unit/src/org/openide/filesystems/FileUtilSfsMethodsTest.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ openide/fs/test/unit/src/org/openide/filesystems/FileUtilSfsMethodsTest.java 24 Dec 2006 18:57:03 -0000 @@ -0,0 +1,118 @@ +/* + * The contents of this file are subject to the terms of the Common Development + * and Distribution License (the License). You may not use this file except in + * compliance with the License. + * + * You can obtain a copy of the License at http://www.netbeans.org/cddl.html + * or http://www.netbeans.org/cddl.txt. + * + * When distributing Covered Code, include this CDDL Header Notice in each file + * and include the License file at http://www.netbeans.org/cddl.txt. + * If applicable, add the following below the CDDL Header, with the fields + * enclosed by brackets [] replaced by your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * The Original Software is NetBeans. The Initial Developer of the Original + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun + * Microsystems, Inc. All Rights Reserved. + */ + +package org.openide.filesystems; +import java.io.IOException; +import junit.framework.TestCase; +import org.openide.util.lookup.Lookups; +import org.openide.util.lookup.ProxyLookup; +/** + * Tests FileUtil.getConfigurationData(), etc. + * + * @author Tim Boudreau + */ +public class FileUtilSfsMethodsTest extends TestCase { + + public FileUtilSfsMethodsTest (String name) { + super (name); + } + + protected void setUp() throws java.lang.Exception { + System.setProperty ("org.openide.util.Lookup", + "org.openide.filesystems.FileUtilSfsMethodsTest$MyLkp"); + } + + 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"); + assertNotNull (f); + FileObject f1 = FileUtil.getConfigurationFile ("folder/file.txt"); + assertNotNull (f1); + assertEquals (f1, f.getFileObject ("file.txt")); + } + + public void testCreateConfigurationFile() throws Exception { + System.out.println("testCreateConfigurationFile"); + FileObject f = FileUtil.getConfigurationFile("somewhere/file2.txt"); + assertNotNull (f); + Exception ioe = null; + FileObject f1 = null; + try { + f1 = FileUtil.getConfigurationFile ("somewhere/file2.txt"); + } catch (Exception e) { + ioe = e; + } + assertNull (ioe); + assertNotNull (f1); + assertEquals (f, f1); + ioe = null; + try { + f = FileUtil.getConfigurationFolder ("somewhere"); + } catch (Exception e) { + ioe = e; + } + assertNull (ioe); + assertEquals (f, f1.getParent()); + + f = FileUtil.getConfigurationFolder("folder"); + FileObject nue = f.createData ("hello.txt"); + assertEquals (nue, FileUtil.getConfigurationFile("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.getConfigurationFolder ("other"); + assertNotNull (f); + Exception ioe = null; + FileObject f1 = null; + try { + f1 = FileUtil.getConfigurationFolder("other"); + } catch (Exception e) { + ioe = e; + } + assertNull (ioe); + assertEquals (f, f1); + } + + private 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); + } + } + } + + public static final class MyLkp extends ProxyLookup { + public MyLkp () { + super (Lookups.singleton (new MyRepo())); + } + } +}