Skip to content

Commit

Permalink
feat/wip: add N5URI.from String
Browse files Browse the repository at this point in the history
  • Loading branch information
bogovicj committed Apr 25, 2024
1 parent cf9da0e commit 019c739
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
38 changes: 38 additions & 0 deletions src/main/java/org/janelia/saalfeldlab/n5/N5URI.java
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,44 @@ public static N5URI from(
return new N5URI(containerPart + groupPart + attributePart);
}

/**
* Generate an {@link N5URI} from a String.
*
* @param uriOrPath
* a string representation of a uri or a path string.
* @return the {@link N5URI}
*/
public static N5URI from(final String uriOrPath) {

URI uri;
try {
uri = URI.create(uriOrPath);
} catch (Throwable ignore) {}

try {
final String[] split = uriOrPath.split("\\?");
final URI tmp = Paths.get(split[0]).toUri();
if (split.length == 1)
uri = tmp;
else {
StringBuffer buildUri = new StringBuffer();
buildUri.append(tmp.toString());
for (int i = 1; i < split.length; i++)
buildUri.append(split[i]);

uri = new URI(buildUri.toString());
}
} catch (Throwable ignore) {}

try {
uri = N5URI.encodeAsUri(uriOrPath);
} catch (URISyntaxException e) {
throw new N5Exception(e);
}

return new N5URI(uri);
}

/**
* Intentionally copied from {@link URI} for internal use
*
Expand Down
9 changes: 6 additions & 3 deletions src/test/java/org/janelia/saalfeldlab/n5/N5URLTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,17 @@ public void testGetRelative() throws URISyntaxException {
@Test
public void testContainerPath() throws URISyntaxException {

final String home = System.getProperty("user.home");
final String posixPath = "/a/b/c/d?e#f";
final String systemPath = home + "?e#f";

assertEquals(
"/a/b/c/d",
new N5URI("/a/b/c/d?e#f").getContainerPath());
N5URI.from(posixPath).getContainerPath());

final String home = System.getProperty("user.home");
assertEquals(
home,
new N5URI(home + "?e#f").getContainerPath());
N5URI.from(systemPath).getContainerPath());

}

Expand Down

0 comments on commit 019c739

Please sign in to comment.