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.benerator.consumer;
28
29 import com.rapiddweller.common.ConfigurationError;
30 import com.rapiddweller.common.IOUtil;
31 import com.rapiddweller.common.SystemInfo;
32 import org.apache.logging.log4j.LogManager;
33 import org.apache.logging.log4j.Logger;
34
35 import java.io.File;
36 import java.io.IOException;
37 import java.io.PrintWriter;
38
39
40
41
42
43
44
45
46
47 public class TextFileExporter extends FormattingConsumer implements FileExporter {
48
49 private static final Logger LOG = LogManager.getLogger(TextFileExporter.class);
50
51
52
53
54
55
56 protected String uri;
57
58
59
60 protected String encoding;
61
62
63
64 protected String lineSeparator;
65
66
67
68 protected boolean append;
69
70
71
72 protected boolean wasAppended;
73
74
75
76
77 protected PrintWriter printer;
78
79
80
81
82
83
84 public TextFileExporter() {
85 this(null, null, null);
86 }
87
88
89
90
91
92
93 public TextFileExporter(String uri) {
94 this(uri, null, null);
95 }
96
97
98
99
100
101
102
103
104 public TextFileExporter(String uri, String encoding, String lineSeparator) {
105 this.uri = (uri != null ? uri : "export.txt");
106 this.encoding = (encoding != null ? encoding : SystemInfo.getFileEncoding());
107 this.lineSeparator = (lineSeparator != null ? lineSeparator : SystemInfo.getLineSeparator());
108 this.append = false;
109 }
110
111
112
113
114
115
116
117
118
119 protected void postInitPrinter(Object data) {
120
121 }
122
123
124
125
126
127
128
129
130 protected void startConsumingImpl(Object data) {
131 printer.print(plainConverter.convert(data));
132 println();
133 }
134
135
136
137
138
139 protected void preClosePrinter() {
140
141 }
142
143
144
145 @Override
146 public String getUri() {
147 return uri;
148 }
149
150
151
152
153
154
155 public void setUri(String uri) {
156 this.uri = uri;
157 }
158
159
160
161
162
163
164 public String getEncoding() {
165 return encoding;
166 }
167
168
169
170
171
172
173 public void setEncoding(String encoding) {
174 this.encoding = encoding;
175 }
176
177
178
179
180
181
182 public String getLineSeparator() {
183 return lineSeparator;
184 }
185
186
187
188
189
190
191 public void setLineSeparator(String lineSeparator) {
192 this.lineSeparator = lineSeparator;
193 }
194
195
196
197
198
199
200 public boolean isAppend() {
201 return append;
202 }
203
204
205
206
207
208
209 public void setAppend(boolean append) {
210 this.append = append;
211 }
212
213
214
215 @Override
216 public final synchronized void startProductConsumption(Object data) {
217 try {
218 if (printer == null) {
219 initPrinter(data);
220 }
221 startConsumingImpl(data);
222 } catch (IOException e) {
223 throw new RuntimeException(e);
224 }
225 }
226
227 @Override
228 public void flush() {
229 if (printer != null) {
230 printer.flush();
231 }
232 }
233
234 @Override
235 public void close() {
236 try {
237 if (printer == null) {
238 try {
239 initPrinter(null);
240 } catch (IOException e) {
241 LOG.error("Error initializing empty file", e);
242 }
243 }
244 preClosePrinter();
245 } finally {
246 assert printer != null;
247 printer.close();
248 }
249 }
250
251
252
253
254
255
256
257
258
259 protected void initPrinter(Object data) throws IOException {
260 if (uri == null) {
261 throw new ConfigurationError("Property 'uri' not set on bean " + getClass().getName());
262 }
263 wasAppended = (append && IOUtil.isURIAvailable(uri));
264
265
266 File directory = new File(uri);
267 if (!wasAppended
268 && directory.getParent() != null
269 && !directory.isDirectory()
270 && !directory.getParentFile().exists()) {
271 boolean result = directory.getParentFile().mkdirs();
272 if (!result) {
273 throw new ConfigurationError("filepath does not exists and can not be created ...");
274 }
275 }
276
277 printer = IOUtil.getPrinterForURI(uri, encoding, append, lineSeparator, true);
278 postInitPrinter(data);
279 }
280
281
282
283
284 protected void println() {
285 printer.print(lineSeparator);
286 }
287
288
289
290 @Override
291 public String toString() {
292 return getClass().getSimpleName() + "[" + uri + "]";
293 }
294
295 }