List of usage examples for org.apache.commons.csv CSVFormat withQuote
public CSVFormat withQuote(final Character quoteChar)
From source file:org.structr.csv.GetCsvHeadersFunction.java
@Override public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) { try {//from w ww .ja v a 2 s.c o m assertArrayHasMinLengthAndMaxLengthAndAllElementsNotNull(sources, 1, 4); try { final String source = sources[0].toString(); String delimiter = ";"; String quoteChar = "\""; String recordSeparator = "\n"; switch (sources.length) { case 4: recordSeparator = (String) sources[3]; case 3: quoteChar = (String) sources[2]; case 2: delimiter = (String) sources[1]; break; } CSVFormat format = CSVFormat.newFormat(delimiter.charAt(0)).withHeader(); if (quoteChar.length() > 0) { format = format.withQuote(quoteChar.charAt(0)); } else { format = format.withQuote(null); } format = format.withRecordSeparator(recordSeparator); format = format.withIgnoreEmptyLines(true); format = format.withIgnoreSurroundingSpaces(true); format = format.withQuoteMode(QuoteMode.ALL); try (final CSVParser parser = new CSVParser(new StringReader(source), format)) { return parser.getHeaderMap().keySet(); } } catch (Throwable t) { logException(t, "{}: Exception for parameter: {}", new Object[] { getName(), getParametersAsString(sources) }); } return ""; } catch (IllegalArgumentException e) { logParameterError(caller, sources, e.getMessage(), ctx.isJavaScriptContext()); return usage(ctx.isJavaScriptContext()); } }
From source file:org.structr.function.FromCsvFunction.java
@Override public Object apply(ActionContext ctx, final GraphObject entity, final Object[] sources) { if (sources != null && sources.length > 0) { if (sources[0] != null) { try { final List<Map<String, String>> objects = new LinkedList<>(); final String source = sources[0].toString(); String delimiter = ";"; String quoteChar = "\""; String recordSeparator = "\n"; switch (sources.length) { case 4: recordSeparator = (String) sources[3]; case 3: quoteChar = (String) sources[2]; case 2: delimiter = (String) sources[1]; break; }// ww w .ja va 2s . c om CSVFormat format = CSVFormat.newFormat(delimiter.charAt(0)).withHeader(); format = format.withQuote(quoteChar.charAt(0)); format = format.withRecordSeparator(recordSeparator); format = format.withIgnoreEmptyLines(true); format = format.withIgnoreSurroundingSpaces(true); format = format.withSkipHeaderRecord(true); format = format.withQuoteMode(QuoteMode.ALL); CSVParser parser = new CSVParser(new StringReader(source), format); for (final CSVRecord record : parser.getRecords()) { objects.add(record.toMap()); } return objects; } catch (Throwable t) { t.printStackTrace(); } } return ""; } return usage(ctx.isJavaScriptContext()); }