1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package com.rapiddweller.platform.csv;
28
29 import com.rapiddweller.benerator.consumer.TextFileExporter;
30 import com.rapiddweller.benerator.engine.DefaultBeneratorContext;
31 import com.rapiddweller.common.ArrayFormat;
32 import com.rapiddweller.common.ArrayUtil;
33 import com.rapiddweller.common.BeanUtil;
34 import com.rapiddweller.common.CollectionUtil;
35 import com.rapiddweller.common.StringUtil;
36 import com.rapiddweller.model.data.ComplexTypeDescriptor;
37 import com.rapiddweller.model.data.ComponentDescriptor;
38 import com.rapiddweller.model.data.Entity;
39 import org.apache.logging.log4j.LogManager;
40 import org.apache.logging.log4j.Logger;
41
42 import java.util.Collection;
43 import java.util.List;
44
45
46
47
48
49
50
51
52
53
54 public class CSVEntityExporter extends TextFileExporter {
55
56 private static final Logger logger =
57 LogManager.getLogger(CSVEntityExporter.class);
58
59
60
61 private static final String DEFAULT_LINE_SEPARATOR = "\r\n";
62
63 private static final String DEFAULT_URI = "export.csv";
64
65
66
67 private String[] columns;
68 private boolean headless;
69 private boolean endWithNewLine;
70 private char separator;
71 private boolean quoteEmpty;
72
73
74
75 private boolean lfRequired;
76
77
78
79
80
81
82 public CSVEntityExporter() {
83 this(DEFAULT_URI);
84 }
85
86
87
88
89
90
91 public CSVEntityExporter(String uri) {
92 this(uri, (String) null);
93 }
94
95
96
97
98
99
100
101 public CSVEntityExporter(String uri, String columnsSpec) {
102 this(uri, columnsSpec,
103 DefaultBeneratorContext.getDefaultCellSeparator(), null,
104 DEFAULT_LINE_SEPARATOR);
105 }
106
107
108
109
110
111
112
113
114
115
116 public CSVEntityExporter(String uri, String columnsSpec, char separator,
117 String encoding, String lineSeparator) {
118 super(uri, encoding, lineSeparator);
119 if (columnsSpec != null) {
120 setColumns(ArrayFormat.parse(columnsSpec, ",", String.class));
121 }
122 this.separator = separator;
123 this.quoteEmpty = true;
124 }
125
126
127
128
129
130
131 public CSVEntityExporter(ComplexTypeDescriptor descriptor) {
132 this(descriptor.getName() + ".csv", descriptor);
133 }
134
135
136
137
138
139
140
141 public CSVEntityExporter(String uri, ComplexTypeDescriptor descriptor) {
142 this(uri, descriptor, DefaultBeneratorContext.getDefaultCellSeparator(),
143 null, DEFAULT_LINE_SEPARATOR);
144 }
145
146
147
148
149
150
151
152
153
154
155 public CSVEntityExporter(String uri, ComplexTypeDescriptor descriptor,
156 char separator, String encoding,
157 String lineSeparator) {
158 super(uri, encoding, lineSeparator);
159 Collection<ComponentDescriptor> componentDescriptors =
160 descriptor.getComponents();
161 List<String> componentNames =
162 BeanUtil.extractProperties(componentDescriptors, "name");
163 this.columns = CollectionUtil.toArray(componentNames, String.class);
164 this.endWithNewLine = false;
165 this.separator = separator;
166 }
167
168
169
170
171
172
173
174
175
176 public void setColumns(String[] columns) {
177 if (ArrayUtil.isEmpty(columns)) {
178 this.columns = null;
179 } else {
180 this.columns = columns.clone();
181 StringUtil.trimAll(this.columns);
182 }
183 }
184
185
186
187
188
189
190 public void setSeparator(char separator) {
191 this.separator = separator;
192 }
193
194
195
196
197
198
199 public boolean isHeadless() {
200 return headless;
201 }
202
203
204
205
206
207
208 public void setHeadless(boolean headless) {
209 this.headless = headless;
210 }
211
212
213
214
215
216
217 public boolean isEndWithNewLine() {
218 return endWithNewLine;
219 }
220
221
222
223
224
225
226 public void setEndWithNewLine(boolean endWithNewLine) {
227 this.endWithNewLine = endWithNewLine;
228 }
229
230
231
232
233
234
235 public boolean isQuoteEmpty() {
236 return quoteEmpty;
237 }
238
239
240
241
242
243
244 public void setQuoteEmpty(boolean quoteEmpty) {
245 this.quoteEmpty = quoteEmpty;
246 }
247
248
249
250 @Override
251 protected void startConsumingImpl(Object object) {
252 logger.debug("exporting {}", object);
253 if (!(object instanceof Entity)) {
254 throw new IllegalArgumentException("Expecting entity");
255 }
256 Entity../../../../com/rapiddweller/model/data/Entity.html#Entity">Entity entity = (Entity) object;
257 if (lfRequired) {
258 println();
259 } else {
260 lfRequired = true;
261 }
262 for (int i = 0; i < columns.length; i++) {
263 if (i > 0) {
264 printer.print(separator);
265 }
266 Object value = entity.getComponent(columns[i]);
267 String out;
268 if (value == null) {
269 out = getNullString();
270 } else {
271 out = plainConverter.convert(value);
272 if (out.length() == 0 && quoteEmpty) {
273 out = "\"\"";
274 } else if (out.indexOf(separator) >= 0) {
275 out = '"' + out + '"';
276 }
277 }
278 printer.print(out);
279 }
280 }
281
282 @Override
283 protected void postInitPrinter(Object object) {
284 Entity../../../../com/rapiddweller/model/data/Entity.html#Entity">Entity entity = (Entity) object;
285
286 if (columns == null && entity != null) {
287 columns = CollectionUtil.toArray(entity.getComponents().keySet());
288 }
289 printHeaderRow();
290 }
291
292 @Override
293 protected void preClosePrinter() {
294 if (endWithNewLine) {
295 println();
296 }
297 }
298
299
300
301
302 private void printHeaderRow() {
303 if (!wasAppended && !headless && columns != null) {
304 if (wasAppended && !endWithNewLine) {
305 println();
306 }
307 for (int i = 0; i < columns.length; i++) {
308 if (i > 0) {
309 printer.print(separator);
310 }
311 printer.print(columns[i]);
312 }
313 lfRequired = true;
314 } else {
315 lfRequired = (wasAppended && !endWithNewLine);
316 }
317 }
318
319
320 }