/** * Returns a new {@code CSVFormat} to trim leading and trailing blanks. * * @return A new CSVFormat that is equal to this but with the trim setting on. * @since 1.3 */ public CSVFormat withTrim() { return withTrim(true); }
format = format.withTrim(context.getProperty(TRIM_FIELDS).asBoolean());
@Test public void testTrimOnOneColumn() throws IOException { final StringWriter sw = new StringWriter(); try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withTrim())) { printer.print(" A "); assertEquals("A", sw.toString()); } }
@Test public void testTrimOffOneColumn() throws IOException { final StringWriter sw = new StringWriter(); try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withTrim(false))) { printer.print(" A "); assertEquals("\" A \"", sw.toString()); } }
@Test public void testTrimOnTwoColumns() throws IOException { final StringWriter sw = new StringWriter(); try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withTrim())) { printer.print(" A "); printer.print(" B "); assertEquals("A,B", sw.toString()); } }
/** * Returns a new {@code CSVFormat} to trim leading and trailing blanks. * * @return A new CSVFormat that is equal to this but with the trim setting on. * @since 1.3 */ public CSVFormat withTrim() { return withTrim(true); }
/** * Returns a new {@code CSVFormat} to trim leading and trailing blanks. * * @return A new CSVFormat that is equal to this but with the trim setting on. * @since 1.3 */ public CSVFormat withTrim() { return withTrim(true); }
.withIgnoreHeaderCase() .withIgnoreEmptyLines() .withTrim()); ) { retVal.setUrl(conceptMapUrl);
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); } }
@SuppressWarnings("deprecation") public MetadataFetcherCsv(DocIndexer docIndexer) { super(docIndexer); String metadataFileName = docIndexer.getParameter("metadataFile"); try ( Reader reader = new InputStreamReader(new BOMInputStream(new FileInputStream(metadataFileName)), "UTF-8"); CSVParser csvParser = new CSVParser(reader, CSVFormat.DEFAULT .withFirstRecordAsHeader() .withIgnoreHeaderCase() .withTrim()); ){ metadata = csvParser.getRecords(); reader.close(); } catch (FileNotFoundException ex) { System.err.println("Metadata file \""+metadataFileName+"\" not found. Not adding metadata."); } catch (IOException ex){ System.err.println("Error while reading \""+metadataFileName+"\". Is it a valid csv file?"); } }
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); } }
format = format.withTrim(config.get("trim").equals("true"));
.withDelimiter(getChar(separator)) .withQuote(getChar(quoteChar)) .withTrim()); records = csvParser.getRecords(); } catch (IOException e) {
private CSVParser createCSVParser(CSVParserSettings parserSettings) { CSVFormat csvFormat = CSVFormat.DEFAULT.withDelimiter(parserSettings.getDelimiter()) .withRecordSeparator(parserSettings.getRecordSeparator()) .withTrim(parserSettings.isTrim()) .withQuote(parserSettings.getQuote()) .withCommentMarker(parserSettings.getCommentMarker()) .withIgnoreEmptyLines(parserSettings.isIgnoreEmptyLines()) .withNullString(parserSettings.getNullString()); try { File file = new File(parserSettings.getPath()); return new CSVParser(new FileReader(file), csvFormat); } catch (Exception e) { throw e instanceof RuntimeException ? (RuntimeException) e : new RuntimeException(e); } } }
private CSVParser createCSVParser(CSVParserSettings parserSettings) { CSVFormat csvFormat = CSVFormat.DEFAULT.withDelimiter(parserSettings.getDelimiter()) .withRecordSeparator(parserSettings.getRecordSeparator()) .withTrim(parserSettings.isTrim()) .withQuote(parserSettings.getQuote()) .withCommentMarker(parserSettings.getCommentMarker()) .withIgnoreEmptyLines(parserSettings.isIgnoreEmptyLines()) .withNullString(parserSettings.getNullString()); try { File file = new File(parserSettings.getPath()); return new CSVParser(new FileReader(file), csvFormat); } catch (Exception e) { throw e instanceof RuntimeException ? (RuntimeException) e : new RuntimeException(e); } } }
public static CSVParser configureCSVParser(File file, Character fieldSeparator, Charset charset, boolean withFirstRecordAsHeader) throws IOException { if (fieldSeparator == null) { fieldSeparator = ','; } CSVFormat csvFormat = CSVFormat.DEFAULT .withDelimiter(fieldSeparator) .withIgnoreEmptyLines(true) .withNullString("") .withIgnoreSurroundingSpaces(true) .withTrim(true); if (withFirstRecordAsHeader) { csvFormat = csvFormat .withFirstRecordAsHeader() .withAllowMissingColumnNames(false) .withIgnoreHeaderCase(false); } else { csvFormat = csvFormat.withHeader((String[]) null).withSkipHeaderRecord(false); } FileInputStream fileInputStream = new FileInputStream(file); InputStreamReader is = new InputStreamReader(fileInputStream, charset); return new CSVParser(is, csvFormat); } }
answer = answer.withTrim(trim);
format = format.withTrim(context.getProperty(TRIM_FIELDS).asBoolean());