use of org.marc4j.MarcException in project RecordManager2 by moravianlibrary.
the class MarcISO2709StreamReader method parseRecord.
private void parseRecord(Record record, byte[] byteArray, byte[] recordBuf, int recordLength) {
Leader ldr;
ldr = factory.newLeader();
ldr.setRecordLength(recordLength);
int directoryLength = 0;
try {
parseLeader(ldr, byteArray);
directoryLength = ldr.getBaseAddressOfData() - (24 + 1);
} catch (IOException e) {
throw new MarcException("error parsing leader with data: " + new String(byteArray), e);
} catch (MarcException e) {
throw new MarcException("error parsing leader with data: " + new String(byteArray), e);
}
// if MARC 21 then check encoding
switch(ldr.getCharCodingScheme()) {
case ' ':
if (!override) {
encoding = "ISO-8859-1";
}
break;
case 'a':
if (!override) {
encoding = "UTF8";
}
}
record.setLeader(ldr);
if ((directoryLength % 12) != 0) {
throw new MarcException("invalid directory");
}
DataInputStream inputrec = new DataInputStream(new ByteArrayInputStream(recordBuf));
int size = directoryLength / 12;
String[] tags = new String[size];
int[] lengths = new int[size];
byte[] tag = new byte[3];
byte[] length = new byte[4];
byte[] start = new byte[5];
String tmp;
try {
for (int i = 0; i < size; i++) {
inputrec.readFully(tag);
tmp = new String(tag);
tags[i] = tmp;
inputrec.readFully(length);
tmp = new String(length);
lengths[i] = Integer.parseInt(tmp);
inputrec.readFully(start);
}
if (inputrec.read() != Constants.FT) {
throw new MarcException("expected field terminator at end of directory");
}
for (int i = 0; i < size; i++) {
getFieldLength(inputrec);
// If tag is for a ControlField; else, try as DataField
if (Constants.CF_TAG_PATTERN.matcher(tags[i]).find()) {
byteArray = new byte[lengths[i] - 1];
inputrec.readFully(byteArray);
if (inputrec.read() != Constants.FT) {
throw new MarcException("expected field terminator at end of field");
}
ControlField field = factory.newControlField();
field.setTag(tags[i]);
field.setData(getDataAsString(byteArray));
record.addVariableField(field);
} else {
byteArray = new byte[lengths[i]];
inputrec.readFully(byteArray);
try {
record.addVariableField(parseDataField(tags[i], byteArray));
} catch (IOException e) {
throw new MarcException("error parsing data field for tag: " + tags[i] + " with data: " + new String(byteArray), e);
}
}
}
if (inputrec.read() != Constants.RT) {
throw new MarcException("expected record terminator");
}
} catch (IOException e) {
throw new MarcException("an error occured reading input", e);
} catch (NumberFormatException e) {
throw new MarcException();
}
}
use of org.marc4j.MarcException in project RecordManager2 by moravianlibrary.
the class MarcXmlParserThread method run.
/**
* Creates a new <code>MarcXmlHandler</code> instance, registers the
* <code>RecordQueue</code> and sends the <code>InputStream</code> to the
* <code>MarcXmlParser</code> parser.
*/
public void run() {
try {
MarcXmlHandler handler = new MarcXmlHandler(queue);
MarcXmlParser parser = new MarcXmlParser(handler);
if (th == null) {
parser.parse(input);
} else {
parser.parse(input, th);
}
} catch (MarcException me) {
queue.passException(me);
} finally {
queue.end();
}
}
Aggregations