use of org.marc4j.MarcError in project mod-source-record-manager by folio-org.
the class MarcRecordParser method parseRecord.
@Override
public ParsedResult parseRecord(String rawRecord) {
ParsedResult result = new ParsedResult();
try {
MarcReader reader = new MarcStreamReader(new ByteArrayInputStream(rawRecord.getBytes(DEFAULT_CHARSET)), DEFAULT_CHARSET.name());
if (reader.hasNext()) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
MarcJsonWriter writer = new MarcJsonWriter(os);
Record record = reader.next();
List<MarcError> errorList = record.getErrors();
if (errorList == null || errorList.isEmpty()) {
writer.write(record);
result.setParsedRecord(new JsonObject(new String(os.toByteArray())));
} else {
List<JsonObject> preparedErrors = new ArrayList<>();
errorList.forEach(e -> preparedErrors.add(buildErrorObject(e)));
prepareResultWithError(result, preparedErrors);
}
return result;
} else {
result.setParsedRecord(new JsonObject());
}
} catch (Exception e) {
LOGGER.error("Error during parse MARC record from raw record", e);
prepareResultWithError(result, Collections.singletonList(new JsonObject().put("name", e.getClass().getName()).put("message", e.getMessage())));
}
return result;
}
Aggregations