return(recordBuf);
}
private void parseRecord(Record record, byte[] byteArray, byte[] recordBuf, int recordLength)
{
Leader ldr;
ldr = factory.newLeader();
ldr.setRecordLength(recordLength);
int directoryLength=0;
// These variables are used when the permissive reader is trying to make its best guess
// as to what character encoding is actually used in the record being processed.
conversionCheck1 = "";
conversionCheck2 = "";
conversionCheck3 = "";
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) {
if (permissive)
{
if (recordBuf[recordBuf.length-1] == Constants.RT && recordBuf[recordBuf.length-2] == Constants.FT)
{
errors.addError("unknown", "n/a", "n/a", ErrorHandler.MAJOR_ERROR,
"Error parsing leader, trying to re-read leader either shorter or longer");
// make an attempt to recover record.
int offset = 0;
while (offset < recordBuf.length)
{
if (recordBuf[offset] == Constants.FT)
{
break;
}
offset++;
}
if (offset % 12 == 1)
{
// move one byte from body to leader, make new leader, and try again
errors.addError("unknown", "n/a", "n/a", ErrorHandler.MAJOR_ERROR,
"Leader appears to be too short, moving one byte from record body to leader, and trying again");
byte oldBody[] = recordBuf;
recordBuf = new byte[oldBody.length-1];
System.arraycopy(oldBody, 1, recordBuf, 0, oldBody.length-1);
directoryLength = offset-1;
ldr.setIndicatorCount(2);
ldr.setSubfieldCodeLength(2);
ldr.setImplDefined1((""+(char)byteArray[7]+" ").toCharArray());
ldr.setImplDefined2((""+(char)byteArray[18]+(char)byteArray[19]+(char)byteArray[20]).toCharArray());
ldr.setEntryMap("4500".toCharArray());
if (byteArray[10] == (byte)' ' || byteArray[10] == (byte)'a') // if its ' ' or 'a'
{
ldr.setCharCodingScheme((char)byteArray[10]);
}
}
else if (offset % 12 == 11)
{
errors.addError("unknown", "n/a", "n/a", ErrorHandler.MAJOR_ERROR,
"Leader appears to be too long, moving one byte from leader to record body, and trying again");
byte oldBody[] = recordBuf;
recordBuf = new byte[oldBody.length+1];
System.arraycopy(oldBody, 0, recordBuf, 1, oldBody.length);
recordBuf[0] = (byte)'0';
directoryLength = offset+1;
ldr.setIndicatorCount(2);
ldr.setSubfieldCodeLength(2);
ldr.setImplDefined1((""+(char)byteArray[7]+" ").toCharArray());
ldr.setImplDefined2((""+(char)byteArray[16]+(char)byteArray[17]+(char)byteArray[18]).toCharArray());
ldr.setEntryMap("4500".toCharArray());
if (byteArray[8] == (byte)' ' || byteArray[8] == (byte)'a') // if its ' ' or 'a'
{
ldr.setCharCodingScheme((char)byteArray[10]);
}
if (byteArray[10] == (byte)' ' || byteArray[10] == (byte)'a') // if its ' ' or 'a'
{
ldr.setCharCodingScheme((char)byteArray[10]);
}
}
else
{
errors.addError("unknown", "n/a", "n/a", ErrorHandler.FATAL,
"error parsing leader with data: " + new String(byteArray));
throw new MarcException("error parsing leader with data: "
+ new String(byteArray), e);
}
}
}
else
{
throw new MarcException("error parsing leader with data: "
+ new String(byteArray), e);
}
}
char tmp[] = ldr.getEntryMap();
if (permissive && !(""+ tmp[0]+tmp[1]+tmp[2]+tmp[3]).equals("4500"))
{
if (tmp[0] >= '0' && tmp[0] <= '9' &&
tmp[1] >= '0' && tmp[1] <= '9' &&
tmp[2] >= '0' && tmp[2] <= '9' &&
tmp[3] >= '0' && tmp[3] <= '9')
{
errors.addError("unknown", "n/a", "n/a", ErrorHandler.ERROR_TYPO,
"Unusual character found at end of leader [ "+tmp[0]+tmp[1]+tmp[2]+tmp[3]+" ]");
}
else
{
errors.addError("unknown", "n/a", "n/a", ErrorHandler.ERROR_TYPO,
"Erroneous character found at end of leader [ "+tmp[0]+tmp[1]+tmp[2]+tmp[3]+" ]; changing them to the standard \"4500\"");
ldr.setEntryMap("4500".toCharArray());
}
}
// if MARC 21 then check encoding
switch (ldr.getCharCodingScheme()) {
case 'a':
encoding = "UTF8";
break;
case ' ':
if (convertToUTF8)
encoding = defaultEncoding;
else
encoding = "ISO8859_1";
break;
default:
if (convertToUTF8)
if (permissive)
{
errors.addError("unknown", "n/a", "n/a", ErrorHandler.MINOR_ERROR,
"Record character encoding should be 'a' or ' ' in this record it is '"+ldr.getCharCodingScheme()+"'. Attempting to guess the correct encoding.");
encoding = "BESTGUESS";
}
else
encoding = defaultEncoding;
else