
org.eobjects.metamodel.csv.SingleLineCsvDataSet Maven / Gradle / Ivy
/**
* eobjects.org MetaModel
* Copyright (C) 2010 eobjects.org
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.eobjects.metamodel.csv;
import java.io.BufferedReader;
import java.io.IOException;
import org.eobjects.metamodel.MetaModelException;
import org.eobjects.metamodel.data.AbstractDataSet;
import org.eobjects.metamodel.data.DataSetHeader;
import org.eobjects.metamodel.data.Row;
import org.eobjects.metamodel.schema.Column;
import org.eobjects.metamodel.util.FileHelper;
import au.com.bytecode.opencsv.CSVParser;
/**
* A specialized DataSet implementation for the CSV module under circumstances
* where multiline values are disabled. In this case we can use a optimized
* CSVParser and also lazy evaluate lines read from the file.
*/
final class SingleLineCsvDataSet extends AbstractDataSet {
private final BufferedReader _reader;
private final CSVParser _csvParser;
private final int _columnsInTable;
private final boolean _failOnInconsistentRowLength;
private volatile int _rowNumber;
private volatile Integer _rowsRemaining;
private volatile Row _row;
public SingleLineCsvDataSet(BufferedReader reader, CSVParser csvParser, Column[] columns, Integer maxRows,
int columnsInTable, boolean failOnInconsistentRowLength) {
super(columns);
_reader = reader;
_csvParser = csvParser;
_columnsInTable = columnsInTable;
_failOnInconsistentRowLength = failOnInconsistentRowLength;
_rowNumber = 0;
_rowsRemaining = maxRows;
}
@Override
public void close() {
FileHelper.safeClose(_reader);
_row = null;
_rowsRemaining = null;
}
@Override
public boolean next() {
if (_rowsRemaining != null && _rowsRemaining > 0) {
_rowsRemaining--;
return nextInternal();
} else if (_rowsRemaining == null) {
return nextInternal();
} else {
return false;
}
}
@Override
protected DataSetHeader getHeader() {
// re-make this method protected so that it's visible for
// SingleLineCsvRow.
return super.getHeader();
}
protected boolean isFailOnInconsistentRowLength() {
return _failOnInconsistentRowLength;
}
protected int getColumnsInTable() {
return _columnsInTable;
}
protected CSVParser getCsvParser() {
return _csvParser;
}
public boolean nextInternal() {
if (_reader == null) {
return false;
}
try {
final String line = _reader.readLine();
if (line == null) {
close();
return false;
}
_rowNumber++;
_row = new SingleLineCsvRow(this, line, _columnsInTable, _failOnInconsistentRowLength, _rowNumber);
return true;
} catch (IOException e) {
close();
throw new MetaModelException("IOException occurred while reading next line of CSV resource", e);
}
}
@Override
public Row getRow() {
return _row;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy