Skip to content

Commit a305794

Browse files
authored
Merge pull request #14 from scijava/tableioservice-options
2 parents 5c5925f + fcbd5ab commit a305794

File tree

9 files changed

+550
-23
lines changed

9 files changed

+550
-23
lines changed

pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>org.scijava</groupId>
77
<artifactId>pom-scijava</artifactId>
8-
<version>28.0.0</version>
8+
<version>29.0.0-beta-3</version>
99
<relativePath />
1010
</parent>
1111

@@ -109,6 +109,11 @@ Wisconsin-Madison, and Friedrich Miescher Institute for Biomedical Research.</li
109109
<groupId>org.scijava</groupId>
110110
<artifactId>scijava-common</artifactId>
111111
</dependency>
112+
<dependency>
113+
<groupId>org.scijava</groupId>
114+
<artifactId>scijava-optional</artifactId>
115+
<version>1.0.0</version>
116+
</dependency>
112117

113118
<!-- Test scope dependencies -->
114119
<dependency>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* #%L
3+
* Table structures for SciJava.
4+
* %%
5+
* Copyright (C) 2012 - 2020 Board of Regents of the University of
6+
* Wisconsin-Madison, and Friedrich Miescher Institute for Biomedical Research.
7+
* %%
8+
* Redistribution and use in source and binary forms, with or without
9+
* modification, are permitted provided that the following conditions are met:
10+
*
11+
* 1. Redistributions of source code must retain the above copyright notice,
12+
* this list of conditions and the following disclaimer.
13+
* 2. Redistributions in binary form must reproduce the above copyright notice,
14+
* this list of conditions and the following disclaimer in the documentation
15+
* and/or other materials provided with the distribution.
16+
*
17+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
21+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27+
* POSSIBILITY OF SUCH DAMAGE.
28+
* #L%
29+
*/
30+
31+
package org.scijava.table.io;
32+
33+
import org.scijava.optional.AbstractOptions;
34+
35+
import java.util.function.Function;
36+
37+
/**
38+
* @author Deborah Schmidt
39+
*/
40+
public class ColumnTableIOOptions extends AbstractOptions<ColumnTableIOOptions> {
41+
42+
public final ColumnTableIOOptions.Values values = new ColumnTableIOOptions.Values();
43+
private static final String parserKey = "parser";
44+
private static final String formatterKey = "formatter";
45+
46+
ColumnTableIOOptions formatter(Function<Object, String> formatter) {
47+
return setValue(formatterKey, formatter);
48+
}
49+
50+
ColumnTableIOOptions parser(Function<String, Object> parser) {
51+
return setValue(parserKey, parser);
52+
}
53+
54+
public class Values extends AbstractValues {
55+
56+
public Function<Object, String> formatter() {
57+
return getValueOrDefault(formatterKey, String::valueOf);
58+
}
59+
60+
public Function<String, Object> parser() {
61+
return getValueOrDefault(parserKey, String::valueOf);
62+
}
63+
}
64+
65+
}

src/main/java/org/scijava/table/io/DefaultTableIOService.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ public boolean canSave(Table<?, ?> table, String destination) {
7171
throw new UnsupportedOperationException("No compatible opener found.");
7272
}
7373

74+
@Override
75+
public Table<?, ?> open(String source, TableIOOptions options) throws IOException {
76+
IOPlugin<?> opener = ioService.getOpener(source);
77+
if (opener != null && Table.class.isAssignableFrom(opener.getDataType())
78+
&& TableIOPlugin.class.isAssignableFrom(opener.getClass())) {
79+
return ((TableIOPlugin)opener).open(source, options);
80+
}
81+
throw new UnsupportedOperationException("No compatible opener found.");
82+
}
83+
7484
@Override
7585
public void save(Table<?, ?> table, String destination) throws IOException {
7686
IOPlugin<Table<?, ?>> saver = ioService.getSaver(table, destination);
@@ -81,4 +91,15 @@ public void save(Table<?, ?> table, String destination) throws IOException {
8191
throw new UnsupportedOperationException("No compatible saver found.");
8292
}
8393
}
94+
95+
@Override
96+
public void save(Table<?, ?> table, String destination, TableIOOptions options) throws IOException {
97+
IOPlugin<Table> saver = ioService.getSaver(table, destination);
98+
if (saver != null && TableIOPlugin.class.isAssignableFrom(saver.getClass())) {
99+
((TableIOPlugin)saver).save(table, destination, options);
100+
}
101+
else {
102+
throw new UnsupportedOperationException("No compatible saver found.");
103+
}
104+
}
84105
}

src/main/java/org/scijava/table/io/ExportTableCommand.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@
4949
@Menu(label = "Export"), @Menu(label = "Table...") })
5050
public class ExportTableCommand extends ContextCommand {
5151

52+
@Parameter(required = false, label = "Write column headers")
53+
private boolean writeColHeaders = true;
54+
55+
@Parameter(required = false, label = "Write row headers")
56+
private boolean writeRowHeaders = true;
57+
58+
@Parameter(required = false, label = "Column delimiter")
59+
private char columnDelimiter = ',';
60+
5261
@Parameter
5362
private LogService log;
5463

@@ -68,7 +77,11 @@ public class ExportTableCommand extends ContextCommand {
6877
@Override
6978
public void run() {
7079
try {
71-
tableIO.save(tableDisplay.get(0), outputFile.getAbsolutePath());
80+
TableIOOptions options = new TableIOOptions()
81+
.writeColumnHeaders(writeColHeaders)
82+
.writeRowHeaders(writeRowHeaders)
83+
.columnDelimiter(columnDelimiter);
84+
tableIO.save(tableDisplay.get(0), outputFile.getAbsolutePath(), options);
7285
}
7386
catch (IOException exc) {
7487
log.error(exc);

0 commit comments

Comments
 (0)