Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write with custom fields #521

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/main/java/com/univocity/parsers/fixed/FixedWidthWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public class FixedWidthWriter extends AbstractWriter<FixedWidthWriterSettings> {
private boolean[] rootIgnore;
private int ignoreCount;

private FixedWidthFields customFields = null;


private char[] rootPaddings;
private boolean defaultHeaderPadding;
Expand Down Expand Up @@ -183,6 +185,12 @@ protected final void initialize(FixedWidthWriterSettings settings) {
}
}

public void writeRowWithCustomFields(Object[] row, FixedWidthFields customFields) {
this.customFields = customFields;
super.writeRow(row);
this.customFields = null;
}

@Override
protected void processRow(Object[] row) {
if (row.length > 0 && lookaheadFormats != null || lookbehindFormats != null) {
Expand Down Expand Up @@ -256,6 +264,14 @@ protected void processRow(Object[] row) {
}
}

if (customFields == null) {
buildRow(row, fieldLengths, fieldAlignments, fieldPaddings, ignore);
} else {
buildRow(row, customFields.getFieldLengths(), customFields.getFieldAlignments(), customFields.getFieldPaddings(), customFields.getFieldsToIgnore());
}
}

private void buildRow(Object[] row, int[] fieldLengths, FieldAlignment[] fieldAlignments, char[] fieldPaddings, boolean[] ignore) {
if (expandRows) {
row = expand(row, fieldLengths.length - ignoreCount, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,60 @@ public void testWriter(char[] lineSeparator) throws Exception {
}
}

@Test(enabled = true, dataProvider = "lineSeparatorProvider")
public void testWriterWithCustomFields(char[] lineSeparator) throws Exception {
String[] expectedHeaders = new String[]{
"DATE", "NAME", "OWED", "INTEREST",
};

String[][] input = new String[][]{
{"2013-FEB-27", "Standard padding", "1.00", "1.000", "- 5 -",},
{"2013-FEB-28", "Custom padding", "12345.99", "1.234", "- 5 -",},
{"2013-FEB-28", "Standard padding", "15000.99", "8.786", "- 5 -",},

};

String lineSeparatorString = new String(lineSeparator);
String expectedResult = "DATE-------NAME----------------------------------OWED----------------INTEREST" + lineSeparatorString
+ "2013-FEB-27Standard padding----------------------1.00----------------1.000---" + lineSeparatorString
+ "2013-FEB-28**** Custom padding000000012345.99*****1.234*****" + lineSeparatorString
+ "2013-FEB-28Standard padding----------------------15000.99------------8.786---" + lineSeparatorString;

FixedWidthFields customFields = new FixedWidthFields()
.addField("1", 15, FieldAlignment.LEFT, '*')
.addField("2", 15, FieldAlignment.RIGHT, ' ')
.addField("3", 15, FieldAlignment.RIGHT, '0')
.addField("4", 15, FieldAlignment.CENTER, '*');

FixedWidthWriterSettings settings = new FixedWidthWriterSettings(getFieldLengths());
settings.getFormat().setLineSeparator(lineSeparator);
settings.getFormat().setPadding('-');
settings.setNullValue("?");

settings.setIgnoreLeadingWhitespaces(false);
settings.setIgnoreTrailingWhitespaces(false);
settings.setHeaders(expectedHeaders);


ByteArrayOutputStream fixedWidthResult = new ByteArrayOutputStream();

FixedWidthWriter writer = new FixedWidthWriter(new OutputStreamWriter(fixedWidthResult, "UTF-8"), settings);
writer.writeHeaders();
writer.writeRow(input[0]);
writer.writeRowWithCustomFields(input[1], customFields);
writer.writeRow(input[2]);
writer.close();

String result = fixedWidthResult.toString();
try {
assertEquals(result, expectedResult);
} catch (Error e) {
result = result.replaceAll("\r", "\\\\r");
System.out.println("FAILED:\n===\n" + result + "\n===");
throw e;
}
}

@Test(enabled = true, dataProvider = "lineSeparatorProvider")
public void testWriterWithSpacesAndOverflow(char[] lineSeparator) throws Exception {
String[] expectedHeaders = new String[]{
Expand Down