diff --git a/src/main/java/com/univocity/parsers/fixed/FixedWidthWriter.java b/src/main/java/com/univocity/parsers/fixed/FixedWidthWriter.java index 015d2577..4e2579dc 100644 --- a/src/main/java/com/univocity/parsers/fixed/FixedWidthWriter.java +++ b/src/main/java/com/univocity/parsers/fixed/FixedWidthWriter.java @@ -50,6 +50,8 @@ public class FixedWidthWriter extends AbstractWriter { private boolean[] rootIgnore; private int ignoreCount; + private FixedWidthFields customFields = null; + private char[] rootPaddings; private boolean defaultHeaderPadding; @@ -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) { @@ -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); } diff --git a/src/test/java/com/univocity/parsers/fixed/FixedWidthWriterTest.java b/src/test/java/com/univocity/parsers/fixed/FixedWidthWriterTest.java index 1c3c1486..1189429a 100755 --- a/src/test/java/com/univocity/parsers/fixed/FixedWidthWriterTest.java +++ b/src/test/java/com/univocity/parsers/fixed/FixedWidthWriterTest.java @@ -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[]{