Commit 4dc3a5a0 authored by Matija Obreza's avatar Matija Obreza
Browse files

CsvDataSourceSheet

parent b3576ec5
......@@ -81,8 +81,7 @@ public class DataSourceLoaderImpl implements DataSourceLoader {
for (DataSourceParser parser : parsers) {
try {
if (parser.supports(sheet.getSourceFile())) {
return parser.loadRows(sheet.getSourceFile(),
sheet.getSheetName(), maxRows);
return parser.loadRows(sheet, maxRows);
}
} catch (UnsupportedDataFormatException e) {
_log.debug(e.getMessage(), e);
......
......@@ -29,7 +29,6 @@ public interface DataSourceParser {
Collection<? extends DataSourceSheet> findSheets(File sourceFile)
throws UnsupportedDataFormatException;
List<Object[]> loadRows(File sourceFile, String sheetName, int maxRows)
throws UnsupportedDataFormatException, FileNotFoundException, IOException;
List<Object[]> loadRows(DataSourceSheet sheet, int maxRows) throws UnsupportedDataFormatException, FileNotFoundException, IOException;
}
......@@ -24,6 +24,7 @@ import java.util.Map;
import org.apache.log4j.Logger;
import org.genesys2.anno.model.Column;
import org.genesys2.anno.parser.CsvDataSourceSheet;
public class DataSourceSheet extends AbstractModelObject {
private static final Logger _log = Logger.getLogger(DataSourceSheet.class);
......@@ -160,4 +161,8 @@ public class DataSourceSheet extends AbstractModelObject {
public Column getColumn(int columnIndex) {
return this.columns.get(columnIndex);
}
public boolean isCsv() {
return this instanceof CsvDataSourceSheet;
}
}
......@@ -28,15 +28,17 @@ import java.util.Collection;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
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 {
private static final Logger _log = Logger.getLogger(CsvDataSourceParser.class);
private final String[] supportedExtensions = new String[] { ".csv", ".txt",
".tab" };
private final String[] supportedExtensions = new String[] { ".csv", ".txt", ".tab" };
@Override
public String[] getSupportedExtensions() {
......@@ -44,32 +46,43 @@ public class CsvDataSourceParser extends BasicDataSourceParser {
}
@Override
public Collection<? extends DataSourceSheet> findSheets(File sourceFile)
throws UnsupportedDataFormatException {
public Collection<? extends DataSourceSheet> findSheets(File sourceFile) throws UnsupportedDataFormatException {
ArrayList<DataSourceSheet> sheets = new ArrayList<DataSourceSheet>();
DataSourceSheet sheetDataSource = new DataSourceSheet(sourceFile);
DataSourceSheet sheetDataSource = new CsvDataSourceSheet(sourceFile);
sheetDataSource.setSheetName(sourceFile.getName());
sheets.add(sheetDataSource);
return sheets;
}
@Override
public List<Object[]> loadRows(File sourceFile, String sheetName,
int maxRows) throws UnsupportedDataFormatException, IOException {
public List<Object[]> loadRows(DataSourceSheet dataSourceSheet, int maxRows) throws UnsupportedDataFormatException, IOException {
List<Object[]> rows = new ArrayList<Object[]>();
File sourceFile = dataSourceSheet.getSourceFile();
String charsetName = "UTF-8";
char quoteChar = '"';
int delimiterChar = ',';
String eolSymbols = "\r\n";
if (dataSourceSheet instanceof CsvDataSourceSheet) {
CsvDataSourceSheet csvSheet = (CsvDataSourceSheet) dataSourceSheet;
charsetName = StringUtils.defaultIfBlank(csvSheet.getCharset(), charsetName);
quoteChar = csvSheet.getQuoteChar();
delimiterChar = csvSheet.getDelimiterChar();
eolSymbols = csvSheet.getEolSymbols();
}
_log.info("Using charset " + charsetName);
Reader reader = null;
try {
reader = new InputStreamReader(new FileInputStream(sourceFile),
Charset.forName(charsetName));
reader = new InputStreamReader(new FileInputStream(sourceFile), Charset.forName(charsetName));
} catch (FileNotFoundException e) {
throw e;
}
try {
CsvPreference preferences = CsvPreference.STANDARD_PREFERENCE;
CsvPreference preferences = new CsvPreference.Builder(quoteChar, delimiterChar, eolSymbols).build();
CsvListReader listReader = new CsvListReader(reader, preferences);
// rows.add(listReader.getHeader(true);
......
/**
* Copyright 2014 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package org.genesys2.anno.parser;
import java.io.File;
import org.genesys2.anno.gui.DataSourceSheet;
public class CsvDataSourceSheet extends DataSourceSheet {
private String charset = "UTF-8";
private char quoteChar = '"';
private char delimiterChar = ',';
private boolean unixEol;
public CsvDataSourceSheet(File sourceFile) {
super(sourceFile);
}
public String getCharset() {
return this.charset;
}
public void setCharset(String charset) {
this.charset = charset;
firePropertyChange("charset", null, this.charset);
}
public char getQuoteChar() {
return this.quoteChar;
}
public void setQuoteChar(char quoteChar) {
this.quoteChar = quoteChar;
firePropertyChange("quoteChar", null, this.charset);
}
public char getDelimiterChar() {
return this.delimiterChar;
}
public void setDelimiterChar(char delimiterChar) {
this.delimiterChar = delimiterChar;
firePropertyChange("delimiterChar", null, this.delimiterChar);
}
public String getEolSymbols() {
return unixEol ? "\r" : "\r\n";
}
public boolean getsUnixEol() {
return unixEol;
}
public void setUnixEol(boolean unixEol) {
this.unixEol = unixEol;
firePropertyChange("unixEol", null, this.unixEol);
}
}
......@@ -31,7 +31,6 @@ import org.apache.poi.xssf.eventusermodel.ReadOnlySharedStringsTable;
import org.apache.poi.xssf.eventusermodel.XSSFReader;
import org.apache.poi.xssf.eventusermodel.XSSFReader.SheetIterator;
import org.apache.poi.xssf.model.StylesTable;
import org.genesys2.anno.gui.DataSourceLoaderImpl;
import org.genesys2.anno.gui.DataSourceSheet;
import org.genesys2.anno.gui.UnsupportedDataFormatException;
import org.genesys2.anno.reader.MyXSSFSheetHandler;
......@@ -54,31 +53,26 @@ public class XlsxDataSourceParser extends BasicDataSourceParser {
}
@Override
public Collection<? extends DataSourceSheet> findSheets(File sourceFile)
throws UnsupportedDataFormatException {
public Collection<? extends DataSourceSheet> findSheets(File sourceFile) throws UnsupportedDataFormatException {
ArrayList<DataSourceSheet> sheets = new ArrayList<DataSourceSheet>();
OPCPackage pkg;
try {
pkg = OPCPackage.open(sourceFile);
} catch (InvalidFormatException e) {
throw new UnsupportedDataFormatException("Could not open "
+ sourceFile.getAbsolutePath(), e);
throw new UnsupportedDataFormatException("Could not open " + sourceFile.getAbsolutePath(), e);
}
try {
XSSFReader r = new XSSFReader(pkg);
XSSFReader.SheetIterator sheetsIterator = (SheetIterator) r
.getSheetsData();
XSSFReader.SheetIterator sheetsIterator = (SheetIterator) r.getSheetsData();
while (sheetsIterator.hasNext()) {
InputStream sheet = sheetsIterator.next();
System.out.println("Processing sheet: '"
+ sheetsIterator.getSheetName() + "'");
System.out.println("Processing sheet: '" + sheetsIterator.getSheetName() + "'");
try {
DataSourceSheet sheetDataSource = new DataSourceSheet(
sourceFile);
DataSourceSheet sheetDataSource = new DataSourceSheet(sourceFile);
sheetDataSource.setSheetName(sheetsIterator.getSheetName());
sheets.add(sheetDataSource);
} finally {
......@@ -90,8 +84,7 @@ public class XlsxDataSourceParser extends BasicDataSourceParser {
} catch (Exception e) {
e.printStackTrace();
throw new UnsupportedDataFormatException("Could not open "
+ sourceFile.getAbsolutePath(), e);
throw new UnsupportedDataFormatException("Could not open " + sourceFile.getAbsolutePath(), e);
} finally {
_log.debug("Closing OPCPackage, no save");
......@@ -99,40 +92,36 @@ public class XlsxDataSourceParser extends BasicDataSourceParser {
return sheets;
}
private XMLReader fetchSheetParser(ContentHandler handler)
throws SAXException {
XMLReader parser = XMLReaderFactory
.createXMLReader("org.apache.xerces.parsers.SAXParser");
private XMLReader fetchSheetParser(ContentHandler handler) throws SAXException {
XMLReader parser = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
parser.setContentHandler(handler);
return parser;
}
@Override
public List<Object[]> loadRows(File sourceFile, String sheetName,
int maxRows) throws UnsupportedDataFormatException {
public List<Object[]> loadRows(DataSourceSheet dataSourceSheet, int maxRows) throws UnsupportedDataFormatException {
File sourceFile = dataSourceSheet.getSourceFile();
String sheetName = dataSourceSheet.getSheetName();
OPCPackage pkg;
try {
pkg = OPCPackage.open(sourceFile);
} catch (InvalidFormatException e) {
throw new UnsupportedDataFormatException("Could not open "
+ sourceFile.getAbsolutePath(), e);
throw new UnsupportedDataFormatException("Could not open " + sourceFile.getAbsolutePath(), e);
}
try {
XSSFReader r = new XSSFReader(pkg);
ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(
pkg);
ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(pkg);
StylesTable styles = r.getStylesTable();
MyXSSFSheetHandler handler = new MyXSSFSheetHandler(styles,
strings, 0, maxRows);
MyXSSFSheetHandler handler = new MyXSSFSheetHandler(styles, strings, 0, maxRows);
XMLReader parser = fetchSheetParser(handler);
XSSFReader.SheetIterator sheetsIterator = (SheetIterator) r
.getSheetsData();
XSSFReader.SheetIterator sheetsIterator = (SheetIterator) r.getSheetsData();
while (sheetsIterator.hasNext()) {
InputStream sheet = sheetsIterator.next();
System.out.println("Found sheet: '"
+ sheetsIterator.getSheetName() + "'");
System.out.println("Found sheet: '" + sheetsIterator.getSheetName() + "'");
if (sheetName.equals(sheetsIterator.getSheetName())) {
_log.debug("Found sheet.");
......@@ -172,7 +161,6 @@ public class XlsxDataSourceParser extends BasicDataSourceParser {
_log.debug("Closing OPCPackage");
}
throw new UnsupportedDataFormatException("Could not parse "
+ sourceFile.getAbsolutePath(), null);
throw new UnsupportedDataFormatException("Could not parse " + sourceFile.getAbsolutePath(), null);
}
}
Supports Markdown
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