diff --git a/src/main/java/org/janelia/saalfeldlab/n5/N5URI.java b/src/main/java/org/janelia/saalfeldlab/n5/N5URI.java index 01f7b16b8..c655abe23 100644 --- a/src/main/java/org/janelia/saalfeldlab/n5/N5URI.java +++ b/src/main/java/org/janelia/saalfeldlab/n5/N5URI.java @@ -8,6 +8,7 @@ import java.nio.charset.CharsetDecoder; import java.nio.charset.CoderResult; import java.nio.charset.CodingErrorAction; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -590,6 +591,42 @@ 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) { + + try { + return new N5URI(new URI(uriOrPath)); + } catch (Throwable ignore) {} + + try { + final String[] split = uriOrPath.split("\\?"); + final URI tmp = Paths.get(split[0]).toUri(); + if (split.length == 1) + return new N5URI(tmp); + else { + StringBuffer buildUri = new StringBuffer(); + buildUri.append(tmp.toString()); + buildUri.append("?"); + for (int i = 1; i < split.length; i++) + buildUri.append(split[i]); + + return new N5URI(new URI(buildUri.toString())); + } + } catch (Throwable ignore) {} + + try { + return new N5URI(N5URI.encodeAsUri(uriOrPath)); + } catch (URISyntaxException e) { + throw new N5Exception(e); + } + } + /** * 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 f8e626fef..d0d7e58b8 100644 --- a/src/test/java/org/janelia/saalfeldlab/n5/N5URLTest.java +++ b/src/test/java/org/janelia/saalfeldlab/n5/N5URLTest.java @@ -2,7 +2,10 @@ import org.junit.Test; +import java.io.File; +import java.io.IOException; import java.net.URISyntaxException; +import java.nio.file.Paths; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -113,4 +116,26 @@ public void testGetRelative() throws URISyntaxException { "s3://janelia-cosem-datasets/jrc_hela-3/jrc_hela-3.n5#f/g", new N5URI("s3://janelia-cosem-datasets/jrc_hela-3/jrc_hela-3.n5").resolve("#f/g").toString()); } + + @Test + public void testContainerPath() throws URISyntaxException, IOException { + + 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", +// N5URI.from(posixPath).getContainerPath()); + + // normalize with File + final N5URI systemUri = N5URI.from(systemPath); + System.out.println( Paths.get(home).toFile().getCanonicalPath()); + System.out.println("system uri path " + systemUri.getURI().getPath()); + assertEquals( + Paths.get(home).toFile().getCanonicalPath(), + new File(systemUri.getURI().getPath()).getCanonicalPath()); + + } + } \ No newline at end of file