Search in sources :

Example 1 with MarcStreamReader

use of org.marc4j.MarcStreamReader 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)

Example 2 with MarcStreamReader

use of org.marc4j.MarcStreamReader 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 3 with MarcStreamReader

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

the class iso2709RecordFactory method getCanonicalXML.

/**
 * create the canonica XML for this object
 */
public Document getCanonicalXML(Object native_object) throws RecordBuilderException {
    org.w3c.dom.Document retval = null;
    try {
        MarcReader reader = new MarcStreamReader(new ByteArrayInputStream((byte[]) native_object));
        javax.xml.transform.dom.DOMResult result = new javax.xml.transform.dom.DOMResult();
        MarcXmlWriter writer = new MarcXmlWriter(result);
        writer.setConverter(new org.marc4j.converter.impl.AnselToUnicode());
        if (reader.hasNext()) {
            Record record = (Record) reader.next();
            writer.write(record);
        }
        writer.close();
        retval = (Document) result.getNode();
    } catch (Exception e) {
        throw new RecordBuilderException("Problem creating marcxml from iso2709", e);
    }
    return retval;
}
Also used : MarcStreamReader(org.marc4j.MarcStreamReader) Document(org.w3c.dom.Document) MarcReader(org.marc4j.MarcReader) org.marc4j(org.marc4j) Record(org.marc4j.marc.Record)

Example 4 with MarcStreamReader

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

the class HttpDataProvider method getList.

/**
 * Pobiera listę rekordów z bazy tenenta
 *
 * @param params kryteria pobierania
 * @param start pierwszy rekord
 * @param limit liczba rekordów
 * @return tablica rekordów
 * @throws UnsupportedEncodingException nieprawidłowe kodowanie
 * @throws MalformedURLException nieprawidłowy url
 * @throws IOException nie udało się odpytać tenanta
 * @throws Exception baza nie istnieje
 */
public Record[] getList(HttpQueryParams params, Integer start, Integer limit) throws UnsupportedEncodingException, MalformedURLException, IOException, Exception {
    String url = params.getTenantUrl() + "/api/z3950server/?" + prepareGetParams(params, start, limit);
    GetMethod method = new GetMethod(url);
    try {
        if (client.executeMethod(method) == HttpStatus.SC_OK) {
            List<Record> list = new ArrayList<>();
            MarcReader reader = new MarcStreamReader(method.getResponseBodyAsStream(), "UTF8");
            while (reader.hasNext()) {
                list.add(reader.next());
            }
            Record[] records = new Record[limit];
            return (Record[]) list.toArray(records);
        } else {
            throw new Exception("Database not found");
        }
    } finally {
        method.releaseConnection();
    }
}
Also used : MarcReader(org.marc4j.MarcReader) GetMethod(org.apache.commons.httpclient.methods.GetMethod) ArrayList(java.util.ArrayList) Record(org.marc4j.marc.Record) MarcStreamReader(org.marc4j.MarcStreamReader) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

MarcReader (org.marc4j.MarcReader)4 MarcStreamReader (org.marc4j.MarcStreamReader)4 Record (org.marc4j.marc.Record)4 ArrayList (java.util.ArrayList)2 Document (org.w3c.dom.Document)2 JsonObject (io.vertx.core.json.JsonObject)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 MalformedURLException (java.net.MalformedURLException)1 GetMethod (org.apache.commons.httpclient.methods.GetMethod)1 RecordBuilderException (org.jzkit.search.util.RecordBuilder.RecordBuilderException)1 org.marc4j (org.marc4j)1 MarcError (org.marc4j.MarcError)1 MarcJsonWriter (org.marc4j.MarcJsonWriter)1