Java Code Examples for org.apache.commons.csv.CSVFormat#withDelimiter()
The following examples show how to use
org.apache.commons.csv.CSVFormat#withDelimiter() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractCsvLayout.java From logging-log4j2 with Apache License 2.0 | 6 votes |
protected static CSVFormat createFormat(final String format, final Character delimiter, final Character escape, final Character quote, final QuoteMode quoteMode, final String nullString, final String recordSeparator) { CSVFormat csvFormat = CSVFormat.valueOf(format); if (isNotNul(delimiter)) { csvFormat = csvFormat.withDelimiter(delimiter); } if (isNotNul(escape)) { csvFormat = csvFormat.withEscape(escape); } if (isNotNul(quote)) { csvFormat = csvFormat.withQuote(quote); } if (quoteMode != null) { csvFormat = csvFormat.withQuoteMode(quoteMode); } if (nullString != null) { csvFormat = csvFormat.withNullString(nullString); } if (recordSeparator != null) { csvFormat = csvFormat.withRecordSeparator(recordSeparator); } return csvFormat; }
Example 2
Source File: ToCsvPragmaInstance.java From CQL with GNU Affero General Public License v3.0 | 5 votes |
public static CSVFormat getFormat(AqlOptions op) { String format0 = "Default"; CSVFormat format = CSVFormat.valueOf(format0); format = format.withDelimiter((Character) op.getOrDefault(AqlOption.csv_field_delim_char)); format = format.withQuote((Character) op.getOrDefault(AqlOption.csv_quote_char)); format = format.withEscape((Character) op.getOrDefault(AqlOption.csv_escape_char)); format = format.withQuoteMode(QuoteMode.ALL); format = format.withNullString(null); return format; }
Example 3
Source File: CsvConverters.java From DataflowTemplates with Apache License 2.0 | 5 votes |
/** * Gets Csv format accoring to <a * href="https://javadoc.io/doc/org.apache.commons/commons-csv">Apache Commons CSV</a>. If user * passed invalid format error is thrown. */ public static CSVFormat getCsvFormat(String formatString, @Nullable String delimiter) { CSVFormat format = CSVFormat.Predefined.valueOf(formatString).getFormat(); // If a delimiter has been passed set it here. if (delimiter != null) { return format.withDelimiter(delimiter.charAt(0)); } return format; }
Example 4
Source File: GanttCSVExport.java From ganttproject with GNU General Public License v3.0 | 5 votes |
private CSVFormat getCSVFormat() { CSVFormat format = CSVFormat.DEFAULT.withEscape('\\'); if (myCsvOptions.sSeparatedChar.length() == 1) { format = format.withDelimiter(myCsvOptions.sSeparatedChar.charAt(0)); } if (myCsvOptions.sSeparatedTextChar.length() == 1) { format = format.withQuote(myCsvOptions.sSeparatedTextChar.charAt(0)); } return format; }
Example 5
Source File: TextUtils.java From oryx with Apache License 2.0 | 5 votes |
private static CSVFormat formatForDelimiter(char delimiter) { CSVFormat format = CSV_FORMAT; if (delimiter != format.getDelimiter()) { format = format.withDelimiter(delimiter); } return format; }
Example 6
Source File: CsvExtractorFactory.java From link-move with Apache License 2.0 | 5 votes |
@Override public Extractor createExtractor(StreamConnector connector, ExtractorModel model) { try { String charsetName = model.getPropertyValue(CHARSET_PROPERTY); Charset charset = charsetName != null ? Charset.forName(charsetName) : defaultCharset; CSVFormat csvFormat = CSVFormat.RFC4180; String delimiter = model.getPropertyValue(DELIMITER_PROPERTY); if (delimiter != null) { if (delimiter.length() != 1) { throw new LmRuntimeException("Invalid delimiter (should be exactly one character): " + delimiter); } csvFormat = csvFormat.withDelimiter(delimiter.charAt(0)); } CsvExtractor extractor = new CsvExtractor(connector, model.getAttributes(), charset, csvFormat); String readFrom = model.getPropertyValue(READ_FROM_PROPERTY); if (readFrom != null) { Integer n = Integer.valueOf(readFrom); extractor.setReadFrom(n); } return extractor; } catch (Exception e) { throw new LmRuntimeException("Failed to create extractor", e); } }
Example 7
Source File: CSVRecordReader.java From incubator-pinot with Apache License 2.0 | 4 votes |
@Override public void init(File dataFile, Set<String> fieldsToRead, @Nullable RecordReaderConfig recordReaderConfig) throws IOException { _dataFile = dataFile; CSVRecordReaderConfig config = (CSVRecordReaderConfig) recordReaderConfig; char multiValueDelimiter; if (config == null) { _format = CSVFormat.DEFAULT.withDelimiter(CSVRecordReaderConfig.DEFAULT_DELIMITER).withHeader(); multiValueDelimiter = CSVRecordReaderConfig.DEFAULT_MULTI_VALUE_DELIMITER; } else { CSVFormat format; String formatString = config.getFileFormat(); if (formatString == null) { format = CSVFormat.DEFAULT; } else { switch (formatString.toUpperCase()) { case "EXCEL": format = CSVFormat.EXCEL; break; case "MYSQL": format = CSVFormat.MYSQL; break; case "RFC4180": format = CSVFormat.RFC4180; break; case "TDF": format = CSVFormat.TDF; break; default: format = CSVFormat.DEFAULT; break; } } char delimiter = config.getDelimiter(); format = format.withDelimiter(delimiter); String csvHeader = config.getHeader(); if (csvHeader == null) { format = format.withHeader(); } else { format = format.withHeader(StringUtils.split(csvHeader, delimiter)); } _format = format; multiValueDelimiter = config.getMultiValueDelimiter(); } _recordExtractor = new CSVRecordExtractor(); CSVRecordExtractorConfig recordExtractorConfig = new CSVRecordExtractorConfig(); recordExtractorConfig.setMultiValueDelimiter(multiValueDelimiter); _recordExtractor.init(fieldsToRead, recordExtractorConfig); init(); }
Example 8
Source File: CsvLoader.java From timbuctoo with GNU General Public License v3.0 | 4 votes |
@JsonCreator public CsvLoader(@JsonProperty("config") Map<String, String> config) { CSVFormat format = CSVFormat.EXCEL; this.config = config; if (config.containsKey("delimiter")) { format = format.withDelimiter(onlyChar(config, "delimiter")); } if (config.containsKey("quoteChar")) { format = format.withQuote(onlyChar(config, "quoteChar")); } if (config.containsKey("quoteMode")) { format = format.withQuoteMode(QuoteMode.valueOf(config.get("quoteMode"))); } if (config.containsKey("commentStart")) { format = format.withCommentMarker(onlyChar(config, "commentStart")); } if (config.containsKey("escape")) { format = format.withEscape(onlyChar(config, "escape")); } if (config.containsKey("ignoreSurroundingSpaces")) { format = format.withIgnoreSurroundingSpaces(config.get("ignoreSurroundingSpaces").equals("true")); } if (config.containsKey("ignoreEmptyLines")) { format = format.withIgnoreEmptyLines(config.get("ignoreEmptyLines").equals("true")); } if (config.containsKey("recordSeparator")) { format = format.withRecordSeparator(config.get("recordSeparator")); } if (config.containsKey("nullString")) { format = format.withNullString(config.get("nullString")); } if (config.containsKey("trim")) { format = format.withTrim(config.get("trim").equals("true")); } if (config.containsKey("trailingDelimiter")) { format = format.withTrailingDelimiter(config.get("trailingDelimiter").equals("true")); } this.format = format .withAllowMissingColumnNames() .withHeader(); }
Example 9
Source File: CSVFormatFactory.java From incubator-batchee with Apache License 2.0 | 4 votes |
static CSVFormat newFormat(final String format, final String delimiter, final String quoteCharacter, final String quoteMode, final String commentMarker, final String escapeCharacter, final String ignoreSurroundingSpaces, final String ignoreEmptyLines, final String recordSeparator, final String nullString, final String headerComments, final String header, final String skipHeaderRecord, final String allowMissingColumnNames, final String readHeaders) { //CHECKSTYLE:ON CSVFormat out = format == null ? CSVFormat.DEFAULT : CSVFormat.valueOf(format); if (delimiter != null) { out = out.withDelimiter(delimiter.charAt(0)); } if (quoteCharacter != null) { out = out.withQuote(quoteCharacter.charAt(0)); } if (quoteMode != null) { out = out.withQuoteMode(QuoteMode.valueOf(quoteMode)); } if (commentMarker != null) { out = out.withCommentMarker(commentMarker.charAt(0)); } if (escapeCharacter != null) { out = out.withEscape(escapeCharacter.charAt(0)); } if (ignoreSurroundingSpaces != null) { out = out.withIgnoreSurroundingSpaces(Boolean.parseBoolean(ignoreSurroundingSpaces)); } if (ignoreEmptyLines != null) { out = out.withIgnoreEmptyLines(Boolean.parseBoolean(ignoreEmptyLines)); } if (recordSeparator != null) { if ("\\n".equals(recordSeparator)) { out = out.withRecordSeparator('\n'); } else if ("\\r\\n".equals(recordSeparator)) { out = out.withRecordSeparator("\r\n"); } else { out = out.withRecordSeparator(recordSeparator); } } if (nullString != null) { out = out.withNullString(nullString); } if (headerComments != null && !headerComments.trim().isEmpty()) { out = out.withHeaderComments(headerComments.split(" *, *")); } if (Boolean.parseBoolean(readHeaders)) { out = out.withHeader(); } if (header != null && !header.trim().isEmpty()) { try { // headers can have CSV header names so parse it there final Iterator<CSVRecord> iterator = out.withHeader(new String[0]).parse(new StringReader(header + '\n' + header)).iterator(); final CSVRecord record = iterator.next(); final List<String> list = new ArrayList<String>(record.size()); for (final String h : record) { list.add(h); } out = out.withHeader(list.toArray(new String[record.size()])); } catch (final IOException e) { // can't occur actually out = out.withHeader(header.split(" *, *")); } } if (skipHeaderRecord != null) { out = out.withSkipHeaderRecord(Boolean.parseBoolean(skipHeaderRecord)); } if (allowMissingColumnNames != null) { out = out.withAllowMissingColumnNames(Boolean.parseBoolean(allowMissingColumnNames)); } return out; }