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