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

Commit

Permalink
Merge pull request #7 from scijava/use-location
Browse files Browse the repository at this point in the history
  • Loading branch information
imagejan authored Aug 14, 2020
2 parents 038d287 + ac88508 commit e270cc6
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 61 deletions.
7 changes: 4 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
<parent>
<groupId>org.scijava</groupId>
<artifactId>pom-scijava</artifactId>
<version>29.2.0</version>
<version>29.2.1</version>
<relativePath />
</parent>

<artifactId>scijava-plugins-io-table</artifactId>
<version>0.3.1-SNAPSHOT</version>
<version>0.4.0-SNAPSHOT</version>

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

<!-- NB: Deploy releases to the SciJava Maven repository. -->
<releaseProfiles>deploy-to-scijava</releaseProfiles>
<scijava-table.version>0.6.1</scijava-table.version>
<scijava-table.version>0.7.0</scijava-table.version>
<scijava-common.version>2.84.0</scijava-common.version>
</properties>

<dependencies>
Expand Down
83 changes: 35 additions & 48 deletions src/main/java/org/scijava/table/DefaultTableIOPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@

package org.scijava.table;

import java.net.URISyntaxException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -43,11 +42,10 @@
import java.util.function.Function;

import org.scijava.Priority;
import org.scijava.io.IOPlugin;
import org.scijava.io.AbstractIOPlugin;
import org.scijava.io.handle.DataHandle;
import org.scijava.io.handle.DataHandleService;
import org.scijava.io.location.Location;
import org.scijava.io.location.LocationService;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;
import org.scijava.table.io.ColumnTableIOOptions;
Expand All @@ -61,11 +59,8 @@
* @author Leon Yang
*/
@SuppressWarnings("rawtypes")
@Plugin(type = IOPlugin.class, priority = Priority.LOW)
public class DefaultTableIOPlugin extends TableIOPlugin {

@Parameter
private LocationService locationService;
@Plugin(type = TableIOPlugin.class, priority = Priority.LOW)
public class DefaultTableIOPlugin extends AbstractIOPlugin<Table> implements TableIOPlugin {

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

@Override
public boolean supportsOpen(final Location source) {
final String ext = FileUtils.getExtension(source.getName()).toLowerCase();
return SUPPORTED_EXTENSIONS.contains(ext);
}

@Override
public boolean supportsOpen(final String source) {
final String ext = FileUtils.getExtension(source).toLowerCase();
return SUPPORTED_EXTENSIONS.contains(ext);
}

@Override
public boolean supportsSave(Object data, String destination) {
return supports(destination) && Table.class.isAssignableFrom(data.getClass());
}

@Override
public boolean supportsSave(final Location source) {
return supportsOpen(source);
}

@Override
public boolean supportsSave(final String source) {
return supportsOpen(source);
Expand Down Expand Up @@ -143,23 +154,16 @@ else if (line.charAt(idx) == separator) {
}

@Override
public GenericTable open(final String source, TableIOOptions options) throws IOException {
public GenericTable open(final Location source, TableIOOptions options) throws IOException {
return open(source, options.values);
}

private GenericTable open(final String source, TableIOOptions.Values options) throws IOException {
private GenericTable open(final Location source, TableIOOptions.Values options) throws IOException {

final Location sourceLocation;
try {
sourceLocation = locationService.resolve(source);
}
catch (final URISyntaxException exc) {
throw new IOException("Unresolvable source: " + source, exc);
}
final GenericTable table = new DefaultGenericTable();

try (final DataHandle<? extends Location> handle = //
dataHandleService.create(sourceLocation))
dataHandleService.create(source))
{
if (!handle.exists()) {
throw new IOException("Cannot open source");
Expand All @@ -180,7 +184,7 @@ private GenericTable open(final String source, TableIOOptions.Values options) th
final String[] lines = text.split("\\R");
if (lines.length == 0) return table;
// process first line to get number of cols
Map<Integer, Function<String, Object>> columnParsers = new HashMap<>();
Map<Integer, Function<String, ?>> columnParsers = new HashMap<>();
{
final ArrayList<String> tokens = processRow(lines[0], separator, quote);
if (readColHeaders) {
Expand All @@ -203,7 +207,7 @@ private GenericTable open(final String source, TableIOOptions.Values options) th
table.appendRow();
}
for (int i = 0; i < cols.size(); i++) {
Function<String, Object> parser = getParser(cols.get(i), i, options);
Function<String, ?> parser = getParser(cols.get(i), i, options);
columnParsers.put(i, parser);
table.set(i, 0, parser.apply(cols.get(i)));
}
Expand Down Expand Up @@ -236,25 +240,21 @@ private GenericTable open(final String source, TableIOOptions.Values options) th
return table;
}

private static Function<String, Object> getParser(String content, int column, TableIOOptions.Values options) {
private static Function<String, ?> getParser(String content, int column, TableIOOptions.Values options) {
ColumnTableIOOptions.Values colOptions = options.column(column);
if(colOptions != null) return colOptions.parser();
if(options.guessParser()) return guessParser(content);
return options.parser();
}

static Function<String, Object> guessParser(String content) {
try {
Integer.valueOf(content);
return Integer::valueOf;
} catch(NumberFormatException ignored) {}
static Function<String, ?> guessParser(String content) {
try {
Long.valueOf(content);
return Long::valueOf;
} catch(NumberFormatException ignored) {}
try {
Double.valueOf(content);
return Double::valueOf;
Function<String, ?> function = s -> Double.valueOf(s
.replace("infinity", "Infinity")
.replace("Nan", "NaN")
);
function.apply(content);
return function;
} catch(NumberFormatException ignored) {}
if(content.equalsIgnoreCase("true")||content.equalsIgnoreCase("false")) {
return Boolean::valueOf;
Expand All @@ -263,29 +263,16 @@ static Function<String, Object> guessParser(String content) {
}

@Override
public void save(final Table table, final String destination)
throws IOException {
save(table, destination, new TableIOOptions().values);
}

@Override
public void save(final Table table, final String destination, final TableIOOptions options)
public void save(final Table table, final Location destination, final TableIOOptions options)
throws IOException {
save(table, destination, options.values);
}

private void save(final Table table, final String destination, final TableIOOptions.Values options)
private void save(final Table table, final Location destination, final TableIOOptions.Values options)
throws IOException {
final Location dstLocation;
try {
dstLocation = locationService.resolve(destination);
}
catch (final URISyntaxException exc) {
throw new IOException("Unresolvable destination: " + destination, exc);
}

try (final DataHandle<Location> handle = //
dataHandleService.create(dstLocation))
dataHandleService.create(destination))
{
final boolean writeRH = options.writeRowHeaders();
final boolean writeCH = options.writeColumnHeaders();
Expand Down
18 changes: 8 additions & 10 deletions src/test/java/org/scijava/table/DefaultTableIOPluginTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,8 @@

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import org.junit.After;
Expand All @@ -52,10 +50,8 @@
import org.scijava.io.handle.DataHandleService;
import org.scijava.io.location.FileLocation;
import org.scijava.io.location.Location;
import org.scijava.plugin.Parameter;
import org.scijava.table.io.TableIOOptions;
import org.scijava.table.io.TableIOPlugin;
import org.scijava.util.ClassUtils;

/**
* Tests for {@link DefaultTableIOPlugin}.
Expand Down Expand Up @@ -115,7 +111,7 @@ public void testDefaultOptions() throws IOException {
}

/**
* Tests if quoting works in different senarios.
* Tests if quoting works in different scenarios.
*/
@Test
public void testQuote() {
Expand Down Expand Up @@ -164,7 +160,7 @@ public void testQuote() {
}

/**
* Tests if samll tables could be opened/saved correctly.
* Tests if small tables can be opened/saved correctly.
*/
@Test
public void testSmallTables() {
Expand Down Expand Up @@ -254,11 +250,13 @@ public void testGuessParser() {
assertEquals(false, DefaultTableIOPlugin.guessParser("false").apply("false"));
assertEquals(123.0, DefaultTableIOPlugin.guessParser("123.0").apply("123.0"));
assertEquals(-123.0, DefaultTableIOPlugin.guessParser("-123.0").apply("-123.0"));
assertEquals(3, DefaultTableIOPlugin.guessParser("3").apply("3"));
assertEquals(36564573745634564L, DefaultTableIOPlugin.guessParser("36564573745634564").apply("36564573745634564"));
assertEquals(3.0, DefaultTableIOPlugin.guessParser("3").apply("3"));
assertEquals(36564573745634564d, DefaultTableIOPlugin.guessParser("36564573745634564").apply("36564573745634564"));
assertEquals(1234567890.0987654321, DefaultTableIOPlugin.guessParser("1.2345678900987654E9").apply("1.2345678900987654E9"));
assertEquals(Double.NaN, DefaultTableIOPlugin.guessParser("NaN").apply("NaN"));
assertEquals(Double.NaN, DefaultTableIOPlugin.guessParser("Nan").apply("Nan"));
assertEquals(Double.NEGATIVE_INFINITY, DefaultTableIOPlugin.guessParser("-Infinity").apply("-Infinity"));
assertEquals(Double.POSITIVE_INFINITY, DefaultTableIOPlugin.guessParser("infinity").apply("infinity"));
assertEquals(0.0, DefaultTableIOPlugin.guessParser("0.0").apply("0.0"));
}

Expand Down Expand Up @@ -293,7 +291,7 @@ private Table openTable(final String tableSource,
tempFiles.add(tempFile);
try (DataHandle<Location> destHandle = dataHandleService.create(new FileLocation(tempFile))) {
destHandle.write(tableSource.getBytes());
result = tableIO.open(tempFile.getAbsolutePath(), options);
result = tableIO.open(destHandle.get(), options);
}
return result;
}
Expand All @@ -307,7 +305,7 @@ private String saveTable(final Table table,
File tempFile = File.createTempFile("saveTest", ".txt");
tempFiles.add(tempFile);
try (DataHandle<Location> sourceHandle = dataHandleService.create(new FileLocation(tempFile))) {
tableIO.save(table, tempFile.getAbsolutePath(), options);
tableIO.save(table, sourceHandle.get(), options);
result = sourceHandle.readString(Integer.MAX_VALUE);
}
return result;
Expand Down

0 comments on commit e270cc6

Please sign in to comment.