From 019c7391fe7f288460d30f26cb452ae61a328e62 Mon Sep 17 00:00:00 2001 From: John Bogovic Date: Thu, 25 Apr 2024 14:59:02 -0400 Subject: [PATCH] feat/wip: add N5URI.from String --- .../org/janelia/saalfeldlab/n5/N5URI.java | 38 +++++++++++++++++++ .../org/janelia/saalfeldlab/n5/N5URLTest.java | 9 +++-- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/janelia/saalfeldlab/n5/N5URI.java b/src/main/java/org/janelia/saalfeldlab/n5/N5URI.java index 01f7b16b..0cd60570 100644 --- a/src/main/java/org/janelia/saalfeldlab/n5/N5URI.java +++ b/src/main/java/org/janelia/saalfeldlab/n5/N5URI.java @@ -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 * diff --git a/src/test/java/org/janelia/saalfeldlab/n5/N5URLTest.java b/src/test/java/org/janelia/saalfeldlab/n5/N5URLTest.java index 3daa79a4..12285390 100644 --- a/src/test/java/org/janelia/saalfeldlab/n5/N5URLTest.java +++ b/src/test/java/org/janelia/saalfeldlab/n5/N5URLTest.java @@ -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()); }