列表的自定义单元处理器

Custom Cell Processor for Lists

我正在尝试为 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;
}
}

creating a custom cell processor 的文档所示,您只需创建一个新的单元处理器,其 execute() 方法 returns 对象作为字符串列表。

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.

中所示