我正在使用BeanIO解析固定宽度的文本文件。例如,进入我的项目的文件看起来像以下固定宽度的文本:
CD DummyValue3
EF DummyValue4 DummyValue5 DummyValue6
AB DummyValue1 DummyValue2
...
在我的映射文件中,我为每个recordID(即AB,CD,EF)声明了一个
record
<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
值? 最佳答案
我通过扩展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);
然后,这应该输出有关错误位置的更多信息。