# HG changeset patch # Parent cf1bbd7899119a0651f0a5ac0c5c2b65a422e1d6 diff --git a/openide.loaders/apichanges.xml b/openide.loaders/apichanges.xml --- a/openide.loaders/apichanges.xml +++ b/openide.loaders/apichanges.xml @@ -109,6 +109,23 @@ + + + + Introduced DataShadow.findOriginal which extracts the original file from + a FileObject representing a shadow/link. + + + + + + + Target FileObject can be obtained, instead of DataObject. The target may be invalid, + or otherwise unsuitable for DataObject creation. Use this method to just dereference + the shadow file. + + + Introduced DataObject.Registration and DataObject.Registrations diff --git a/openide.loaders/manifest.mf b/openide.loaders/manifest.mf --- a/openide.loaders/manifest.mf +++ b/openide.loaders/manifest.mf @@ -1,6 +1,6 @@ Manifest-Version: 1.0 OpenIDE-Module: org.openide.loaders -OpenIDE-Module-Specification-Version: 7.41 +OpenIDE-Module-Specification-Version: 7.42 OpenIDE-Module-Localizing-Bundle: org/openide/loaders/Bundle.properties OpenIDE-Module-Provides: org.netbeans.modules.templates.v1_0 OpenIDE-Module-Layer: org/netbeans/modules/openide/loaders/layer.xml diff --git a/openide.loaders/src/org/openide/loaders/DataShadow.java b/openide.loaders/src/org/openide/loaders/DataShadow.java --- a/openide.loaders/src/org/openide/loaders/DataShadow.java +++ b/openide.loaders/src/org/openide/loaders/DataShadow.java @@ -391,17 +391,19 @@ } /** - * Tries to load the original file from a shadow. + * Tries to locate the original file from a shadow. * Looks for file contents as well as the originalFile/originalFileSystem attributes. - * Attempts to discover locations for migrated files in configuration. + * Attempts to discover locations for migrated files in configuration. Returns + * {@code null}, if the target file could not be found (broken link) * * @param fileObject a data shadow - * @return the original DataObject referenced by the shadow - * @throws IOException error during load or broken link + * @return the original FileObject referenced by the shadow + * @throws IOException error during load * * @see Utilities#translate + * @since 7.42 */ - protected static DataObject deserialize(FileObject fileObject) throws IOException { + public static FileObject findOriginal(FileObject fileObject) throws IOException { String[] fileAndFileSystem = readOriginalFileAndFileSystem(fileObject); String path = fileAndFileSystem[0]; assert path != null; @@ -465,12 +467,31 @@ } } } + return target; + } + + /** + * Tries to load the original file from a shadow. + * Looks for file contents as well as the originalFile/originalFileSystem attributes. + * Attempts to discover locations for migrated files in configuration. + * + * @param fileObject a data shadow + * @return the original DataObject referenced by the shadow + * @throws IOException error during load or broken link + * + * @see Utilities#translate + */ + protected static DataObject deserialize(FileObject fileObject) throws IOException { + FileObject target = findOriginal(fileObject); if (target != null) { return DataObject.find(target); } else { - throw new FileNotFoundException(path + ':' + fsname); + String[] fileAndFileSystem = readOriginalFileAndFileSystem(fileObject); + String path = fileAndFileSystem[0]; + throw new FileNotFoundException(path + ':' + fileAndFileSystem[1]); } } + static URL readURL(FileObject fileObject) throws IOException { String[] fileAndFileSystem = readOriginalFileAndFileSystem(fileObject); String path = fileAndFileSystem[0]; diff --git a/openide.loaders/test/unit/src/org/openide/loaders/DataShadowTest.java b/openide.loaders/test/unit/src/org/openide/loaders/DataShadowTest.java --- a/openide.loaders/test/unit/src/org/openide/loaders/DataShadowTest.java +++ b/openide.loaders/test/unit/src/org/openide/loaders/DataShadowTest.java @@ -437,4 +437,46 @@ R action = new R(); FileUtil.runAtomicAction(action); } + + /** + * Tests the method findOriginal() + * @throws Exception + */ + public void testFindOriginal() throws Exception { + FileSystem fs = FileUtil.createMemoryFileSystem(); + FileObject orig = FileUtil.createData(fs.getRoot(), "path/to/orig"); + FileObject shadow = FileUtil.createData(fs.getRoot(), "link.shadow"); + shadow.setAttribute("originalFile", "path/to/orig"); + assertEquals("found the right original file", + orig, DataShadow.findOriginal(shadow)); + } + + /** + * Checks that findOriginal returns null on broken (but formally + * correct) shadow + * @throws Exception C + */ + public void testFindMissingOriginal() throws Exception { + FileSystem fs = FileUtil.createMemoryFileSystem(); + FileObject shadow = FileUtil.createData(fs.getRoot(), "link.shadow"); + shadow.setAttribute("originalFile", "path/to/orig"); + assertNull("null should be returned for missing target", DataShadow.findOriginal(shadow)); + } + + /** + * Checks that findOriginal throws Exception on a malformed + * shadow + * @throws Exception + */ + public void testFindBrokenOriginal() throws Exception { + FileSystem fs = FileUtil.createMemoryFileSystem(); + FileObject orig = FileUtil.createData(fs.getRoot(), "path/to/orig"); + FileObject shadow = FileUtil.createData(fs.getRoot(), "link.shadow"); + try { + DataShadow.findOriginal(shadow); + fail("IOException should be thrown on malformed shadow"); + } catch (IOException ex) { + // this is oK + } + } }