Skip to content

Commit 545caf0

Browse files
committed
Do not hardcode String-to-FileLocation conversions
That's what LocationService.resolve is for.
1 parent 5f690ce commit 545caf0

File tree

5 files changed

+66
-32
lines changed

5 files changed

+66
-32
lines changed

src/main/java/org/scijava/io/IOPlugin.java

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@
3030
package org.scijava.io;
3131

3232
import java.io.IOException;
33+
import java.net.URISyntaxException;
3334

3435
import org.scijava.io.location.FileLocation;
3536
import org.scijava.io.location.Location;
37+
import org.scijava.io.location.LocationService;
3638
import org.scijava.plugin.HandlerPlugin;
3739
import org.scijava.plugin.Plugin;
3840

@@ -58,22 +60,32 @@ public interface IOPlugin<D> extends HandlerPlugin<Location> {
5860
/** Checks whether the I/O plugin can open data from the given source. */
5961
@SuppressWarnings("unused")
6062
default boolean supportsOpen(final String source) {
61-
return supportsOpen(new FileLocation(source));
63+
try {
64+
return supportsOpen(context().service(LocationService.class).resolve(source));
65+
}
66+
catch (final URISyntaxException exc) {
67+
return false;
68+
}
6269
}
6370

6471
/** Checks whether the I/O plugin can open data from the given location. */
65-
default boolean supportsOpen(Location source) {
72+
default boolean supportsOpen(final Location source) {
6673
return false;
6774
}
6875

6976
/** Checks whether the I/O plugin can save data to the given destination. */
7077
@SuppressWarnings("unused")
7178
default boolean supportsSave(final String destination) {
72-
return supportsSave(new FileLocation(destination));
79+
try {
80+
return supportsSave(context().service(LocationService.class).resolve(destination));
81+
}
82+
catch (final URISyntaxException exc) {
83+
return false;
84+
}
7385
}
7486

7587
/** Checks whether the I/O plugin can save data to the given location. */
76-
default boolean supportsSave(Location destination) {
88+
default boolean supportsSave(final Location destination) {
7789
return false;
7890
}
7991

@@ -85,7 +97,7 @@ default boolean supportsSave(final Object data, final String destination) {
8597
return supportsSave(destination) && getDataType().isInstance(data);
8698
}
8799

88-
default boolean supportsSave(Object data, Location destination) {
100+
default boolean supportsSave(final Object data, final Location destination) {
89101
return supportsSave(destination) && getDataType().isInstance(data);
90102
}
91103

@@ -96,17 +108,23 @@ default D open(final String source) throws IOException {
96108
}
97109

98110
/** Opens data from the given location. */
99-
default D open(Location source) throws IOException {
111+
default D open(final Location source) throws IOException {
100112
throw new UnsupportedOperationException();
101113
}
114+
102115
/** Saves the given data to the specified destination. */
103116
@SuppressWarnings("unused")
104117
default void save(final D data, final String destination) throws IOException {
105-
save(data, new FileLocation(destination));
118+
try {
119+
save(data, context().service(LocationService.class).resolve(destination));
120+
}
121+
catch (final URISyntaxException exc) {
122+
throw new UnsupportedOperationException(exc);
123+
}
106124
}
107125

108126
/** Saves the given data to the specified location. */
109-
default void save(D data, Location destination) throws IOException {
127+
default void save(final D data, final Location destination) throws IOException {
110128
throw new UnsupportedOperationException();
111129
}
112130

src/main/java/org/scijava/io/TypedIOService.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@
3030
package org.scijava.io;
3131

3232
import java.io.IOException;
33+
import java.net.URISyntaxException;
3334

3435
import org.scijava.io.location.FileLocation;
3536
import org.scijava.io.location.Location;
37+
import org.scijava.io.location.LocationService;
3638
import org.scijava.plugin.HandlerService;
3739
import org.scijava.service.SciJavaService;
3840

@@ -51,7 +53,12 @@ public interface TypedIOService<D> extends HandlerService<Location, IOPlugin<D>>
5153
* location.
5254
*/
5355
default IOPlugin<D> getOpener(final String source) {
54-
return getOpener(new FileLocation(source));
56+
try {
57+
return getOpener(context().service(LocationService.class).resolve(source));
58+
}
59+
catch (final URISyntaxException exc) {
60+
return null;
61+
}
5562
}
5663

5764
/**
@@ -70,7 +77,12 @@ default IOPlugin<D> getOpener(Location source) {
7077
* location.
7178
*/
7279
default IOPlugin<D> getSaver(final D data, final String destination) {
73-
return getSaver(data, new FileLocation(destination));
80+
try {
81+
return getSaver(data, context().service(LocationService.class).resolve(destination));
82+
}
83+
catch (final URISyntaxException exc) {
84+
return null;
85+
}
7486
}
7587

7688
/**

src/main/java/org/scijava/io/event/DataOpenedEvent.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,14 @@ public DataOpenedEvent(final Location location, final Object data) {
4949
*/
5050
@Deprecated
5151
public DataOpenedEvent(final String source, final Object data) {
52-
this(new FileLocation(source), data);
52+
super(source, data);
5353
}
5454

5555
/**
5656
* @deprecated use {@link #getLocation} instead
5757
*/
5858
@Deprecated
5959
public String getSource() {
60-
try {
61-
FileLocation fileLocation = (FileLocation) getLocation();
62-
return fileLocation.getFile().getAbsolutePath();
63-
} catch(ClassCastException e) {
64-
return getLocation().getURI().toString();
65-
}
60+
return getDescriptor();
6661
}
67-
6862
}

src/main/java/org/scijava/io/event/DataSavedEvent.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,14 @@ public DataSavedEvent(final Location destination, final Object data) {
4949
*/
5050
@Deprecated
5151
public DataSavedEvent(final String destination, final Object data) {
52-
this(new FileLocation(destination), data);
52+
super(destination, data);
5353
}
5454

5555
/**
5656
* @deprecated use {@link #getLocation} instead
5757
*/
5858
@Deprecated
5959
public String getDestination() {
60-
try {
61-
FileLocation fileLocation = (FileLocation) getLocation();
62-
return fileLocation.getFile().getAbsolutePath();
63-
} catch(ClassCastException e) {
64-
return getLocation().getURI().toString();
65-
}
60+
return getDescriptor();
6661
}
6762
}

src/main/java/org/scijava/io/event/IOEvent.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,12 @@
2929

3030
package org.scijava.io.event;
3131

32+
import java.net.URISyntaxException;
33+
3234
import org.scijava.event.SciJavaEvent;
3335
import org.scijava.io.location.FileLocation;
3436
import org.scijava.io.location.Location;
37+
import org.scijava.io.location.LocationService;
3538

3639
/**
3740
* An event indicating that I/O (e.g., opening or saving) has occurred.
@@ -43,6 +46,10 @@ public abstract class IOEvent extends SciJavaEvent {
4346
/** The data location (source or destination). */
4447
private final Location location;
4548

49+
/** @deprecated use {@link #location} instead */
50+
@Deprecated
51+
private final String descriptor;
52+
4653
/** The data for which I/O took place. */
4754
private final Object data;
4855

@@ -51,17 +58,26 @@ public abstract class IOEvent extends SciJavaEvent {
5158
*/
5259
@Deprecated
5360
public IOEvent(final String descriptor, final Object data) {
54-
this(new FileLocation(descriptor), data);
61+
this.location = null;
62+
this.descriptor = descriptor;
63+
this.data = data;
5564
}
5665

5766
public IOEvent(final Location location, final Object data) {
5867
this.location = location;
68+
this.descriptor = null;
5969
this.data = data;
6070
}
6171

6272
/** Gets the data location (source or destination). */
6373
public Location getLocation() {
64-
return location;
74+
if (location != null) return location;
75+
try {
76+
return context().service(LocationService.class).resolve(descriptor);
77+
}
78+
catch (final URISyntaxException exc) {
79+
return null;
80+
}
6581
}
6682

6783
/** Gets the data for which I/O took place. */
@@ -82,12 +98,11 @@ public String toString() {
8298
*/
8399
@Deprecated
84100
public String getDescriptor() {
85-
try {
86-
FileLocation fileLocation = (FileLocation) getLocation();
101+
if (descriptor != null) return descriptor;
102+
if (location instanceof FileLocation) {
103+
final FileLocation fileLocation = (FileLocation) location;
87104
return fileLocation.getFile().getAbsolutePath();
88-
} catch(ClassCastException e) {
89-
return getLocation().getURI().toString();
90105
}
106+
return location.getURI().toString();
91107
}
92-
93108
}

0 commit comments

Comments
 (0)