1 package au.com.bytecode.opencsv;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.io.*;
20 import java.sql.ResultSet;
21 import java.sql.SQLException;
22 import java.util.List;
23
24
25
26
27
28
29 public class CSVWriter implements Closeable, Flushable {
30
31 public static final int INITIAL_STRING_SIZE = 128;
32
33 private Writer rawWriter;
34
35 private PrintWriter pw;
36
37 private char separator;
38
39 private char quotechar;
40
41 private char escapechar;
42
43 private String lineEnd;
44
45
46
47
48 public static final char DEFAULT_ESCAPE_CHARACTER = '"';
49
50
51
52
53 public static final char DEFAULT_SEPARATOR = ',';
54
55
56
57
58
59 public static final char DEFAULT_QUOTE_CHARACTER = '"';
60
61
62
63
64 public static final char NO_QUOTE_CHARACTER = '\u0000';
65
66
67
68
69 public static final char NO_ESCAPE_CHARACTER = '\u0000';
70
71
72
73
74 public static final String DEFAULT_LINE_END = "\n";
75
76 private ResultSetHelper resultService = new ResultSetHelperService();
77
78
79
80
81
82
83 public CSVWriter(Writer writer) {
84 this(writer, DEFAULT_SEPARATOR);
85 }
86
87
88
89
90
91
92
93 public CSVWriter(Writer writer, char separator) {
94 this(writer, separator, DEFAULT_QUOTE_CHARACTER);
95 }
96
97
98
99
100
101
102
103
104 public CSVWriter(Writer writer, char separator, char quotechar) {
105 this(writer, separator, quotechar, DEFAULT_ESCAPE_CHARACTER);
106 }
107
108
109
110
111
112
113
114
115
116 public CSVWriter(Writer writer, char separator, char quotechar, char escapechar) {
117 this(writer, separator, quotechar, escapechar, DEFAULT_LINE_END);
118 }
119
120
121
122
123
124
125
126
127
128
129 public CSVWriter(Writer writer, char separator, char quotechar, String lineEnd) {
130 this(writer, separator, quotechar, DEFAULT_ESCAPE_CHARACTER, lineEnd);
131 }
132
133
134
135
136
137
138
139
140
141
142
143 public CSVWriter(Writer writer, char separator, char quotechar, char escapechar, String lineEnd) {
144 this.rawWriter = writer;
145 this.pw = new PrintWriter(writer);
146 this.separator = separator;
147 this.quotechar = quotechar;
148 this.escapechar = escapechar;
149 this.lineEnd = lineEnd;
150 }
151
152
153
154
155
156
157
158
159
160
161
162 public void writeAll(List<String[]> allLines, boolean applyQuotesToAll) {
163 for (String[] line : allLines) {
164 writeNext(line, applyQuotesToAll);
165 }
166 }
167
168
169
170
171
172
173
174
175 public void writeAll(List<String[]> allLines) {
176 for (String[] line : allLines) {
177 writeNext(line);
178 }
179 }
180
181 protected void writeColumnNames(ResultSet rs)
182 throws SQLException {
183
184 writeNext(resultService.getColumnNames(rs));
185 }
186
187
188
189
190
191
192
193
194
195
196
197 public void writeAll(java.sql.ResultSet rs, boolean includeColumnNames) throws SQLException, IOException {
198 writeAll(rs, includeColumnNames, false);
199 }
200
201
202
203
204
205
206
207
208
209 public void writeAll(java.sql.ResultSet rs, boolean includeColumnNames, boolean trim) throws SQLException, IOException {
210
211
212 if (includeColumnNames) {
213 writeColumnNames(rs);
214 }
215
216 while (rs.next()) {
217 writeNext(resultService.getColumnValues(rs, trim));
218 }
219 }
220
221
222
223
224
225
226
227
228
229 public void writeNext(String[] nextLine, boolean applyQuotesToAll) {
230
231 if (nextLine == null)
232 return;
233
234 StringBuilder sb = new StringBuilder(INITIAL_STRING_SIZE);
235 for (int i = 0; i < nextLine.length; i++) {
236
237 if (i != 0) {
238 sb.append(separator);
239 }
240
241 String nextElement = nextLine[i];
242
243 if (nextElement == null)
244 continue;
245
246 Boolean stringContainsSpecialCharacters = stringContainsSpecialCharacters(nextElement);
247
248 if ((applyQuotesToAll || stringContainsSpecialCharacters) && quotechar != NO_QUOTE_CHARACTER)
249 sb.append(quotechar);
250
251 if (stringContainsSpecialCharacters) {
252 sb.append(processLine(nextElement));
253 } else {
254 sb.append(nextElement);
255 }
256
257 if ((applyQuotesToAll || stringContainsSpecialCharacters) && quotechar != NO_QUOTE_CHARACTER)
258 sb.append(quotechar);
259 }
260
261 sb.append(lineEnd);
262 pw.write(sb.toString());
263 }
264
265
266
267
268
269
270
271 public void writeNext(String[] nextLine) {
272 writeNext(nextLine, true);
273 }
274
275 private boolean stringContainsSpecialCharacters(String line) {
276 return line.indexOf(quotechar) != -1 || line.indexOf(escapechar) != -1 || line.indexOf(separator) != -1 || line.indexOf("\n") != -1 || line.indexOf("\r") != -1;
277 }
278
279 protected StringBuilder processLine(String nextElement) {
280 StringBuilder sb = new StringBuilder(INITIAL_STRING_SIZE);
281 for (int j = 0; j < nextElement.length(); j++) {
282 char nextChar = nextElement.charAt(j);
283 if (escapechar != NO_ESCAPE_CHARACTER && nextChar == quotechar) {
284 sb.append(escapechar).append(nextChar);
285 } else if (escapechar != NO_ESCAPE_CHARACTER && nextChar == escapechar) {
286 sb.append(escapechar).append(nextChar);
287 } else {
288 sb.append(nextChar);
289 }
290 }
291
292 return sb;
293 }
294
295
296
297
298
299
300 public void flush() throws IOException {
301
302 pw.flush();
303
304 }
305
306
307
308
309
310
311 public void close() throws IOException {
312 flush();
313 pw.close();
314 rawWriter.close();
315 }
316
317
318
319
320 public boolean checkError() {
321 return pw.checkError();
322 }
323
324 public void setResultService(ResultSetHelper resultService) {
325 this.resultService = resultService;
326 }
327
328 public void flushQuietly() {
329 try {
330 flush();
331 } catch (IOException e) {
332
333 }
334 }
335 }