Skip to content
This repository was archived by the owner on Sep 28, 2022. It is now read-only.

Commit 2cc51f6

Browse files
frauzufallimagejan
authored andcommitted
Adapt to API changes in scijava-table 0.7.0
* Use Location instead of String * Parser is now Function<String, ?> instead of Function<String, Object>
1 parent 038d287 commit 2cc51f6

File tree

3 files changed

+34
-46
lines changed

3 files changed

+34
-46
lines changed

pom.xml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
<parent>
66
<groupId>org.scijava</groupId>
77
<artifactId>pom-scijava</artifactId>
8-
<version>29.2.0</version>
8+
<version>29.2.1</version>
99
<relativePath />
1010
</parent>
1111

1212
<artifactId>scijava-plugins-io-table</artifactId>
13-
<version>0.3.1-SNAPSHOT</version>
13+
<version>0.4.0-SNAPSHOT</version>
1414

1515
<name>SciJava IO Plugin: Tables</name>
1616
<description>I/O plugins for SciJava table objects.</description>
@@ -100,7 +100,8 @@ Wisconsin-Madison and University of Konstanz.</license.copyrightOwners>
100100

101101
<!-- NB: Deploy releases to the SciJava Maven repository. -->
102102
<releaseProfiles>deploy-to-scijava</releaseProfiles>
103-
<scijava-table.version>0.6.1</scijava-table.version>
103+
<scijava-table.version>0.7.0</scijava-table.version>
104+
<scijava-common.version>2.84.0</scijava-common.version>
104105
</properties>
105106

106107
<dependencies>

src/main/java/org/scijava/table/DefaultTableIOPlugin.java

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030

3131
package org.scijava.table;
3232

33-
import java.net.URISyntaxException;
3433
import java.io.IOException;
3534
import java.util.ArrayList;
3635
import java.util.Arrays;
@@ -43,11 +42,10 @@
4342
import java.util.function.Function;
4443

4544
import org.scijava.Priority;
46-
import org.scijava.io.IOPlugin;
45+
import org.scijava.io.AbstractIOPlugin;
4746
import org.scijava.io.handle.DataHandle;
4847
import org.scijava.io.handle.DataHandleService;
4948
import org.scijava.io.location.Location;
50-
import org.scijava.io.location.LocationService;
5149
import org.scijava.plugin.Parameter;
5250
import org.scijava.plugin.Plugin;
5351
import org.scijava.table.io.ColumnTableIOOptions;
@@ -61,11 +59,8 @@
6159
* @author Leon Yang
6260
*/
6361
@SuppressWarnings("rawtypes")
64-
@Plugin(type = IOPlugin.class, priority = Priority.LOW)
65-
public class DefaultTableIOPlugin extends TableIOPlugin {
66-
67-
@Parameter
68-
private LocationService locationService;
62+
@Plugin(type = TableIOPlugin.class, priority = Priority.LOW)
63+
public class DefaultTableIOPlugin extends AbstractIOPlugin<Table> implements TableIOPlugin {
6964

7065
@Parameter
7166
private DataHandleService dataHandleService;
@@ -76,12 +71,28 @@ public class DefaultTableIOPlugin extends TableIOPlugin {
7671
.unmodifiableSet(new HashSet<>(Arrays.asList("csv", "txt", "prn", "dif",
7772
"rtf")));
7873

74+
@Override
75+
public boolean supportsOpen(final Location source) {
76+
final String ext = FileUtils.getExtension(source.getName()).toLowerCase();
77+
return SUPPORTED_EXTENSIONS.contains(ext);
78+
}
79+
7980
@Override
8081
public boolean supportsOpen(final String source) {
8182
final String ext = FileUtils.getExtension(source).toLowerCase();
8283
return SUPPORTED_EXTENSIONS.contains(ext);
8384
}
8485

86+
@Override
87+
public boolean supportsSave(Object data, String destination) {
88+
return supports(destination) && Table.class.isAssignableFrom(data.getClass());
89+
}
90+
91+
@Override
92+
public boolean supportsSave(final Location source) {
93+
return supportsOpen(source);
94+
}
95+
8596
@Override
8697
public boolean supportsSave(final String source) {
8798
return supportsOpen(source);
@@ -143,23 +154,16 @@ else if (line.charAt(idx) == separator) {
143154
}
144155

145156
@Override
146-
public GenericTable open(final String source, TableIOOptions options) throws IOException {
157+
public GenericTable open(final Location source, TableIOOptions options) throws IOException {
147158
return open(source, options.values);
148159
}
149160

150-
private GenericTable open(final String source, TableIOOptions.Values options) throws IOException {
161+
private GenericTable open(final Location source, TableIOOptions.Values options) throws IOException {
151162

152-
final Location sourceLocation;
153-
try {
154-
sourceLocation = locationService.resolve(source);
155-
}
156-
catch (final URISyntaxException exc) {
157-
throw new IOException("Unresolvable source: " + source, exc);
158-
}
159163
final GenericTable table = new DefaultGenericTable();
160164

161165
try (final DataHandle<? extends Location> handle = //
162-
dataHandleService.create(sourceLocation))
166+
dataHandleService.create(source))
163167
{
164168
if (!handle.exists()) {
165169
throw new IOException("Cannot open source");
@@ -180,7 +184,7 @@ private GenericTable open(final String source, TableIOOptions.Values options) th
180184
final String[] lines = text.split("\\R");
181185
if (lines.length == 0) return table;
182186
// process first line to get number of cols
183-
Map<Integer, Function<String, Object>> columnParsers = new HashMap<>();
187+
Map<Integer, Function<String, ?>> columnParsers = new HashMap<>();
184188
{
185189
final ArrayList<String> tokens = processRow(lines[0], separator, quote);
186190
if (readColHeaders) {
@@ -203,7 +207,7 @@ private GenericTable open(final String source, TableIOOptions.Values options) th
203207
table.appendRow();
204208
}
205209
for (int i = 0; i < cols.size(); i++) {
206-
Function<String, Object> parser = getParser(cols.get(i), i, options);
210+
Function<String, ?> parser = getParser(cols.get(i), i, options);
207211
columnParsers.put(i, parser);
208212
table.set(i, 0, parser.apply(cols.get(i)));
209213
}
@@ -236,7 +240,7 @@ private GenericTable open(final String source, TableIOOptions.Values options) th
236240
return table;
237241
}
238242

239-
private static Function<String, Object> getParser(String content, int column, TableIOOptions.Values options) {
243+
private static Function<String, ?> getParser(String content, int column, TableIOOptions.Values options) {
240244
ColumnTableIOOptions.Values colOptions = options.column(column);
241245
if(colOptions != null) return colOptions.parser();
242246
if(options.guessParser()) return guessParser(content);
@@ -263,29 +267,16 @@ static Function<String, Object> guessParser(String content) {
263267
}
264268

265269
@Override
266-
public void save(final Table table, final String destination)
267-
throws IOException {
268-
save(table, destination, new TableIOOptions().values);
269-
}
270-
271-
@Override
272-
public void save(final Table table, final String destination, final TableIOOptions options)
270+
public void save(final Table table, final Location destination, final TableIOOptions options)
273271
throws IOException {
274272
save(table, destination, options.values);
275273
}
276274

277-
private void save(final Table table, final String destination, final TableIOOptions.Values options)
275+
private void save(final Table table, final Location destination, final TableIOOptions.Values options)
278276
throws IOException {
279-
final Location dstLocation;
280-
try {
281-
dstLocation = locationService.resolve(destination);
282-
}
283-
catch (final URISyntaxException exc) {
284-
throw new IOException("Unresolvable destination: " + destination, exc);
285-
}
286277

287278
try (final DataHandle<Location> handle = //
288-
dataHandleService.create(dstLocation))
279+
dataHandleService.create(destination))
289280
{
290281
final boolean writeRH = options.writeRowHeaders();
291282
final boolean writeCH = options.writeColumnHeaders();

src/test/java/org/scijava/table/DefaultTableIOPluginTest.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,8 @@
3636

3737
import java.io.File;
3838
import java.io.IOException;
39-
import java.lang.reflect.Field;
4039
import java.util.ArrayList;
4140
import java.util.Arrays;
42-
import java.util.HashMap;
4341
import java.util.List;
4442

4543
import org.junit.After;
@@ -52,10 +50,8 @@
5250
import org.scijava.io.handle.DataHandleService;
5351
import org.scijava.io.location.FileLocation;
5452
import org.scijava.io.location.Location;
55-
import org.scijava.plugin.Parameter;
5653
import org.scijava.table.io.TableIOOptions;
5754
import org.scijava.table.io.TableIOPlugin;
58-
import org.scijava.util.ClassUtils;
5955

6056
/**
6157
* Tests for {@link DefaultTableIOPlugin}.
@@ -293,7 +289,7 @@ private Table openTable(final String tableSource,
293289
tempFiles.add(tempFile);
294290
try (DataHandle<Location> destHandle = dataHandleService.create(new FileLocation(tempFile))) {
295291
destHandle.write(tableSource.getBytes());
296-
result = tableIO.open(tempFile.getAbsolutePath(), options);
292+
result = tableIO.open(destHandle.get(), options);
297293
}
298294
return result;
299295
}
@@ -307,7 +303,7 @@ private String saveTable(final Table table,
307303
File tempFile = File.createTempFile("saveTest", ".txt");
308304
tempFiles.add(tempFile);
309305
try (DataHandle<Location> sourceHandle = dataHandleService.create(new FileLocation(tempFile))) {
310-
tableIO.save(table, tempFile.getAbsolutePath(), options);
306+
tableIO.save(table, sourceHandle.get(), options);
311307
result = sourceHandle.readString(Integer.MAX_VALUE);
312308
}
313309
return result;

0 commit comments

Comments
 (0)