Step2:メタ情報をExcelに移動する
Step2ではメタプログラミングの前準備を行います
まず①でExcel形式の設定ファイルを読み込みます。設定ファイルconfig.
このコードのポイントとなるのが②です。Step1では4つのデータ項目の数だけ処理を逐一書いていましたが、
さて、
このようにプログラムを書かなくても、
No | データ名称 | 長さ |
---|---|---|
1 | 送信日 | 8 |
2 | ユーザ名 | 10 |
3 | メールアドレス | 20 |
4 | ポイント | 5 |
public static void main(String[] args) throws Exception {
byte[] bytes = FileUtils.readFileToByteArray(
new File("data.txt"));
DataTable config = new XlsReader( ┓
new File("config.xls")).read().getTable(0); ┛①
MessageParser parser =
new MessageParser(bytes, config);
parser.parse();
}
private static class MessageParser {
...
private DataTable config;
public MessageParser(byte[] bytes, DataTable config) {
this.bytes = new ByteArray(bytes);
this.config = config;
}
public void parse() throws Exception {
while (index < bytes.getLength() - 1) {
Map<String, Object> record =
new HashMap<String, Object>();
for (int i = 0; i < config.getRowSize(); i++) { ┓
DataRow row = config.getRow(i); |
String name = ┓ |
(String) row.getValue("データ名称"); ┛③ |
int length = ┓ |
((BigDecimal) row.getValue("バイト数")) | |
.intValue(); ┛④ |
String value = getString(length); ―⑤ |
record.put(name, value); ―⑥ |
} ┛②
System.out.println(record);
}
}
...
}
考察:メタ情報とは何?
メタ情報はメタデータとも言われ、