liquibase.util.csv.opencsv.bean
Interface CsvToBeanFilter


public interface CsvToBeanFilter

Here's an example showing how to use CsvToBean with a column name mapping and line filtering. It assumes that there is a class named Feature defined with setters setName(String) and setState(String). The FEATURE_NAME and STATE columns in the CSV file will be used. Any additional columns will be ignored. The filter will eliminate any lines where the STATE value is "production".

 private class StateFilter implements CsvToBeanFilter {

 	private final MappingStrategy strategy;

 	public NonProductionFilter(MappingStrategy strategy) {
 		this.strategy = strategy;
 	}

 	public boolean allowLine(String[] line) {
 		int index = strategy.getColumnIndex("STATE");
 		String value = line[index];
 		boolean result = !"production".equals(value);
 		return result;
 	}

 }

 public List<Feature> parseCsv(InputStreamReader streamReader) {
 	HeaderColumnNameTranslateMappingStrategy<Feature> strategy = new HeaderColumnNameTranslateMappingStrategy();
 	Map<String, String> columnMap = new HashMap();
 	columnMap.put("FEATURE_NAME", "name");
 	columnMap.put("STATE", "state");
 	strategy.setColumnMapping(columnMap);
 	strategy.setType(Feature.class);
 	CSVReader reader = new CSVReader(streamReader);
 	CsvToBeanFilter filter = new StateFilter(strategy);
 	return new CsvToBean().parse(strategy, reader, filter);
 }
 
 


Method Summary
 boolean allowLine(String[] line)
          Determines if a line from the CSV file will be included in the output of CsvToBean.
 

Method Detail

allowLine

boolean allowLine(String[] line)
Determines if a line from the CSV file will be included in the output of CsvToBean. If the CSV file has a header row, it may be useful for implementations to call MappingStrategy.getColumnIndex(java.lang.String) to identify the correct column indexes to examine.

Parameters:
line - a line of data from the CSV file
Returns:
true if the line is to be included in the output. Otherwise, false.


Copyright © 2016 Liquibase.org. All rights reserved.