-
-
Notifications
You must be signed in to change notification settings - Fork 253
Expand file tree
/
Copy pathFixedWidthWriter.java
More file actions
337 lines (305 loc) · 11.5 KB
/
FixedWidthWriter.java
File metadata and controls
337 lines (305 loc) · 11.5 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*******************************************************************************
* Copyright 2014 Univocity Software Pty Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package com.univocity.parsers.fixed;
import com.univocity.parsers.common.*;
import java.io.*;
import java.nio.charset.*;
/**
* A fast and flexible fixed-with writer implementation.
*
* @author Univocity Software Pty Ltd - <a href="mailto:parsers@univocity.com">parsers@univocity.com</a>
* @see FixedWidthFormat
* @see FixedWidthFields
* @see FixedWidthWriterSettings
* @see FixedWidthParser
* @see AbstractWriter
*/
public class FixedWidthWriter extends AbstractWriter<FixedWidthWriterSettings> {
private int[] fieldLengths;
private FieldAlignment[] fieldAlignments;
private char[] fieldPaddings;
private char padding;
private char defaultPadding;
private int length;
private FieldAlignment alignment;
private Lookup[] lookaheadFormats;
private Lookup[] lookbehindFormats;
private char[] lookupChars;
private Lookup lookbehindFormat;
private int[] rootLengths;
private FieldAlignment[] rootAlignments;
private boolean[] ignore;
private boolean[] rootIgnore;
private int ignoreCount;
private char[] rootPaddings;
private boolean defaultHeaderPadding;
private FieldAlignment defaultHeaderAlignment;
/**
* The FixedWidthWriter supports all settings provided by {@link FixedWidthWriterSettings}, and requires this configuration to be properly initialized.
* <p><strong>Important: </strong> by not providing an instance of {@link java.io.Writer} to this constructor, only the operations that write to Strings are available.</p>
*
* @param settings the fixed-width writer configuration
*/
public FixedWidthWriter(FixedWidthWriterSettings settings) {
this((Writer) null, settings);
}
/**
* The FixedWidthWriter supports all settings provided by {@link FixedWidthWriterSettings}, and requires this configuration to be properly initialized.
*
* @param writer the output resource that will receive fixed-width records produced by this class.
* @param settings the fixed-width writer configuration
*/
public FixedWidthWriter(Writer writer, FixedWidthWriterSettings settings) {
super(writer, settings);
}
/**
* The FixedWidthWriter supports all settings provided by {@link FixedWidthWriterSettings}, and requires this configuration to be properly initialized.
*
* @param file the output file that will receive fixed-width records produced by this class.
* @param settings the fixed-width writer configuration
*/
public FixedWidthWriter(File file, FixedWidthWriterSettings settings) {
super(file, settings);
}
/**
* The FixedWidthWriter supports all settings provided by {@link FixedWidthWriterSettings}, and requires this configuration to be properly initialized.
*
* @param file the output file that will receive fixed-width records produced by this class.
* @param encoding the encoding of the file
* @param settings the fixed-width writer configuration
*/
public FixedWidthWriter(File file, String encoding, FixedWidthWriterSettings settings) {
super(file, encoding, settings);
}
/**
* The FixedWidthWriter supports all settings provided by {@link FixedWidthWriterSettings}, and requires this configuration to be properly initialized.
*
* @param file the output file that will receive fixed-width records produced by this class.
* @param encoding the encoding of the file
* @param settings the fixed-width writer configuration
*/
public FixedWidthWriter(File file, Charset encoding, FixedWidthWriterSettings settings) {
super(file, encoding, settings);
}
/**
* The FixedWidthWriter supports all settings provided by {@link FixedWidthWriterSettings}, and requires this configuration to be properly initialized.
*
* @param output the output stream that will be written with the fixed-width records produced by this class.
* @param settings the fixed-width writer configuration
*/
public FixedWidthWriter(OutputStream output, FixedWidthWriterSettings settings) {
super(output, settings);
}
/**
* The FixedWidthWriter supports all settings provided by {@link FixedWidthWriterSettings}, and requires this configuration to be properly initialized.
*
* @param output the output stream that will be written with the fixed-width records produced by this class.
* @param encoding the encoding of the stream
* @param settings the fixed-width writer configuration
*/
public FixedWidthWriter(OutputStream output, String encoding, FixedWidthWriterSettings settings) {
super(output, encoding, settings);
}
/**
* The FixedWidthWriter supports all settings provided by {@link FixedWidthWriterSettings}, and requires this configuration to be properly initialized.
*
* @param output the output stream that will be written with the fixed-width records produced by this class.
* @param encoding the encoding of the stream
* @param settings the fixed-width writer configuration
*/
public FixedWidthWriter(OutputStream output, Charset encoding, FixedWidthWriterSettings settings) {
super(output, encoding, settings);
}
/**
* Initializes the Fixed-Width writer with CSV-specific configuration
*
* @param settings the Fixed-Width writer configuration
*/
protected final void initialize(FixedWidthWriterSettings settings) {
FixedWidthFormat format = settings.getFormat();
this.padding = format.getPadding();
this.defaultPadding = padding;
this.fieldLengths = settings.getAllLengths();
this.fieldAlignments = settings.getFieldAlignments();
this.fieldPaddings = settings.getFieldPaddings();
this.ignore = settings.getFieldsToIgnore();
if (ignore != null) {
for (int i = 0; i < ignore.length; i++) {
if (ignore[i]) {
ignoreCount++;
}
}
}
this.lookaheadFormats = settings.getLookaheadFormats();
this.lookbehindFormats = settings.getLookbehindFormats();
this.defaultHeaderPadding = settings.getUseDefaultPaddingForHeaders();
this.defaultHeaderAlignment = settings.getDefaultAlignmentForHeaders();
super.enableNewlineAfterRecord(settings.getWriteLineSeparatorAfterRecord());
if (lookaheadFormats != null || lookbehindFormats != null) {
lookupChars = new char[Lookup.calculateMaxLookupLength(lookaheadFormats, lookbehindFormats)];
rootLengths = fieldLengths;
rootAlignments = fieldAlignments;
rootPaddings = fieldPaddings;
rootIgnore = ignore;
} else {
lookupChars = null;
rootLengths = null;
rootAlignments = null;
rootPaddings = null;
rootIgnore = null;
}
}
@Override
protected void processRow(Object[] row) {
if (row.length > 0 && lookaheadFormats != null || lookbehindFormats != null) {
int dstBegin = 0;
for (int i = 0; i < row.length && dstBegin < lookupChars.length; i++) {
String value = String.valueOf(row[i]);
int len = value.length();
if (dstBegin + len > lookupChars.length) {
len = lookupChars.length - dstBegin;
}
value.getChars(0, len, lookupChars, dstBegin);
dstBegin += len;
}
for (int i = lookupChars.length - 1; i > dstBegin; i--) {
lookupChars[i] = '\0';
}
boolean matched = false;
if (lookaheadFormats != null) {
for (int i = 0; i < lookaheadFormats.length; i++) {
if (lookaheadFormats[i].matches(lookupChars)) {
fieldLengths = lookaheadFormats[i].lengths;
fieldAlignments = lookaheadFormats[i].alignments;
fieldPaddings = lookaheadFormats[i].paddings;
ignore = lookaheadFormats[i].ignore;
matched = true;
break;
}
}
if (lookbehindFormats != null && matched) {
lookbehindFormat = null;
for (int i = 0; i < lookbehindFormats.length; i++) {
if (lookbehindFormats[i].matches(lookupChars)) {
lookbehindFormat = lookbehindFormats[i];
break;
}
}
}
} else {
for (int i = 0; i < lookbehindFormats.length; i++) {
if (lookbehindFormats[i].matches(lookupChars)) {
lookbehindFormat = lookbehindFormats[i];
matched = true;
fieldLengths = rootLengths;
fieldAlignments = rootAlignments;
fieldPaddings = rootPaddings;
ignore = rootIgnore;
break;
}
}
}
if (!matched) {
if (lookbehindFormat == null) {
if (rootLengths == null) {
throw new TextWritingException("Cannot write with the given configuration. No default field lengths defined and no lookahead/lookbehind value match '" + new String(lookupChars) + '\'', getRecordCount(), row);
}
fieldLengths = rootLengths;
fieldAlignments = rootAlignments;
fieldPaddings = rootPaddings;
ignore = rootIgnore;
} else {
fieldLengths = lookbehindFormat.lengths;
fieldAlignments = lookbehindFormat.alignments;
fieldPaddings = lookbehindFormat.paddings;
ignore = lookbehindFormat.ignore;
}
}
}
if (expandRows) {
row = expand(row, fieldLengths.length - ignoreCount, null);
}
final int lastIndex = fieldLengths.length < row.length ? fieldLengths.length : row.length;
int off = 0;
for (int i = 0; i < lastIndex + off; i++) {
length = fieldLengths[i];
if (ignore[i]) {
off++;
this.appender.fill(' ', length);
} else {
alignment = fieldAlignments[i];
padding = fieldPaddings[i];
if (writingHeaders) {
if (defaultHeaderPadding) {
padding = defaultPadding;
}
if (defaultHeaderAlignment != null) {
alignment = defaultHeaderAlignment;
}
}
String nextElement = getStringValue(row[i - off]);
boolean allowTrim = allowTrim(i);
processElement(nextElement, allowTrim);
appendValueToRow();
}
}
}
private void append(String element, boolean allowTrim) {
int start = 0;
if (allowTrim && this.ignoreLeading) {
start = skipLeadingWhitespace(whitespaceRangeStart, element);
}
int padCount = alignment.calculatePadding(length, element.length() - start);
length -= padCount;
appender.fill(padding, padCount);
if (allowTrim && this.ignoreTrailing) {
int i = start;
while (i < element.length() && length > 0) {
for (; i < element.length() && length-- > 0; i++) {
char nextChar = element.charAt(i);
appender.appendIgnoringWhitespace(nextChar);
}
if (length == -1 && appender.whitespaceCount() > 0) {
//if we got here then the value to write got truncated exactly after one or more whitespaces.
//In this case, if the whitespaces are not at the end of the truncated value they will be put back to the output.
for (int j = i; j < element.length(); j++) {
if (element.charAt(j) > ' ') {
//resets the whitespace count so the original whitespaces are printed to the output.
appender.resetWhitespaceCount();
break;
}
}
if (appender.whitespaceCount() > 0) {
length = 0;
}
}
length += appender.whitespaceCount();
appendValueToRow();
}
} else {
for (int i = start; i < element.length() && length-- > 0; i++) {
char nextChar = element.charAt(i);
appender.append(nextChar);
}
}
}
private void processElement(String element, boolean allowTrim) {
if (element != null) {
append(element, allowTrim);
}
appender.fill(padding, length);
}
}