Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/main/java/org/janelia/saalfeldlab/n5/N5URI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*
Expand Down
25 changes: 25 additions & 0 deletions src/test/java/org/janelia/saalfeldlab/n5/N5URLTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());

}

}