Skip to content

Commit

Permalink
update code to use URI methods
Browse files Browse the repository at this point in the history
  • Loading branch information
StephanPreibisch committed Oct 16, 2024
1 parent 7977d1a commit 752bccf
Show file tree
Hide file tree
Showing 14 changed files with 39 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1159,16 +1159,8 @@ else if (choice.equals( Z_VARIABLE_CHOICE ))

URI chosenPathXMLURI, chosenPathDataURI;

try
{
chosenPathXMLURI = new URI( chosenPathXML );
chosenPathDataURI = new URI( chosenPathData );
}
catch ( URISyntaxException e )
{
IOFunctions.println( "Could not convert provided paths into a URI: " + e );
return null;
}
chosenPathXMLURI = URITools.toURI( chosenPathXML );
chosenPathDataURI = URITools.toURI( chosenPathData );

if ( !URITools.isKnownScheme( chosenPathDataURI ) )
{
Expand Down Expand Up @@ -1270,8 +1262,8 @@ public int compare(Group< ViewDescription > o1, Group< ViewDescription > o2)
if (params == null)
return null;

String dataPath = URITools.removeFilePrefix( chosenPathDataURI );
String xmlPath = URITools.removeFilePrefix( chosenPathXMLURI );
String dataPath = URITools.fromURI( chosenPathDataURI );
String xmlPath = URITools.fromURI( chosenPathXMLURI );

if ( !new File( dataPath ).exists() )
{
Expand Down
12 changes: 2 additions & 10 deletions src/main/java/net/preibisch/mvrecon/fiji/plugin/XMLSaveAs.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,7 @@ public static URI saveAs( final SpimData2 data, final String suggestedFileName )
final String uriString = gd.getNextString();
final URI newXMLPath, newBaseDir;

try
{
newXMLPath = new URI( uriString );
}
catch ( URISyntaxException ex )
{
IOFunctions.println( "Could not convert provided XML path into a URI: " + ex );
return null;
}
newXMLPath = URITools.toURI( uriString );

if ( !URITools.isKnownScheme( newXMLPath ) )
{
Expand All @@ -117,7 +109,7 @@ else if ( N5ImageLoader.class.isInstance( imgLoader ) )
final URI n5URI = ((N5ImageLoader)imgLoader).getN5URI();

IOFunctions.println( "Path of N5 (stays in old location): " + n5URI );
data.getSequenceDescription().setImgLoader( new N5ImageLoader( URITools.toFile( n5URI ), data.getSequenceDescription() ) );
data.getSequenceDescription().setImgLoader( new N5ImageLoader( new File( URITools.fromURI( n5URI ) ), data.getSequenceDescription() ) );
}
else if ( Hdf5ImageLoader.class.isInstance( imgLoader ) )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
import net.preibisch.mvrecon.fiji.plugin.util.PluginHelper;
import net.preibisch.mvrecon.fiji.spimdata.EmptyEntity;
import net.preibisch.mvrecon.fiji.spimdata.NamePattern;
import net.preibisch.mvrecon.fiji.spimdata.SpimData2;
import util.URITools;

/**
Expand Down Expand Up @@ -255,10 +254,7 @@ public boolean queryXML(
this.attributeChoice = new HashMap< String, Integer >();

// try parsing if it ends with XML
try
{
tryParsing( new URI( defaultXMLURI ), false );
} catch (URISyntaxException e) {}
tryParsing( URITools.toURI( defaultXMLURI ), false );

if ( additionalTitle != null && additionalTitle.length() > 0 )
gd = new GenericDialogPlus( "Select dataset for " + additionalTitle );
Expand Down Expand Up @@ -318,14 +314,7 @@ public boolean queryXML(
// try to parse the file anyways
boolean success;

try
{
success = tryParsing( new URI( xmlURI ), true );
}
catch (URISyntaxException e)
{
success = false;
}
success = tryParsing( URITools.toURI( xmlURI ), true );

if ( buttonText != null && xmlURI.toLowerCase().equals( "define" ) && buttonText.get( 0 ).equals( "Define a new dataset" ) )
{
Expand Down Expand Up @@ -906,9 +895,9 @@ public void textValueChanged( final TextEvent t )
// try parsing if it ends with XML
try
{
tryParsing( new URI( xmlFilename ), false );
tryParsing( URITools.toURI( xmlFilename ), false );
}
catch (URISyntaxException e)
catch (Exception e)
{
lpq.message1 = errorMsg1;
lpq.message2 = noMsg2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import net.preibisch.legacy.io.IOFunctions;
import net.preibisch.mvrecon.fiji.plugin.util.GUIHelper;
import net.preibisch.mvrecon.fiji.plugin.util.PluginHelper;
import util.URITools;

public class ParametersResaveN5Api
{
Expand Down Expand Up @@ -171,16 +172,8 @@ public static ParametersResaveN5Api getParamtersIJ(

if ( askForPaths )
{
try
{
n5params.xmlURI = new URI( gdp.getNextString() );
n5params.n5URI = new URI( gdp.getNextString() );
}
catch ( URISyntaxException e )
{
IOFunctions.println( "Cannot create URIs for provided paths: " + e );
return null;
}
n5params.xmlURI = URITools.toURI( gdp.getNextString() );
n5params.n5URI = URITools.toURI( gdp.getNextString() );

IOFunctions.println( "XML & metadata path: " + n5params.xmlURI );
IOFunctions.println( "Image data path: " + n5params.n5URI );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ public static SpimData2 resaveN5(
{
// we need to init with File and not with URI, since otherwise the N5ImageLoader will trigger the exception above if this object is re-used
// this seems to not matter when opening directly from disc...
sdReduced.getSequenceDescription().setImgLoader( new N5ImageLoader( URITools.toFile( n5Params.n5URI ), sdReduced.getSequenceDescription() ) );
sdReduced.getSequenceDescription().setImgLoader( new N5ImageLoader( new File( URITools.fromURI( n5Params.n5URI ) ), sdReduced.getSequenceDescription() ) );
n5Writer.close();
}
else if ( format == StorageFormat.N5 ) // some cloud location
Expand All @@ -277,7 +277,7 @@ else if ( format == StorageFormat.N5 ) // some cloud location
}
else if ( format == StorageFormat.HDF5 )
{
sdReduced.getSequenceDescription().setImgLoader( new Hdf5ImageLoader( new File( URITools.removeFilePrefix( n5Params.n5URI ) ), null, sdReduced.getSequenceDescription() ) );
sdReduced.getSequenceDescription().setImgLoader( new Hdf5ImageLoader( new File( URITools.fromURI( n5Params.n5URI ) ), null, sdReduced.getSequenceDescription() ) );
n5Writer.close();
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void run( final String arg0 )
final SpimData2 data = lpq.getData();
final List< ViewId > viewIds = SpimData2.getAllViewIdsSorted( data, lpq.getViewSetupsToProcess(), lpq.getTimePointsToProcess() );

final File file = new File( URITools.removeFilePrefix( params.getXMLPath() ) );
final File file = new File( URITools.fromURI( params.getXMLPath() ) );

// write the TIFF's
writeTIFF( data, viewIds, file.getParent(), params.compress, progressWriter );
Expand Down Expand Up @@ -136,17 +136,7 @@ public static ParametersResaveAsTIFF getParameters()
if ( !fullPath.endsWith( ".xml" ) )
fullPath += ".xml";

final URI uri;

try
{
uri = new URI( fullPath );
}
catch (URISyntaxException e )
{
IOFunctions.println( "Cannot interpret '" + fullPath + "' as URI. Stopping." );
return null;
}
final URI uri = URITools.toURI( fullPath );

if ( !URITools.isFile( uri ) )
{
Expand Down Expand Up @@ -214,7 +204,6 @@ public static void writeTIFF( final SpimData spimData, final List< ViewId > view
}
}


public static SpimData2 createXMLObject( final SpimData2 spimData, final List< ViewId > viewIds, final ParametersResaveAsTIFF params )
{
int layoutTP = 0, layoutChannels = 0, layoutIllum = 0, layoutAngles = 0, layoutTiles = 0;
Expand Down Expand Up @@ -263,7 +252,7 @@ public static SpimData2 createXMLObject( final SpimData2 spimData, final List< V

// Re-assemble a new SpimData object containing the subset of viewsetups and timepoints selected
final SpimData2 newSpimData;
final URI newBasePath = new File( URITools.removeFilePrefix( params.getXMLPath() ) ).getParentFile().toURI();
final URI newBasePath = URITools.toURI( new File( URITools.fromURI( params.getXMLPath() ) ).getParentFile().toString() );

boolean isEqual = false;

Expand All @@ -282,7 +271,7 @@ public static SpimData2 createXMLObject( final SpimData2 spimData, final List< V
newSpimData = SpimData2Tools.reduceSpimData2( spimData, viewIds, newBasePath );

final StackImgLoaderIJ imgLoader = new StackImgLoaderIJ(
new File( URITools.removeFilePrefix( params.getXMLPath() ) ).getParentFile(),
new File( URITools.fromURI( params.getXMLPath() ) ).getParentFile(),
filename,
layoutTP, layoutChannels, layoutIllum, layoutAngles, layoutTiles, newSpimData.getSequenceDescription() );
newSpimData.getSequenceDescription().setImgLoader( imgLoader );
Expand Down
20 changes: 0 additions & 20 deletions src/main/java/net/preibisch/mvrecon/fiji/spimdata/SpimData2.java
Original file line number Diff line number Diff line change
Expand Up @@ -577,24 +577,4 @@ public static < V extends ViewId > ArrayList< Group< V > > filterGroupsForMissin

return groupsOut;
}

public static void main( String[] args )
{
File f = new File( "/nrs/" );
URI u = f.toURI();
//u = URI.create( "s3://myBucket/" ) ;

URI u2 = URI.create( u.toString() + ( u.toString().endsWith( "/" ) ? "" : "/") + "test.xml" );


System.out.println( u );
System.out.println( u2 );
System.out.println( URITools.getParent( u2 ) );
System.out.println( URITools.getParent( URI.create("/nrs/test.xml") ) );
System.out.println( new File( u2 ) );
System.out.println( new File( URI.create("file:/nrs/test.xml") ) );
// System.out.println( new File( URI.create("/nrs/test.xml") ) ); // FAILS
System.out.println( URITools.removeFilePrefix( u2 ) );
System.out.println( Paths.get(u2.getPath()).getFileName().toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -716,11 +716,7 @@ public ArrayList< ExplorerWindowSetable > initPopups()

// add link to wiki
popups.add( new LabelPopUp( "Help" ) );
try
{
popups.add( new SimpleHyperlinkPopup("Browse Wiki...", new URI( "https://imagej.net/Multiview-Reconstruction" )) );
}
catch ( URISyntaxException e ) { e.printStackTrace(); }
popups.add( new SimpleHyperlinkPopup("Browse Wiki...", URITools.toURI( "https://imagej.net/Multiview-Reconstruction" )) );

return popups;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ else if (choice == JOptionPane.YES_OPTION)
params.xmlPath = panel.xml();

// write the TIFF's
Resave_TIFF.writeTIFF( data, viewIds, new File( URITools.removeFilePrefix( params.getXMLPath() ) ).getParent(), params.compress, progressWriter );
Resave_TIFF.writeTIFF( data, viewIds, new File( URITools.fromURI( params.getXMLPath() ) ).getParent(), params.compress, progressWriter );

// write the XML
final SpimData2 newSpimData = Resave_TIFF.createXMLObject( data, viewIds, params );
Expand Down Expand Up @@ -231,7 +231,7 @@ else if ( index == 2 || index == 3 ) // HDF5, compressed HDF5
File hdf5File;
int i = 1;

String xml = URITools.removeFilePrefix( panel.xml() );
String xml = URITools.fromURI( panel.xml() );

do
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class XmlIOFileMapImgLoaderLOCI2 implements XmlIoBasicImgLoader< FileMapI
@Override
public Element toXml( final FileMapImgLoaderLOCI2 imgLoader, final URI basePathURI )
{
return toXml(imgLoader, new File( URITools.removeFilePrefix( basePathURI ) ) );
return toXml(imgLoader, new File( URITools.fromURI( basePathURI ) ) );
}

@Override
Expand Down Expand Up @@ -92,7 +92,7 @@ public Element toXml(FileMapImgLoaderLOCI2 imgLoader, File basePath)
public FileMapImgLoaderLOCI2 fromXml(Element elem, final URI basePathURI,
AbstractSequenceDescription< ?, ?, ? > sequenceDescription)
{
return fromXml(elem, new File( URITools.removeFilePrefix( basePathURI ) ), sequenceDescription);
return fromXml(elem, new File( URITools.fromURI( basePathURI ) ), sequenceDescription);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,15 @@ public boolean saveInterestPoints( final boolean forceWrite )

try
{
final File dir = new File( URITools.removeFilePrefix( getBaseDir() ), getFile().getParent() );
final File dir = new File( URITools.fromURI( getBaseDir() ), getFile().getParent() );

if ( !dir.exists() )
{
IOFunctions.println( "Creating directory: " + dir );
dir.mkdirs();
}

final File f = new File( URITools.removeFilePrefix( getBaseDir() ), getFile().toString() + getInterestPointsExt() );
final File f = new File( URITools.fromURI( getBaseDir() ), getFile().toString() + getInterestPointsExt() );
final PrintWriter out = TextFileAccess.openFileWriteEx( f );

// header
Expand Down Expand Up @@ -194,15 +194,15 @@ public boolean saveCorrespondingInterestPoints( final boolean forceWrite )

try
{
final File dir = new File( URITools.removeFilePrefix( getBaseDir() ), getFile().getParent() );
final File dir = new File( URITools.fromURI( getBaseDir() ), getFile().getParent() );

if ( !dir.exists() )
{
IOFunctions.println( "Creating directory: " + dir );
dir.mkdirs();
}

final File f = new File( URITools.removeFilePrefix( getBaseDir() ), getFile().toString() + getCorrespondencesExt() );
final File f = new File( URITools.fromURI( getBaseDir() ), getFile().toString() + getCorrespondencesExt() );

final PrintWriter out = TextFileAccess.openFileWriteEx( f );

Expand Down Expand Up @@ -241,7 +241,7 @@ protected boolean loadCorrespondences()
{
final ArrayList< CorrespondingInterestPoints > correspondingInterestPoints = new ArrayList<>();

final BufferedReader in = TextFileAccess.openFileReadEx( new File( URITools.removeFilePrefix( getBaseDir() ), getFile().toString() + getCorrespondencesExt() ) );
final BufferedReader in = TextFileAccess.openFileReadEx( new File( URITools.fromURI( getBaseDir() ), getFile().toString() + getCorrespondencesExt() ) );

// the header
do {} while ( !in.readLine().startsWith( "id" ) );
Expand Down Expand Up @@ -285,7 +285,7 @@ protected boolean loadInterestPoints()
{
final ArrayList< InterestPoint > interestPoints = new ArrayList<>();

final BufferedReader in = TextFileAccess.openFileReadEx( new File( URITools.removeFilePrefix( getBaseDir() ), getFile().toString() + getInterestPointsExt() ) );
final BufferedReader in = TextFileAccess.openFileReadEx( new File( URITools.fromURI( getBaseDir() ), getFile().toString() + getInterestPointsExt() ) );

// the header
do {} while ( !in.readLine().startsWith( "id" ) );
Expand Down Expand Up @@ -323,15 +323,15 @@ protected boolean loadInterestPoints()
@Override
public boolean deleteInterestPoints()
{
final File ip = new File( URITools.removeFilePrefix( getBaseDir() ), getFile().toString() + getInterestPointsExt() );
final File ip = new File( URITools.fromURI( getBaseDir() ), getFile().toString() + getInterestPointsExt() );

return ip.delete();
}

@Override
public boolean deleteCorrespondingInterestPoints()
{
final File corr = new File( URITools.removeFilePrefix( getBaseDir() ), getFile().toString() + getCorrespondencesExt() );
final File corr = new File( URITools.fromURI( getBaseDir() ), getFile().toString() + getCorrespondencesExt() );

return corr.delete();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,10 @@ public <T extends RealType<T> & NativeType<T>> boolean exportImage(
{
if ( storageType == StorageFormat.HDF5 )
{
final File dir = new File( URITools.removeFilePrefix( path ) ).getParentFile();
final File dir = new File( URITools.fromURI( path ) ).getParentFile();
if ( !dir.exists() )
dir.mkdirs();
driverVolumeWriter = new N5HDF5Writer( URITools.removeFilePrefix( path ) );
driverVolumeWriter = new N5HDF5Writer( URITools.fromURI( path ) );
}
else if ( storageType == StorageFormat.N5 || storageType == StorageFormat.ZARR )
{
Expand Down Expand Up @@ -529,7 +529,7 @@ else if ( storageType == StorageFormat.N5 )

try
{
this.path = new URI( defaultPathURI = gd.getNextString().trim() );
this.path = URITools.toURI( defaultPathURI = gd.getNextString().trim() );
}
catch ( Exception e )
{
Expand All @@ -553,7 +553,7 @@ else if ( storageType == StorageFormat.N5 )
{
try
{
this.xmlOut = new URI( defaultXMLOutURI = gd.getNextString().trim() );//defaultXMLOut = gd.getNextString();
this.xmlOut = URITools.toURI( defaultXMLOutURI = gd.getNextString().trim() );//defaultXMLOut = gd.getNextString();
}
catch ( Exception e )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public static Pair<Boolean, Boolean> writeSpimData(
else if ( StorageFormat.N5.equals(storageType) ) // some cloud location
sequence.setImgLoader( new N5CloudImageLoader( null, n5PathURI, sequence) );
else if ( StorageFormat.HDF5.equals(storageType) )
sequence.setImgLoader( new Hdf5ImageLoader( new File( URITools.removeFilePrefix( n5PathURI ) ), null, sequence) );
sequence.setImgLoader( new Hdf5ImageLoader( new File( URITools.fromURI( n5PathURI ) ), null, sequence) );
else
throw new RuntimeException( storageType + " not supported." );

Expand Down Expand Up @@ -287,7 +287,7 @@ else if ( StorageFormat.HDF5.equals(storageType) )
else if ( StorageFormat.N5.equals(storageType) ) // some cloud location
sequence.setImgLoader( new N5CloudImageLoader( null, n5PathURI, sequence) );
else if ( StorageFormat.HDF5.equals(storageType) )
sequence.setImgLoader( new Hdf5ImageLoader( new File( URITools.removeFilePrefix( n5PathURI ) ), null, sequence) );
sequence.setImgLoader( new Hdf5ImageLoader( new File( URITools.fromURI( n5PathURI ) ), null, sequence) );
else
throw new RuntimeException( storageType + " not supported." );

Expand Down
Loading

0 comments on commit 752bccf

Please sign in to comment.