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();
}
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;
}
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;
}
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;
}
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());
}
}
}
Aggregations