All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eobjects.metamodel.csv.SingleLineCsvRow Maven / Gradle / Ivy

There is a newer version: 5.3.6
Show newest version
/**
 * 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.IOException;

import org.eobjects.metamodel.MetaModelException;
import org.eobjects.metamodel.data.AbstractRow;
import org.eobjects.metamodel.data.DataSetHeader;
import org.eobjects.metamodel.data.Style;
import org.eobjects.metamodel.schema.Column;

import au.com.bytecode.opencsv.CSVParser;

/**
 * Specialized row implementation for single-line CSV values
 */
final class SingleLineCsvRow extends AbstractRow {

    private static final long serialVersionUID = 1L;

    private final SingleLineCsvDataSet _dataSet;
    private final String _line;
    private final int _columnsInTable;
    private final boolean _failOnInconsistentRowLength;
    private final int _rowNumber;
    private String[] _values;

    public SingleLineCsvRow(SingleLineCsvDataSet dataSet, final String line, final int columnsInTable,
            final boolean failOnInconsistentRowLength, final int rowNumber) {
        _dataSet = dataSet;
        _line = line;
        _columnsInTable = columnsInTable;
        _failOnInconsistentRowLength = failOnInconsistentRowLength;
        _rowNumber = rowNumber;
        _values = null;
    }

    private String[] getValuesInternal() {
        if (_values == null) {
            final CSVParser parser = _dataSet.getCsvParser();
            final String[] csvValues;
            try {
                csvValues = parser.parseLine(_line);
            } catch (IOException e) {
                throw new MetaModelException("Failed to parse CSV line no. " + _rowNumber + ": " + _line);
            }

            if (_failOnInconsistentRowLength) {
                if (_columnsInTable != csvValues.length) {
                    throw new InconsistentRowLengthException(_columnsInTable, SingleLineCsvRow.this, csvValues,
                            _rowNumber);
                }
            }

            // convert the line's values into the row values that where
            // requested
            final DataSetHeader header = _dataSet.getHeader();
            final int size = header.size();
            final String[] rowValues = new String[size];

            for (int i = 0; i < size; i++) {
                final Column column = header.getSelectItem(i).getColumn();
                final int columnNumber = column.getColumnNumber();
                if (columnNumber < csvValues.length) {
                    rowValues[i] = csvValues[columnNumber];
                } else {
                    // Ticket #125: Missing values should be enterpreted as
                    // null.
                    rowValues[i] = null;
                }
            }

            _values = rowValues;
        }
        return _values;
    }

    @Override
    public Object getValue(int index) throws IndexOutOfBoundsException {
        final String[] values = getValuesInternal();
        assert values != null;
        return values[index];
    }

    @Override
    public Style getStyle(int index) throws IndexOutOfBoundsException {
        return Style.NO_STYLE;
    }

    @Override
    protected DataSetHeader getHeader() {
        return _dataSet.getHeader();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy