001package org.kuali.ole.converter;
002
003import org.kuali.ole.pojo.bib.BibliographicRecord;
004import org.marc4j.MarcReader;
005import org.marc4j.MarcStreamReader;
006import org.marc4j.MarcWriter;
007import org.marc4j.MarcXmlWriter;
008import org.marc4j.marc.Record;
009
010import java.io.*;
011
012/**
013 * Created by IntelliJ IDEA.
014 * User: pvsubrah
015 * Date: 4/2/12
016 * Time: 3:59 PM
017 * To change this template use File | Settings | File Templates.
018 */
019public class MarcXMLConverter {
020    /**
021     * @param inputFile
022     * @return converted marc xml File Name
023     * @throws java.io.FileNotFoundException This method takes in a raw marc file and converts into a marc xml file.
024     */
025    public String convertRawMarcToXML(File inputFile) throws FileNotFoundException {
026        InputStream input = new FileInputStream(inputFile);
027        String fileName = inputFile.getName().replace(".mrc", ".xml");
028        FileOutputStream fileOutputStream = new FileOutputStream(new File(fileName));
029        MarcReader reader = new MarcStreamReader(input);
030        MarcWriter writer = new MarcXmlWriter(fileOutputStream, true);
031
032        while (reader.hasNext()) {
033            Record record = reader.next();
034            writer.write(record);
035        }
036        writer.close();
037        return fileName;
038    }
039
040
041    /**
042     * @param content
043     * @return converted marc xml
044     */
045    public String convert(String content) {
046        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(content.getBytes());
047        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
048
049        MarcReader reader = new MarcStreamReader(byteArrayInputStream);
050        MarcWriter writer = new MarcXmlWriter(byteArrayOutputStream, true);
051
052        while (reader.hasNext()) {
053            Record record = reader.next();
054            writer.write(record);
055        }
056        writer.close();
057        return new String(byteArrayOutputStream.toByteArray());
058    }
059
060    public void generateMarcBean(BibliographicRecord orderRecord) {
061
062    }
063}