001package org.kuali.ole;
002
003import org.marc4j.MarcReader;
004import org.marc4j.MarcStreamReader;
005import org.marc4j.MarcWriter;
006import org.marc4j.MarcXmlWriter;
007import org.marc4j.marc.*;
008import org.marc4j.marc.impl.Verifier;
009
010import java.io.*;
011
012
013/**
014 * Created with IntelliJ IDEA.
015 * User: jpillan
016 * Date: 2/7/13
017 * Time: 11:28 AM
018 * To change this template use File | Settings | File Templates.
019 */
020
021public class MRKToMARCXMLConverter {
022    MarcFactory marcFactory = MarcFactory.newInstance();
023
024    public String convert(String content) {
025        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(content.getBytes());
026        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
027        MarcWriter writer = new MarcXmlWriter(byteArrayOutputStream, true);
028
029        Record record;
030        String inline;
031
032        BufferedReader br = null;
033        br = new BufferedReader(new InputStreamReader(byteArrayInputStream));
034        record = marcFactory.newRecord();
035
036        try {
037            while ((inline = br.readLine()) != null) {
038                if (inline.equals("") || inline.startsWith(" ")) {
039                    writer.write(record);
040                    record = marcFactory.newRecord();
041                } else {
042                    record = addLineToRecord(inline, record);
043                }
044            }
045        } catch (IOException e) {
046
047        }
048
049        writer.write(record);
050        writer.close();
051        return new String(byteArrayOutputStream.toByteArray());
052
053    }
054
055    public Record addLineToRecord(String inline, Record record) {
056        String tag = "";
057
058        tag = inline.substring(1, 4);
059
060        if (tag.equals("LDR")) {
061            Leader leader = marcFactory.newLeader(inline.substring(6, 30));
062            record.setLeader(leader);
063        } else if (Verifier.isControlField(tag)) {
064            ControlField cf = marcFactory.newControlField(tag, inline.substring(6));
065            record.addVariableField(cf);
066        } else {
067            record.addVariableField(processDatafield(inline));
068        }
069
070        return record;
071    }
072
073    private DataField processDatafield(String inline) {
074        String tag;
075        char ind1;
076        char ind2;
077
078        tag = inline.substring(1, 4);
079
080        if (inline.substring(6, 7).equals("\""))
081            ind1 = ' ';
082        else
083            ind1 = inline.charAt(6);
084
085        if (inline.substring(7, 8).equals("\""))
086            ind2 = ' ';
087        else
088            ind2 = inline.charAt(7);
089
090        DataField df = marcFactory.newDataField(tag, ind1, ind2);
091
092        String[] tagSplit = inline.substring(8).split("\\$");
093        Subfield sf = null;
094        for (String text : tagSplit) {
095            if (!text.equals("") && !text.equals(" ")) {
096                char subfield = text.charAt(0);
097                String subfieldText = text.substring(1);
098                sf = marcFactory.newSubfield(subfield, subfieldText);
099                df.addSubfield(sf);
100            }
101        }
102        return df;
103    }
104}