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

The following examples show how to use org.apache.commons.csv.CSVFormat#parse() . 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: LicenseStoreData.java    From LicenseScout with Apache License 2.0 6 votes vote down vote up
/**
 * Reads license URL mappings from a CSV file.
 * 
 * @param inputStream an input stream to read the file contents from
 * @param log the logger
 * @throws IOException
 */
public void readUrlMappings(final InputStream inputStream, final ILSLog log) throws IOException {
    final CSVFormat csvFormat = CSVFormat.DEFAULT.withDelimiter(',').withCommentMarker('#');
    try (final BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"))) {
        final CSVParser csvParser = csvFormat.parse(br);
        for (final CSVRecord record : csvParser) {
            final String url = record.get(0).trim();
            final int numLicenseIdentifiers = record.size() - 1;
            final List<License> licenses = new ArrayList<>();
            for (int i = 0; i < numLicenseIdentifiers; i++) {
                final String licenseIdentifier = record.get(i + 1).trim();
                final License license = getLicenseBySpdxIdentifier(licenseIdentifier);
                if (license != null) {
                    licenses.add(license);
                } else {
                    log.info("readUrlMappings: license identifier not found: " + licenseIdentifier);
                }
            }
            urlMappings.put(url, licenses);
        }
    }
}
 
Example 2
Source File: LicenseStoreData.java    From LicenseScout with Apache License 2.0 6 votes vote down vote up
/**
 * Reads license name mappings from a CSV file.
 * 
 * @param inputStream an input stream to read the file contents from
 * @param log the logger
 * @throws IOException
 */
public void readNameMappings(final InputStream inputStream, final ILSLog log) throws IOException {
    final CSVFormat csvFormat = CSVFormat.DEFAULT.withDelimiter(',').withCommentMarker('#');
    try (final BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"))) {
        final CSVParser csvParser = csvFormat.parse(br);
        for (final CSVRecord record : csvParser) {
            final String mappedName = record.get(0).trim();
            final int numLicenseIdentifiers = record.size() - 1;
            final List<License> licenses = new ArrayList<>();
            for (int i = 0; i < numLicenseIdentifiers; i++) {
                final String licenseIdentifier = record.get(i + 1).trim();
                final License license = getLicenseBySpdxIdentifier(licenseIdentifier);
                if (license != null) {
                    licenses.add(license);
                } else {
                    log.info("readNameMappings: license identifier not found: " + licenseIdentifier);
                }
            }
            nameMappings.put(mappedName, licenses);
        }
    }
}
 
Example 3
Source File: UserCSVUploadPost.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 6 votes vote down vote up
protected void processCSVUpload(InputStream input, List<Map<QName,String>> users)
    throws IOException
{
    InputStreamReader reader = new InputStreamReader(input, Charset.forName("UTF-8"));
    CSVFormat format = CSVFormat.EXCEL;
    CSVParser csv = format.parse(reader);

    String[][] data = csv.getRecords().stream()
        .map(record -> {
            List<String> recordValues = new ArrayList<>();
            record.iterator().forEachRemaining(recordValues::add);
            return recordValues.toArray(String[]::new);
        }).toArray(String[][]::new);

    if (data.length > 0)
    {
        processSpreadsheetUpload(data, users);
    }
}
 
Example 4
Source File: Intersection.java    From core with GNU General Public License v3.0 6 votes vote down vote up
public static List<Intersection> readIntersections(String fileName) {
	List<Intersection> intersections = new ArrayList<Intersection>();
	
	try {
		Reader in = new BufferedReader(new InputStreamReader(
				new FileInputStream(fileName), "UTF-8"));
		CSVFormat formatter = CSVFormat.DEFAULT.withHeader().
				withCommentMarker('-');

		// Parse the file
		Iterable<CSVRecord> records = formatter.parse(in);
		Iterator<CSVRecord> iterator = records.iterator();
		while (iterator.hasNext()) {
			// Determine the record to process
			CSVRecord record = iterator.next();
			Intersection i = getIntersection(record);
			intersections.add(i);
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
	
	return intersections;
}
 
Example 5
Source File: Loc.java    From core with GNU General Public License v3.0 6 votes vote down vote up
public static List<Loc> readLocs(String fileName) {
	List<Loc> locs = new ArrayList<Loc>();
	
	try {
		Reader in = new BufferedReader(new InputStreamReader(
				new FileInputStream(fileName), "UTF-8"));
		CSVFormat formatter = 
				CSVFormat.DEFAULT.withHeader().withCommentMarker('-');
		
		// Parse the file
		Iterable<CSVRecord> records = formatter.parse(in);
		Iterator<CSVRecord> iterator = records.iterator();
		while (iterator.hasNext()) {
			// Determine the record to process
			CSVRecord record = iterator.next();
			Loc loc = getLoc(record);
			if (loc.accuracy < MAX_ALLOWED_ACCURACY)
			locs.add(loc);
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
	
	return locs;
}
 
Example 6
Source File: DataTable.java    From bookish with MIT License 5 votes vote down vote up
public void parseCSV(String csv) {
	try {
		Reader in = new StringReader(csv);
		CSVFormat format = CSVFormat.EXCEL.withHeader();
		CSVParser parser = format.parse(in);
		Set<String> colNames = parser.getHeaderMap().keySet();
		this.colNames.addAll(colNames);
		this.firstColIsIndex = true;
		for (CSVRecord record : parser) {
			List<String> row = new ArrayList<>();
			for (int i = 0; i<record.size(); i++) {
				String v = record.get(i);
				boolean isInt = false;
				try {
					Integer.parseInt(v);
					isInt = true;
				}
				catch (NumberFormatException nfe) {
					isInt = false;
				}
				if ( !isInt && !NumberUtils.isDigits(v) && NumberUtils.isCreatable(v) ) {
					v = String.format("%.4f",Precision.round(Double.valueOf(v), 4));
				}
				else {
					v = abbrevString(v, 25);
				}
				row.add(v);
			}
			rows.add(row);
		}
	}
	catch (Exception e) {
		throw new RuntimeException(e);
	}
}
 
Example 7
Source File: CSVUtil.java    From searoute with European Union Public License 1.2 5 votes vote down vote up
/**
 * @param filePath
 * @param cf
 * @return
 */
public static ArrayList<Map<String,String>> load(String filePath, CSVFormat cf) {
	ArrayList<Map<String,String>> data = new ArrayList<>();
	try {
		//parse file
		Reader in = new FileReader(filePath);
		Iterable<CSVRecord> raws = cf.parse(in);

		//read data
		for (CSVRecord raw : raws) data.add(raw.toMap());

		in.close();
	} catch (Exception e) { e.printStackTrace(); }
	return data;
}
 
Example 8
Source File: UserAgentFileParser.java    From browscap-java with MIT License 5 votes vote down vote up
private UserAgentParser parse(final Reader input) throws IOException, ParseException {

        final List<Rule> rules = new ArrayList<>();
        final CSVFormat format = CSVFormat.DEFAULT.withSkipHeaderRecord();
        try (CSVParser records = format.parse(input)) {
            for (final CSVRecord record : records) {
                final Rule rule = getRule(record);
                if (rule != null) {
                    rules.add(rule);
                }
            }
        }
        return new UserAgentParserImpl(rules.toArray(new Rule[0]), myDomain, getDefaultCapabilities());
    }
 
Example 9
Source File: WideEcrfFileReader.java    From hmftools with GNU General Public License v3.0 5 votes vote down vote up
@NotNull
private static List<CSVRecord> readCsvSkipHeader(@NotNull String pathToCsv, char delimiter) throws IOException {
    CSVFormat format = CSVFormat.DEFAULT.withDelimiter(delimiter);
    CSVParser parser = format.parse(new BufferedReader(new InputStreamReader(new FileInputStream(pathToCsv))));

    List<CSVRecord> records = parser.getRecords();
    return records.subList(1, records.size());
}
 
Example 10
Source File: CommonsCsvReader.java    From incubator-batchee with Apache License 2.0 5 votes vote down vote up
@Override
public void open(final Serializable checkpoint) throws Exception {
    final CSVFormat csvFormat = newFormat();
    parser = csvFormat.parse(newReader());
    iterator = new IteratorReader<CSVRecord>(parser.iterator());

    mapperInstance = mapper == null ?
        new BeanLocator.LocatorInstance<CsvReaderMapper>(
            mapping != null ? new DefaultMapper(Thread.currentThread().getContextClassLoader().loadClass(mapping)) : NOOP_MAPPER, null) :
        BeanLocator.Finder.get(locator).newInstance(CsvReaderMapper.class, mapper);


    super.open(checkpoint);
}
 
Example 11
Source File: DataTable.java    From bookish with MIT License 4 votes vote down vote up
public void parseCSV(String csv) {
	try {
		Reader in = new StringReader(csv);
		CSVFormat format = CSVFormat.EXCEL.withHeader();
		CSVParser parser = format.parse(in);
		this.firstColIsIndex = false;
		for (CSVRecord record : parser) {
			if ( !firstColIsIndex && Character.isAlphabetic(record.get(0).charAt(0)) ) {
				// latch if we see alpha not number
				firstColIsIndex = true;
			}
			List<String> row = new ArrayList<>();
			for (int i = 0; i<record.size(); i++) {
				String v = record.get(i);
				boolean isInt = false;
				try {
					Integer.parseInt(v);
					isInt = true;
				}
				catch (NumberFormatException nfe) {
					isInt = false;
				}
				if ( !isInt && !NumberUtils.isDigits(v) && NumberUtils.isCreatable(v) ) {
					v = String.format("%.4f",Precision.round(Double.valueOf(v), 4));
				}
				else {
					v = abbrevString(v, 25);
				}
				row.add(v);
			}
			rows.add(row);
		}
		Set<String> colNames = parser.getHeaderMap().keySet();
		if ( !firstColIsIndex ) {
			colNames.remove(""); // remove index column name
		}
		this.colNames.addAll(colNames);
	}
	catch (Exception e) {
		throw new RuntimeException(e);
	}
}
 
Example 12
Source File: CsvParser.java    From scava with Eclipse Public License 2.0 4 votes vote down vote up
public CsvParser(String path) throws Exception {
	Reader reader = new FileReader(path);
	CSVFormat format = CSVFormat.RFC4180.withFirstRecordAsHeader();
	parser = format.parse(reader);
}