format = format.withNullString(CSVUtils.unescape(context.getProperty(NULL_STRING).getValue()));
public CSVFormat getCSVFormat() { return CSVFormat.DEFAULT .withDelimiter(delimiter) .withRecordSeparator(recordSeparator) .withNullString(nullString) .withEscape(escapeCharacter) .withQuote(quoteCharacter) .withQuoteMode(quoteMode); }
@Test public void testPrintCustomNullValues() throws IOException { final StringWriter sw = new StringWriter(); try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withNullString("NULL"))) { printer.printRecord("a", null, "b"); assertEquals("a,NULL,b" + recordSeparator, sw.toString()); } }
@Test public void testWithoutQuoteMode() throws Exception { final CSVFormat format = CSVFormat.EXCEL .withNullString("N/A") .withIgnoreSurroundingSpaces(true); final StringBuffer buffer = new StringBuffer(); final CSVPrinter printer = new CSVPrinter(buffer, format); printer.printRecord(new Object[] { null, "Hello", null, "World" }); Assert.assertEquals("N/A,Hello,N/A,World\r\n", buffer.toString()); }
@Test public void testQuoteModeAll() throws Exception { final CSVFormat format = CSVFormat.EXCEL .withNullString("N/A") .withIgnoreSurroundingSpaces(true) .withQuoteMode(QuoteMode.ALL); final StringBuffer buffer = new StringBuffer(); final CSVPrinter printer = new CSVPrinter(buffer, format); printer.printRecord(new Object[] { null, "Hello", null, "World" }); Assert.assertEquals("\"N/A\",\"Hello\",\"N/A\",\"World\"\r\n", buffer.toString()); }
@Test public void testQuoteModeMinimal() throws Exception { final CSVFormat format = CSVFormat.EXCEL .withNullString("N/A") .withIgnoreSurroundingSpaces(true) .withQuoteMode(QuoteMode.MINIMAL); final StringBuffer buffer = new StringBuffer(); final CSVPrinter printer = new CSVPrinter(buffer, format); printer.printRecord(new Object[] { null, "Hello", null, "World" }); Assert.assertEquals("N/A,Hello,N/A,World\r\n", buffer.toString()); }
@Test public void testQuoteModeNonNumeric() throws Exception { final CSVFormat format = CSVFormat.EXCEL .withNullString("N/A") .withIgnoreSurroundingSpaces(true) .withQuoteMode(QuoteMode.NON_NUMERIC); final StringBuffer buffer = new StringBuffer(); final CSVPrinter printer = new CSVPrinter(buffer, format); printer.printRecord(new Object[] { null, "Hello", null, "World" }); Assert.assertEquals("N/A,\"Hello\",N/A,\"World\"\r\n", buffer.toString()); }
@Test public void testWithEmptyValues() throws Exception { final CSVFormat format = CSVFormat.EXCEL .withNullString("N/A") .withIgnoreSurroundingSpaces(true) .withQuoteMode(QuoteMode.ALL); final StringBuffer buffer = new StringBuffer(); final CSVPrinter printer = new CSVPrinter(buffer, format); printer.printRecord(new Object[] { "", "Hello", "", "World" }); //printer.printRecord(new Object[] { null, "Hello", null, "World" }); Assert.assertEquals("\"\",\"Hello\",\"\",\"World\"\r\n", buffer.toString()); } }
@Test public void testQuoteModeAllNonNull() throws Exception { final CSVFormat format = CSVFormat.EXCEL .withNullString("N/A") .withIgnoreSurroundingSpaces(true) .withQuoteMode(QuoteMode.ALL_NON_NULL); final StringBuffer buffer = new StringBuffer(); final CSVPrinter printer = new CSVPrinter(buffer, format); printer.printRecord(new Object[] { null, "Hello", null, "World" }); Assert.assertEquals("N/A,\"Hello\",N/A,\"World\"\r\n", buffer.toString()); }
@Test public void testParseCustomNullValues() throws IOException { final StringWriter sw = new StringWriter(); final CSVFormat format = CSVFormat.DEFAULT.withNullString("NULL"); try (final CSVPrinter printer = new CSVPrinter(sw, format)) { printer.printRecord("a", null, "b"); } final String csvString = sw.toString(); assertEquals("a,NULL,b" + recordSeparator, csvString); try (final CSVParser iterable = format.parse(new StringReader(csvString))) { final Iterator<CSVRecord> iterator = iterable.iterator(); final CSVRecord record = iterator.next(); assertEquals("a", record.get(0)); assertEquals(null, record.get(1)); assertEquals("b", record.get(2)); assertFalse(iterator.hasNext()); } }
@Test public void testMySqlNullOutput() throws IOException { Object[] s = new String[] { "NULL", null }; CSVFormat format = CSVFormat.MYSQL.withQuote(DQUOTE_CHAR).withNullString("NULL").withQuoteMode(QuoteMode.NON_NUMERIC); StringWriter writer = new StringWriter(); try (final CSVPrinter printer = new CSVPrinter(writer, format)) { format = CSVFormat.MYSQL.withNullString("\\N"); writer = new StringWriter(); try (final CSVPrinter printer = new CSVPrinter(writer, format)) { format = CSVFormat.MYSQL.withNullString("\\N"); writer = new StringWriter(); try (final CSVPrinter printer = new CSVPrinter(writer, format)) { format = CSVFormat.MYSQL.withNullString("\\N"); writer = new StringWriter(); try (final CSVPrinter printer = new CSVPrinter(writer, format)) { format = CSVFormat.MYSQL.withNullString("NULL"); writer = new StringWriter(); try (final CSVPrinter printer = new CSVPrinter(writer, format)) {
public void testPostgreSqlCsvNullOutput() throws IOException { Object[] s = new String[] { "NULL", null }; CSVFormat format = CSVFormat.POSTGRESQL_CSV.withQuote(DQUOTE_CHAR).withNullString("NULL").withQuoteMode(QuoteMode.ALL_NON_NULL); StringWriter writer = new StringWriter(); try (final CSVPrinter printer = new CSVPrinter(writer, format)) { format = CSVFormat.POSTGRESQL_CSV.withNullString("\\N"); writer = new StringWriter(); try (final CSVPrinter printer = new CSVPrinter(writer, format)) { format = CSVFormat.POSTGRESQL_CSV.withNullString("\\N"); writer = new StringWriter(); try (final CSVPrinter printer = new CSVPrinter(writer, format)) { format = CSVFormat.POSTGRESQL_CSV.withNullString("\\N"); writer = new StringWriter(); try (final CSVPrinter printer = new CSVPrinter(writer, format)) { format = CSVFormat.POSTGRESQL_CSV.withNullString("NULL"); writer = new StringWriter(); try (final CSVPrinter printer = new CSVPrinter(writer, format)) {
public void testPostgreSqlCsvTextOutput() throws IOException { Object[] s = new String[] { "NULL", null }; CSVFormat format = CSVFormat.POSTGRESQL_TEXT.withQuote(DQUOTE_CHAR).withNullString("NULL").withQuoteMode(QuoteMode.ALL_NON_NULL); StringWriter writer = new StringWriter(); try (final CSVPrinter printer = new CSVPrinter(writer, format)) { format = CSVFormat.POSTGRESQL_TEXT.withNullString("\\N"); writer = new StringWriter(); try (final CSVPrinter printer = new CSVPrinter(writer, format)) { format = CSVFormat.POSTGRESQL_TEXT.withNullString("\\N"); writer = new StringWriter(); try (final CSVPrinter printer = new CSVPrinter(writer, format)) { format = CSVFormat.POSTGRESQL_TEXT.withNullString("\\N"); writer = new StringWriter(); try (final CSVPrinter printer = new CSVPrinter(writer, format)) { format = CSVFormat.POSTGRESQL_TEXT.withNullString("NULL"); writer = new StringWriter(); try (final CSVPrinter printer = new CSVPrinter(writer, format)) {
format = format.withHeader("author", "title", "publishDate"); format = format.withHeaderComments("headerComment"); format = format.withNullString("NULL"); format = format.withIgnoreEmptyLines(true); format = format.withIgnoreSurroundingSpaces(true);
/** * Create IS24-CSV format. */ public Is24CsvFormat() { super(CSVFormat.newFormat('|') .withRecordSeparator(RECORD_SEPARATOR) .withNullString(StringUtils.EMPTY)); }
/** * Returns the standard CSV format used by Obevo by both readers (for deploy) and writers (for reverse-engineering) of CSV. */ public static CSVFormat getCsvFormat(char delim, String nullToken) { return CSVFormat.newFormat(delim).withRecordSeparator("\r\n").withIgnoreSurroundingSpaces(true).withQuote('"').withEscape('\\').withNullString(nullToken); }
/** * Returns the standard CSV format used by Obevo by both readers (for deploy) and writers (for reverse-engineering) of CSV. */ public static CSVFormat getCsvFormat(char delim, String nullToken) { return CSVFormat.newFormat(delim).withRecordSeparator("\r\n").withIgnoreSurroundingSpaces(true).withQuote('"').withEscape('\\').withNullString(nullToken); }
/** * Each String in the stream is a CSV file * @return stream of parsed insert queries */ public Stream<Map<String, Object>> convert() { try{ CSVParser csvParser = CSVFormat.newFormat(separator) .withIgnoreEmptyLines() .withEscape('\\' ) .withFirstRecordAsHeader() .withQuote(quote) .withNullString(nullString) .parse(reader); return stream(csvParser.iterator()).map(this::parse); } catch (IOException e){ throw new RuntimeException(e); } }
private CSVParser createCSVParser(CSVParserSettings parserSettings) { CSVFormat csvFormat = CSVFormat.DEFAULT.withDelimiter(parserSettings.getDelimiter()) .withRecordSeparator(parserSettings.getRecordSeparator()) .withTrim(parserSettings.isTrim()) .withQuote(parserSettings.getQuote()) .withCommentMarker(parserSettings.getCommentMarker()) .withIgnoreEmptyLines(parserSettings.isIgnoreEmptyLines()) .withNullString(parserSettings.getNullString()); try { File file = new File(parserSettings.getPath()); return new CSVParser(new FileReader(file), csvFormat); } catch (Exception e) { throw e instanceof RuntimeException ? (RuntimeException) e : new RuntimeException(e); } } }
private CSVParser createCSVParser(CSVParserSettings parserSettings) { CSVFormat csvFormat = CSVFormat.DEFAULT.withDelimiter(parserSettings.getDelimiter()) .withRecordSeparator(parserSettings.getRecordSeparator()) .withTrim(parserSettings.isTrim()) .withQuote(parserSettings.getQuote()) .withCommentMarker(parserSettings.getCommentMarker()) .withIgnoreEmptyLines(parserSettings.isIgnoreEmptyLines()) .withNullString(parserSettings.getNullString()); try { File file = new File(parserSettings.getPath()); return new CSVParser(new FileReader(file), csvFormat); } catch (Exception e) { throw e instanceof RuntimeException ? (RuntimeException) e : new RuntimeException(e); } } }