Skip to content

Commit 904e4b9

Browse files
authored
Merge pull request #321 from scijava/location-service-fallback
LocationService#resolve() add fall-back to FileLocation
2 parents 6f337e8 + d75e1a3 commit 904e4b9

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

src/main/java/org/scijava/io/location/LocationService.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
package org.scijava.io.location;
3434

35+
import java.io.File;
3536
import java.net.URI;
3637
import java.net.URISyntaxException;
3738

@@ -50,25 +51,29 @@ public interface LocationService extends HandlerService<URI, LocationResolver>,
5051

5152
/**
5253
* Turns the given string into an {@link URI}, then resolves it to a
53-
* {@link Location}
54-
*
55-
* @param uri the uri to resolve
54+
* {@link Location}.
55+
*
56+
* @param uriString the uri to resolve
5657
* @return the resolved {@link Location}
5758
* @throws URISyntaxException if the URI is malformed
5859
*/
59-
default Location resolve(final String uri) throws URISyntaxException {
60-
return resolve(new URI(uri));
60+
default Location resolve(final String uriString) throws URISyntaxException {
61+
return resolve(new URI(uriString));
6162
}
6263

6364
/**
64-
* Resolves the given {@link URI} to a location.
65-
*
65+
* Resolves the given {@link URI} to a location. If the {@code scheme} part of
66+
* the URI is {@code null} the path component is resolved as a local file.
67+
*
6668
* @param uri the uri to resolve
6769
* @return the resolved {@link Location} or <code>null</code> if no resolver
6870
* could be found.
6971
* @throws URISyntaxException if the URI is malformed
7072
*/
71-
default Location resolve(final URI uri) throws URISyntaxException {
73+
default Location resolve(URI uri) throws URISyntaxException {
74+
if (uri.getScheme() == null) { // Fallback for local files
75+
uri = new File(uri.getPath()).toURI();
76+
}
7277
final LocationResolver resolver = getResolver(uri);
7378
return resolver != null ? resolver.resolve(uri) : null;
7479
}

src/test/java/org/scijava/io/location/LocationServiceTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,17 @@ public void testResolve() throws URISyntaxException {
6363
assertEquals(uri, loc.resolve(uri.toString()).getURI());
6464
}
6565

66+
@Test
67+
public void testFallBack() throws URISyntaxException {
68+
final Context ctx = new Context(LocationService.class);
69+
final LocationService loc = ctx.getService(LocationService.class);
70+
71+
final String uri = new File(".").getAbsolutePath();
72+
final Location res = loc.resolve(uri);
73+
74+
assertTrue(res instanceof FileLocation);
75+
FileLocation resFile = (FileLocation) res;
76+
assertEquals(uri, resFile.getFile().getAbsolutePath());
77+
}
78+
6679
}

0 commit comments

Comments
 (0)