1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.supercsv.io;
17
18 import java.io.BufferedWriter;
19 import java.io.IOException;
20 import java.io.Writer;
21 import java.util.List;
22
23 import org.supercsv.encoder.CsvEncoder;
24 import org.supercsv.prefs.CsvPreference;
25 import org.supercsv.util.CsvContext;
26 import org.supercsv.util.Util;
27
28
29
30
31
32
33
34 public abstract class AbstractCsvWriter implements ICsvWriter {
35
36 private final Writer writer;
37
38 private final CsvPreference preference;
39
40 private final CsvEncoder encoder;
41
42
43 private int lineNumber = 0;
44
45
46 private int rowNumber = 0;
47
48
49 private int columnNumber = 0;
50
51
52
53
54
55
56
57
58
59
60
61 public AbstractCsvWriter(final Writer writer, final CsvPreference preference) {
62 this(writer, preference, true);
63 }
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78 public AbstractCsvWriter(final Writer writer, final CsvPreference preference, boolean bufferizeWriter) {
79 if( writer == null ) {
80 throw new NullPointerException("writer should not be null");
81 } else if( preference == null ) {
82 throw new NullPointerException("preference should not be null");
83 }
84
85 this.writer = bufferizeWriter ? new BufferedWriter(writer) : writer;
86 this.preference = preference;
87 this.encoder = preference.getEncoder();
88 }
89
90
91
92
93 public void close() throws IOException {
94 writer.close();
95 }
96
97
98
99
100 public void flush() throws IOException {
101 writer.flush();
102 }
103
104
105
106
107
108
109
110
111 protected void incrementRowAndLineNo() {
112 lineNumber++;
113 rowNumber++;
114 }
115
116
117
118
119 public int getLineNumber() {
120 return lineNumber;
121 }
122
123
124
125
126 public int getRowNumber() {
127 return rowNumber;
128 }
129
130
131
132
133
134
135
136
137
138
139
140
141
142 protected void writeRow(final List<?> columns) throws IOException {
143 writeRow(Util.objectListToStringArray(columns));
144 }
145
146
147
148
149
150
151
152
153
154
155
156
157
158 protected void writeRow(final Object... columns) throws IOException {
159 writeRow(Util.objectArrayToStringArray(columns));
160 }
161
162
163
164
165
166
167
168
169
170
171
172
173
174 protected void writeRow(final String... columns) throws IOException {
175
176 if( columns == null ) {
177 throw new NullPointerException(String.format("columns to write should not be null on line %d", lineNumber));
178 } else if( columns.length == 0 ) {
179 throw new IllegalArgumentException(String.format("columns to write should not be empty on line %d",
180 lineNumber));
181 }
182
183 StringBuilder builder = new StringBuilder();
184 for( int i = 0; i < columns.length; i++ ) {
185
186 columnNumber = i + 1;
187
188 if( i > 0 ) {
189 builder.append((char) preference.getDelimiterChar());
190 }
191
192 final String csvElement = columns[i];
193 if( csvElement != null ) {
194 final CsvContext context = new CsvContext(lineNumber, rowNumber, columnNumber);
195 final String escapedCsv = encoder.encode(csvElement, context, preference);
196 builder.append(escapedCsv);
197 lineNumber = context.getLineNumber();
198 }
199
200 }
201
202 builder.append(preference.getEndOfLineSymbols());
203 writer.write(builder.toString());
204 }
205
206
207
208
209 public void writeComment(final String comment) throws IOException {
210
211 lineNumber++;
212
213 if( comment == null ) {
214 throw new NullPointerException(String.format("comment to write should not be null on line %d", lineNumber));
215 }
216
217 writer.write(comment + preference.getEndOfLineSymbols());
218
219 }
220
221
222
223
224 public void writeHeader(final String... header) throws IOException {
225
226
227 incrementRowAndLineNo();
228
229 writeRow(header);
230 }
231
232 }