Commit 0a0121c3 authored by Matija Obreza's avatar Matija Obreza

Read CSV data

parent b357edb0
......@@ -72,6 +72,11 @@
<artifactId>super-csv</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
<build>
<plugins>
......
package org.genesys2.anno.gui;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ExecutorService;
......@@ -172,7 +174,7 @@ public class AppWindow {
display.sleep();
}
}
// Close thread pool
threadPool.shutdown();
}
......@@ -374,13 +376,13 @@ public class AppWindow {
i++;
}
}
CTabItem newSheetTab = SheetTabFactory.createComposite(
tabFolder, currentSheet);
final SheetDisplay sheetDisplay = (SheetDisplay) newSheetTab
.getControl();
// Load stuff
threadPool.execute(new Runnable() {
......@@ -403,6 +405,10 @@ public class AppWindow {
} catch (UnsupportedDataFormatException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
......
package org.genesys2.anno.gui;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
public interface DataSourceLoader {
......@@ -9,5 +11,5 @@ public interface DataSourceLoader {
DataSourceFile loadDataSource(File file);
List<Object[]> loadRows(DataSourceSheet sheet, int maxRows) throws UnsupportedDataFormatException;
List<Object[]> loadRows(DataSourceSheet sheet, int maxRows) throws UnsupportedDataFormatException, FileNotFoundException, IOException;
}
package org.genesys2.anno.gui;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
......@@ -55,7 +57,7 @@ public class DataSourceLoaderImpl implements DataSourceLoader {
@Override
public List<Object[]> loadRows(DataSourceSheet sheet, int maxRows)
throws UnsupportedDataFormatException {
throws UnsupportedDataFormatException, FileNotFoundException, IOException {
// Iterate over parsers and see if something comes out
for (DataSourceParser parser : parsers) {
try {
......
package org.genesys2.anno.gui;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
......@@ -12,6 +14,6 @@ public interface DataSourceParser {
throws UnsupportedDataFormatException;
List<Object[]> loadRows(File sourceFile, String sheetName, int maxRows)
throws UnsupportedDataFormatException;
throws UnsupportedDataFormatException, FileNotFoundException, IOException;
}
package org.genesys2.anno.parser;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.genesys2.anno.gui.DataSourceSheet;
import org.genesys2.anno.gui.UnsupportedDataFormatException;
import org.supercsv.io.CsvListReader;
import org.supercsv.prefs.CsvPreference;
public class CsvDataSourceParser extends BasicDataSourceParser {
......@@ -23,16 +30,43 @@ public class CsvDataSourceParser extends BasicDataSourceParser {
throws UnsupportedDataFormatException {
ArrayList<DataSourceSheet> sheets = new ArrayList<DataSourceSheet>();
DataSourceSheet sheetDataSource = new DataSourceSheet(sourceFile);
sheetDataSource.setSheetName("CSV Data");
sheetDataSource.setSheetName(sourceFile.getName());
sheets.add(sheetDataSource);
return sheets;
}
@Override
public List<Object[]> loadRows(File sourceFile, String sheetName,
int maxRows) throws UnsupportedDataFormatException {
int maxRows) throws UnsupportedDataFormatException, IOException {
List<Object[]> rows = new ArrayList<Object[]>();
System.err.println("NOT READING CSV!");
return rows;
Reader reader = null;
try {
reader = new FileReader(sourceFile);
} catch (FileNotFoundException e) {
throw e;
}
try {
CsvPreference preferences = CsvPreference.STANDARD_PREFERENCE;
CsvListReader listReader = new CsvListReader(reader, preferences);
// rows.add(listReader.getHeader(true);
for (int i = 0; i < maxRows; i++) {
List<String> row = listReader.read();
if (row == null)
break;
rows.add(row.toArray());
}
listReader.close();
return rows;
} catch (IOException e) {
throw e;
} finally {
IOUtils.closeQuietly(reader);
}
}
}
package org.genesys2.anno;
import static org.junit.Assert.fail;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.junit.Test;
import org.supercsv.io.CsvListReader;
import org.supercsv.prefs.CsvPreference;
public class CsvFileTest {
@Test
public void test1() {
File file = new File("/Users/matijaobreza/Downloads", "seldata-1.csv");
Reader reader = null;
try {
reader = new FileReader(file);
} catch (FileNotFoundException e) {
fail(e.getMessage());
}
try {
CsvPreference preferences = CsvPreference.STANDARD_PREFERENCE;
CsvListReader listReader = new CsvListReader(reader, preferences);
listReader.getHeader(true); // skip the header (can't be used with
// CsvListReader)
for (int i = 0; i < 100; i++) {
List<String> row = listReader.read();
System.err.println(ArrayUtils.toString(row));
}
listReader.close();
} catch (IOException e) {
fail(e.getMessage());
} finally {
IOUtils.closeQuietly(reader);
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment