|
1 | 1 | package com.coreoz.windmill.exports.exporters.csv; |
2 | 2 |
|
| 3 | +import com.coreoz.windmill.exports.config.ExportMapping; |
| 4 | +import com.coreoz.windmill.files.BomCharset; |
| 5 | +import com.opencsv.CSVWriter; |
| 6 | +import lombok.SneakyThrows; |
| 7 | + |
3 | 8 | import java.io.ByteArrayOutputStream; |
4 | 9 | import java.io.IOException; |
5 | 10 | import java.io.OutputStream; |
6 | 11 | import java.io.OutputStreamWriter; |
7 | 12 | import java.util.List; |
8 | 13 |
|
9 | | -import com.coreoz.windmill.files.BomCharset; |
10 | | -import com.coreoz.windmill.exports.config.ExportMapping; |
11 | | -import com.opencsv.CSVWriter; |
12 | | - |
13 | | -import lombok.SneakyThrows; |
14 | | - |
15 | 14 | public class CsvExporter<T> { |
16 | | - |
17 | | - private final Iterable<T> rows; |
18 | | - private final ExportMapping<T> mapping; |
19 | | - private final ExportCsvConfig exportConfig; |
20 | | - private CSVWriter csvWriter; |
21 | | - |
22 | | - public CsvExporter(Iterable<T> rows, ExportMapping<T> mapping, ExportCsvConfig exportConfig) { |
23 | | - this.rows = rows; |
24 | | - this.mapping = mapping; |
25 | | - this.exportConfig = exportConfig; |
26 | | - } |
27 | | - |
28 | | - /** |
29 | | - * Write the export file in an existing {@link OutputStream}. |
30 | | - * |
31 | | - * This {@link OutputStream} will not be closed automatically: |
32 | | - * it should be closed manually after this method is called. |
33 | | - * |
34 | | - * @throws IOException if anything can't be written. |
35 | | - */ |
36 | | - @SneakyThrows |
37 | | - public OutputStream writeTo(OutputStream outputStream) { |
38 | | - csvWriter = new CSVWriter( |
39 | | - new OutputStreamWriter(outputStream, exportConfig.getCharset().getCharset()), |
40 | | - exportConfig.getSeparator(), |
41 | | - exportConfig.getQuoteChar(), |
42 | | - exportConfig.getEscapeChar(), |
43 | | - exportConfig.getLineEnd() |
44 | | - ); |
45 | | - writeBom(outputStream); |
46 | | - writeRows(); |
47 | | - return outputStream; |
48 | | - } |
49 | | - |
50 | | - /** |
51 | | - * @throws IOException if anything can't be written. |
52 | | - */ |
53 | | - public byte[] toByteArray() { |
54 | | - ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); |
55 | | - writeTo(byteOutputStream); |
56 | | - return byteOutputStream.toByteArray(); |
57 | | - } |
58 | | - |
59 | | - // internals |
60 | | - |
61 | | - @SneakyThrows |
62 | | - private void writeBom(OutputStream outputStream) { |
63 | | - BomCharset encodingCharset = exportConfig.getCharset(); |
64 | | - if (encodingCharset != null) { |
| 15 | + private final Iterable<T> rows; |
| 16 | + private final ExportMapping<T> mapping; |
| 17 | + private final ExportCsvConfig exportConfig; |
| 18 | + private CSVWriter csvWriter; |
| 19 | + |
| 20 | + public CsvExporter(Iterable<T> rows, ExportMapping<T> mapping, ExportCsvConfig exportConfig) { |
| 21 | + this.rows = rows; |
| 22 | + this.mapping = mapping; |
| 23 | + this.exportConfig = exportConfig; |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Write the export file in an existing {@link OutputStream}. |
| 28 | + * <p> |
| 29 | + * This {@link OutputStream} will not be closed automatically: |
| 30 | + * it should be closed manually after this method is called. |
| 31 | + * |
| 32 | + * @throws IOException if anything can't be written. |
| 33 | + */ |
| 34 | + @SneakyThrows |
| 35 | + public OutputStream writeTo(OutputStream outputStream) { |
| 36 | + csvWriter = new CSVWriter( |
| 37 | + new OutputStreamWriter(outputStream, exportConfig.getCharset().getCharset()), |
| 38 | + exportConfig.getSeparator(), |
| 39 | + exportConfig.getQuoteChar(), |
| 40 | + exportConfig.getEscapeChar(), |
| 41 | + exportConfig.getLineEnd() |
| 42 | + ); |
| 43 | + writeBom(outputStream); |
| 44 | + writeRows(); |
| 45 | + return outputStream; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @throws IOException if anything can't be written. |
| 50 | + */ |
| 51 | + public byte[] toByteArray() { |
| 52 | + ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); |
| 53 | + writeTo(byteOutputStream); |
| 54 | + return byteOutputStream.toByteArray(); |
| 55 | + } |
| 56 | + |
| 57 | + // internals |
| 58 | + |
| 59 | + @SneakyThrows |
| 60 | + private void writeBom(OutputStream outputStream) { |
| 61 | + BomCharset encodingCharset = exportConfig.getCharset(); |
| 62 | + if (encodingCharset != null) { |
65 | 63 | encodingCharset.writeBomBytes(outputStream); |
66 | | - } |
67 | | - } |
68 | | - |
69 | | - private void writeRows() { |
70 | | - writeHeaderRow(); |
71 | | - |
72 | | - for(T row : rows) { |
73 | | - writeRow(row); |
74 | | - } |
75 | | - } |
76 | | - |
77 | | - private void writeHeaderRow() { |
78 | | - List<String> headerColumn = mapping.headerColumns(); |
79 | | - if(!headerColumn.isEmpty()) { |
80 | | - String[] csvRowValues = new String[headerColumn.size()]; |
81 | | - for (int i = 0; i < headerColumn.size(); i++) { |
82 | | - csvRowValues[i] = stringValue(headerColumn.get(i)); |
83 | | - } |
84 | | - csvWriter.writeNext(csvRowValues,exportConfig.isApplyQuotesToAll()); |
85 | | - } |
86 | | - } |
87 | | - |
88 | | - @SneakyThrows |
89 | | - private void writeRow(T row) { |
90 | | - String[] csvRowValues = new String[mapping.columnsCount()]; |
91 | | - for (int i = 0; i < mapping.columnsCount(); i++) { |
92 | | - csvRowValues[i] = stringValue(mapping.cellValue(i, row)); |
93 | | - } |
94 | | - csvWriter.writeNext(csvRowValues, exportConfig.isApplyQuotesToAll()); |
95 | | - csvWriter.flush(); |
96 | | - } |
97 | | - |
98 | | - private String stringValue(final Object object) { |
99 | | - if (object == null) { |
100 | | - return ""; |
101 | | - } |
102 | | - return object.toString(); |
103 | | - } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private void writeRows() { |
| 68 | + writeHeaderRow(); |
| 69 | + |
| 70 | + for (T row : rows) { |
| 71 | + writeRow(row); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + private void writeHeaderRow() { |
| 76 | + List<String> headerColumn = mapping.headerColumns(); |
| 77 | + if (!headerColumn.isEmpty()) { |
| 78 | + String[] csvRowValues = new String[headerColumn.size()]; |
| 79 | + for (int i = 0; i < headerColumn.size(); i++) { |
| 80 | + csvRowValues[i] = sanitizeValue(headerColumn.get(i), stringValue(headerColumn.get(i))); |
| 81 | + } |
| 82 | + csvWriter.writeNext(csvRowValues, exportConfig.isApplyQuotesToAll()); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + @SneakyThrows |
| 87 | + private void writeRow(T row) { |
| 88 | + String[] csvRowValues = new String[mapping.columnsCount()]; |
| 89 | + List<String> headerColumns = mapping.headerColumns(); |
| 90 | + for (int i = 0; i < mapping.columnsCount(); i++) { |
| 91 | + String columnName = i < headerColumns.size() ? headerColumns.get(i) : null; |
| 92 | + csvRowValues[i] = sanitizeValue(columnName, stringValue(mapping.cellValue(i, row))); |
| 93 | + } |
| 94 | + csvWriter.writeNext(csvRowValues, exportConfig.isApplyQuotesToAll()); |
| 95 | + csvWriter.flush(); |
| 96 | + } |
| 97 | + |
| 98 | + private String sanitizeValue(String columnName, String value) { |
| 99 | + if ( |
| 100 | + exportConfig.isSanitizeFormulas() |
| 101 | + && (columnName == null || !exportConfig.getFieldNamesExcludedFromSanitization().contains(columnName)) |
| 102 | + && isDangerousValue(value) |
| 103 | + ) { |
| 104 | + return "'" + value; |
| 105 | + } |
| 106 | + return value; |
| 107 | + } |
| 108 | + |
| 109 | + private static boolean isDangerousValue(String value) { |
| 110 | + if (value == null || value.isEmpty()) { |
| 111 | + return false; |
| 112 | + } |
| 113 | + char firstChar = value.charAt(0); |
| 114 | + return firstChar == '=' |
| 115 | + || firstChar == '+' |
| 116 | + || firstChar == '-' |
| 117 | + || firstChar == '@' |
| 118 | + || firstChar == '\t' |
| 119 | + || firstChar == '\r'; |
| 120 | + } |
| 121 | + |
| 122 | + private static String stringValue(final Object object) { |
| 123 | + if (object == null) { |
| 124 | + return ""; |
| 125 | + } |
| 126 | + return object.toString(); |
| 127 | + } |
104 | 128 |
|
105 | 129 | } |
0 commit comments