Java Code Examples for org.apache.commons.csv.CSVFormat#RFC4180

The following examples show how to use org.apache.commons.csv.CSVFormat#RFC4180 . 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: CsvUtils.java    From webtau with Apache License 2.0 8 votes vote down vote up
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);
    }
}
 
Example 2
Source File: ResultFormatterCsv.java    From FROST-Server with GNU Lesser General Public License v3.0 8 votes vote down vote up
@Override
public String format(ResourcePath path, Query query, Object result, boolean useAbsoluteNavigationLinks) {
    EntityType type = path.getMainElementType();
    CsvElementSet elementSet = new CsvElementSet("");
    elementSet.initFrom(type, query);

    StringWriter writer = new StringWriter();
    try (CSVPrinter printer = new CSVPrinter(writer, CSVFormat.RFC4180)) {
        CsvRowCollector rowCollector = new CsvRowCollector(printer);
        elementSet.writeHeader(rowCollector);
        rowCollector.flush();
        elementSet.writeData(rowCollector, result);
    } catch (IOException ex) {
        LOGGER.error("Failed to generate CSV String.", ex);
    }
    return writer.toString();
}
 
Example 3
Source File: InformationExtraction2Postgres.java    From newsleak with GNU Affero General Public License v3.0 8 votes vote down vote up
private void mappingIdsInMetadata(String mappedMetadataFile) throws Exception {
	// read mappings file
	FileInputStream fis = new FileInputStream(this.dataDirectory + File.separator + this.metadataFile + ".id-map");
	ObjectInputStream ois = new ObjectInputStream(fis);
	HashMap<Integer, ArrayList<Integer>> documentIdMapping = (HashMap<Integer, ArrayList<Integer>>) ois
			.readObject();
	ois.close();

	// open metadata file, replace ids, write to temporary metadata file
	BufferedWriter writer = new BufferedWriter(new FileWriter(mappedMetadataFile));
	CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.RFC4180);

	BufferedReader reader = new BufferedReader(new FileReader(this.dataDirectory + File.separator + this.metadataFile));
	Iterable<CSVRecord> records = CSVFormat.RFC4180.parse(reader);
	for (CSVRecord record : records) {
		Integer tmpDocId = Integer.parseInt(record.get(0));
		if (documentIdMapping.containsKey(tmpDocId)) { 
			ArrayList<Integer> mappedIds = documentIdMapping.get(tmpDocId);
			int nParts = mappedIds.size();
			int partCounter = 0;
			for (Integer newsleakDocId : mappedIds) {
				String key = StringUtils.capitalize(record.get(1));
				String value = record.get(2);
				if (nParts > 1 && key.equals("Subject")) {
					partCounter++;
					value += " (" + partCounter + "/" + nParts + ")";
				}
				ArrayList<String> meta = new ArrayList<String>();
				meta.add(newsleakDocId.toString());
				meta.add(key);
				meta.add(value);
				meta.add(record.get(3));
				csvPrinter.printRecord(meta);
			}
		}
	}
	csvPrinter.close();
	reader.close();
}
 
Example 4
Source File: TestForm.java    From zstack with Apache License 2.0 8 votes vote down vote up
private String createCsvContent() {
    String[][] origin = {
            {"aBoolean", "anInt", "aFloat", "aDouble", "aLong", "test"},
            {"true", "2.3", "3.3", "4.3", "5.3"},
            {"True","","","","","a,b,c"},
    };

    try {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        OutputStreamWriter out =new OutputStreamWriter(os, Charset.forName("UTF-8"));
        CSVPrinter printer = new CSVPrinter(out, CSVFormat.RFC4180);
        printer.printRecords(origin);
        out.close();

        return Base64.getEncoder().encodeToString(os.toByteArray());
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}
 
Example 5
Source File: CSVUtils.java    From nifi with Apache License 2.0 6 votes vote down vote up
public static CSVFormat createCSVFormat(final PropertyContext context, final Map<String, String> variables) {
    final String formatName = context.getProperty(CSV_FORMAT).getValue();
    if (formatName.equalsIgnoreCase(CUSTOM.getValue())) {
        return buildCustomFormat(context, variables);
    }
    if (formatName.equalsIgnoreCase(RFC_4180.getValue())) {
        return CSVFormat.RFC4180;
    } else if (formatName.equalsIgnoreCase(EXCEL.getValue())) {
        return CSVFormat.EXCEL;
    } else if (formatName.equalsIgnoreCase(TDF.getValue())) {
        return CSVFormat.TDF;
    } else if (formatName.equalsIgnoreCase(MYSQL.getValue())) {
        return CSVFormat.MYSQL;
    } else if (formatName.equalsIgnoreCase(INFORMIX_UNLOAD.getValue())) {
        return CSVFormat.INFORMIX_UNLOAD;
    } else if (formatName.equalsIgnoreCase(INFORMIX_UNLOAD_CSV.getValue())) {
        return CSVFormat.INFORMIX_UNLOAD_CSV;
    } else {
        return CSVFormat.DEFAULT;
    }
}
 
Example 6
Source File: MetadataResource.java    From newsleak with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Append a list of metadata entries for one document to the temporary metadata
 * file.
 *
 * @param metadata
 *            the metadata
 */
public synchronized void appendMetadata(List<List<String>> metadata) {
	try {
		BufferedWriter writer = new BufferedWriter(new FileWriter(metadataFile, true));
		CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.RFC4180);
		csvPrinter.printRecords(metadata);
		csvPrinter.close();
	} catch (IOException e) {
		e.printStackTrace();
	}
}
 
Example 7
Source File: AgreementUtils.java    From webanno with Apache License 2.0 5 votes vote down vote up
public static InputStream generateCsvReport(CodingAgreementResult aResult)
    throws IOException
{
    ByteArrayOutputStream buf = new ByteArrayOutputStream();
    try (CSVPrinter printer = new CSVPrinter(new OutputStreamWriter(buf, "UTF-8"),
            CSVFormat.RFC4180)) {
        toCSV(printer, aResult);
    }

    return new ByteArrayInputStream(buf.toByteArray());
}
 
Example 8
Source File: ETLSummaryGenerator.java    From WhiteRabbit with Apache License 2.0 5 votes vote down vote up
static void writeCsv(String filename, List<Row> rows) {
    if (!filename.toLowerCase().endsWith(".csv")) {
        filename = filename + ".csv";
    }

    // TODO: try with resources
    WriteCSVFileWithHeader out = new WriteCSVFileWithHeader(filename, CSVFormat.RFC4180);
    for (Row row : rows) {
        out.write(row);
    }
    out.close();
}
 
Example 9
Source File: CsvExtractorFactory.java    From link-move with Apache License 2.0 5 votes vote down vote up
@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 10
Source File: ReadCsv.java    From opentest with MIT License 4 votes vote down vote up
/**
 * Returns a CSVFormat object given the CSV format as a string.
 *
 * @param format
 * @return
 */
private CSVFormat getCsvFormat(String format) {
    CSVFormat csvFormat = null;

    switch (format.trim().toLowerCase()) {
        case "default":
            csvFormat = CSVFormat.DEFAULT;
            break;
        case "excel":
            csvFormat = CSVFormat.EXCEL;
            break;
        case "informixunload":
        case "informix-unload":
        case "informix_unload":
            csvFormat = CSVFormat.INFORMIX_UNLOAD;
            break;
        case "informixunloadcsv":
        case "informix-unload-csv":
        case "informix_unload_csv":
            csvFormat = CSVFormat.INFORMIX_UNLOAD_CSV;
            break;
        case "mysql":
            csvFormat = CSVFormat.MYSQL;
            break;
        case "postgres":
        case "postgresql-csv":
        case "postgresql_csv":
            csvFormat = CSVFormat.POSTGRESQL_CSV;
            break;
        case "postgresql-text":
        case "postgresql_text":
            csvFormat = CSVFormat.POSTGRESQL_TEXT;
            break;
        case "rfc4180":
            csvFormat = CSVFormat.RFC4180;
        case "tdf":
            csvFormat = CSVFormat.TDF;
        default:
            throw new RuntimeException(String.format("CSV format \"%s\" is not among the supported formats"));
    }

    return csvFormat;
}
 
Example 11
Source File: CSVUtils.java    From govpay with GNU General Public License v3.0 4 votes vote down vote up
public static CSVUtils getInstance(CSVFormat csvFormat) {
	if(csvFormat != null)
		return new CSVUtils(csvFormat);
	else
		return new CSVUtils(CSVFormat.RFC4180);
}
 
Example 12
Source File: CSVWriter.java    From LiquidDonkey with MIT License 4 votes vote down vote up
public static CSVWriter create() {
    return new CSVWriter(CSVFormat.RFC4180);
}
 
Example 13
Source File: WriteCSVFileWithHeader.java    From WhiteRabbit with Apache License 2.0 4 votes vote down vote up
public WriteCSVFileWithHeader(String fileName) {
	this(fileName, CSVFormat.RFC4180);
}
 
Example 14
Source File: CSVRecordReader.java    From incubator-pinot with Apache License 2.0 4 votes vote down vote up
@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();
}