Search in sources :

Example 1 with MarcException

use of org.marc4j.MarcException in project RecordManager2 by moravianlibrary.

the class MarcISO2709StreamReader method hasNext.

/**
 * Returns true if the iteration has more records, false otherwise.
 */
public boolean hasNext() {
    try {
        input.mark(10);
        // check whether there are at least 10 bytes left
        byte[] bytes = new byte[10];
        if (input.read(bytes) < 10) {
            return false;
        }
        input.reset();
    } catch (IOException e) {
        throw new MarcException(e.getMessage(), e);
    }
    return true;
}
Also used : MarcException(org.marc4j.MarcException) IOException(java.io.IOException)

Example 2 with MarcException

use of org.marc4j.MarcException in project RecordManager2 by moravianlibrary.

the class MarcISO2709StreamReader method parseLeader.

private void parseLeader(Leader ldr, byte[] leaderData) throws IOException {
    InputStreamReader isr = new InputStreamReader(new ByteArrayInputStream(leaderData), "ISO-8859-1");
    char[] tmp = new char[5];
    isr.read(tmp);
    // Skip over bytes for record length, If we get here, its already been
    // computed.
    ldr.setRecordStatus((char) isr.read());
    ldr.setTypeOfRecord((char) isr.read());
    tmp = new char[2];
    isr.read(tmp);
    ldr.setImplDefined1(tmp);
    ldr.setCharCodingScheme((char) isr.read());
    char indicatorCount = (char) isr.read();
    char subfieldCodeLength = (char) isr.read();
    char[] baseAddr = new char[5];
    isr.read(baseAddr);
    tmp = new char[3];
    isr.read(tmp);
    ldr.setImplDefined2(tmp);
    tmp = new char[4];
    isr.read(tmp);
    ldr.setEntryMap(tmp);
    isr.close();
    try {
        ldr.setIndicatorCount(Integer.parseInt(String.valueOf(indicatorCount)));
    } catch (NumberFormatException e) {
        throw new MarcException("unable to parse indicator count", e);
    }
    try {
        ldr.setSubfieldCodeLength(Integer.parseInt(String.valueOf(subfieldCodeLength)));
    } catch (NumberFormatException e) {
        throw new MarcException("unable to parse subfield code length", e);
    }
    try {
        ldr.setBaseAddressOfData(Integer.parseInt(new String(baseAddr)));
    } catch (NumberFormatException e) {
        throw new MarcException("unable to parse base address of data", e);
    }
}
Also used : MarcException(org.marc4j.MarcException) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 3 with MarcException

use of org.marc4j.MarcException in project RecordManager2 by moravianlibrary.

the class MarcFactoryImpl method newDataField.

/**
 * Creates a new data field with the given tag and indicators and subfields
 * and returns the instance.
 *
 * @return DataField
 */
@Override
public DataField newDataField(String tag, char ind1, char ind2, String... subfieldCodesAndData) {
    DataField df = new DataFieldImpl(tag, ind1, ind2);
    if (subfieldCodesAndData.length % 2 == 1) {
        throw new MarcException("Error: must provide even number of parameters for subfields: code, data, code, data, ...");
    }
    for (int i = 0; i < subfieldCodesAndData.length; i += 2) {
        if (subfieldCodesAndData[i].length() != 1) {
            throw new MarcException("Error: subfieldCode must be a single character");
        }
        Subfield sf = newSubfield(subfieldCodesAndData[i].charAt(0), subfieldCodesAndData[i + 1]);
        df.addSubfield(sf);
    }
    return (df);
}
Also used : MarcException(org.marc4j.MarcException) DataField(org.marc4j.marc.DataField) Subfield(org.marc4j.marc.Subfield)

Example 4 with MarcException

use of org.marc4j.MarcException in project RecordManager2 by moravianlibrary.

the class MarcISO2709StreamReader method next.

/**
 * Returns the next record in the iteration.
 *
 * @return Record - the record object
 */
public Record next() {
    record = factory.newRecord();
    try {
        // skip non numeric characters at the begin (cr, lf etc)
        byte nextByte;
        while (hasNext()) {
            input.mark(1);
            nextByte = input.readByte();
            Character c = new Character((char) nextByte);
            if (Character.isDigit(c)) {
                input.reset();
                break;
            }
        }
        byte[] byteArray = new byte[24];
        input.readFully(byteArray);
        int recordLength = parseRecordLength(byteArray);
        byte[] recordBuf = new byte[recordLength - 24];
        input.readFully(recordBuf);
        parseRecord(record, byteArray, recordBuf, recordLength);
        return (record);
    } catch (EOFException e) {
        throw new MarcException("Premature end of file encountered", e);
    } catch (IOException e) {
        throw new MarcException("an error occured reading input", e);
    } catch (NegativeArraySizeException e) {
        throw new MarcException("wrong record length");
    }
}
Also used : MarcException(org.marc4j.MarcException) EOFException(java.io.EOFException) IOException(java.io.IOException)

Example 5 with MarcException

use of org.marc4j.MarcException in project RecordManager2 by moravianlibrary.

the class MarcISO2709StreamReader method parseRecordLength.

private int parseRecordLength(byte[] leaderData) throws IOException {
    InputStreamReader isr = new InputStreamReader(new ByteArrayInputStream(leaderData), "ISO-8859-1");
    int length = -1;
    // skip non numeric characters at the begin of record
    char[] cbuf = new char[1];
    while (isr.ready()) {
        isr.read(cbuf);
        if (Character.isDigit(cbuf[0])) {
            break;
        }
    }
    char[] tmp = new char[4];
    isr.read(tmp);
    try {
        length = Integer.parseInt(new String(cbuf) + new String(tmp));
    } catch (NumberFormatException e) {
        throw new MarcException("unable to parse record length", e);
    }
    return (length);
}
Also used : MarcException(org.marc4j.MarcException) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream)

Aggregations

MarcException (org.marc4j.MarcException)7 ByteArrayInputStream (java.io.ByteArrayInputStream)3 IOException (java.io.IOException)3 InputStreamReader (java.io.InputStreamReader)2 DataInputStream (java.io.DataInputStream)1 EOFException (java.io.EOFException)1 MarcXmlParser (org.marc4j.MarcXmlParser)1 ControlField (org.marc4j.marc.ControlField)1 DataField (org.marc4j.marc.DataField)1 Leader (org.marc4j.marc.Leader)1 Subfield (org.marc4j.marc.Subfield)1