Skip to content

Commit

Permalink
issue #245 : add MultiFileRecordReader
Browse files Browse the repository at this point in the history
  • Loading branch information
fmbenhassine committed Feb 15, 2017
1 parent 29c2a9b commit b7d79dd
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.easybatch.core.reader;

import java.io.File;

/**
* Abstract class for all file readers.
*
* @author Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com)
*/
public abstract class AbstractFileRecordReader implements RecordReader {

protected File file;

public AbstractFileRecordReader(File file) {
this.file = file;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.easybatch.core.reader;

import org.easybatch.core.record.Record;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
* Read records from multiple files <strong>having the same format</strong>.
*
* Returns records read by the delegate reader.
*
* @author Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com)
*/
public class MultiFileRecordReader implements RecordReader {

private File directory;

private FilenameFilter filenameFilter;

private AbstractFileRecordReader delegate;

private List<File> files = new ArrayList<>();

public MultiFileRecordReader(final File directory, final FilenameFilter filenameFilter, final AbstractFileRecordReader delegate) {
this.directory = directory;
this.filenameFilter = filenameFilter;
this.delegate = delegate;
}

@Override
public void open() throws Exception {
Collections.addAll(files, directory.listFiles(filenameFilter));
}

@Override
public Record readRecord() throws Exception {
// TODO implement logic
return delegate.readRecord();
}

@Override
public void close() throws Exception {
delegate.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@

package org.easybatch.flatfile;

import org.easybatch.core.reader.AbstractFileRecordReader;
import org.easybatch.core.reader.RecordReader;
import org.easybatch.core.record.Header;
import org.easybatch.core.record.StringRecord;
import org.easybatch.core.util.Utils;

import java.io.File;
import java.nio.charset.Charset;
Expand All @@ -42,9 +42,8 @@
*
* @author Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com)
*/
public class FlatFileRecordReader implements RecordReader {
public class FlatFileRecordReader extends AbstractFileRecordReader {

private File input;
private Scanner scanner;
private String charsetName;
private long currentRecordNumber;
Expand Down Expand Up @@ -84,7 +83,7 @@ public FlatFileRecordReader(final File input) {
* @param charsetName the encoding to use to read the file
*/
public FlatFileRecordReader(final File input, final String charsetName) {
this.input = input;
super(input);
this.charsetName = charsetName;
}

Expand All @@ -95,8 +94,7 @@ public FlatFileRecordReader(final File input, final String charsetName) {
* @param path the input file path
*/
public FlatFileRecordReader(final Path path) {
Utils.checkNotNull(path, "path");
this.input = path.toFile();
super(path.toFile());
this.charsetName = Charset.defaultCharset().name();
}

Expand All @@ -108,8 +106,7 @@ public FlatFileRecordReader(final Path path) {
* @param charsetName the encoding to use to read the file
*/
public FlatFileRecordReader(final Path path, final String charsetName) {
Utils.checkNotNull(path, "path");
this.input = path.toFile();
super(path.toFile());
this.charsetName = charsetName;
}

Expand All @@ -126,7 +123,7 @@ public StringRecord readRecord() {
@Override
public void open() throws Exception {
currentRecordNumber = 0;
scanner = new Scanner(input, charsetName);
scanner = new Scanner(file, charsetName);
}

@Override
Expand All @@ -137,7 +134,7 @@ public void close() {
}

private String getDataSourceName() {
return input.getAbsolutePath();
return file.getAbsolutePath();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@

package org.easybatch.json;

import org.easybatch.core.reader.AbstractFileRecordReader;
import org.easybatch.core.record.Record;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
Expand All @@ -46,15 +49,32 @@
*
* @author Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com)
*/
public class JsonFileRecordReader extends JsonRecordReader {
public class JsonFileRecordReader extends AbstractFileRecordReader {

private JsonRecordReader jsonRecordReader;

/**
* Create a new {@link JsonFileRecordReader}.
*
* <p>This reader produces {@link JsonRecord} instances.</p>
*/
public JsonFileRecordReader(final File jsonFile) throws FileNotFoundException {
super(new FileInputStream(jsonFile));
super(jsonFile);
jsonRecordReader = new JsonRecordReader(new FileInputStream(jsonFile));
}

@Override
public void open() throws Exception {
jsonRecordReader.open();
}

@Override
public Record readRecord() throws Exception {
return jsonRecordReader.readRecord();
}

@Override
public void close() throws Exception {
jsonRecordReader.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@

package org.easybatch.xml;

import org.easybatch.core.reader.AbstractFileRecordReader;
import org.easybatch.core.record.Record;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
Expand All @@ -35,10 +38,27 @@
*
* @author Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com)
*/
public class XmlFileRecordReader extends XmlRecordReader {
public class XmlFileRecordReader extends AbstractFileRecordReader {

private XmlRecordReader xmlRecordReader;

public XmlFileRecordReader(final String rootElementName, final File xmlFile) throws FileNotFoundException {
super(rootElementName, new FileInputStream(xmlFile));
super(xmlFile);
xmlRecordReader = new XmlRecordReader(rootElementName, new FileInputStream(xmlFile));
}

@Override
public void open() throws Exception {
xmlRecordReader.open();
}

@Override
public Record readRecord() throws Exception {
return xmlRecordReader.readRecord();
}

@Override
public void close() throws Exception {
xmlRecordReader.close();
}
}

0 comments on commit b7d79dd

Please sign in to comment.