Univocity 主细节关系 bean

Univocity master detail relationship beans

我有一个像这样的固定结构文件

5 条记录,GM、AB、TM、DM、IN

TM和DM如你所见是主从关系

而且 DM 和 IN 也是主从关系

GM01012020
ABXX-43432
TM        CODE1
DM   DESC1
INDESC1
INDESC2
INDESC3
DM   DESC2
INDESC1
INDESC2
INDESC3
TM        CODE2
DM   DESC3
INDESC1
INDESC2
DM   DESC4
INDESC1

我设法收集了 bean 中的所有行,但分开了..我需要 找到关联主记录的方法

    FixedWidthParserSettings settings;

    settings = new FixedWidthParserSettings();
    settings.setAutoConfigurationEnabled(true);
    settings.setHeaderExtractionEnabled(false);
    settings.setRecordEndsOnNewline(true);
    settings.trimValues(false);

    FixedWidthFields gm = FixedWidthFields.forParsing(Gm.class);
    FixedWidthFields ab = FixedWidthFields.forParsing(Ab.class);
    FixedWidthFields tm = FixedWidthFields.forParsing(Tm.class);
    FixedWidthFields dm = FixedWidthFields.forParsing(Dm.class);
    FixedWidthFields in = FixedWidthFields.forParsing(In.class);

    settings.addFormatForLookahead("GM", gm);
    settings.addFormatForLookahead("AB", ab);
    settings.addFormatForLookahead("TM", tm);
    settings.addFormatForLookahead("DM", dm);
    settings.addFormatForLookahead("IN", in);

    final BeanListProcessor<Gm> gmProcessor = new BeanListProcessor<Gm>(Gm.class);
    final BeanListProcessor<Ab> abprocessor = new BeanListProcessor<Ab>(Ab.class);
    final BeanListProcessor<Tm> tmProcessor = new BeanListProcessor<Tm>(Tm.class);
    final BeanListProcessor<Dm> dmProcessor = new BeanListProcessor<Dm>(Dm.class);
    final BeanListProcessor<In> inProcessor = new BeanListProcessor<In>(In.class);

    InputValueSwitch inputValueSwitch = new InputValueSwitch(0);

    inputValueSwitch.addSwitchForValue("GM", bgmProcessor);
    inputValueSwitch.addSwitchForValue("AB", abProcessor);
    inputValueSwitch.addSwitchForValue("TM", tmProcessor);
    inputValueSwitch.addSwitchForValue("DM", dmProcessor);
    inputValueSwitch.addSwitchForValue("IN", inProcessor);

    settings.setProcessor(inputValueSwitch);

    inputValueSwitch.setDefaultSwitch(bgmProcessor);
    FixedWidthParser parser = new FixedWidthParser(settings);

    parser.parse(f);

    //Gm bgmRecord = (Gm) gmProcessor.getBeans();
    List<Ab> abRecords = abProcessor.getBeans();
    List<Tm> tmRecords = tmProcessor.getBeans();
    List<Dm> dmRecords = dmProcessor.getBeans();
    List<In> inRecords = inProcessor.getBeans();

我可以设法提取所有记录,但仍然无法关联 MD 关系.. 我已经阅读了有关 univocity 文档的示例,但无法使其正常工作

Tm Bean中有一个Dm bean的列表,当然在Dm bean中有一个Bean[=12=的列表]

更新

我正在使用 MasterDetailListProcessor,但只能获得一级主细节

你应该这样实现rowProcessorSwitched

InputValueSwitch inputValueSwitch = new InputValueSwitch(0) {
    private Tm currentTmMaster;
    private Dm currentDmMaster;

    public void rowProcessorSwitched(RowProcessor from, RowProcessor to) {
        if (from == tmProcessor && to == dmProcessor) {
            // going From TM to DM, assign as currentTm the last element of list
            currentTmMaster = tmProcessor.getBeans().get(tmProcessor.getBeans().size() - 1);
        }

        if (from == dmProcessor && to == inProcessor) {
            // going from a new block of In, assign to the current Tm elem, all the dm(s) elements
            currentTmMaster.getDm().addAll(dmProcessor.getBeans());
            currentDmMaster = dmProcessor.getBeans().get(dmProcessor.getBeans().size() - 1);
            dmProcessor.getBeans().clear();
        }

        if (from == inProcessor) {
            // each block of In, assign it to the current DM
            currentDmMaster.getIn().addAll(inProcessor.getBeans());
            inProcessor.getBeans().clear();
        }
    }
};