-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Group reader/writer into format adapters
- Loading branch information
Showing
11 changed files
with
236 additions
and
120 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
src/main/java/com/widen/tabitha/formats/FormatAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.widen.tabitha.formats; | ||
|
||
import com.widen.tabitha.reader.ReaderOptions; | ||
import com.widen.tabitha.reader.RowReader; | ||
import com.widen.tabitha.writer.RowWriter; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
/** | ||
* Provides factory methods for creating readers and writers of a particular format. | ||
*/ | ||
public interface FormatAdapter { | ||
/** | ||
* Create a row reader for a file at the given path. | ||
* | ||
* @param path The path of the file to read. | ||
* @param options Options to pass to the reader. | ||
* @return A new row reader. | ||
* @throws IOException if an I/O error occurs. | ||
*/ | ||
default RowReader createReader(Path path, ReaderOptions options) throws IOException { | ||
return createReader(Files.newInputStream(path), options); | ||
} | ||
|
||
/** | ||
* Create a row reader for an input stream. | ||
* | ||
* @param inputStream The input stream to read. | ||
* @param options Options to pass to the reader. | ||
* @return A new row reader. | ||
* @throws IOException if an I/O error occurs. | ||
*/ | ||
RowReader createReader(InputStream inputStream, ReaderOptions options) throws IOException; | ||
|
||
/** | ||
* Create a row writer that writes to the given path. | ||
* | ||
* @param path The path to write to. | ||
* @return A new row writer. | ||
* @throws IOException if an I/O error occurs. | ||
*/ | ||
default RowWriter createWriter(Path path) throws IOException { | ||
return createWriter(Files.newOutputStream(path)); | ||
} | ||
|
||
/** | ||
* Create a row writer that writes to the given output stream. | ||
* | ||
* @param outputStream The output stream to write to. | ||
* @return A new row writer. | ||
* @throws IOException if an I/O error occurs. | ||
*/ | ||
RowWriter createWriter(OutputStream outputStream) throws IOException; | ||
} |
110 changes: 110 additions & 0 deletions
110
src/main/java/com/widen/tabitha/formats/FormatRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package com.widen.tabitha.formats; | ||
|
||
import com.widen.tabitha.formats.delimited.DelimitedFormat; | ||
import com.widen.tabitha.formats.delimited.DelimitedRowReader; | ||
import com.widen.tabitha.formats.delimited.DelimitedRowWriter; | ||
import com.widen.tabitha.formats.excel.WorkbookRowWriter; | ||
import com.widen.tabitha.formats.excel.XLSRowReader; | ||
import com.widen.tabitha.formats.excel.XLSXRowReader; | ||
import com.widen.tabitha.reader.InlineHeaderReader; | ||
import com.widen.tabitha.reader.ReaderOptions; | ||
import com.widen.tabitha.reader.RowReader; | ||
import com.widen.tabitha.writer.RowWriter; | ||
import io.reactivex.Maybe; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.nio.file.Path; | ||
|
||
/** | ||
* Manages the adapters for the file formats supported by Tabitha. | ||
* <p> | ||
* You probably want to use {@link com.widen.tabitha.reader.RowReaders} or {@link com.widen.tabitha.writer.RowWriter} | ||
* instead. | ||
*/ | ||
public class FormatRegistry { | ||
/** | ||
* Get a format factory for handling the given MIME type. | ||
* | ||
* @param mimeType The format MIME type. | ||
* @return A format adapter, if one could be found. | ||
*/ | ||
public static Maybe<FormatAdapter> forMimeType(String mimeType) { | ||
switch (mimeType) { | ||
case "text/csv": | ||
case "text/plain": | ||
return Maybe.just(new FormatAdapter() { | ||
@Override | ||
public RowReader createReader(InputStream inputStream, ReaderOptions options) { | ||
return decorateReader(new DelimitedRowReader(inputStream, DelimitedFormat.CSV), options); | ||
} | ||
|
||
@Override | ||
public RowWriter createWriter(OutputStream outputStream) { | ||
return new DelimitedRowWriter(outputStream, DelimitedFormat.CSV); | ||
} | ||
}); | ||
|
||
case "text/tab-separated-values": | ||
return Maybe.just(new FormatAdapter() { | ||
@Override | ||
public RowReader createReader(InputStream inputStream, ReaderOptions options) { | ||
return decorateReader(new DelimitedRowReader(inputStream, DelimitedFormat.TSV), options); | ||
} | ||
|
||
@Override | ||
public RowWriter createWriter(OutputStream outputStream) { | ||
return new DelimitedRowWriter(outputStream, DelimitedFormat.TSV); | ||
} | ||
}); | ||
|
||
case "application/vnd.ms-excel": | ||
return Maybe.just(new FormatAdapter() { | ||
@Override | ||
public RowReader createReader(Path path, ReaderOptions options) throws IOException { | ||
return decorateReader(XLSRowReader.open(path, options), options); | ||
} | ||
|
||
@Override | ||
public RowReader createReader(InputStream inputStream, ReaderOptions options) throws IOException { | ||
return decorateReader(XLSRowReader.open(inputStream, options), options); | ||
} | ||
|
||
@Override | ||
public RowWriter createWriter(OutputStream outputStream) { | ||
return WorkbookRowWriter.xls(outputStream); | ||
} | ||
}); | ||
|
||
case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": | ||
case "application/x-tika-ooxml": | ||
return Maybe.just(new FormatAdapter() { | ||
@Override | ||
public RowReader createReader(Path path, ReaderOptions options) throws IOException { | ||
return decorateReader(XLSXRowReader.open(path, options), options); | ||
} | ||
|
||
@Override | ||
public RowReader createReader(InputStream inputStream, ReaderOptions options) throws IOException { | ||
return decorateReader(XLSXRowReader.open(inputStream, options), options); | ||
} | ||
|
||
@Override | ||
public RowWriter createWriter(OutputStream outputStream) { | ||
return WorkbookRowWriter.xlsx(outputStream); | ||
} | ||
}); | ||
|
||
default: | ||
return Maybe.empty(); | ||
} | ||
} | ||
|
||
private static RowReader decorateReader(RowReader reader, ReaderOptions options) { | ||
if (options.isInlineHeaders()) { | ||
reader = new InlineHeaderReader(reader); | ||
} | ||
return reader; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.