我正在尝试为SuperCSV创建一个自定义单元处理器,它将处理文件中字段的列表。我已经改变了bean构造函数以适应列表,现在我只需要创建处理器,我想知道是否有人有这方面的经验。我已经阅读了文档......感谢任何帮助!
Bean构造函数:
public class CustomerBean {
private String name;
private List<String> manufacturer;
private String model;
private List<String> owner;
public CustomerBean() {
}
public CustomerBean(final String name, final List<String> manufacturer,
final String model, final List<String> owner,
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getManufacturer() {
return manufacturer;
}
public void setManufacturer(List<String> manufacturer) {
this.manufacturer = manufacturer;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public List<String> getOwner() {
return owner;
}
public void setOwner(List<String> owner) {
this.owner = owner;
}
public String toString() {
return String
.format("[Name=%s, Manufacturer=%s, Model=%s, Owner=%s]",
getName(), getManufacturer(), getModel(), getOwner());
}
处理器:
public class ParseHandler {
private static CellProcessor[] getProcessors() {
final CellProcessor[] processors = new CellProcessor[] {
new Optional(), //name
new Optional(), //manufacturer - needs to be processed as a list!
new Optional(), //model
new Optional(), //owner - needs to be processed as a list!
new Optional(), //integration team
new Optional(), //shipping
new Optional(), //hardware/software
new Optional(), //subsystem
new Optional(), //plane
new Optional(), //integration stand-alone
new Optional(), //integration interface
new Optional(), //function
new Optional(), //help links
new Optional(), //installation instructions
new Optional(), //test steps
new Optional(), //lead engineer
};
return processors;
}
public static CustomerBean readWithCsvBeanReader(Path path) throws IOException {
ICsvBeanReader beanReader = null;
CustomerBean customer = null;
System.out.println("Processing File: " + path);
try {
beanReader = new CsvBeanReader(new FileReader(path.toString()), CsvPreference.STANDARD_PREFERENCE);
//header elements are the same as the bean property names
final String[] header = {"name", "manufacturer", "model", "owner", "integrationTeam", "shipping", "hardwareSoftware", "subsystem", "plane", "integrationStandalone",
"integrationInterface", "function", "helpLinks", "installationInstructions", "testSteps", "leadEngineer"};
beanReader.getHeader(true);
final CellProcessor[] processors = getProcessors();
if ((customer = beanReader.read(CustomerBean.class, header, processors)) != null) {
System.out.println(String.format("%s", customer.toString()));
}
} finally {
if (beanReader != null) {
beanReader.close();
}
} return customer;
}
}
答案 0 :(得分:0)
正如creating a custom cell processor的文档所示,您只需要创建一个新的单元处理器,其execute()
方法将对象作为字符串列表返回。
public class ParseStringList extends CellProcessorAdaptor {
public ParseStringList() {
super();
}
public Object execute(Object value, CsvContext context) {
validateInputNotNull(value, context); // throws an Exception if the input is null
List<String> result = new ArrayList<String>();
result.add((String) object);
return result;
}
}
然后,您可以使用new Optional(new ParseStringList())
作为manufacturer
列的处理器。
或者(不需要自定义单元处理器),您可以使用CsvDozerBeanReader
和索引映射(例如manufacturer[0]
),如documentation所示。