Search in sources :

Example 1 with Leader

use of org.marc4j.marc.Leader in project RecordManager2 by moravianlibrary.

the class MarcXmlHandler method endElement.

/**
 * An event fired at the end of an element.
 *
 * @param uri
 * @param name
 * @param qName
 */
public void endElement(String uri, String name, String qName) throws SAXException {
    String realname = (name.length() == 0) ? qName : name;
    Integer elementType = (Integer) elementMap.get(realname);
    if (elementType == null) {
        return;
    }
    switch(elementType.intValue()) {
        case COLLECTION_ID:
            break;
        case RECORD_ID:
            queue.push(record);
            break;
        case LEADER_ID:
            while (sb.length() < 24) sb.append(" ");
            Leader leader = factory.newLeader(sb.toString());
            record.setLeader(leader);
            break;
        case CONTROLFIELD_ID:
            controlField.setData(sb.toString());
            record.addVariableField(controlField);
            break;
        case DATAFIELD_ID:
            record.addVariableField(dataField);
            break;
        case SUBFIELD_ID:
            subfield.setData(sb.toString());
            dataField.addSubfield(subfield);
    }
}
Also used : Leader(org.marc4j.marc.Leader)

Example 2 with Leader

use of org.marc4j.marc.Leader 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();
    }
}
Also used : MarcException(org.marc4j.MarcException) Leader(org.marc4j.marc.Leader) ControlField(org.marc4j.marc.ControlField) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream)

Example 3 with Leader

use of org.marc4j.marc.Leader in project RecordManager2 by moravianlibrary.

the class MarcLineStreamReader method parseLine.

private void parseLine(Record record, String strRecord) {
    String[] arrayRec = strRecord.split("\n");
    for (String line : arrayRec) {
        String tag = line.substring(0, 3);
        Matcher matcher = LDR_PATTERN.matcher(line);
        if (matcher.find()) {
            Leader ldr;
            ldr = factory.newLeader(matcher.group(2));
            record.setLeader(ldr);
        } else if (Constants.CF_TAG_PATTERN.matcher(tag).find()) {
            record.addVariableField(factory.newControlField(tag, line.substring(4)));
        } else {
            char ind1 = line.charAt(4);
            char ind2 = line.charAt(5);
            String data = line.substring(6);
            if (data.length() > 0 && data.charAt(0) == '$') {
                DataField df = factory.newDataField(tag, ind1, ind2);
                record.addVariableField(parseDataField(df, data));
            } else {
                record.addVariableField(factory.newControlField(tag, line.substring(4)));
            }
        }
    }
}
Also used : Leader(org.marc4j.marc.Leader) DataField(org.marc4j.marc.DataField) Matcher(java.util.regex.Matcher)

Aggregations

Leader (org.marc4j.marc.Leader)3 ByteArrayInputStream (java.io.ByteArrayInputStream)1 DataInputStream (java.io.DataInputStream)1 IOException (java.io.IOException)1 Matcher (java.util.regex.Matcher)1 MarcException (org.marc4j.MarcException)1 ControlField (org.marc4j.marc.ControlField)1 DataField (org.marc4j.marc.DataField)1