Univocity Parser - 如何在解析的 bean 中获取行号

问题描述

使用 univocity FixedWidthParser 时,如何将记录/行号添加为解析 bean 中的字段?

  1. 是否有一个选项可以将 bean 属性标记为记录编号,例如:
   // Bean representing a record
   public class MyRecord {
  
          @RecordNumber //like this,record or line number from the context is assigned as value
          long recordNumber; 
      
          @Parsed
          String name;
         
          //other parsed fields
          }
      }
  1. 我使用的是 InputValueSwitch。有没有办法在 rowProcessorSwitched()获取上下文,以便我可以尝试:
final BeanListProcessor<MyRecord> myProcessor = new BeanListProcessor<MyRecord>(
                    MyRecord.class);

public void rowProcessorSwitched(RowProcessor from,RowProcessor to) {
    ParsingContext context = ... //how to get this?
    long lineNumber = context.currentLine(); //if i can get this...
    if (from == myProcessor){
      (MyRecord)myProcessor.getBeans().get(0).setRecordNumber(lineNumber); //...then this should be possible
      //other switch logic
    }
} 

更新: 在 InputSwitch 中,我尝试实现 processstarted

   ParsingContext refToContext = null;
   
   public void processstarted(ParsingContext context) {
     refToContext = context;
     super.processstarted(context);
  } 

在 rowProcessorSwitched() 中使用它,

long lineNumber = refToContext.currentLine() - 1;

这是一种有效的方法吗?是否可靠 - 尤其是在解析过程中遇到错误

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)