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