Search in sources :

Example 6 with MarcReader

use of org.marc4j.MarcReader in project z3950-server by mol-pl.

the class iso2709 method toString.

public String toString() {
    StringWriter sw = new StringWriter();
    try {
        MarcReader reader = new MarcReader();
        TaggedWriter handler = new TaggedWriter(sw);
        MarcSource source = new MarcSource(reader, new ByteArrayInputStream(this.source_record));
        MarcResult result = new MarcResult();
        result.setHandler(handler);
        Converter converter = new Converter();
        converter.convert(source, result);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return sw.toString();
}
Also used : StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) MarcReader(org.marc4j.MarcReader) MarcReaderException(org.marc4j.MarcReaderException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 7 with MarcReader

use of org.marc4j.MarcReader in project z3950-server by mol-pl.

the class Iso2709Utf8RecordFactory method getCanonicalXML.

/**
 * Tworzy marcxml z obiektu binarnego
 *
 * @param native_object dokument binarny
 * @return marcxml
 * @throws RecordBuilderException
 */
@Override
public Document getCanonicalXML(Object native_object) throws RecordBuilderException {
    org.w3c.dom.Document retval = null;
    try {
        MarcReader reader = new MarcStreamReader(new ByteArrayInputStream((byte[]) native_object), "UTF8");
        javax.xml.transform.dom.DOMResult result = new javax.xml.transform.dom.DOMResult();
        MarcXmlWriter writer = new MarcXmlWriter(result);
        if (reader.hasNext()) {
            Record record = (Record) reader.next();
            writer.write(record);
        } else {
            log.warn("No marc record found in reader stream");
        }
        writer.close();
        retval = (Document) result.getNode();
    } catch (Exception e) {
        throw new RecordBuilderException("Problem creating marcxml from iso2709", e);
    }
    return retval;
}
Also used : RecordBuilderException(org.jzkit.search.util.RecordBuilder.RecordBuilderException) MarcStreamReader(org.marc4j.MarcStreamReader) RecordBuilderException(org.jzkit.search.util.RecordBuilder.RecordBuilderException) Document(org.w3c.dom.Document) MarcReader(org.marc4j.MarcReader) Record(org.marc4j.marc.Record)

Example 8 with MarcReader

use of org.marc4j.MarcReader in project mod-source-record-manager by folio-org.

the class AdditionalFieldsUtil method addFieldToMarcRecord.

/**
 * Adds field if it does not exist and a subfield with a value to that field
 *
 * @param record   record that needs to be updated
 * @param field    field that should contain new subfield
 * @param subfield new subfield to add
 * @param value    value of the subfield to add
 * @return true if succeeded, false otherwise
 */
public static boolean addFieldToMarcRecord(Record record, String field, char subfield, String value) {
    boolean result = false;
    try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
        if (record != null && record.getParsedRecord() != null && record.getParsedRecord().getContent() != null) {
            MarcReader reader = buildMarcReader(record);
            MarcWriter streamWriter = new MarcStreamWriter(new ByteArrayOutputStream());
            MarcJsonWriter jsonWriter = new MarcJsonWriter(os);
            MarcFactory factory = MarcFactory.newInstance();
            if (reader.hasNext()) {
                org.marc4j.marc.Record marcRecord = reader.next();
                VariableField variableField = getSingleFieldByIndicators(marcRecord.getVariableFields(field), INDICATOR, INDICATOR);
                DataField dataField;
                if (variableField != null && ((DataField) variableField).getIndicator1() == INDICATOR && ((DataField) variableField).getIndicator2() == INDICATOR) {
                    dataField = (DataField) variableField;
                    marcRecord.removeVariableField(variableField);
                    dataField.removeSubfield(dataField.getSubfield(subfield));
                } else {
                    dataField = factory.newDataField(field, INDICATOR, INDICATOR);
                }
                dataField.addSubfield(factory.newSubfield(subfield, value));
                marcRecord.addVariableField(dataField);
                // use stream writer to recalculate leader
                streamWriter.write(marcRecord);
                jsonWriter.write(marcRecord);
                record.setParsedRecord(record.getParsedRecord().withContent(new JsonObject(new String(os.toByteArray())).encode()));
                result = true;
            }
        }
    } catch (Exception e) {
        LOGGER.error("Failed to add additional subfield {} for field {} to record {}", subfield, field, record.getId(), e);
    }
    return result;
}
Also used : JsonObject(io.vertx.core.json.JsonObject) MarcFactory(org.marc4j.marc.MarcFactory) ByteArrayOutputStream(java.io.ByteArrayOutputStream) VariableField(org.marc4j.marc.VariableField) DataField(org.marc4j.marc.DataField) MarcReader(org.marc4j.MarcReader) MarcWriter(org.marc4j.MarcWriter) MarcJsonWriter(org.marc4j.MarcJsonWriter) MarcStreamWriter(org.marc4j.MarcStreamWriter)

Example 9 with MarcReader

use of org.marc4j.MarcReader in project mod-source-record-manager by folio-org.

the class AdditionalFieldsUtil method addControlledFieldToMarcRecord.

/**
 * Adds new controlled field to marc record
 *
 * @param record record that needs to be updated
 * @param field  tag of controlled field
 * @param value  value of the field to add
 * @return true if succeeded, false otherwise
 */
public static boolean addControlledFieldToMarcRecord(Record record, String field, String value) {
    boolean result = false;
    try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
        if (record != null && record.getParsedRecord() != null && record.getParsedRecord().getContent() != null) {
            MarcReader reader = buildMarcReader(record);
            MarcWriter streamWriter = new MarcStreamWriter(new ByteArrayOutputStream());
            MarcJsonWriter jsonWriter = new MarcJsonWriter(os);
            MarcFactory factory = MarcFactory.newInstance();
            if (reader.hasNext()) {
                org.marc4j.marc.Record marcRecord = reader.next();
                ControlField dataField = factory.newControlField(field, value);
                marcRecord.addVariableField(dataField);
                // use stream writer to recalculate leader
                streamWriter.write(marcRecord);
                jsonWriter.write(marcRecord);
                record.setParsedRecord(record.getParsedRecord().withContent(new JsonObject(new String(os.toByteArray())).encode()));
                result = true;
            }
        }
    } catch (Exception e) {
        LOGGER.error("Failed to add additional controlled field {} to record {}", field, record.getId(), e);
    }
    return result;
}
Also used : JsonObject(io.vertx.core.json.JsonObject) MarcFactory(org.marc4j.marc.MarcFactory) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ControlField(org.marc4j.marc.ControlField) MarcReader(org.marc4j.MarcReader) MarcWriter(org.marc4j.MarcWriter) MarcJsonWriter(org.marc4j.MarcJsonWriter) MarcStreamWriter(org.marc4j.MarcStreamWriter)

Example 10 with MarcReader

use of org.marc4j.MarcReader in project mod-oai-pmh by folio-org.

the class MarcXmlMapper method convert.

/**
 * Convert MarcJson to MarcXML.
 *
 * @param source String representation of MarcJson source.
 * @return byte[] representation of MarcXML
 */
public byte[] convert(String source) {
    var operationId = UUID.randomUUID().toString();
    metricsCollectingService.startMetric(operationId, PARSE_XML);
    StopWatch timer = logger.isDebugEnabled() ? StopWatch.createStarted() : null;
    /*
     * Fix indicators which comes like "ind1": "\\" in the source string and values are converted to '\'
     * which contradicts to the MARC21slim.xsd schema. So replacing unexpected char by space
     */
    source = DOUBLE_BACKSLASH_PATTERN.matcher(source).replaceAll(" ");
    try (InputStream inputStream = new ByteArrayInputStream(source.getBytes(StandardCharsets.UTF_8))) {
        MarcReader marcJsonReader = new MarcJsonReader(inputStream);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        Record record = marcJsonReader.next();
        MarcXmlWriter.writeSingleRecord(record, out, false, false);
        return out.toByteArray();
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    } finally {
        metricsCollectingService.endMetric(operationId, PARSE_XML);
        if (timer != null) {
            timer.stop();
            logger.debug("Marc-json converted to MarcXml after {} ms.", timer.getTime());
        }
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) MarcReader(org.marc4j.MarcReader) MarcJsonReader(org.marc4j.MarcJsonReader) Record(org.marc4j.marc.Record) UncheckedIOException(java.io.UncheckedIOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) StopWatch(org.apache.commons.lang3.time.StopWatch)

Aggregations

MarcReader (org.marc4j.MarcReader)17 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 JsonObject (io.vertx.core.json.JsonObject)10 MarcJsonWriter (org.marc4j.MarcJsonWriter)10 MarcStreamWriter (org.marc4j.MarcStreamWriter)9 MarcWriter (org.marc4j.MarcWriter)9 MarcFactory (org.marc4j.marc.MarcFactory)7 VariableField (org.marc4j.marc.VariableField)7 DataField (org.marc4j.marc.DataField)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 PostProcessingException (org.folio.services.exceptions.PostProcessingException)5 Record (org.marc4j.marc.Record)5 MarcStreamReader (org.marc4j.MarcStreamReader)4 ControlField (org.marc4j.marc.ControlField)4 MarcJsonReader (org.marc4j.MarcJsonReader)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 MarcFieldProtectionSetting (org.folio.rest.jaxrs.model.MarcFieldProtectionSetting)2 InputStream (java.io.InputStream)1 StringWriter (java.io.StringWriter)1