1 package com.opencsv;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.io.IOException;
20 import java.io.Writer;
21
22
23
24
25
26
27 public class CSVWriter extends AbstractCSVWriter {
28
29 protected final char separator;
30 protected final char quotechar;
31 protected final char escapechar;
32
33
34
35
36
37
38 public CSVWriter(Writer writer) {
39 this(writer, DEFAULT_SEPARATOR, DEFAULT_QUOTE_CHARACTER, DEFAULT_ESCAPE_CHARACTER, DEFAULT_LINE_END);
40 }
41
42
43
44
45
46
47
48
49
50
51 public CSVWriter(Writer writer, char separator, char quotechar, char escapechar, String lineEnd) {
52 super(writer, lineEnd);
53 this.escapechar = escapechar;
54 this.quotechar = quotechar;
55 this.separator = separator;
56 }
57
58
59 @Override
60 protected void writeNext(String[] nextLine, boolean applyQuotesToAll, Appendable appendable) throws IOException {
61 if (nextLine == null) {
62 return;
63 }
64
65 for (int i = 0; i < nextLine.length; i++) {
66
67 if (i != 0) {
68 appendable.append(separator);
69 }
70
71 String nextElement = nextLine[i];
72
73 if (nextElement == null) {
74 continue;
75 }
76
77 Boolean stringContainsSpecialCharacters = stringContainsSpecialCharacters(nextElement);
78
79 appendQuoteCharacterIfNeeded(applyQuotesToAll, appendable, stringContainsSpecialCharacters);
80
81 if (stringContainsSpecialCharacters) {
82 processLine(nextElement, appendable);
83 } else {
84 appendable.append(nextElement);
85 }
86
87 appendQuoteCharacterIfNeeded(applyQuotesToAll, appendable, stringContainsSpecialCharacters);
88 }
89
90 appendable.append(lineEnd);
91 writer.write(appendable.toString());
92 }
93
94 private void appendQuoteCharacterIfNeeded(boolean applyQuotesToAll, Appendable appendable, Boolean stringContainsSpecialCharacters) throws IOException {
95 if ((applyQuotesToAll || stringContainsSpecialCharacters) && quotechar != NO_QUOTE_CHARACTER) {
96 appendable.append(quotechar);
97 }
98 }
99
100
101
102
103
104
105 protected boolean stringContainsSpecialCharacters(String line) {
106 return line.indexOf(quotechar) != -1
107 || line.indexOf(escapechar) != -1
108 || line.indexOf(separator) != -1
109 || line.contains(DEFAULT_LINE_END)
110 || line.contains("\r");
111 }
112
113
114
115
116
117
118
119 protected void processLine(String nextElement, Appendable appendable) throws IOException {
120 for (int j = 0; j < nextElement.length(); j++) {
121 char nextChar = nextElement.charAt(j);
122 processCharacter(appendable, nextChar);
123 }
124 }
125
126
127
128
129
130
131
132 protected void processCharacter(Appendable appendable, char nextChar) throws IOException {
133 if (escapechar != NO_ESCAPE_CHARACTER && checkCharactersToEscape(nextChar)) {
134 appendable.append(escapechar);
135 }
136 appendable.append(nextChar);
137 }
138
139
140
141
142
143
144
145
146
147 protected boolean checkCharactersToEscape(char nextChar) {
148 return quotechar == NO_QUOTE_CHARACTER
149 ? (nextChar == quotechar || nextChar == escapechar || nextChar == separator || nextChar == '\n')
150 : (nextChar == quotechar || nextChar == escapechar);
151 }
152
153 }