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 190480
Collapse All | Expand All

(-)a/openide.filesystems/apichanges.xml (+21 lines)
Lines 938-943 Link Here
938
            <class package="org.openide.filesystems" name="FileUtil"/>
938
            <class package="org.openide.filesystems" name="FileUtil"/>
939
            <issue number="40410"/>
939
            <issue number="40410"/>
940
        </change>
940
        </change>
941
        <change id="FileUtil.normalizePath">
942
            <api name="filesystems"/>
943
            <summary>
944
                <code>FileUtil.normalizePath</code> added
945
            </summary>
946
            <version major="7" minor="42"/>
947
            <date day="22" month="9" year="2010"/>
948
            <author login="vv159170"/>
949
            <compatibility addition="yes" binary="compatible" source="compatible" semantic="compatible" deprecation="no" deletion="no" modification="no"/>
950
            <description>
951
                <p>
952
                    The method 
953
                    <code>FileUtil.normalizePath(String)</code> was added as a
954
                    refinement of 
955
                    <code>FileUtil.normalizeFile</code> that does prevent String to File to String
956
                    conversion on client side when intention is to convert unnormalized path to normalized path presentation. 
957
                </p>
958
            </description>
959
            <class package="org.openide.filesystems" name="FileUtil"/>
960
            <issue number="190480"/>
961
        </change>        
941
        <change id="FileObject.setImportant">
962
        <change id="FileObject.setImportant">
942
            <api name="filesystems"/>
963
            <api name="filesystems"/>
943
            <summary>
964
            <summary>
(-)a/openide.filesystems/manifest.mf (-1 / +1 lines)
Lines 2-6 Link Here
2
OpenIDE-Module: org.openide.filesystems
2
OpenIDE-Module: org.openide.filesystems
3
OpenIDE-Module-Localizing-Bundle: org/openide/filesystems/Bundle.properties
3
OpenIDE-Module-Localizing-Bundle: org/openide/filesystems/Bundle.properties
4
OpenIDE-Module-Layer: org/openide/filesystems/resources/layer.xml
4
OpenIDE-Module-Layer: org/openide/filesystems/resources/layer.xml
5
OpenIDE-Module-Specification-Version: 7.41
5
OpenIDE-Module-Specification-Version: 7.42
6
6
(-)a/openide.filesystems/src/org/openide/filesystems/FileUtil.java (+27 lines)
Lines 1672-1677 Link Here
1672
     * (including filechoosers), configuration files, Ant properties, etc. <em>Internal</em>
1672
     * (including filechoosers), configuration files, Ant properties, etc. <em>Internal</em>
1673
     * calculations should not need to renormalize paths since {@link File#listFiles},
1673
     * calculations should not need to renormalize paths since {@link File#listFiles},
1674
     * {@link File#getParentFile}, etc. will not produce abnormal variants.
1674
     * {@link File#getParentFile}, etc. will not produce abnormal variants.
1675
     * @param path file path to normalize
1676
     * @return normalized file path
1677
     * @since 7.42
1678
     */
1679
    public static String normalizePath(final String path) {
1680
        Map<String, String> normalizedPaths = getNormalizedFilesMap();
1681
        String normalized = normalizedPaths.get(path);
1682
        if (normalized == null) {
1683
            File ret = normalizeFileImpl(new File(path));
1684
            normalized = ret.getPath();
1685
            normalizedPaths.put(path, normalized);
1686
        }
1687
        return normalized;
1688
    }
1689
    
1690
    /**
1691
     * Normalize a file path to a clean form.
1692
     * This method may for example make sure that the returned file uses
1693
     * the natural case on Windows; that old Windows 8.3 filenames are changed to the long form;
1694
     * that relative paths are changed to be
1695
     * absolute; that <code>.</code> and <code>..</code> sequences are removed; etc.
1696
     * Unlike {@link File#getCanonicalFile} this method will not traverse symbolic links on Unix.
1697
     * <p>This method involves some overhead and should not be called frivolously.
1698
     * Generally it should be called on <em>incoming</em> pathnames that are gotten from user input
1699
     * (including filechoosers), configuration files, Ant properties, etc. <em>Internal</em>
1700
     * calculations should not need to renormalize paths since {@link File#listFiles},
1701
     * {@link File#getParentFile}, etc. will not produce abnormal variants.
1675
     * @param file file to normalize
1702
     * @param file file to normalize
1676
     * @return normalized file
1703
     * @return normalized file
1677
     * @since 4.48
1704
     * @since 4.48
(-)a/openide.filesystems/test/unit/src/org/openide/filesystems/FileUtilTest.java (-12 / +45 lines)
Lines 253-258 Link Here
253
    /** Tests normalizeFile() method. */
253
    /** Tests normalizeFile() method. */
254
    public void testNormalizeFile() throws IOException {
254
    public void testNormalizeFile() throws IOException {
255
        // pairs of path before and after normalization
255
        // pairs of path before and after normalization
256
        Map<String, String> paths = createNormalizedPaths();
257
258
        for (String path : paths.keySet()) {
259
            File file = new File(path);
260
            assertTrue("Idempotency violated for path: " + path, FileUtil.normalizeFile(FileUtil.normalizeFile(file)).equals(FileUtil.normalizeFile(file)));
261
            assertEquals("File not normalized: " + path, paths.get(path), FileUtil.normalizeFile(file).getPath());
262
        }
263
    }
264
265
    public void testNormalizeFileIsCached() throws Exception {
266
        File f = new File(getWorkDir(), "text.txt");
267
        CharSequence log = Log.enable(FileUtil.class.getName(), Level.FINE);
268
        File one = FileUtil.normalizeFile(f);
269
        String msg = "FileUtil.normalizeFile for " + f;
270
        if (log.toString().indexOf(msg) == -1) {
271
            fail("One querfy for the file shall be in logs:\n" + log);
272
        }
273
        CharSequence log2 = Log.enable(FileUtil.class.getName(), Level.FINE);
274
        File two = FileUtil.normalizeFile(f);
275
        if (log2.toString().contains(msg)) {
276
            fail("No second FileUtil.normalizeFile for in:\n" + log);
277
        }
278
        assertEquals("Files are equal", one, two);
279
    }
280
281
    /** Tests normalizePath() method. */
282
    public void testNormalizePath() throws IOException {
283
        // pairs of path before and after normalization
284
        Map<String, String> paths = createNormalizedPaths();
285
286
        for (String path : paths.keySet()) {
287
            assertTrue("Idempotency violated for path: " + path, FileUtil.normalizePath(FileUtil.normalizePath(path)).equals(FileUtil.normalizePath(path)));
288
            assertEquals("File path not normalized: " + path, paths.get(path), FileUtil.normalizePath(path));
289
        }
290
    }
291
292
    private Map<String, String> createNormalizedPaths() throws IOException {
293
        // pairs of path before and after normalization
256
        Map<String, String> paths = new HashMap<String, String>();
294
        Map<String, String> paths = new HashMap<String, String>();
257
        if (Utilities.isWindows()) {
295
        if (Utilities.isWindows()) {
258
            paths.put("A:\\", "A:\\");
296
            paths.put("A:\\", "A:\\");
Lines 291-320 Link Here
291
        // #137407 - java.io.File(".") should be normalized
329
        // #137407 - java.io.File(".") should be normalized
292
        paths.put(".", new File(".").getCanonicalPath());
330
        paths.put(".", new File(".").getCanonicalPath());
293
        paths.put("..", new File("..").getCanonicalPath());
331
        paths.put("..", new File("..").getCanonicalPath());
294
332
        return paths;
295
        for (String path : paths.keySet()) {
296
            File file = new File(path);
297
            assertTrue("Idempotency violated for path: " + path, FileUtil.normalizeFile(FileUtil.normalizeFile(file)).equals(FileUtil.normalizeFile(file)));
298
            assertEquals("File not normalized: " + path, paths.get(path), FileUtil.normalizeFile(file).getPath());
299
        }
300
    }
333
    }
301
334
    
302
    public void testNormalizeFileIsCached() throws Exception {
335
    public void testNormalizePathIsCached() throws Exception {
303
        File f = new File(getWorkDir(), "text.txt");
336
        File f = new File(getWorkDir(), "textPath.txt");
337
        String path = f.getPath();
304
        CharSequence log = Log.enable(FileUtil.class.getName(), Level.FINE);
338
        CharSequence log = Log.enable(FileUtil.class.getName(), Level.FINE);
305
        File one = FileUtil.normalizeFile(f);
339
        String one = FileUtil.normalizePath(path);
306
        String msg = "FileUtil.normalizeFile for " + f;
340
        String msg = "FileUtil.normalizeFile for " + f;
307
        if (log.toString().indexOf(msg) == -1) {
341
        if (log.toString().indexOf(msg) == -1) {
308
            fail("One querfy for the file shall be in logs:\n" + log);
342
            fail("One querfy for the file shall be in logs:\n" + log);
309
        }
343
        }
310
        CharSequence log2 = Log.enable(FileUtil.class.getName(), Level.FINE);
344
        CharSequence log2 = Log.enable(FileUtil.class.getName(), Level.FINE);
311
        File two = FileUtil.normalizeFile(f);
345
        String two = FileUtil.normalizePath(path);
312
        if (log2.toString().contains(msg)) {
346
        if (log2.toString().contains(msg)) {
313
            fail("No second FileUtil.normalizeFile for in:\n" + log);
347
            fail("No second FileUtil.normalizeFile for in:\n" + log);
314
        }
348
        }
315
        assertEquals("Files are equal", one, two);
349
        assertEquals("Files are equal", one, two);
316
    }
350
    }
317
318
351
319
    /** Tests that only resolvers are queried which supply at least one of
352
    /** Tests that only resolvers are queried which supply at least one of
320
     * MIME types given in array in FileUtil.getMIMEType(fo, String[]).
353
     * MIME types given in array in FileUtil.getMIMEType(fo, String[]).

Return to bug 190480