01: package com.quantum.flatfiles.converter;
02:
03: import java.io.File;
04: import java.io.FileWriter;
05: import java.io.IOException;
06: import java.util.Iterator;
07:
08: import com.quantum.csv.wizard.ConverterCSV;
09: import com.quantum.flatfiles.QuantumFlatFilesPlugin;
10: import com.quantum.flatfiles.preferences.CSVPreferences;
11: import com.quantum.sql.SQLResultSetResults;
12:
13: import org.eclipse.jface.preference.IPreferenceStore;
14:
15: public class ResultSetToCSVConverter implements Converter {
16:
17: private IPreferenceStore store = QuantumFlatFilesPlugin
18: .getDefault().getPreferenceStore();
19: private static final String LINE_SEPARATOR = System
20: .getProperty("line.separator");
21:
22: public void convert(String fileName, SQLResultSetResults results)
23: throws IOException {
24:
25: File outputFile = new File(fileName);
26: FileWriter writer = new FileWriter(outputFile);
27:
28: String defaultColumnSeparator = store
29: .getString(CSVPreferences.COLUMN_SEPARATOR);
30: if (defaultColumnSeparator == "") {
31: defaultColumnSeparator = ",";
32: }
33: defaultColumnSeparator = defaultColumnSeparator.substring(0, 1);
34: String[] columns = results.getColumnNames();
35: if (store.getBoolean(CSVPreferences.WRITE_HEADERS)) {
36: writer.write(ConverterCSV.filter(columns,
37: defaultColumnSeparator.charAt(0)));
38: writer.write(LINE_SEPARATOR);
39: }
40:
41: for (Iterator i = results.getRowIterator(); i.hasNext();) {
42: SQLResultSetResults.Row row = (SQLResultSetResults.Row) i
43: .next();
44: writer.write(ConverterCSV.filter(row.getAsStringArray(),
45: defaultColumnSeparator.charAt(0)));
46: writer.write(LINE_SEPARATOR);
47: writer.flush();
48: }
49: writer.close();
50:
51: }
52:
53: public String getFileExtension() {
54: return ".csv";
55: }
56:
57: }
|