Skip to content

Commit

Permalink
XmlIoN5ImageLoader: Allow non-file URIs
Browse files Browse the repository at this point in the history
  • Loading branch information
tpietzsch committed Sep 14, 2024
1 parent ddb7b91 commit 94f9516
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions src/main/java/bdv/img/n5/XmlIoN5ImageLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@

import java.io.File;
import java.net.URI;
import java.util.regex.Pattern;

import org.janelia.saalfeldlab.n5.N5FSReader;
import org.jdom2.Element;
Expand All @@ -48,16 +47,43 @@
@ImgLoaderIo( format = "bdv.n5", type = N5ImageLoader.class )
public class XmlIoN5ImageLoader implements XmlIoBasicImgLoader< N5ImageLoader >
{
public static boolean PREFER_URI_FOR_LOCAL_FILES = true;

@Override
public Element toXml( final N5ImageLoader imgLoader, final File basePath )
{
return toXml( imgLoader, basePath == null ? null : basePath.toURI() );
}

@Override
public Element toXml( final N5ImageLoader imgLoader, final URI basePathURI )
{
final Element elem = new Element( "ImageLoader" );
elem.setAttribute( IMGLOADER_FORMAT_ATTRIBUTE_NAME, "bdv.n5" );
elem.setAttribute( "version", "1.0" );
elem.addContent( XmlHelpers.pathElement( "n5", imgLoader.getN5File(), basePath ) );
elem.setAttribute( "version", "1.1" );
final File n5File = getLocalFile( imgLoader.getN5URI() );
if ( !PREFER_URI_FOR_LOCAL_FILES && n5File != null )
{
final File basePath = basePathURI == null ? null : new File( basePathURI );
elem.addContent( XmlHelpers.pathElement( "n5", n5File, basePath ) );
}
else
{
elem.addContent( XmlHelpers.pathElementURI( "n5", imgLoader.getN5URI(), basePathURI ) );
}
return elem;
}

private static File getLocalFile( final URI uri )
{
if ( "file".equalsIgnoreCase( uri.getScheme() ) )
return new File( uri );
else if ( uri.getScheme() == null )
return new File( uri.getPath() );
else
return null;
}

@Override
public N5ImageLoader fromXml( final Element elem, final File basePath, final AbstractSequenceDescription< ?, ?, ? > sequenceDescription )
{
Expand All @@ -75,7 +101,7 @@ public N5ImageLoader fromXml( final Element elem, final URI basePathURI, final A
{
final String scheme = uri.getScheme();
final boolean hasScheme = scheme != null;
if ( !hasScheme || FILE_SCHEME.asPredicate().test( scheme ) )
if ( !hasScheme || "file".equalsIgnoreCase( uri.getScheme() ) )
{
final String path = hasScheme
? new File( uri ).getAbsolutePath()
Expand All @@ -97,6 +123,4 @@ public N5ImageLoader fromXml( final Element elem, final URI basePathURI, final A
return null;
}
}

private final static Pattern FILE_SCHEME = Pattern.compile("file", Pattern.CASE_INSENSITIVE);
}

0 comments on commit 94f9516

Please sign in to comment.