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

Commit e270cc6

Browse files
authored
Merge pull request #7 from scijava/use-location
2 parents 038d287 + ac88508 commit e270cc6

File tree

3 files changed

+47
-61
lines changed

3 files changed

+47
-61
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: 35 additions & 48 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,25 +240,21 @@ 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);
243247
return options.parser();
244248
}
245249

246-
static Function<String, Object> guessParser(String content) {
247-
try {
248-
Integer.valueOf(content);
249-
return Integer::valueOf;
250-
} catch(NumberFormatException ignored) {}
250+
static Function<String, ?> guessParser(String content) {
251251
try {
252-
Long.valueOf(content);
253-
return Long::valueOf;
254-
} catch(NumberFormatException ignored) {}
255-
try {
256-
Double.valueOf(content);
257-
return Double::valueOf;
252+
Function<String, ?> function = s -> Double.valueOf(s
253+
.replace("infinity", "Infinity")
254+
.replace("Nan", "NaN")
255+
);
256+
function.apply(content);
257+
return function;
258258
} catch(NumberFormatException ignored) {}
259259
if(content.equalsIgnoreCase("true")||content.equalsIgnoreCase("false")) {
260260
return Boolean::valueOf;
@@ -263,29 +263,16 @@ static Function<String, Object> guessParser(String content) {
263263
}
264264

265265
@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)
266+
public void save(final Table table, final Location destination, final TableIOOptions options)
273267
throws IOException {
274268
save(table, destination, options.values);
275269
}
276270

277-
private void save(final Table table, final String destination, final TableIOOptions.Values options)
271+
private void save(final Table table, final Location destination, final TableIOOptions.Values options)
278272
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-
}
286273

287274
try (final DataHandle<Location> handle = //
288-
dataHandleService.create(dstLocation))
275+
dataHandleService.create(destination))
289276
{
290277
final boolean writeRH = options.writeRowHeaders();
291278
final boolean writeCH = options.writeColumnHeaders();

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

Lines changed: 8 additions & 10 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}.
@@ -115,7 +111,7 @@ public void testDefaultOptions() throws IOException {
115111
}
116112

117113
/**
118-
* Tests if quoting works in different senarios.
114+
* Tests if quoting works in different scenarios.
119115
*/
120116
@Test
121117
public void testQuote() {
@@ -164,7 +160,7 @@ public void testQuote() {
164160
}
165161

166162
/**
167-
* Tests if samll tables could be opened/saved correctly.
163+
* Tests if small tables can be opened/saved correctly.
168164
*/
169165
@Test
170166
public void testSmallTables() {
@@ -254,11 +250,13 @@ public void testGuessParser() {
254250
assertEquals(false, DefaultTableIOPlugin.guessParser("false").apply("false"));
255251
assertEquals(123.0, DefaultTableIOPlugin.guessParser("123.0").apply("123.0"));
256252
assertEquals(-123.0, DefaultTableIOPlugin.guessParser("-123.0").apply("-123.0"));
257-
assertEquals(3, DefaultTableIOPlugin.guessParser("3").apply("3"));
258-
assertEquals(36564573745634564L, DefaultTableIOPlugin.guessParser("36564573745634564").apply("36564573745634564"));
253+
assertEquals(3.0, DefaultTableIOPlugin.guessParser("3").apply("3"));
254+
assertEquals(36564573745634564d, DefaultTableIOPlugin.guessParser("36564573745634564").apply("36564573745634564"));
259255
assertEquals(1234567890.0987654321, DefaultTableIOPlugin.guessParser("1.2345678900987654E9").apply("1.2345678900987654E9"));
260256
assertEquals(Double.NaN, DefaultTableIOPlugin.guessParser("NaN").apply("NaN"));
257+
assertEquals(Double.NaN, DefaultTableIOPlugin.guessParser("Nan").apply("Nan"));
261258
assertEquals(Double.NEGATIVE_INFINITY, DefaultTableIOPlugin.guessParser("-Infinity").apply("-Infinity"));
259+
assertEquals(Double.POSITIVE_INFINITY, DefaultTableIOPlugin.guessParser("infinity").apply("infinity"));
262260
assertEquals(0.0, DefaultTableIOPlugin.guessParser("0.0").apply("0.0"));
263261
}
264262

@@ -293,7 +291,7 @@ private Table openTable(final String tableSource,
293291
tempFiles.add(tempFile);
294292
try (DataHandle<Location> destHandle = dataHandleService.create(new FileLocation(tempFile))) {
295293
destHandle.write(tableSource.getBytes());
296-
result = tableIO.open(tempFile.getAbsolutePath(), options);
294+
result = tableIO.open(destHandle.get(), options);
297295
}
298296
return result;
299297
}
@@ -307,7 +305,7 @@ private String saveTable(final Table table,
307305
File tempFile = File.createTempFile("saveTest", ".txt");
308306
tempFiles.add(tempFile);
309307
try (DataHandle<Location> sourceHandle = dataHandleService.create(new FileLocation(tempFile))) {
310-
tableIO.save(table, tempFile.getAbsolutePath(), options);
308+
tableIO.save(table, sourceHandle.get(), options);
311309
result = sourceHandle.readString(Integer.MAX_VALUE);
312310
}
313311
return result;

0 commit comments

Comments
 (0)