Index: masterfs/test/unit/src/org/netbeans/modules/masterfs/MasterFileObjectTestHid.java =================================================================== RCS file: /cvs/openide/masterfs/test/unit/src/org/netbeans/modules/masterfs/MasterFileObjectTestHid.java,v retrieving revision 1.33 diff -u -r1.33 MasterFileObjectTestHid.java --- masterfs/test/unit/src/org/netbeans/modules/masterfs/MasterFileObjectTestHid.java 8 Aug 2006 15:54:12 -0000 1.33 +++ masterfs/test/unit/src/org/netbeans/modules/masterfs/MasterFileObjectTestHid.java 29 Aug 2006 09:43:00 -0000 @@ -67,6 +67,19 @@ }; } + public void testCreateFolderOrDataFile() throws IOException { + FileObject fo = FileUtil.createFolder(getWorkDir()); + assertNotNull(fo); + File f = FileUtil.toFile(fo); + assertNotNull(f); + fo = FileUtil.createFolder(new File(f,"a/b/c")); + assertNotNull(fo); + f = FileUtil.toFile(fo); + assertNotNull(f); + fo = FileUtil.createData(new File(f,"a/b/c")); + assertNotNull(fo); + } + public void testGetNameExt2() throws IOException { FileObject fold1 = FileUtil.createFolder( FileBasedFileSystem.getFileObject(getWorkDir()),getName()); Index: fs/apichanges.xml =================================================================== RCS file: /cvs/openide/fs/apichanges.xml,v retrieving revision 1.10 diff -u -r1.10 apichanges.xml --- fs/apichanges.xml 1 Jul 2006 09:08:03 -0000 1.10 +++ fs/apichanges.xml 29 Aug 2006 09:43:00 -0000 @@ -23,6 +23,26 @@ Filesystems API + + + Added additional methods FileUtil.createData + and FileUtil.createFolder that take java.io.File as a parameter. + + + + + + +

+ Added two utility methods for createtion of folders and data files + that take java.io.File as a parameter: + public static FileObject createFolder (final File folder) throws IOException and + public static FileObject createData (final File folder) throws IOException +

+
+ + +
Semantics of XMLFileSystem's methodvalue extended to Index: fs/src/org/openide/filesystems/FileUtil.java =================================================================== RCS file: /cvs/openide/fs/src/org/openide/filesystems/FileUtil.java,v retrieving revision 1.25 diff -u -r1.25 FileUtil.java --- fs/src/org/openide/filesystems/FileUtil.java 28 Jul 2006 16:04:27 -0000 1.25 +++ fs/src/org/openide/filesystems/FileUtil.java 29 Aug 2006 09:43:00 -0000 @@ -39,6 +39,7 @@ import java.util.Iterator; import java.util.Map; import java.util.Set; +import java.util.Stack; import java.util.StringTokenizer; import java.util.WeakHashMap; import java.util.jar.JarEntry; @@ -99,6 +100,92 @@ private FileUtil() { } + /** Returns a folder on given filesystem if such a folder exists. + * If not then a folder is created, including any necessary but nonexistent parent + * folders. Note that if this operation fails it may have succeeded in creating some of the necessary + * parent folders. + * + * @param folder + * @throws java.io.IOException if the creation fails + * @return the existing or new folder + */ + public static FileObject createFolder (final File folder) throws IOException { + //doesn't work for LocalFileSystem (just on MasterFS) - probably not needed + FileObject retval = null; + File root = getRoot(folder); + FileObject rootFo = FileUtil.toFileObject(root); + assert rootFo != null : root.getAbsolutePath(); + final String relativePath = getRelativePath(root, folder); + try { + retval = FileUtil.createFolder(rootFo,relativePath); + } catch (IOException ex) { + //thus retval = null; + } + //if refresh needed because of external changes + if (retval == null || !retval.isValid()) { + rootFo.getFileSystem().refresh(false); + retval = FileUtil.createFolder(rootFo,relativePath); + } + assert retval != null; + return retval; + } + + /** Returns a data file on given filesystem if such a data file exists. + * If not then a data file is created, including any necessary but nonexistent parent + * folders. Note that if this operation fails it may have succeeded in creating some of the necessary + * parent folders. + * + * @param data + * @throws java.io.IOException + * @throws java.io.IOException if the creation fails + * @return the existing or new data file + */ + public static FileObject createData (final File data) throws IOException { + //doesn't work for LocalFileSystem (just on MasterFS) - probably not needed + FileObject retval = null; + File root = getRoot(data); + FileObject rootFo = FileUtil.toFileObject(root); + assert rootFo != null : root.getAbsolutePath(); + final String relativePath = getRelativePath(root, data); + try { + retval = FileUtil.createData(rootFo,relativePath); + } catch (IOException ex) { + //thus retval = null; + } + //if refresh needed because of external changes + if (retval == null || !retval.isValid()) { + rootFo.getFileSystem().refresh(false); + retval = FileUtil.createData(rootFo,relativePath); + } + assert retval != null; + return retval; + } + + private static File getRoot(final File dir) { + File retval = dir; + for (; retval.getParentFile() != null; retval = retval.getParentFile()); + assert retval != null && retval.exists(); + return retval; + } + + private static String getRelativePath(final File dir, final File file) { + Stack stack = new Stack (); + File tempFile = file; + while(tempFile != null && !tempFile.equals(dir)) { + stack.push (tempFile.getName()); + tempFile = tempFile.getParentFile(); + } + assert tempFile != null : file.getAbsolutePath() + "not found in " + dir.getAbsolutePath();//NOI18N + StringBuilder retval = new StringBuilder(); + while (!stack.isEmpty()) { + retval.append((String)stack.pop()); + if (!stack.isEmpty()) { + retval.append("/");//NOI18N + } + } + return retval.toString(); + } + /** Copies stream of files. *

* Please be aware, that this method doesn't close any of passed streams.