public static String[] parseAsCsv(String key, String value, Function<String, String> valueProcessor) { String cleanValue = MultivalueProperty.trimFieldsAndRemoveEmptyFields(value); List<String> result = new ArrayList<>(); try (CSVParser csvParser = CSVFormat.RFC4180 .withHeader((String) null) .withIgnoreEmptyLines() .withIgnoreSurroundingSpaces() .parse(new StringReader(cleanValue))) { List<CSVRecord> records = csvParser.getRecords(); if (records.isEmpty()) { return ArrayUtils.EMPTY_STRING_ARRAY; } processRecords(result, records, valueProcessor); return result.toArray(new String[result.size()]); } catch (IOException e) { throw new IllegalStateException("Property: '" + key + "' doesn't contain a valid CSV value: '" + value + "'", e); } }
/** * Returns a new {@code CSVFormat} with the trimming behavior of the format set to {@code true}. * * @return A new CSVFormat that is equal to this but with the specified trimming behavior. * @see #withIgnoreSurroundingSpaces(boolean) * @since 1.1 */ public CSVFormat withIgnoreSurroundingSpaces() { return this.withIgnoreSurroundingSpaces(true); }
@Test public void testWithoutNullString() 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(",\"Hello\",,\"World\"\r\n", buffer.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 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 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()); } }
/** * Sets the trimming behavior of the format to {@code true}. * * @return A new CSVFormat that is equal to this but with the specified trimming behavior. * @see #withIgnoreSurroundingSpaces(boolean) * @since 1.1 */ public CSVFormat withIgnoreSurroundingSpaces() { return this.withIgnoreSurroundingSpaces(true); }
/** * Returns a new {@code CSVFormat} with the trimming behavior of the format set to {@code true}. * * @return A new CSVFormat that is equal to this but with the specified trimming behavior. * @see #withIgnoreSurroundingSpaces(boolean) * @since 1.1 */ public CSVFormat withIgnoreSurroundingSpaces() { return this.withIgnoreSurroundingSpaces(true); }
/** * Returns a new {@code CSVFormat} with the trimming behavior of the format set to {@code true}. * * @return A new CSVFormat that is equal to this but with the specified trimming behavior. * @see #withIgnoreSurroundingSpaces(boolean) * @since 1.1 */ public CSVFormat withIgnoreSurroundingSpaces() { return this.withIgnoreSurroundingSpaces(true); }
format = format.withIgnoreEmptyLines(Boolean.parseBoolean(option_parts[1])); } else if ("IgnoreSpaces".equalsIgnoreCase(option_parts[0])) { format = format.withIgnoreSurroundingSpaces(Boolean.parseBoolean(option_parts[1])); } else if ("CommentStart".equalsIgnoreCase(option_parts[0])) { format = format.withCommentMarker(option_parts[1].charAt(0));
format = format.withIgnoreEmptyLines(Boolean.parseBoolean(option_parts[1])); } else if ("IgnoreSpaces".equalsIgnoreCase(option_parts[0])) { format = format.withIgnoreSurroundingSpaces(Boolean.parseBoolean(option_parts[1])); } else if ("CommentStart".equalsIgnoreCase(option_parts[0])) { format = format.withCommentMarker(option_parts[1].charAt(0));
format = format.withNullString("NULL"); format = format.withIgnoreEmptyLines(true); format = format.withIgnoreSurroundingSpaces(true); format = format.withQuote('"'); format = format.withQuoteMode(QuoteMode.ALL);
private static CSVParser readCsvRecords(List<String> header, String content) { try { CSVFormat csvFormat = CSVFormat.RFC4180; if (header.isEmpty()) { csvFormat = csvFormat.withFirstRecordAsHeader(); } return csvFormat. withIgnoreSurroundingSpaces(). withIgnoreEmptyLines(). withTrim(). withDelimiter(','). parse(new StringReader(content)); } catch (IOException e) { throw new RuntimeException(e); } }
private static CSVParser readCsvRecords(List<String> header, String content) { try { CSVFormat csvFormat = CSVFormat.RFC4180; if (header.isEmpty()) { csvFormat = csvFormat.withFirstRecordAsHeader(); } return csvFormat. withIgnoreSurroundingSpaces(). withIgnoreEmptyLines(). withTrim(). withDelimiter(','). parse(new StringReader(content)); } catch (IOException e) { throw new RuntimeException(e); } }
/** * 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); }
private CSVParser getInternalParser(final Reader reader) { final CSVFormat csvFormat = CSVFormat.newFormat(columnSeparator).withQuote(quoteCharacter).withEscape(escapeCharacter) .withRecordSeparator(lineEnding).withIgnoreEmptyLines(true).withIgnoreSurroundingSpaces(true); try { return new CSVParser(reader, csvFormat); } catch (final IOException e) { throw new MetafactureException(e); } }