Commit 1d47143a authored by R.W.Majeed's avatar R.W.Majeed

CSV export implementation added

parent de030d23
package de.sekmi.histream.export.csv;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Path;
import de.sekmi.histream.export.ExportWriter;
import de.sekmi.histream.export.TableWriter;
public class CSVExport implements ExportWriter{
private char fieldSeparator;
private Charset charset;
private Path directory;
private String filenameExtension;
public CSVExport(Path directory, char fieldSeparator){
this.charset = Charset.forName("UTF-8");
this.directory = directory;
this.fieldSeparator = fieldSeparator;
}
public Charset getCharset(){
return charset;
}
public Path getDirectory(){
return directory;
}
public char getFieldSeparator(){
return fieldSeparator;
}
private String fileWithExtension(String name){
return name+filenameExtension;
}
/**
* Escape data before it is written to the output file.
* <p>
* If the data contains newline characters or field separators,
* these sequences will be escaped.
* </p>
* @param data data field
* @return escaped data field
*/
protected String escapeData(String data){
// TODO do proper escaping
return data.replace(fieldSeparator, ' ').replace('\n', ' ');
}
@Override
public TableWriter openPatientTable() throws IOException {
return new Table(this, fileWithExtension("patient"));
}
@Override
public TableWriter openVisitTable() throws IOException {
return new Table(this, fileWithExtension("encounter"));
}
@Override
public TableWriter openEAVTable(String id) throws IOException {
return new Table(this, fileWithExtension(id));
}
}
package de.sekmi.histream.export.csv;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import de.sekmi.histream.export.TableWriter;
class Table implements TableWriter{
private final CSVExport export;
private PrintWriter out;
/**
* @param csvWriter
* @throws IOException
*/
Table(CSVExport csvWriter, String filename) throws IOException {
export = csvWriter;
Path file = export.getDirectory().resolve(filename);
OutputStream os = Files.newOutputStream(file, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE);
// no checked exceptions below this point (otherwise must make sure os is closed)
out = new PrintWriter(new OutputStreamWriter(os, export.getCharset()));
// TODO does closing the writer also close the output stream?
}
private void printRow(String[] data){
for( int i=0; i<data.length; i++ ){
if( i != 0 ){
out.print(export.getFieldSeparator());
}
out.print(export.escapeData(data[i]));
}
out.println();
}
@Override
public void header(String[] headers) {
printRow(headers);
}
@Override
public void row(String[] columns) throws IOException {
printRow(columns);
}
@Override
public void close() throws IOException {
out.close();
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment