Skip to content

Commit 0d95340

Browse files
committed
LocationService: fall back to FileLocation more
Specifically: if the URI resolution returns null, rather than actually returning null, let's wrap the string into a FileLocation as a fallback. See imagej/pyimagej#285.
1 parent 055a449 commit 0d95340

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

Diff for: src/main/java/org/scijava/io/location/LocationService.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,15 @@ public interface LocationService extends HandlerService<URI, LocationResolver>,
5656
*/
5757
default Location resolve(final String uriString) throws URISyntaxException {
5858
try {
59-
return resolve(new URI(uriString));
59+
Location loc = resolve(new URI(uriString));
60+
if (loc != null) return loc;
6061
}
6162
catch (final URISyntaxException exc) {
6263
// In general, filenames are not valid URI strings.
6364
// Particularly on Windows, there are backslashes, which are invalid in URIs.
6465
// So we explicitly turn this string into a file if an error happens above.
65-
return resolve(new File(uriString).toURI());
6666
}
67+
return resolve(new File(uriString).toURI());
6768
}
6869

6970
/**

Diff for: src/test/java/org/scijava/io/location/LocationServiceTest.java

+19
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
package org.scijava.io.location;
3131

3232
import static org.junit.Assert.assertEquals;
33+
import static org.junit.Assert.assertNull;
3334
import static org.junit.Assert.assertTrue;
3435

3536
import java.io.File;
@@ -43,6 +44,7 @@
4344
* Tests {@link LocationService}.
4445
*
4546
* @author Gabriel Einsdorf
47+
* @author Curtis Rueden
4648
*/
4749
public class LocationServiceTest {
4850

@@ -60,6 +62,23 @@ public void testResolve() throws URISyntaxException {
6062
assertEquals(uri, loc.resolve(uri.toString()).getURI());
6163
}
6264

65+
@Test
66+
public void testResolveWindowsPath() throws URISyntaxException {
67+
final Context ctx = new Context(LocationService.class);
68+
final LocationService loc = ctx.getService(LocationService.class);
69+
70+
String pSlash = "C:/Windows/FilePath/image.tif";
71+
final Location locSlash = loc.resolve(pSlash);
72+
assertTrue(locSlash instanceof FileLocation);
73+
74+
String pBackslash = pSlash.replace('/', '\\');
75+
final Location locBackslash = loc.resolve(pBackslash);
76+
assertTrue(locBackslash instanceof FileLocation);
77+
78+
final Location locSlashURI = loc.resolve(new URI(pSlash));
79+
assertNull(locSlashURI);
80+
}
81+
6382
@Test
6483
public void testFallBack() throws URISyntaxException {
6584
final Context ctx = new Context(LocationService.class);

0 commit comments

Comments
 (0)