package gov.nara.nwts.ftapp.importer;


import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.Vector;

/**
 * Abstract class handling the import of a character-delimited text file allowing for individual values to be wrapped by quotation marks.
 * @author TBrady
 *
 */
public class DelimitedFileWriter {
	BufferedWriter bw;
	String sep;
	
	public static final String LF = "\n";
	public static final String CRLF = "\r\n";
	String lineend;

	public DelimitedFileWriter(File f, String sep) throws FileNotFoundException, UnsupportedEncodingException {
		this(f, sep, LF);
	}
	public DelimitedFileWriter(File f, String sep, String lineend) throws FileNotFoundException, UnsupportedEncodingException {
		bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f), "UTF-8"));
		this.sep = sep;
		this.lineend = lineend;
	}

	public DelimitedFileWriter(OutputStream os, String sep) throws FileNotFoundException, UnsupportedEncodingException {
		this(os, sep, LF);
	}
	public DelimitedFileWriter(OutputStream os, String sep, String lineend) throws FileNotFoundException, UnsupportedEncodingException {
		bw = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
		this.sep = sep;
		this.lineend = lineend;
	}

	public void writeField(String s) throws IOException {
		writeField(s, false);
	}
	public void writeField(String s, boolean isLast) throws IOException {
		if (s==null) s= "";
		if (s.contains(sep)) {
			s = '"' + s.replaceAll("\"", "\"\"") + '"';
		}
		bw.write(s);
		bw.write(isLast ? lineend : sep);
	}
	public void writeRow(Vector<String> row) throws IOException {
		int i=0;
		for(String s: row) {
			i++;
			writeField(s, i == row.size());
		}
	}
	public void writeData(Vector<Vector<String>> data) throws IOException {
		for(Vector<String> row: data) writeRow(row);
		bw.close();
	}

    public static void writeFile(File f, String sep, Vector<Vector<String>> data) throws IOException {
    	DelimitedFileWriter dfw = new DelimitedFileWriter(f, sep);
    	dfw.writeData(data);
    }

    public static void writeCsv(File f, Vector<Vector<String>> data) throws IOException {
    	writeFile(f, ",", data);
    }

	public void close() {
		try {
			bw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

@@ -871,32 +871,49 @@
 , String lineend
+, String encoding
 ) throws FileNot
@@ -1034,23 +1034,24 @@
 eam(f), 
-%22UTF-8%22
+encoding
 ));%0D%0A%09%09t
@@ -1088,32 +1088,197 @@
  = lineend;%0D%0A%09%7D%0D
+%0A%09public DelimitedFileWriter(File f, String sep, String lineend) throws FileNotFoundException, UnsupportedEncodingException %7B%0D%0A%09%09this(f, sep, lineend, %22UTF-8%22);%0D%0A%09%7D%0D
 %0A%0D%0A%09public Delim
@@ -1536,32 +1536,224 @@
 ingException %7B%0D%0A
+%09%09this(os, sep, lineend, %22UTF-8%22);%0D%0A%09%7D%0D%0A%09public DelimitedFileWriter(OutputStream os, String sep, String lineend, String encoding) throws FileNotFoundException, UnsupportedEncodingException %7B%0D%0A
 %09%09bw = new Buffe
@@ -1789,23 +1789,24 @@
 ter(os, 
-%22UTF-8%22
+encoding
 ));%0D%0A%09%09t

Allow alternate encodings when creating a file