This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository nuiton-csv. See https://gitlab.nuiton.org/nuiton/nuiton-csv.git commit 2e7527b919048756b0021c97a218874d9d00cadb Author: Arnaud Thimel <thimel@codelutin.com> Date: Tue May 1 16:05:57 2018 +0200 Add 'rawContent' support in ImportRow --- src/main/java/org/nuiton/csv/Import2.java | 11 ++- src/main/java/org/nuiton/csv/ImportConf.java | 18 +++++ src/main/java/org/nuiton/csv/ImportRow.java | 28 +++++++ src/test/java/org/nuiton/csv/Import2Test.java | 107 +++++++++++++++++++------- 4 files changed, 134 insertions(+), 30 deletions(-) diff --git a/src/main/java/org/nuiton/csv/Import2.java b/src/main/java/org/nuiton/csv/Import2.java index 9e90202..2a11e3a 100644 --- a/src/main/java/org/nuiton/csv/Import2.java +++ b/src/main/java/org/nuiton/csv/Import2.java @@ -310,9 +310,10 @@ public class Import2<E> implements Iterable<ImportRow<E>>, Closeable { } this.conf = conf; this.model = model; - reader = new CsvReader(inputStream, model.getSeparator(), Charset.forName("UTF-8")); - reader.setTrimWhitespace(true); + this.reader = new CsvReader(inputStream, model.getSeparator(), Charset.forName("UTF-8")); + this.reader.setTrimWhitespace(true); this.reader.setSafetySwitch(conf.isSafetySwitch()); + this.reader.setCaptureRawRecord(conf.isCaptureRawRecord()); } protected Import2(ImportConf conf, ImportModel<E> model, Reader reader) { @@ -324,6 +325,7 @@ public class Import2<E> implements Iterable<ImportRow<E>>, Closeable { this.reader = new CsvReader(reader, model.getSeparator()); this.reader.setTrimWhitespace(true); this.reader.setSafetySwitch(conf.isSafetySwitch()); + this.reader.setCaptureRawRecord(conf.isCaptureRawRecord()); } /** @@ -386,6 +388,11 @@ public class Import2<E> implements Iterable<ImportRow<E>>, Closeable { boolean strictMode = conf.isStrictMode(); + if (conf.isCaptureRawRecord()) { + String rawRecord = reader.getRawRecord(); + row.setRawRecord(rawRecord); + } + for (ImportableColumn<E, Object> field : columns) { // read value from csv cell diff --git a/src/main/java/org/nuiton/csv/ImportConf.java b/src/main/java/org/nuiton/csv/ImportConf.java index f321c28..86553b4 100644 --- a/src/main/java/org/nuiton/csv/ImportConf.java +++ b/src/main/java/org/nuiton/csv/ImportConf.java @@ -67,6 +67,15 @@ public class ImportConf { */ protected boolean cloneImportRow = false; + /** + * Flag to ask for raw record in result. + * <p> + * By default, raw record are not included. + * + * @since 3.0 + */ + protected boolean captureRawRecord = false; + public boolean isSafetySwitch() { return safetySwitch; } @@ -98,4 +107,13 @@ public class ImportConf { public void setCloneImportRow(boolean cloneImportRow) { this.cloneImportRow = cloneImportRow; } + + public boolean isCaptureRawRecord() { + return captureRawRecord; + } + + public void setCaptureRawRecord(boolean captureRawRecord) { + this.captureRawRecord = captureRawRecord; + } + } diff --git a/src/main/java/org/nuiton/csv/ImportRow.java b/src/main/java/org/nuiton/csv/ImportRow.java index 07db7f8..2552dab 100644 --- a/src/main/java/org/nuiton/csv/ImportRow.java +++ b/src/main/java/org/nuiton/csv/ImportRow.java @@ -22,6 +22,8 @@ package org.nuiton.csv; * #L% */ +import com.google.common.base.Optional; +import com.google.common.base.Strings; import com.google.common.collect.Sets; import org.apache.commons.collections4.CollectionUtils; @@ -49,11 +51,18 @@ public class ImportRow<E> { protected boolean next; + /** + * The source data. May be empty if {@link ImportConf#captureRawRecord} is false. + * When project will be Java8 based, use Java8 Optional + */ + protected Optional<String> rawRecord = Optional.absent(); + public ImportRow(ImportRow<E> row) { this.lineNumber = row.getLineNumber(); this.bean = row.getBean(); this.errors = Sets.newHashSet(row.getErrors()); this.setNext(row.hasNext()); + this.rawRecord = row.getRawRecord(); } public ImportRow() { @@ -98,6 +107,25 @@ public class ImportRow<E> { this.bean = bean; lineNumber++; errors = null; + rawRecord = Optional.absent(); + } + + public Optional<String> getRawRecord() { + return rawRecord; } + public void setRawRecord(String rawRecord) { + this.rawRecord = Optional.fromNullable(Strings.emptyToNull(rawRecord)); + } + + @Override + public String toString() { + return "ImportRow{" + + "lineNumber=" + lineNumber + + ", bean=" + bean + + ", errors=" + errors + + ", next=" + next + + ", rawRecord=" + rawRecord + + '}'; + } } diff --git a/src/test/java/org/nuiton/csv/Import2Test.java b/src/test/java/org/nuiton/csv/Import2Test.java index 009bf5a..a153928 100644 --- a/src/test/java/org/nuiton/csv/Import2Test.java +++ b/src/test/java/org/nuiton/csv/Import2Test.java @@ -22,6 +22,8 @@ package org.nuiton.csv; * #L% */ +import com.google.common.base.Joiner; +import com.google.common.collect.ImmutableList; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -123,41 +125,90 @@ public class Import2Test { assertRowEquals(rows.get(2), DateUtil.createDate(7, 12, 2011), null, "3ème ligne", RowBeanEnum.TWO); } - protected List<RowBean> importContent(ImportConf conf, ImportModel<RowBean> model, + @Test + public void testCaptureRawRecord() { + String row1 = "2011-12-05;18;\"1ère ligne\";ZERO"; + String row2 = "2011-12-06;19;\"2ème ligne\";ONE"; + String row3 = "2011-12-07;21;\"3ème ligne\";TWO"; + String row4 = ";;"; + String content = "DATE;NUMBER;TITLE;ROWBEANENUM\n" + Joiner.on("\n").join(row1, row2, row3, row4); + + ImportConf conf = new ImportConf(); + conf.setCaptureRawRecord(true); + conf.setCloneImportRow(true); + + try ( + StringReader reader = new StringReader(content); + Import2<RowBean> rowImport = Import2.newImport(conf, importModel, reader) + ) { + ImmutableList<ImportRow<RowBean>> rows = ImmutableList.copyOf(rowImport); + + Assert.assertEquals(4, rows.size()); + Assert.assertTrue(rows.get(0).getRawRecord().isPresent()); + Assert.assertEquals(row1, rows.get(0).getRawRecord().get()); + Assert.assertTrue(rows.get(1).getRawRecord().isPresent()); + Assert.assertEquals(row2, rows.get(1).getRawRecord().get()); + Assert.assertTrue(rows.get(2).getRawRecord().isPresent()); + Assert.assertEquals(row3, rows.get(2).getRawRecord().get()); + Assert.assertTrue(rows.get(3).getRawRecord().isPresent()); + Assert.assertEquals(row4, rows.get(3).getRawRecord().get()); + } + } + + @Test + public void testDontCaptureRawRecord() { + String row1 = "2011-12-05;18;\"1ère ligne\";ZERO"; + String row2 = "2011-12-06;19;\"2ème ligne\";ONE"; + String row3 = "2011-12-07;21;\"3ème ligne\";TWO"; + String row4 = ";;"; + String content = "DATE;NUMBER;TITLE;ROWBEANENUM\n" + Joiner.on("\n").join(row1, row2, row3, row4); + + ImportConf conf = new ImportConf(); + conf.setCaptureRawRecord(false); + conf.setCloneImportRow(true); + + + try ( + StringReader reader = new StringReader(content); + Import2<RowBean> rowImport = Import2.newImport(conf, importModel, reader) + ) { + ImmutableList<ImportRow<RowBean>> rows = ImmutableList.copyOf(rowImport); + + Assert.assertEquals(4, rows.size()); + Assert.assertFalse(rows.get(0).getRawRecord().isPresent()); + Assert.assertFalse(rows.get(1).getRawRecord().isPresent()); + Assert.assertFalse(rows.get(2).getRawRecord().isPresent()); + Assert.assertFalse(rows.get(3).getRawRecord().isPresent()); + } + } + + protected List<RowBean> importContent(ImportConf conf, + ImportModel<RowBean> model, String content) throws IOException { - Reader reader = new StringReader(content); - try { - Import2<RowBean> rowImport = Import2.newImport(conf, model, reader); - try { - List<RowBean> result = new ArrayList<RowBean>(); - for (ImportRow<RowBean> row : rowImport) { - result.add(row.getBean()); - } - return result; - } finally { - rowImport.close(); + + try ( + Reader reader = new StringReader(content); + Import2<RowBean> rowImport = Import2.newImport(conf, model, reader) + ){ + List<RowBean> result = new ArrayList<RowBean>(); + for (ImportRow<RowBean> row : rowImport) { + result.add(row.getBean()); } - } finally { - reader.close(); - } + return result; + } } protected List<RowBean> importContent(ImportModel<RowBean> model, String content) throws IOException { - Reader reader = new StringReader(content); - try { - Import2<RowBean> rowImport = Import2.newImport(model, reader); - try { - List<RowBean> result = new ArrayList<RowBean>(); - for (ImportRow<RowBean> row : rowImport) { - result.add(row.getBean()); - } - return result; - } finally { - rowImport.close(); + try ( + Reader reader = new StringReader(content); + Import2<RowBean> rowImport = Import2.newImport(model, reader) + ){ + List<RowBean> result = new ArrayList<RowBean>(); + for (ImportRow<RowBean> row : rowImport) { + result.add(row.getBean()); } - } finally { - reader.close(); + return result; } } -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.