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;
|
|
|
|
|
|
|
|
import com.metaweb.gridworks.model.Column;
|
|
|
|
import com.metaweb.gridworks.model.Project;
|
|
|
|
import com.metaweb.gridworks.model.Row;
|
|
|
|
|
|
|
|
public class TsvCsvImporter implements Importer {
|
|
|
|
|
2010-02-24 19:52:54 +01:00
|
|
|
public void read(Reader reader, Project project, Properties options, int skip, int limit)
|
2010-02-05 20:19:38 +01:00
|
|
|
throws Exception {
|
|
|
|
|
|
|
|
LineNumberReader lnReader = new LineNumberReader(reader);
|
|
|
|
try {
|
|
|
|
String sep = null; // auto-detect TSV or CSV
|
|
|
|
String line = null;
|
|
|
|
boolean first = true;
|
|
|
|
int cellCount = 1;
|
|
|
|
|
2010-02-24 19:52:54 +01:00
|
|
|
int rowsWithData = 0;
|
2010-02-05 20:19:38 +01:00
|
|
|
while ((line = lnReader.readLine()) != null) {
|
2010-02-07 07:16:46 +01:00
|
|
|
if (line.trim().length() == 0) {
|
2010-02-05 20:19:38 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sep == null) {
|
|
|
|
int tab = line.indexOf('\t');
|
|
|
|
if (tab >= 0) {
|
|
|
|
sep = "\t";
|
|
|
|
} else {
|
|
|
|
sep = ",";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (first) {
|
|
|
|
String[] cells = line.split(sep);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
if ((sep.charAt(0) == ',') ? ImporterUtilities.parseCSVIntoRow(row, line) : ImporterUtilities.parseTSVIntoRow(row, line)) {
|
2010-02-24 19:52:54 +01:00
|
|
|
rowsWithData++;
|
|
|
|
|
|
|
|
if (skip <= 0 || rowsWithData > skip) {
|
|
|
|
project.rows.add(row);
|
|
|
|
project.columnModel.setMaxCellIndex(Math.max(project.columnModel.getMaxCellIndex(), row.cells.size()));
|
|
|
|
|
|
|
|
if (limit > 0 && project.rows.size() >= limit) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-02-05 20:19:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
lnReader.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void read(InputStream inputStream, Project project,
|
2010-02-24 19:52:54 +01:00
|
|
|
Properties options, int skip, int limit) throws Exception {
|
2010-02-05 20:19:38 +01:00
|
|
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean takesReader() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|