Java Code Examples for org.apache.commons.csv.CSVFormat#withIgnoreSurroundingSpaces()

The following examples show how to use org.apache.commons.csv.CSVFormat#withIgnoreSurroundingSpaces() . 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: CsvLoader.java    From timbuctoo with GNU General Public License v3.0 4 votes vote down vote up
@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 2
Source File: CSVFormatFactory.java    From incubator-batchee with Apache License 2.0 4 votes vote down vote up
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;
    }