30
30
31
31
package org .scijava .table ;
32
32
33
- import java .net .URISyntaxException ;
34
33
import java .io .IOException ;
35
34
import java .util .ArrayList ;
36
35
import java .util .Arrays ;
43
42
import java .util .function .Function ;
44
43
45
44
import org .scijava .Priority ;
46
- import org .scijava .io .IOPlugin ;
45
+ import org .scijava .io .AbstractIOPlugin ;
47
46
import org .scijava .io .handle .DataHandle ;
48
47
import org .scijava .io .handle .DataHandleService ;
49
48
import org .scijava .io .location .Location ;
50
- import org .scijava .io .location .LocationService ;
51
49
import org .scijava .plugin .Parameter ;
52
50
import org .scijava .plugin .Plugin ;
53
51
import org .scijava .table .io .ColumnTableIOOptions ;
61
59
* @author Leon Yang
62
60
*/
63
61
@ 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 {
69
64
70
65
@ Parameter
71
66
private DataHandleService dataHandleService ;
@@ -76,12 +71,28 @@ public class DefaultTableIOPlugin extends TableIOPlugin {
76
71
.unmodifiableSet (new HashSet <>(Arrays .asList ("csv" , "txt" , "prn" , "dif" ,
77
72
"rtf" )));
78
73
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
+
79
80
@ Override
80
81
public boolean supportsOpen (final String source ) {
81
82
final String ext = FileUtils .getExtension (source ).toLowerCase ();
82
83
return SUPPORTED_EXTENSIONS .contains (ext );
83
84
}
84
85
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
+
85
96
@ Override
86
97
public boolean supportsSave (final String source ) {
87
98
return supportsOpen (source );
@@ -143,23 +154,16 @@ else if (line.charAt(idx) == separator) {
143
154
}
144
155
145
156
@ Override
146
- public GenericTable open (final String source , TableIOOptions options ) throws IOException {
157
+ public GenericTable open (final Location source , TableIOOptions options ) throws IOException {
147
158
return open (source , options .values );
148
159
}
149
160
150
- private GenericTable open (final String source , TableIOOptions .Values options ) throws IOException {
161
+ private GenericTable open (final Location source , TableIOOptions .Values options ) throws IOException {
151
162
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
- }
159
163
final GenericTable table = new DefaultGenericTable ();
160
164
161
165
try (final DataHandle <? extends Location > handle = //
162
- dataHandleService .create (sourceLocation ))
166
+ dataHandleService .create (source ))
163
167
{
164
168
if (!handle .exists ()) {
165
169
throw new IOException ("Cannot open source" );
@@ -180,7 +184,7 @@ private GenericTable open(final String source, TableIOOptions.Values options) th
180
184
final String [] lines = text .split ("\\ R" );
181
185
if (lines .length == 0 ) return table ;
182
186
// 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 <>();
184
188
{
185
189
final ArrayList <String > tokens = processRow (lines [0 ], separator , quote );
186
190
if (readColHeaders ) {
@@ -203,7 +207,7 @@ private GenericTable open(final String source, TableIOOptions.Values options) th
203
207
table .appendRow ();
204
208
}
205
209
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 );
207
211
columnParsers .put (i , parser );
208
212
table .set (i , 0 , parser .apply (cols .get (i )));
209
213
}
@@ -236,25 +240,21 @@ private GenericTable open(final String source, TableIOOptions.Values options) th
236
240
return table ;
237
241
}
238
242
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 ) {
240
244
ColumnTableIOOptions .Values colOptions = options .column (column );
241
245
if (colOptions != null ) return colOptions .parser ();
242
246
if (options .guessParser ()) return guessParser (content );
243
247
return options .parser ();
244
248
}
245
249
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 ) {
251
251
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 ;
258
258
} catch (NumberFormatException ignored ) {}
259
259
if (content .equalsIgnoreCase ("true" )||content .equalsIgnoreCase ("false" )) {
260
260
return Boolean ::valueOf ;
@@ -263,29 +263,16 @@ static Function<String, Object> guessParser(String content) {
263
263
}
264
264
265
265
@ 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 )
273
267
throws IOException {
274
268
save (table , destination , options .values );
275
269
}
276
270
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 )
278
272
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
- }
286
273
287
274
try (final DataHandle <Location > handle = //
288
- dataHandleService .create (dstLocation ))
275
+ dataHandleService .create (destination ))
289
276
{
290
277
final boolean writeRH = options .writeRowHeaders ();
291
278
final boolean writeCH = options .writeColumnHeaders ();
0 commit comments