When using Univocity FixedWidthParser
, how can I add the record/line number as a field in the parsed bean?
- Is there an option where a bean property can be tagged as Record Number like:
// 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
}
}
- I am using a
InputValueSwitch
. Is there a way where the context can be got within therowProcessorSwitched()
so that I can try:
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
}
}
Update: Within the InputSwitch, I have tried to implement processStarted
ParsingContext refToContext = null;
public void processStarted(ParsingContext context) {
refToContext = context;
super.processStarted(context);
}
and within the rowProcessorSwitched() use it like,
long lineNumber = refToContext.currentLine() - 1;
Is this a valid approach? Is it dependable - esp, when errors are faced during parsing.