Search in sources :

Example 1 with MarcXmlReader

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

Example 2 with MarcXmlReader

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;
}
Also used : XMLSerializer(org.apache.xml.serialize.XMLSerializer) java.io(java.io) MarcWriter(org.marc4j.MarcWriter) Record(org.marc4j.marc.Record) MarcXmlReader(org.marc4j.MarcXmlReader) MarcStreamWriter(org.marc4j.MarcStreamWriter)

Example 3 with MarcXmlReader

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;
}
Also used : XMLSerializer(org.apache.xml.serialize.XMLSerializer) java.io(java.io) RecordBuilderException(org.jzkit.search.util.RecordBuilder.RecordBuilderException) MarcWriter(org.marc4j.MarcWriter) Record(org.marc4j.marc.Record) MarcXmlReader(org.marc4j.MarcXmlReader) MarcStreamWriter(org.marc4j.MarcStreamWriter)

Example 4 with MarcXmlReader

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");
    }
}
Also used : StringInputStream(org.codehaus.plexus.util.StringInputStream) MarcWriter(org.marc4j.MarcWriter) Record(org.marc4j.marc.Record) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) MarcXmlReader(org.marc4j.MarcXmlReader) MarcStreamWriter(org.marc4j.MarcStreamWriter) IOException(java.io.IOException)

Aggregations

MarcXmlReader (org.marc4j.MarcXmlReader)4 Record (org.marc4j.marc.Record)4 MarcStreamWriter (org.marc4j.MarcStreamWriter)3 MarcWriter (org.marc4j.MarcWriter)3 java.io (java.io)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 XMLSerializer (org.apache.xml.serialize.XMLSerializer)2 JsonObject (io.vertx.core.json.JsonObject)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 StringInputStream (org.codehaus.plexus.util.StringInputStream)1 RecordBuilderException (org.jzkit.search.util.RecordBuilder.RecordBuilderException)1 MarcJsonWriter (org.marc4j.MarcJsonWriter)1