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
13
14
15
16
17
18 public class MarcXMLGenerator {
19
20
21
22
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
42
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 }