List of usage examples for org.apache.commons.csv CSVFormat newFormat
public static CSVFormat newFormat(final char delimiter)
From source file:org.asoem.greyfish.utils.space.cluster.DBSCANTest.java
@Before public void setUp() throws Exception { csvRecords = CSVParser.parse(Resources.getResource(DBSCANTest.class, "iris.csv"), Charset.defaultCharset(), CSVFormat.newFormat(',').withHeader().withQuote('"').withQuoteMode(QuoteMode.NON_NUMERIC)); }
From source file:org.asqatasun.referential.creator.CodeGeneratorMojo.java
/** * * @return/*from w w w.j av a 2s .c om*/ */ private Iterable<CSVRecord> getCsv() { // we parse the csv file to extract the first line and get the headers LineIterator lineIterator; try { lineIterator = FileUtils.lineIterator(dataFile, Charset.defaultCharset().name()); } catch (IOException ex) { Logger.getLogger(CodeGeneratorMojo.class.getName()).log(Level.SEVERE, null, ex); lineIterator = null; } String[] csvHeaders = lineIterator != null ? lineIterator.next().split(String.valueOf(delimiter)) : new String[0]; isCriterionPresent = extractCriterionFromCsvHeader(csvHeaders); try { extractAvailableLangsFromCsvHeader(csvHeaders); } catch (I18NLanguageNotFoundException ex) { Logger.getLogger(CodeGeneratorMojo.class.getName()).log(Level.SEVERE, null, ex); return null; } // from here we just add each line to a build to re-create the csv content // without the first line. StringBuilder strb = new StringBuilder(); while (lineIterator.hasNext()) { strb.append(lineIterator.next()); strb.append("\n"); } Reader in; try { in = new StringReader(strb.toString()); CSVFormat csvf = CSVFormat.newFormat(delimiter).withHeader(csvHeaders); return csvf.parse(in); } catch (FileNotFoundException ex) { Logger.getLogger(CodeGeneratorMojo.class.getName()).log(Level.SEVERE, null, ex); return null; } catch (IOException ex) { Logger.getLogger(CodeGeneratorMojo.class.getName()).log(Level.SEVERE, null, ex); return null; } }
From source file:org.asqatasun.rules.doc.utils.exportaw22torgaa3ruledesign.ExtractCsvAndCopy.java
public Iterable<CSVRecord> getCsv() throws IOException { // we parse the csv file to extract the first line and get the headers LineIterator lineIterator;//from w w w .j a va2 s . com lineIterator = FileUtils.lineIterator(DATA_FILE); csvHeaders = lineIterator.next().split(String.valueOf(DELIMITER)); StringBuilder strb = new StringBuilder(); while (lineIterator.hasNext()) { strb.append(lineIterator.next()); strb.append("\n"); } Reader in; try { in = new StringReader(strb.toString()); CSVFormat csvf = CSVFormat.newFormat(DELIMITER).withHeader(csvHeaders); return csvf.parse(in); } catch (FileNotFoundException ex) { return null; } catch (IOException ex) { return null; } }
From source file:org.easybatch.extensions.apache.common.csv.ApacheCommonCsvRecordMarshaller.java
/** * Create a record marshaller./* www .j a v a2s . com*/ * * @param fieldExtractor the field extractor * @param delimiter the field delimiter * @param qualifier the field qualifier * @throws IntrospectionException If the object to marshal cannot be introspected */ public ApacheCommonCsvRecordMarshaller(RecordFieldExtractor fieldExtractor, final char delimiter, final char qualifier) throws IntrospectionException { this.fieldExtractor = fieldExtractor; this.csvFormat = CSVFormat.newFormat(delimiter).withQuote(qualifier).withQuoteMode(QuoteMode.ALL) .withRecordSeparator(null);// recordSeparator is forced to null to avoid CSVPrinter to print new lines. New lines are written later by EasyBatch RecordWriter }
From source file:org.failearly.dataz.template.generator.csv.PredefinedCsvFormat.java
PredefinedCsvFormat(char delimiter) { this.format = CSVFormat.newFormat(delimiter); }
From source file:org.openestate.io.idx.IdxFormat.java
/** * Create IDX format. */ public IdxFormat() { super(CSVFormat.newFormat('#').withRecordSeparator(RECORD_SEPARATOR).withNullString(StringUtils.EMPTY)); }
From source file:org.openestate.io.is24_csv.Is24CsvFormat.java
/** * Create IS24-CSV format.// w w w.j a va 2 s.co m */ public Is24CsvFormat() { super(CSVFormat.newFormat('|').withRecordSeparator(RECORD_SEPARATOR).withNullString(StringUtils.EMPTY)); }
From source file:org.opens.referentiel.creator.CodeGeneratorMojo.java
/** * * @return//w w w. j a v a 2s. c o m */ private Iterable<CSVRecord> getCsv() { // we parse the csv file to extract the first line and get the headers LineIterator lineIterator; try { lineIterator = FileUtils.lineIterator(dataFile); } catch (IOException ex) { Logger.getLogger(CodeGeneratorMojo.class.getName()).log(Level.SEVERE, null, ex); lineIterator = null; } String[] csvHeaders = lineIterator.next().split(String.valueOf(delimiter)); isCriterionPresent = extractCriterionFromCsvHeader(csvHeaders); try { extractAvailableLangsFromCsvHeader(csvHeaders); } catch (I18NLanguageNotFoundException ex) { Logger.getLogger(CodeGeneratorMojo.class.getName()).log(Level.SEVERE, null, ex); return null; } // from here we just add each line to a build to re-create the csv content // without the first line. StringBuilder strb = new StringBuilder(); while (lineIterator.hasNext()) { strb.append(lineIterator.next()); strb.append("\n"); } Reader in; try { in = new StringReader(strb.toString()); CSVFormat csvf = CSVFormat.newFormat(delimiter).withHeader(csvHeaders); return csvf.parse(in); } catch (FileNotFoundException ex) { Logger.getLogger(CodeGeneratorMojo.class.getName()).log(Level.SEVERE, null, ex); return null; } catch (IOException ex) { Logger.getLogger(CodeGeneratorMojo.class.getName()).log(Level.SEVERE, null, ex); return null; } }
From source file:org.structr.csv.FromCsvFunction.java
@Override public Object apply(ActionContext ctx, final GraphObject entity, final Object[] sources) { if (arrayHasMinLengthAndMaxLengthAndAllElementsNotNull(sources, 1, 4)) { try {/* w w w .j a va2 s. c om*/ 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; } 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) { logException(t, "{0}: Exception for parameter: {1}", new Object[] { getName(), getParametersAsString(sources) }); } return ""; } else { logParameterError(entity, sources, ctx.isJavaScriptContext()); } return usage(ctx.isJavaScriptContext()); }
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 .j a 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()); } }