use of org.marc4j.MarcXmlReader in project mod-source-record-manager by folio-org.
the class XmlRecordParser method parseRecord.
@Override
public ParsedResult parseRecord(String rawRecord) {
ParsedResult result = new ParsedResult();
try {
MarcXmlReader reader = new MarcXmlReader(new ByteArrayInputStream(rawRecord.getBytes(StandardCharsets.UTF_8)));
if (reader.hasNext()) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
MarcJsonWriter writer = new MarcJsonWriter(os);
Record record = reader.next();
writer.write(record);
result.setParsedRecord(new JsonObject(new String(os.toByteArray())));
} else {
result.setParsedRecord(new JsonObject());
}
} catch (Exception e) {
LOGGER.error("Error during parse MARC record from MARC XML data", e);
result.setHasError(true);
result.setErrors(new JsonObject().put("name", e.getClass().getName()).put("message", e.getMessage()));
}
return result;
}
use of org.marc4j.MarcXmlReader in project z3950-server by mol-pl.
the class iso2709RecordFactory method createFrom.
/**
* @param input_fragment Source Fragment
* @param esn Optional Element Set Name (F,B, For XML, a schema to request);
* @return native_object
*/
public Object createFrom(Document input_dom, String esn) throws RecordBuilderException {
byte[] result = null;
log.debug("iso2709 from marcxml");
try {
log.debug("open pip output stream");
PipedOutputStream pos = new PipedOutputStream();
XMLSerializer serializer = new XMLSerializer();
MarcXmlReader reader = new MarcXmlReader(new PipedInputStream(pos));
serializer.setOutputByteStream(pos);
log.debug("Serialize dom to output stream");
serializer.serialize(input_dom);
log.debug("flush");
pos.flush();
pos.close();
log.debug("attempt to read marcxml from pipe");
if (reader.hasNext()) {
Record record = reader.next();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
MarcWriter writer = new MarcStreamWriter(baos);
writer.write(record);
result = baos.toByteArray();
log.debug("Result of transform to marc: " + new String(result));
} else {
log.warn("No marc record found in reader stream");
}
} catch (java.io.IOException ioe) {
throw new RecordBuilderException("Problem converting MARCXML to iso2709", ioe);
}
log.debug("result=" + result);
return result;
}
use of org.marc4j.MarcXmlReader in project z3950-server by mol-pl.
the class Iso2709Utf8RecordFactory method createFrom.
/**
* Tworzy dokument binarny z marcxml
*
* @param input_dom marcxml
* @param esn Optional Element Set Name (F,B, For XML, a schema to request);
* @return native_object
* @throws org.jzkit.search.util.RecordBuilder.RecordBuilderException
*/
@Override
public Object createFrom(Document input_dom, String esn) throws RecordBuilderException {
byte[] result = null;
log.debug("iso2709 from marcxml");
try {
log.debug("open pip output stream");
PipedOutputStream pos = new PipedOutputStream();
XMLSerializer serializer = new XMLSerializer();
MarcXmlReader reader = new MarcXmlReader(new PipedInputStream(pos));
serializer.setOutputByteStream(pos);
log.debug("Serialize dom to output stream");
serializer.serialize(input_dom);
log.debug("flush");
pos.flush();
pos.close();
log.debug("attempt to read marcxml from pipe");
if (reader.hasNext()) {
Record record = reader.next();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
MarcWriter writer = new MarcStreamWriter(baos, "UTF8");
writer.write(record);
result = baos.toByteArray();
log.debug("Result of transform to marc: " + new String(result));
} else {
log.warn("No marc record found in reader stream");
}
} catch (java.io.IOException ioe) {
throw new RecordBuilderException("Problem converting marcxml to iso2709", ioe);
}
log.debug("result=" + result);
return result;
}
use of org.marc4j.MarcXmlReader in project z3950-server by mol-pl.
the class MolNetResultSet method prepareDummyRecord.
/**
* Tworzy pusty rekord, za wypadek gdyby serwer zwrócił łędny rekord
* baz tego sypie się napełnianie cache i serwer się zapętla
*
* @throws Exception nie udało się sparsować przykładowego rekordu
*/
private void prepareDummyRecord() throws Exception {
try {
String doc = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<marc:collection xmlns:marc=\"http://www.loc.gov/MARC21/slim\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd\">\n" + "<marc:record>\n" + "<marc:datafield tag=\"010\" ind1=\" \" ind2=\" \">\n" + "<marc:subfield code=\"a\">CANNOT FETCH RECORD</marc:subfield>\n" + "</marc:datafield>\n" + "</marc:record>\n" + "</marc:collection>";
MarcXmlReader reader = new MarcXmlReader(new StringInputStream(doc));
if (reader.hasNext()) {
Record record = reader.next();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
MarcWriter writer = new MarcStreamWriter(baos);
writer.write(record);
dummyRecord = baos.toByteArray();
writer.close();
baos.close();
}
} catch (IOException ex) {
throw new Exception("Parsing resultset failed");
}
}
Aggregations