BeanIO InvalidRecordGroupException, дающий неправильный номер строки
Я использую BeanIO для анализа текстового файла с фиксированной шириной. Например, файл, входящий в мой проект, выглядит как следующий текст с фиксированной шириной:
CD DummyValue3
EF DummyValue4 DummyValue5 DummyValue6
AB DummyValue1 DummyValue2
...
В моих файлах сопоставлений у меня есть record
объявляется для каждого идентификатора записи (например, AB, CD, EF)
<record name="dummyRecord" template="AB"
class="com.company.project.DummyRecordClass" minOccurs="0"
maxOccurs="1" />
Тогда у меня есть template
для каждой записи:
<template name="AB">
<field name="recordID" length="3" rid="true" literal="AB"
lenientPadding="true" minOccurs="0" />
<field name="value1" length="12" lenientPadding="true"
minOccurs="1" required="true"/>
<field name="value2" length="12" lenientPadding="true"
minOccurs="1" required="true"/>
</template>
Так как value1
а также value2
иметь minOccurs = 1
также как и required="true"
(Да, я знаю, что это избыточно, но это уже было в коде, и у нас есть тысячи таких полей), они должны существовать, если у меня есть сегмент AB.
Так что, если я должен был передать следующий файл в мою программу:
CD DummyValue3
EF DummyValue4 DummyValue5 DummyValue6
AB DummyValue1
Я получаю следующее InvalidRecordGroupException:
org.beanio.InvalidRecordGroupException: Invalid 'ediMessage' record group at line 1
Однако, поскольку пропущенное поле фактически находится в строке 3, отладка может оказаться сложной задачей, когда в приложение поступает 500-600 строк данных.
Есть ли способ заставить beanIO выводить правильный номер строки или даже template
а также field
значение, когда обязательное поле отсутствует?
1 ответ
Я использую пользовательскую реализацию org.beanio.BeanReaderErrorHandler
расширяя org.beanio.BeanReaderErrorHandlerSupport
создать LoggingErrorHandler
,
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();
}
}
Вы можете изменить createErrorMessage()
способ сохранить только ту информацию, которую вы ищете.
Конечно, вы можете сделать с информацией много других вещей, например записать ее в файл, отправить ее по электронной почте и т. Д. См. Раздел BeanReaderErrorHandler в документации для получения альтернатив.
Затем вы должны зарегистрировать обработчик ошибок в BeanReader
пример:
BeanReader beanReader = ....
beanReader.setErrorHandler(errorHandler);
Это должно затем вывести больше информации о том, где находятся ошибки.