Java BeanIO InvalidRecordGroupException提供错误的行号
我正在使用BeanIO解析一个固定宽度的文本文件。例如,进入我的项目的文件看起来像以下固定宽度的文本:Java BeanIO InvalidRecordGroupException提供错误的行号,java,parsing,bean-io,Java,Parsing,Bean Io,我正在使用BeanIO解析一个固定宽度的文本文件。例如,进入我的项目的文件看起来像以下固定宽度的文本: CD DummyValue3 EF DummyValue4 DummyValue5 DummyValue6 AB DummyValue1 DummyValue2 ... 在映射文件中,我为每个recordID(即AB、CD、EF)声明了一条记录 因为value1和value2有一个minOccurs=1以及一个required=“true”(是的,我知道这是多余的,但它已经存在于代码
CD DummyValue3
EF DummyValue4 DummyValue5 DummyValue6
AB DummyValue1 DummyValue2
...
在映射文件中,我为每个recordID(即AB、CD、EF)声明了一条记录
因为value1
和value2
有一个minOccurs=1
以及一个required=“true”
(是的,我知道这是多余的,但它已经存在于代码中,我们有数千个字段),如果我有AB段,它们必须存在
因此,如果我要将以下文件传递到我的程序中:
CD DummyValue3
EF DummyValue4 DummyValue5 DummyValue6
AB DummyValue1
我收到以下InvalidRecordGroupException:
org.beanio.InvalidRecordGroupException:第1行的“ediMessage”记录组无效
但是,由于缺少的字段实际上位于第3行,因此当有500-600行数据进入应用程序时,调试可能会非常困难
有没有办法让beanIO输出正确的行号,或者在缺少必填字段时,甚至是模板
和字段
值?我通过扩展org.beanio.BeanReaderErrorHandler
来创建LoggingErrorHandler
来使用org.beanio.BeanReaderErrorHandler
的自定义实现
import org.beanio.BeanReaderErrorHandlerSupport;
import org.beanio.BeanReaderException;
import org.beanio.InvalidRecordException;
import org.beanio.MalformedRecordException;
import org.beanio.RecordContext;
import org.beanio.UnexpectedRecordException;
import org.beanio.UnidentifiedRecordException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* A BeanIO {@code BeanReaderErrorHandler} that logs the error messages.
*
* @author Nico Schlebusch
*/
public class LoggingBeanReaderErrorHandler extends BeanReaderErrorHandlerSupport {
private static final Logger LOGGER = LoggerFactory.getLogger(LoggingBeanReaderErrorHandler.class);
/**
* Creates a new instance of {@code LoggingBeanReaderErrorHandler}.
*/
public LoggingBeanReaderErrorHandler() {
}
/**
* {@inheritDoc}
*
* @see org.beanio.BeanReaderErrorHandlerSupport#invalidRecord(org.beanio.InvalidRecordException)
*/
@Override
public void invalidRecord(final InvalidRecordException ex) throws BeanReaderException {
LOGGER.error("{}", createErrorMessage(ex));
}
/**
* {@inheritDoc}
*
* @see org.beanio.BeanReaderErrorHandlerSupport#unexpectedRecord(org.beanio.UnexpectedRecordException)
*/
@Override
public void unexpectedRecord(final UnexpectedRecordException ex) throws BeanReaderException {
LOGGER.error("{}", createErrorMessage(ex));
}
/**
* {@inheritDoc}
*
* @see org.beanio.BeanReaderErrorHandlerSupport#unidentifiedRecord(org.beanio.UnidentifiedRecordException)
*/
@Override
public void unidentifiedRecord(final UnidentifiedRecordException ex) throws BeanReaderException {
LOGGER.error("{}", createErrorMessage(ex));
}
/**
* {@inheritDoc}
*
* @see org.beanio.BeanReaderErrorHandlerSupport#malformedRecord(org.beanio.MalformedRecordException)
*/
@Override
public void malformedRecord(final MalformedRecordException ex) throws BeanReaderException {
LOGGER.error("{}", createErrorMessage(ex));
}
/**
* {@inheritDoc}
*
* @see org.beanio.BeanReaderErrorHandlerSupport#fatalError(org.beanio.BeanReaderException)
*/
@Override
public void fatalError(final BeanReaderException ex) throws BeanReaderException {
LOGGER.error("{}", createErrorMessage(ex));
}
/**
* Creates an error message using the exception to get the RecordContext from which a meaningful error
* message can be constructed.
*
* @param ex the exception containing the error information.
*
* @return a string describing the error(s).
*/
protected String createErrorMessage(final BeanReaderException ex) {
final String message = ex.getMessage();
final StringBuilder errorMessage = new StringBuilder(message.length() * 5);
// if a bean object is mapped to a record group,
// the exception may contain more than one record
for (int i = 0, j = ex.getRecordCount(); i < j; i++) {
final RecordContext recordContext = ex.getRecordContext(i);
final String recordName = recordContext.getRecordName();
final String text = recordContext.getRecordText();
errorMessage.append(String.format("%s: %s%n", message, text));
if (recordContext.hasRecordErrors()) {
for (final String error : recordContext.getRecordErrors()) {
errorMessage.append(String.format("Record '%s' - %s%n", recordName, error));
}
}
if (recordContext.hasFieldErrors()) {
for (final String field : recordContext.getFieldErrors().keySet()) {
for (final String error : recordContext.getFieldErrors(field)) {
errorMessage.append(String.format("Field '%s' - %s%n", field, error));
}
}
}
}
return errorMessage.toString();
}
}
这将输出有关错误位置的更多信息
CD DummyValue3
EF DummyValue4 DummyValue5 DummyValue6
AB DummyValue1
import org.beanio.BeanReaderErrorHandlerSupport;
import org.beanio.BeanReaderException;
import org.beanio.InvalidRecordException;
import org.beanio.MalformedRecordException;
import org.beanio.RecordContext;
import org.beanio.UnexpectedRecordException;
import org.beanio.UnidentifiedRecordException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* A BeanIO {@code BeanReaderErrorHandler} that logs the error messages.
*
* @author Nico Schlebusch
*/
public class LoggingBeanReaderErrorHandler extends BeanReaderErrorHandlerSupport {
private static final Logger LOGGER = LoggerFactory.getLogger(LoggingBeanReaderErrorHandler.class);
/**
* Creates a new instance of {@code LoggingBeanReaderErrorHandler}.
*/
public LoggingBeanReaderErrorHandler() {
}
/**
* {@inheritDoc}
*
* @see org.beanio.BeanReaderErrorHandlerSupport#invalidRecord(org.beanio.InvalidRecordException)
*/
@Override
public void invalidRecord(final InvalidRecordException ex) throws BeanReaderException {
LOGGER.error("{}", createErrorMessage(ex));
}
/**
* {@inheritDoc}
*
* @see org.beanio.BeanReaderErrorHandlerSupport#unexpectedRecord(org.beanio.UnexpectedRecordException)
*/
@Override
public void unexpectedRecord(final UnexpectedRecordException ex) throws BeanReaderException {
LOGGER.error("{}", createErrorMessage(ex));
}
/**
* {@inheritDoc}
*
* @see org.beanio.BeanReaderErrorHandlerSupport#unidentifiedRecord(org.beanio.UnidentifiedRecordException)
*/
@Override
public void unidentifiedRecord(final UnidentifiedRecordException ex) throws BeanReaderException {
LOGGER.error("{}", createErrorMessage(ex));
}
/**
* {@inheritDoc}
*
* @see org.beanio.BeanReaderErrorHandlerSupport#malformedRecord(org.beanio.MalformedRecordException)
*/
@Override
public void malformedRecord(final MalformedRecordException ex) throws BeanReaderException {
LOGGER.error("{}", createErrorMessage(ex));
}
/**
* {@inheritDoc}
*
* @see org.beanio.BeanReaderErrorHandlerSupport#fatalError(org.beanio.BeanReaderException)
*/
@Override
public void fatalError(final BeanReaderException ex) throws BeanReaderException {
LOGGER.error("{}", createErrorMessage(ex));
}
/**
* Creates an error message using the exception to get the RecordContext from which a meaningful error
* message can be constructed.
*
* @param ex the exception containing the error information.
*
* @return a string describing the error(s).
*/
protected String createErrorMessage(final BeanReaderException ex) {
final String message = ex.getMessage();
final StringBuilder errorMessage = new StringBuilder(message.length() * 5);
// if a bean object is mapped to a record group,
// the exception may contain more than one record
for (int i = 0, j = ex.getRecordCount(); i < j; i++) {
final RecordContext recordContext = ex.getRecordContext(i);
final String recordName = recordContext.getRecordName();
final String text = recordContext.getRecordText();
errorMessage.append(String.format("%s: %s%n", message, text));
if (recordContext.hasRecordErrors()) {
for (final String error : recordContext.getRecordErrors()) {
errorMessage.append(String.format("Record '%s' - %s%n", recordName, error));
}
}
if (recordContext.hasFieldErrors()) {
for (final String field : recordContext.getFieldErrors().keySet()) {
for (final String error : recordContext.getFieldErrors(field)) {
errorMessage.append(String.format("Field '%s' - %s%n", field, error));
}
}
}
}
return errorMessage.toString();
}
}
BeanReader beanReader = ....
beanReader.setErrorHandler(errorHandler);