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

com.tecacet.jflat.FlatFileReader Maven / Gradle / Ivy

Go to download

JFlat's purpose is to convert flat files to Java Beans and vice versa. Some supported formats are CSV, fixed-width, arbitrary delimiters, and excel files. There are also tools to generate Java Beans to/from JDBC and Swing tables. JFlat is highly extensible and can be adopted to support additional formats.

The newest version!
/*
 Copyright 2008 TecAceT Ltd.

 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 com.tecacet.jflat;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

/**
 * Reads a flat file into a collection of beans. It uses a LineParser to parse
 * each line into tokens and a ReaderRowMapper to convert the tokens to beans.
 * 
 * @author Dimitri Papaioannou
 * 
 * @param 
 */
public class FlatFileReader implements StructuredFileReader {

	protected LineIterator lineIterator;

	/**
	 * The default line to start reading.
	 */
	protected static final int DEFAULT_SKIP_LINES = 0;

	/**
	 * lines to skip before reading the first line
	 */
	protected int skipLines;

	/**
	 * Maximum lines to read
	 */
	protected Integer maxLines;

	protected LineParser lineParser;

	protected ReaderRowMapper rowMapper;

	@SuppressWarnings("unchecked")
	public FlatFileReader(Reader reader, LineParser parser) {
		this(reader, parser, new DefaultRowMapper());
	}

	public FlatFileReader(Reader reader, LineParser parser,
			ReaderRowMapper mapper) {
		this(new BufferedReaderLineIterator(new BufferedReader(reader)),
				parser, mapper);
	}

	public FlatFileReader(LineIterator lineIterator, LineParser parser,
			ReaderRowMapper mapper) {
		this.lineIterator = lineIterator;
		this.lineParser = parser;
		this.rowMapper = mapper;
		this.skipLines = DEFAULT_SKIP_LINES;
	}

	public FlatFileReader(Reader reader, ReaderRowMapper mapper) {
		this(reader, null, mapper);
	}

	@Override
	public void readWithCallback(FlatFileReaderCallback callback)
			throws IOException {
		for (int i = 0; i < skipLines; i++) {
			lineIterator.getNextLine();
		}
		int row = 0;
		String[] nextLineAsTokens = readNext();
		while (nextLineAsTokens != null) {
			if (maxLines != null && row > maxLines) {
				break;
			}
			T bean = rowMapper.getRow(nextLineAsTokens, ++row);
			processRow(callback, row, nextLineAsTokens, bean);
			nextLineAsTokens = readNext();
		}
	}

	protected void processRow(FlatFileReaderCallback callback, int row,
			String[] nextLineAsTokens, T bean) {
		callback.processRow(row, nextLineAsTokens, bean);
	}

	@Override
	public List readAll() throws IOException {
		final List allElements = new ArrayList();
		readWithCallback(new FlatFileReaderCallback() {

			public void processRow(int rowIndex, String[] tokens, T bean) {
				if (bean != null) {
					allElements.add(bean);
				}
			}
		});
		return allElements;
	}

	protected String[] readNext() throws IOException {
		String line = lineIterator.getNextLine();
		if (line == null) {
			return null;
		}
		return lineParser.parseLine(line);
	}

	/**
	 * Clean up resources
	 * 
	 * @throws IOException
	 */
	public void close() throws IOException {
		lineIterator.close();
	}

	/**
	 * The number of lines to skip before reading a file
	 * 
	 * @return number of skipped lines
	 */
	public int getSkipLines() {
		return skipLines;
	}

	/**
	 * Set the number of lines to skip before reading a file.
	 * 
	 * @param skipLines
	 *            number of skipped lines
	 */
	public void setSkipLines(int skipLines) {
		this.skipLines = skipLines;
	}

	public void setMaxLines(int maxLines) {
		this.maxLines = maxLines;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy