View Javadoc
1   package org.kuali.ole.converter;
2   
3   import org.kuali.ole.pojo.bib.BibliographicRecord;
4   import org.marc4j.MarcReader;
5   import org.marc4j.MarcStreamReader;
6   import org.marc4j.MarcWriter;
7   import org.marc4j.MarcXmlWriter;
8   import org.marc4j.marc.Record;
9   
10  import java.io.*;
11  
12  /**
13   * Created by IntelliJ IDEA.
14   * User: pvsubrah
15   * Date: 4/2/12
16   * Time: 3:59 PM
17   * To change this template use File | Settings | File Templates.
18   */
19  public class MarcXMLConverter {
20      /**
21       * @param inputFile
22       * @return converted marc xml File Name
23       * @throws java.io.FileNotFoundException This method takes in a raw marc file and converts into a marc xml file.
24       */
25      public String convertRawMarcToXML(File inputFile) throws FileNotFoundException {
26          InputStream input = new FileInputStream(inputFile);
27          String fileName = inputFile.getName().replace(".mrc", ".xml");
28          FileOutputStream fileOutputStream = new FileOutputStream(new File(fileName));
29          MarcReader reader = new MarcStreamReader(input);
30          MarcWriter writer = new MarcXmlWriter(fileOutputStream, true);
31  
32          while (reader.hasNext()) {
33              Record record = reader.next();
34              writer.write(record);
35          }
36          writer.close();
37          return fileName;
38      }
39  
40  
41      /**
42       * @param content
43       * @return converted marc xml
44       */
45      public String convert(String content) {
46          ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(content.getBytes());
47          ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
48  
49          MarcReader reader = new MarcStreamReader(byteArrayInputStream);
50          MarcWriter writer = new MarcXmlWriter(byteArrayOutputStream, true);
51  
52          while (reader.hasNext()) {
53              Record record = reader.next();
54              writer.write(record);
55          }
56          writer.close();
57          return new String(byteArrayOutputStream.toByteArray());
58      }
59  
60      public void generateMarcBean(BibliographicRecord orderRecord) {
61  
62      }
63  }