Skip to content

Commit c6b82b5

Browse files
authored
Merge pull request #8 from scijava/add-table-io-service
Add TableIOService and test
2 parents eadc3c0 + 2ceaf97 commit c6b82b5

File tree

5 files changed

+273
-1
lines changed

5 files changed

+273
-1
lines changed

pom.xml

+3-1
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>26.0.0</version>
8+
<version>27.0.1</version>
99
<relativePath />
1010
</parent>
1111

@@ -100,6 +100,8 @@
100100
<license.copyrightOwners>Board of Regents of the University of
101101
Wisconsin-Madison, and Friedrich Miescher Institute for Biomedical Research.</license.copyrightOwners>
102102

103+
<scijava-common.version>2.81.0</scijava-common.version>
104+
103105
<!-- NB: Deploy releases to the SciJava Maven repository. -->
104106
<releaseProfiles>deploy-to-scijava</releaseProfiles>
105107
</properties>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*-
2+
* #%L
3+
* Table structures for SciJava.
4+
* %%
5+
* Copyright (C) 2012 - 2019 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 java.io.IOException;
34+
35+
import org.scijava.io.IOPlugin;
36+
import org.scijava.io.IOService;
37+
import org.scijava.plugin.Parameter;
38+
import org.scijava.plugin.Plugin;
39+
import org.scijava.service.AbstractService;
40+
import org.scijava.service.Service;
41+
import org.scijava.table.Table;
42+
43+
@Plugin(type = Service.class)
44+
public class DefaultTableIOService extends AbstractService implements
45+
TableIOService
46+
{
47+
48+
@Parameter
49+
private IOService ioService;
50+
51+
@Override
52+
public boolean canOpen(String source) {
53+
IOPlugin<?> opener = ioService.getOpener(source);
54+
if (opener == null) return false;
55+
return Table.class.isAssignableFrom(opener.getDataType());
56+
}
57+
58+
@Override
59+
public boolean canSave(Table<?, ?> table, String destination) {
60+
IOPlugin<Table<?, ?>> saver = ioService.getSaver(table, destination);
61+
if (saver == null) return false;
62+
return saver.supportsSave(destination);
63+
}
64+
65+
@Override
66+
public Table<?, ?> open(String source) throws IOException {
67+
IOPlugin<?> opener = ioService.getOpener(source);
68+
if (opener != null && Table.class.isAssignableFrom(opener.getDataType())) {
69+
return (Table<?, ?>) opener.open(source);
70+
}
71+
throw new UnsupportedOperationException("No compatible opener found.");
72+
}
73+
74+
@Override
75+
public void save(Table<?, ?> table, String destination) throws IOException {
76+
IOPlugin<Table<?, ?>> saver = ioService.getSaver(table, destination);
77+
if (saver != null) {
78+
saver.save(table, destination);
79+
}
80+
else {
81+
throw new UnsupportedOperationException("No compatible saver found.");
82+
}
83+
}
84+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*-
2+
* #%L
3+
* Table structures for SciJava.
4+
* %%
5+
* Copyright (C) 2012 - 2019 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 java.io.IOException;
34+
35+
import org.scijava.service.SciJavaService;
36+
import org.scijava.table.Table;
37+
38+
public interface TableIOService extends SciJavaService {
39+
40+
boolean canOpen(String source);
41+
42+
boolean canSave(Table<?, ?> table, String destination);
43+
44+
Table<?, ?> open(String source) throws IOException;
45+
46+
void save(Table<?, ?> table, String destination) throws IOException;
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*-
2+
* #%L
3+
* Table structures for SciJava.
4+
* %%
5+
* Copyright (C) 2012 - 2019 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+
package org.scijava.table;
31+
32+
import org.scijava.io.AbstractIOPlugin;
33+
34+
@SuppressWarnings("rawtypes")
35+
public class FakeTableIOPlugin extends AbstractIOPlugin<Table> {
36+
37+
@Override
38+
public Class<Table> getDataType() {
39+
return Table.class;
40+
}
41+
42+
@Override
43+
public boolean supportsOpen(String loc) {
44+
return loc.endsWith("csv");
45+
}
46+
47+
@Override
48+
public boolean supportsSave(String loc) {
49+
return loc.endsWith("csv");
50+
}
51+
52+
@Override
53+
public Table open(String loc) {
54+
return new DefaultGenericTable();
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*-
2+
* #%L
3+
* Table structures for SciJava.
4+
* %%
5+
* Copyright (C) 2012 - 2019 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;
32+
33+
import static org.junit.Assert.assertTrue;
34+
import static org.junit.Assert.fail;
35+
36+
import java.io.IOException;
37+
38+
import org.junit.After;
39+
import org.junit.Before;
40+
import org.junit.Test;
41+
import org.scijava.Context;
42+
import org.scijava.io.IOPlugin;
43+
import org.scijava.plugin.PluginInfo;
44+
import org.scijava.plugin.PluginService;
45+
import org.scijava.table.io.DefaultTableIOService;
46+
import org.scijava.table.io.TableIOService;
47+
48+
public class TableIOServiceTest {
49+
50+
private Context context;
51+
52+
@SuppressWarnings("rawtypes")
53+
@Before
54+
public void setUp() {
55+
context = new Context();
56+
PluginInfo<IOPlugin> info = PluginInfo.create(FakeTableIOPlugin.class,
57+
IOPlugin.class);
58+
context.service(PluginService.class).addPlugin(info);
59+
}
60+
61+
@After
62+
public void tearDown() {
63+
context.dispose();
64+
context = null;
65+
}
66+
67+
@Test
68+
public void testTableIOService() {
69+
String tableFile = "fakeTableFile.csv";
70+
GenericTable table = new DefaultGenericTable();
71+
TableIOService tableIOService = context.getService(TableIOService.class);
72+
assertTrue(tableIOService.getClass().equals(DefaultTableIOService.class));
73+
assertTrue(tableIOService.canOpen(tableFile));
74+
assertTrue(tableIOService.canSave(table, tableFile));
75+
try {
76+
Table<?, ?> data = tableIOService.open(tableFile);
77+
assertTrue(Table.class.isAssignableFrom(data.getClass()));
78+
}
79+
catch (IOException exc) {
80+
fail(exc.toString());
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)