格式化 bean 生成器的 header 字段 - Java
Formatting header field for bean generator - Java
我编写了一个程序来解析 csv 文件并根据要放入数据库的数据创建一个 bean。一切正常,但是现在这将被移出测试环境,必须添加来自 csv 的真实 header 名称。这些 header 包含空格和 /。我正在寻找一种允许我的解析器读取这些 header 的方法。当我定义 header 名称时,我必须使用 camelCasing,而且我无法插入空格或其他字符。有没有办法改变这个?
这是我的构造函数(integrationTeam 需要是 Integration Team,softwareHardware 需要是 Hardware/Software -- 就像在 csv header 中一样)
public class BeanGen {
public BeanGen(
final String name,
final String manufacturer,
final String model,
final String owner,
final String integrationTeam,
final String shipping,
final String hardwareSoftware,
final String subsystem,
final String plane,
final String integrationStandalone,
final String integrationInterface,
final String function,
final String helpLinks,
final String installationInstructions,
final String testSteps,
final String leadEngineer)
{
this.name = name;
this.manufacturer = manufacturer;
this.model = model;
this.owner = owner;
this.integrationTeam = integrationTeam;
this.shipping = shipping;
this.hardwareSoftware = hardwareSoftware;
this.subsystem = subsystem;
this.plane = plane;
this.integrationStandalone = integrationStandalone;
this.integrationInterface = integrationInterface;
this.function = function;
this.helpLinks = helpLinks;
this.installationInstructions = installationInstructions;
this.testSteps = testSteps;
this.leadEngineer = leadEngineer;
}
这是处理构造函数的解析器
public class ParseHandler {
private static CellProcessor[] getProcessors() {
final CellProcessor[] processors = new CellProcessor[] {
new Optional(),
new Optional(),
new Optional(),
new Optional(),
new Optional(),
new Optional(),
new Optional(),
new Optional(),
new Optional(),
new Optional(),
new Optional(),
new Optional(),
new Optional(),
new Optional(),
new Optional(),
new Optional(),
};
return processors;
}
public static BeanGen readWithCsvBeanReader(Path path) throws IOException {
ICsvBeanReader beanReader = null;
BeanGen projectBean = null;
System.out.println("Processing File: " + path);
try {
beanReader = new CsvBeanReader(new FileReader(path.toString()), CsvPreference.STANDARD_PREFERENCE);
// the header elements are used to map the values to the bean (names
// must match)
final String[] header = beanReader.getHeader(true);
final CellProcessor[] processors = getProcessors();
if ((projectBean = beanReader.read(BeanGen.class, header, processors)) != null) {
System.out.println(String.format("%s", projectBean.toString()));
}
} finally {
if (beanReader != null) {
beanReader.close();
}
} return projectBean;
}
}
请参阅 Super CSV documentation 部分 使用 CsvBeanReader 读取 :
This relies on the fact that the column names in the header of the CSV file [...] match up exactly with the bean's field names, and the bean has the appropriate setters defined for each field.
If your header doesn't match (or there is no header), then you can simply define your own name mapping array.
你读取 header 并将其作为第二个参数传递给 beanReader.read()
。但是根据 API reference 第二个参数是一个包含 bean 属性 名称的字符串数组。所以你应该传递类似
new String[] { "name", "manufacturer", "model", "owner", "integrationTeam", ... }
作为第二个参数。所以第一个 CSV 列匹配 bean 字段 name
,第二个字段匹配 bean 字段 manufacturer
,等等
我编写了一个程序来解析 csv 文件并根据要放入数据库的数据创建一个 bean。一切正常,但是现在这将被移出测试环境,必须添加来自 csv 的真实 header 名称。这些 header 包含空格和 /。我正在寻找一种允许我的解析器读取这些 header 的方法。当我定义 header 名称时,我必须使用 camelCasing,而且我无法插入空格或其他字符。有没有办法改变这个?
这是我的构造函数(integrationTeam 需要是 Integration Team,softwareHardware 需要是 Hardware/Software -- 就像在 csv header 中一样)
public class BeanGen {
public BeanGen(
final String name,
final String manufacturer,
final String model,
final String owner,
final String integrationTeam,
final String shipping,
final String hardwareSoftware,
final String subsystem,
final String plane,
final String integrationStandalone,
final String integrationInterface,
final String function,
final String helpLinks,
final String installationInstructions,
final String testSteps,
final String leadEngineer)
{
this.name = name;
this.manufacturer = manufacturer;
this.model = model;
this.owner = owner;
this.integrationTeam = integrationTeam;
this.shipping = shipping;
this.hardwareSoftware = hardwareSoftware;
this.subsystem = subsystem;
this.plane = plane;
this.integrationStandalone = integrationStandalone;
this.integrationInterface = integrationInterface;
this.function = function;
this.helpLinks = helpLinks;
this.installationInstructions = installationInstructions;
this.testSteps = testSteps;
this.leadEngineer = leadEngineer;
}
这是处理构造函数的解析器
public class ParseHandler {
private static CellProcessor[] getProcessors() {
final CellProcessor[] processors = new CellProcessor[] {
new Optional(),
new Optional(),
new Optional(),
new Optional(),
new Optional(),
new Optional(),
new Optional(),
new Optional(),
new Optional(),
new Optional(),
new Optional(),
new Optional(),
new Optional(),
new Optional(),
new Optional(),
new Optional(),
};
return processors;
}
public static BeanGen readWithCsvBeanReader(Path path) throws IOException {
ICsvBeanReader beanReader = null;
BeanGen projectBean = null;
System.out.println("Processing File: " + path);
try {
beanReader = new CsvBeanReader(new FileReader(path.toString()), CsvPreference.STANDARD_PREFERENCE);
// the header elements are used to map the values to the bean (names
// must match)
final String[] header = beanReader.getHeader(true);
final CellProcessor[] processors = getProcessors();
if ((projectBean = beanReader.read(BeanGen.class, header, processors)) != null) {
System.out.println(String.format("%s", projectBean.toString()));
}
} finally {
if (beanReader != null) {
beanReader.close();
}
} return projectBean;
}
}
请参阅 Super CSV documentation 部分 使用 CsvBeanReader 读取 :
This relies on the fact that the column names in the header of the CSV file [...] match up exactly with the bean's field names, and the bean has the appropriate setters defined for each field. If your header doesn't match (or there is no header), then you can simply define your own name mapping array.
你读取 header 并将其作为第二个参数传递给 beanReader.read()
。但是根据 API reference 第二个参数是一个包含 bean 属性 名称的字符串数组。所以你应该传递类似
new String[] { "name", "manufacturer", "model", "owner", "integrationTeam", ... }
作为第二个参数。所以第一个 CSV 列匹配 bean 字段 name
,第二个字段匹配 bean 字段 manufacturer
,等等