Search in sources :

Example 1 with MarcError

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;
}
Also used : ArrayList(java.util.ArrayList) JsonObject(io.vertx.core.json.JsonObject) MarcStreamReader(org.marc4j.MarcStreamReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) MarcReader(org.marc4j.MarcReader) Record(org.marc4j.marc.Record) MarcError(org.marc4j.MarcError) MarcJsonWriter(org.marc4j.MarcJsonWriter)

Aggregations

JsonObject (io.vertx.core.json.JsonObject)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ArrayList (java.util.ArrayList)1 MarcError (org.marc4j.MarcError)1 MarcJsonWriter (org.marc4j.MarcJsonWriter)1 MarcReader (org.marc4j.MarcReader)1 MarcStreamReader (org.marc4j.MarcStreamReader)1 Record (org.marc4j.marc.Record)1