2010-02-05 20:19:38 +01:00
|
|
|
package com.metaweb.gridworks.importers;
|
|
|
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.LineNumberReader;
|
|
|
|
import java.io.Reader;
|
|
|
|
import java.util.Properties;
|
|
|
|
|
|
|
|
import org.apache.commons.lang.NotImplementedException;
|
2010-04-01 00:34:21 +02:00
|
|
|
import org.apache.commons.lang.StringUtils;
|
2010-02-05 20:19:38 +01:00
|
|
|
|
2010-04-01 00:34:21 +02:00
|
|
|
import com.metaweb.gridworks.importers.parsers.CSVRowParser;
|
|
|
|
import com.metaweb.gridworks.importers.parsers.RowParser;
|
|
|
|
import com.metaweb.gridworks.importers.parsers.SeparatorRowParser;
|
2010-02-05 20:19:38 +01:00
|
|
|
import com.metaweb.gridworks.model.Column;
|
|
|
|
import com.metaweb.gridworks.model.Project;
|
|
|
|
import com.metaweb.gridworks.model.Row;
|
|
|
|
|
|
|
|
public class TsvCsvImporter implements Importer {
|
|
|
|
|
2010-03-03 05:19:58 +01:00
|
|
|
public void read(Reader reader, Project project, Properties options, int skip, int limit)
|
|
|
|
throws Exception {
|
|
|
|
|
|
|
|
LineNumberReader lnReader = new LineNumberReader(reader);
|
|
|
|
try {
|
2010-04-01 00:34:21 +02:00
|
|
|
String sep = options.getProperty("separator"); // auto-detect if not present
|
|
|
|
String line = null;
|
2010-03-03 05:19:58 +01:00
|
|
|
boolean first = true;
|
|
|
|
int cellCount = 1;
|
2010-04-01 00:34:21 +02:00
|
|
|
RowParser parser = (sep == null || (sep.length() == 0)) ? null : new SeparatorRowParser(sep);
|
2010-03-03 05:19:58 +01:00
|
|
|
|
|
|
|
int rowsWithData = 0;
|
|
|
|
while ((line = lnReader.readLine()) != null) {
|
|
|
|
if (line.trim().length() == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-04-01 00:34:21 +02:00
|
|
|
if (parser == null) {
|
2010-03-03 05:19:58 +01:00
|
|
|
int tab = line.indexOf('\t');
|
|
|
|
if (tab >= 0) {
|
|
|
|
sep = "\t";
|
2010-04-01 00:34:21 +02:00
|
|
|
parser = new SeparatorRowParser(sep);
|
2010-03-03 05:19:58 +01:00
|
|
|
} else {
|
|
|
|
sep = ",";
|
2010-04-01 00:34:21 +02:00
|
|
|
parser = new CSVRowParser();
|
2010-03-03 05:19:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (first) {
|
2010-04-01 00:34:21 +02:00
|
|
|
String[] cells = StringUtils.splitPreserveAllTokens(line, sep);
|
|
|
|
|
2010-03-03 05:19:58 +01:00
|
|
|
first = false;
|
|
|
|
for (int c = 0; c < cells.length; c++) {
|
|
|
|
String cell = cells[c];
|
|
|
|
if (cell.startsWith("\"") && cell.endsWith("\"")) {
|
|
|
|
cell = cell.substring(1, cell.length() - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
Column column = new Column(c, cell);
|
|
|
|
|
|
|
|
project.columnModel.columns.add(column);
|
|
|
|
}
|
|
|
|
|
|
|
|
cellCount = cells.length;
|
|
|
|
} else {
|
|
|
|
Row row = new Row(cellCount);
|
|
|
|
|
2010-04-01 00:34:21 +02:00
|
|
|
if (parser.parseRow(row, line)) {
|
2010-03-03 05:19:58 +01:00
|
|
|
rowsWithData++;
|
|
|
|
|
|
|
|
if (skip <= 0 || rowsWithData > skip) {
|
|
|
|
project.rows.add(row);
|
2010-03-12 01:23:01 +01:00
|
|
|
project.columnModel.setMaxCellIndex(row.cells.size());
|
2010-03-03 05:19:58 +01:00
|
|
|
|
|
|
|
if (limit > 0 && project.rows.size() >= limit) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
lnReader.close();
|
|
|
|
}
|
|
|
|
}
|
2010-02-05 20:19:38 +01:00
|
|
|
|
2010-03-03 05:19:58 +01:00
|
|
|
public void read(InputStream inputStream, Project project,
|
|
|
|
Properties options, int skip, int limit) throws Exception {
|
|
|
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
2010-02-05 20:19:38 +01:00
|
|
|
|
2010-03-03 05:19:58 +01:00
|
|
|
public boolean takesReader() {
|
|
|
|
return true;
|
|
|
|
}
|
2010-02-05 20:19:38 +01:00
|
|
|
}
|