/** * @param elements numbers to join by space to make one line of text * @return one line of text, formatted according to PMML quoting rules */ public static String joinPMMLDelimitedNumbers(Iterable<? extends Number> elements) { // bit of a workaround because NON_NUMERIC quote mode still quote "-1"! CSVFormat format = formatForDelimiter(' ').withQuoteMode(QuoteMode.NONE); // No quoting, no need to convert quoting return doJoinDelimited(elements, format); }
public static byte[] exportAsCSV(QueryResult result) { final ByteArrayOutputStream out = new ByteArrayOutputStream(); final CSVPrinter csvPrinter; try { final CSVFormat format = CSVFormat.DEFAULT.withQuoteMode(QuoteMode.NON_NUMERIC); csvPrinter = new CSVPrinter(new PrintWriter(out), format); csvPrinter.printRecord(result.getMetadata().stream().map(SchemaField::getName) .collect(Collectors.toList())); csvPrinter.printRecords(Iterables.transform(result.getResult(), input -> Iterables.transform(input, input1 -> { if (input1 instanceof List || input1 instanceof Map) { return JsonHelper.encode(input1); } if (input1 instanceof byte[]) { return DatatypeConverter.printBase64Binary((byte[]) input1); } return input1; }))); csvPrinter.flush(); } catch (IOException e) { throw Throwables.propagate(e); } return out.toByteArray(); }
.withRecordSeparator("\n") .withHeader(Header.class) .withQuoteMode(QuoteMode.ALL)); ) { for (ConceptMapGroupComponent group : theConceptMap.getGroup()) {
if (quoteValue != null) { final QuoteMode quoteMode = QuoteMode.valueOf(quoteValue.getValue()); format = format.withQuoteMode(quoteMode);
public CSVFormat getCSVFormat() { return CSVFormat.DEFAULT .withDelimiter(delimiter) .withRecordSeparator(recordSeparator) .withNullString(nullString) .withEscape(escapeCharacter) .withQuote(quoteCharacter) .withQuoteMode(quoteMode); }
format = format.withQuote('"').withQuoteMode(theQuoteMode);
@Test public void testPrintOnePositiveInteger() throws IOException { final StringWriter sw = new StringWriter(); try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuoteMode(QuoteMode.MINIMAL))) { printer.print(Integer.MAX_VALUE); assertEquals(String.valueOf(Integer.MAX_VALUE), sw.toString()); } }
@Test public void testQuoteNonNumeric() throws IOException { final StringWriter sw = new StringWriter(); try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuoteMode(QuoteMode.NON_NUMERIC))) { printer.printRecord("a", "b\nc", Integer.valueOf(1)); assertEquals("\"a\",\"b\nc\",1" + recordSeparator, sw.toString()); } }
@Test public void testQuoteAll() throws IOException { final StringWriter sw = new StringWriter(); try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuoteMode(QuoteMode.ALL))) { printer.printRecord("a", "b\nc", "d"); assertEquals("\"a\",\"b\nc\",\"d\"" + recordSeparator, sw.toString()); } }
@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 testDelimeterQuoteNONE() throws IOException { final StringWriter sw = new StringWriter(); final CSVFormat format = CSVFormat.DEFAULT.withEscape('!').withQuoteMode(QuoteMode.NONE); try (final CSVPrinter printer = new CSVPrinter(sw, format)) { printer.print("a,b,c"); printer.print("xyz"); assertEquals("a!,b!,c,xyz", sw.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()); } }
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)) {
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 = format.withIgnoreSurroundingSpaces(true); format = format.withQuote('"'); format = format.withQuoteMode(QuoteMode.ALL); format = format.withRecordSeparator('\n'); format = format.withSkipHeaderRecord(false);
@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)) {