diff --git a/org.eclipse.scout.rt.shared.test/src/test/java/org/eclipse/scout/rt/shared/csv/CsvHelperTest.java b/org.eclipse.scout.rt.shared.test/src/test/java/org/eclipse/scout/rt/shared/csv/CsvHelperTest.java index 99d1f5665ce..bf692f1506e 100644 --- a/org.eclipse.scout.rt.shared.test/src/test/java/org/eclipse/scout/rt/shared/csv/CsvHelperTest.java +++ b/org.eclipse.scout.rt.shared.test/src/test/java/org/eclipse/scout/rt/shared/csv/CsvHelperTest.java @@ -19,6 +19,7 @@ import java.io.FileReader; import java.io.IOException; import java.io.Reader; +import java.io.StringReader; import java.io.StringWriter; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; @@ -301,6 +302,26 @@ public void testImportWithoutHeaderRowsAndUtfWithBomEncoding() throws IOExceptio } } + @Test + public void testEndOfLineDefault() { + CsvHelper csvHelper = new CsvHelper(); + String testInputLine = "1,2,3#4,5,6\n7,8,9,10,11"; + Object[][] expectedData = new Object[][]{{"1", "2", "3#4", "5", "6"}, {"7", "8", "9", "10", "11"}}; + + Object[][] actualData = csvHelper.importData(new StringReader(testInputLine), 0, null, Integer.MAX_VALUE); + assertEquals(expectedData, actualData); + } + + @Test + public void testEndOfLine() { + CsvHelper csvHelper = new CsvHelper(null, (char) 0, (char) 0, null, ch -> ch == '#'); + String testInputLine = "1;2;3#4;5;6\n7;8;9;10;11"; + Object[][] expectedData = new Object[][]{{"1", "2", "3"}, {"4", "5", "6\n7", "8", "9", "10", "11"}}; + + Object[][] actualData = csvHelper.importData(new StringReader(testInputLine), 0, null, Integer.MAX_VALUE); + assertEquals(expectedData, actualData); + } + protected void addIfSupported(String charset, List charsets) { if (Charset.isSupported(charset)) { charsets.add(Charset.forName(charset)); diff --git a/org.eclipse.scout.rt.shared/src/main/java/org/eclipse/scout/rt/shared/csv/CsvHelper.java b/org.eclipse.scout.rt.shared/src/main/java/org/eclipse/scout/rt/shared/csv/CsvHelper.java index 74e52c2422d..578b03ca3df 100644 --- a/org.eclipse.scout.rt.shared/src/main/java/org/eclipse/scout/rt/shared/csv/CsvHelper.java +++ b/org.eclipse.scout.rt.shared/src/main/java/org/eclipse/scout/rt/shared/csv/CsvHelper.java @@ -31,12 +31,14 @@ import java.util.Iterator; import java.util.List; import java.util.Locale; +import java.util.function.Function; import org.eclipse.scout.rt.platform.BEANS; import org.eclipse.scout.rt.platform.exception.PlatformExceptionTranslator; import org.eclipse.scout.rt.platform.exception.ProcessingException; import org.eclipse.scout.rt.platform.nls.NlsLocale; import org.eclipse.scout.rt.platform.util.BomInputStreamReader; +import org.eclipse.scout.rt.platform.util.BooleanUtility; import org.eclipse.scout.rt.platform.util.CollectionUtility; import org.eclipse.scout.rt.platform.util.NumberFormatProvider; import org.eclipse.scout.rt.platform.util.StringUtility; @@ -54,11 +56,13 @@ public class CsvHelper { */ public static final String IGNORED_COLUMN_NAME = "null"; private static final Logger LOG = LoggerFactory.getLogger(CsvHelper.class); + public static final Function DEFAULT_NEW_LINE_FUNCTION = ch -> ch == '\n' || ch == '\r'; private final Locale m_locale; private final char m_separatorChar;// ";" private final char m_textDelimiterChar;// "\"" private final String m_lineSeparator;// "\n" + private final Function m_isEndOfLine; // ch -> ch == '\n' || ch == '\r' private int m_colCount; private List m_colNames; private List m_colTypes; @@ -75,11 +79,16 @@ public CsvHelper(Locale locale, String separatorChar, String textDelimiterChar, } public CsvHelper(Locale locale, char separatorChar, char textDelimiterChar, String lineSeparator) { + this(locale, separatorChar, textDelimiterChar, lineSeparator, DEFAULT_NEW_LINE_FUNCTION); + } + + public CsvHelper(Locale locale, char separatorChar, char textDelimiterChar, String lineSeparator, Function isEndOfLine) { m_locale = locale == null ? NlsLocale.get() : locale; m_separatorChar = separatorChar != 0x00 ? separatorChar : ';'; m_textDelimiterChar = textDelimiterChar != 0x00 ? textDelimiterChar : '"'; m_lineSeparator = lineSeparator != null ? lineSeparator : "\n"; m_colFormat = new ArrayList<>(); + m_isEndOfLine = isEndOfLine != null ? isEndOfLine : DEFAULT_NEW_LINE_FUNCTION; } public Locale getLocale() { @@ -102,6 +111,10 @@ public boolean isEncodeLineSeparator() { return m_encodeLineSeparator; } + public boolean isEndOfLine(char ch) { + return BooleanUtility.nvl(m_isEndOfLine.apply(ch)); + } + /** * Configures whether a text containing the line separator should be encoded. */ @@ -426,7 +439,7 @@ protected List importRow(Reader reader) throws IOException { String token; int ch = reader.read(); - while (ch == '\n' || ch == '\r') { + while (isEndOfLine((char) ch)) { ch = reader.read(); } if (ch < 0) { @@ -443,7 +456,7 @@ protected List importRow(Reader reader) throws IOException { curBuf.append((char) ch); } else {// ch<0 or out of string or end of line - if (ch == getSeparatorChar() || ch < 0 || ch == '\n' || ch == '\r') { + if (ch == getSeparatorChar() || ch < 0 || isEndOfLine((char) ch)) { // consume token token = curBuf.toString(); curBuf.setLength(0); @@ -464,7 +477,7 @@ protected List importRow(Reader reader) throws IOException { token = decodeText(token); cellList.add(token); // check if end of current line - if (ch < 0 || ch == '\n' || ch == '\r') { + if (ch < 0 || isEndOfLine((char) ch)) { break; } }