This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 81527
Collapse All | Expand All

(-)masterfs/test/unit/src/org/netbeans/modules/masterfs/MasterFileObjectTestHid.java (+13 lines)
Lines 67-72 Link Here
67
        };
67
        };
68
    }
68
    }
69
    public void  testCreateFolderOrDataFile() throws IOException {
70
        FileObject fo = FileUtil.createFolder(getWorkDir());
71
        assertNotNull(fo);
72
        File f = FileUtil.toFile(fo);
73
        assertNotNull(f);
74
        fo = FileUtil.createFolder(new File(f,"a/b/c"));
75
        assertNotNull(fo);
76
        f = FileUtil.toFile(fo);
77
        assertNotNull(f);
78
        fo = FileUtil.createData(new File(f,"a/b/c"));
79
        assertNotNull(fo);
80
    }
81
69
    public void  testGetNameExt2() throws IOException {
82
    public void  testGetNameExt2() throws IOException {
70
        FileObject fold1 = FileUtil.createFolder(
83
        FileObject fold1 = FileUtil.createFolder(
71
                FileBasedFileSystem.getFileObject(getWorkDir()),getName());
84
                FileBasedFileSystem.getFileObject(getWorkDir()),getName());
(-)fs/apichanges.xml (+20 lines)
Lines 23-28 Link Here
23
        <apidef name="filesystems">Filesystems API</apidef>
23
        <apidef name="filesystems">Filesystems API</apidef>
24
    </apidefs>
24
    </apidefs>
25
    <changes>
25
    <changes>
26
        <change id="createData-and-createFolder-take-File-as-parameter">
27
            <api name="filesystems"/>
28
            <summary>Added additional methods <code>FileUtil.createData</code>
29
                and <code>FileUtil.createFolder</code> that take <code>java.io.File</code> as a parameter.
30
            </summary>
31
            <version major="7" minor="0"/>
32
            <date day="29" month="8" year="2006"/>
33
            <author login="rmatous"/>
34
            <compatibility addition="yes" binary="compatible" source="compatible" semantic="compatible" deprecation="no" deletion="no" modification="no"/>
35
            <description>
36
                <p>
37
                    Added two utility methods for createtion of folders and data files
38
                    that take <code>java.io.File</code> as a parameter:
39
                    <code>public static FileObject createFolder (final File folder) throws IOException</code> and
40
                    <code>public static FileObject createData (final File folder) throws IOException</code>
41
                </p>
42
            </description>
43
            <class package="org.openide.filesystems" name="FileUtil"/>
44
            <issue number="81527"/>
45
        </change>
26
        <change id="method-value-with-map">
46
        <change id="method-value-with-map">
27
            <api name="filesystems"/>
47
            <api name="filesystems"/>
28
            <summary>Semantics of XMLFileSystem's methodvalue extended to
48
            <summary>Semantics of XMLFileSystem's methodvalue extended to
(-)fs/src/org/openide/filesystems/FileUtil.java (+87 lines)
Lines 39-44 Link Here
39
import java.util.Iterator;
39
import java.util.Iterator;
40
import java.util.Map;
40
import java.util.Map;
41
import java.util.Set;
41
import java.util.Set;
42
import java.util.Stack;
42
import java.util.StringTokenizer;
43
import java.util.StringTokenizer;
43
import java.util.WeakHashMap;
44
import java.util.WeakHashMap;
44
import java.util.jar.JarEntry;
45
import java.util.jar.JarEntry;
Lines 99-104 Link Here
99
    private FileUtil() {
100
    private FileUtil() {
100
    }
101
    }
102
    /** Returns a folder on given filesystem if such a folder exists.
103
     * If not then a folder is created, including any necessary but nonexistent parent
104
     * folders. Note that if this operation fails it may have succeeded in creating some of the necessary
105
     * parent folders.
106
     *
107
     * @param folder
108
     * @throws java.io.IOException if the creation fails
109
     * @return the existing or new folder
110
     */
111
    public static FileObject createFolder (final File folder) throws IOException {
112
        //doesn't work for LocalFileSystem (just on MasterFS) - probably not needed
113
        FileObject retval = null;
114
        File root = getRoot(folder);
115
        FileObject rootFo = FileUtil.toFileObject(root);
116
        assert rootFo != null : root.getAbsolutePath();
117
        final String relativePath = getRelativePath(root, folder);
118
        try {
119
            retval = FileUtil.createFolder(rootFo,relativePath);
120
        } catch (IOException ex) {
121
            //thus retval = null;
122
        }
123
        //if refresh needed because of external changes
124
        if (retval == null || !retval.isValid()) {
125
            rootFo.getFileSystem().refresh(false);
126
            retval = FileUtil.createFolder(rootFo,relativePath);
127
        }
128
        assert retval != null;
129
        return retval;
130
    }
131
132
    /** Returns a data file on given filesystem if such a data file exists.
133
     * If not then a data file is created, including any necessary but nonexistent parent
134
     * folders. Note that if this operation fails it may have succeeded in creating some of the necessary
135
     * parent folders.
136
     *
137
     * @param data
138
     * @throws java.io.IOException
139
     * @throws java.io.IOException if the creation fails
140
     * @return the existing or new data file
141
     */
142
    public static FileObject createData (final File data) throws IOException {
143
        //doesn't work for LocalFileSystem (just on MasterFS) - probably not needed
144
        FileObject retval = null;
145
        File root = getRoot(data);
146
        FileObject rootFo = FileUtil.toFileObject(root);
147
        assert rootFo != null : root.getAbsolutePath();
148
        final String relativePath = getRelativePath(root, data);
149
        try {
150
            retval = FileUtil.createData(rootFo,relativePath);
151
        } catch (IOException ex) {
152
            //thus retval = null;
153
        }
154
        //if refresh needed because of external changes
155
        if (retval == null || !retval.isValid()) {
156
            rootFo.getFileSystem().refresh(false);
157
            retval = FileUtil.createData(rootFo,relativePath);
158
        }
159
        assert retval != null;
160
        return retval;
161
    }
162
163
    private static File getRoot(final File dir) {
164
        File retval = dir;
165
        for (; retval.getParentFile() != null; retval = retval.getParentFile());
166
        assert retval != null && retval.exists();
167
        return retval;
168
    }
169
170
    private static String getRelativePath(final File dir, final File file) {
171
        Stack stack = new Stack ();
172
        File tempFile = file;
173
        while(tempFile != null && !tempFile.equals(dir)) {
174
            stack.push (tempFile.getName());
175
            tempFile = tempFile.getParentFile();
176
        }
177
        assert tempFile != null : file.getAbsolutePath() + "not found in " + dir.getAbsolutePath();//NOI18N
178
        StringBuilder retval = new StringBuilder();
179
        while (!stack.isEmpty()) {
180
            retval.append((String)stack.pop());
181
            if (!stack.isEmpty()) {
182
                retval.append("/");//NOI18N
183
            }
184
        }
185
        return retval.toString();
186
    }
187
101
    /** Copies stream of files.
188
    /** Copies stream of files.
102
    * <P>
189
    * <P>
103
    * Please be aware, that this method doesn't close any of passed streams.
190
    * Please be aware, that this method doesn't close any of passed streams.

Return to bug 81527