Marc4J calculating record length and base address of data

73 views Asked by At

I'm working with Unimarc, using the marc4j library. In my system, I'm already able to get an instance of org.marc4j.marc.Record. Now I'm having a hard time figuring out how to calculate the record length and base address of data. that are used in the record leader.

I've been trying to find how to do this using the marc4j library or outside of it and couldn't find it so far.

How can I get these two pieces of information?

1

There are 1 answers

0
André Luiz On

I couldn't find anything in the documentation but I managed to do it. Functions below:

public String getISO2709 (org.marc4j.marc.Record record ){
    OutputStream output = new OutputStream() {
        private StringBuilder string = new StringBuilder();

        @Override
        public void write(int b) throws IOException {
            this.string.append((char) b );
        }

        public String toString() {
            return this.string.toString();
        }
    };
    MarcStreamWriter writer = new MarcStreamWriter(output);
    writer.write(record);
    return output.toString();
}


public String getNoticeLength(org.marc4j.marc.Record record, org.marc4j.marc.Leader currentLeader){

    int recordLength = this.getISO2709(record).length();

    String ret = StringUtils.padLeft(String.valueOf(recordLength), '0', 5);
    if(ret.length() > 5) ret ="00000"; // since the header has space for only 5 chars, this 00000 will indicate the length was invalid

    return ret;
}

public  String getBaseAddressOfData(org.marc4j.marc.Record record, org.marc4j.marc.Leader currentLeader){

    String iso2709 = this.getISO2709(record);
    StringBuilder sb = new StringBuilder();

    for (ControlField cf : record.getControlFields()) {
        sb.append(cf.getData() + "\u001E"); // this is the character that separates the values for the control fields iso2709
    }

    int index = iso2709.indexOf(sb.toString());

    String ret = StringUtils.padLeft(String.valueOf(index), '0', 5);
    if(ret.length() > 5) ret="00000";

    return ret;
}