Search in sources :

Example 1 with MarcPermissiveStreamReader

use of org.marc4j.MarcPermissiveStreamReader in project OpenRefine by OpenRefine.

the class MarcImporter method createParserUIInitializationData.

@Override
public JSONObject createParserUIInitializationData(ImportingJob job, java.util.List<JSONObject> fileRecords, String format) {
    if (fileRecords.size() > 0) {
        JSONObject firstFileRecord = fileRecords.get(0);
        File file = ImportingUtilities.getFile(job, firstFileRecord);
        File tempFile = new File(file.getAbsolutePath() + ".xml");
        try {
            InputStream inputStream = new FileInputStream(file);
            OutputStream outputStream = new FileOutputStream(tempFile);
            try {
                MarcWriter writer = new MarcXmlWriter(outputStream, true);
                MarcPermissiveStreamReader reader = new MarcPermissiveStreamReader(inputStream, true, true);
                while (reader.hasNext()) {
                    Record record = reader.next();
                    writer.write(record);
                }
                writer.close();
            } finally {
                try {
                    outputStream.close();
                    inputStream.close();
                    if (// write failed. Most of time because of wrong Marc format
                    tempFile.length() == 0)
                        tempFile.delete();
                    else
                        // only set json if write the temp file successfully:
                        JSONUtilities.safePut(firstFileRecord, "location", JSONUtilities.getString(firstFileRecord, "location", "") + ".xml");
                //                        file.delete(); // get rid of our original file
                } catch (IOException e) {
                // Just ignore - not much we can do anyway
                }
            }
        } catch (IOException e) {
            logger.error("Failed to create temporary XML file from MARC file", e);
        }
    }
    JSONObject options = super.createParserUIInitializationData(job, fileRecords, format);
    return options;
}
Also used : JSONObject(org.json.JSONObject) MarcPermissiveStreamReader(org.marc4j.MarcPermissiveStreamReader) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) MarcWriter(org.marc4j.MarcWriter) Record(org.marc4j.marc.Record) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) MarcXmlWriter(org.marc4j.MarcXmlWriter)

Example 2 with MarcPermissiveStreamReader

use of org.marc4j.MarcPermissiveStreamReader in project OpenRefine by OpenRefine.

the class MarcImporter method createParserUIInitializationData.

@Override
public ObjectNode createParserUIInitializationData(ImportingJob job, List<ObjectNode> fileRecords, String format) {
    if (fileRecords.size() > 0) {
        ObjectNode firstFileRecord = fileRecords.get(0);
        File file = ImportingUtilities.getFile(job, firstFileRecord);
        File tempFile = new File(file.getAbsolutePath() + ".xml");
        try {
            InputStream inputStream = new FileInputStream(file);
            OutputStream outputStream = new FileOutputStream(tempFile);
            try {
                MarcWriter writer = new MarcXmlWriter(outputStream, true);
                MarcPermissiveStreamReader reader = new MarcPermissiveStreamReader(inputStream, true, true);
                while (reader.hasNext()) {
                    Record record = reader.next();
                    writer.write(record);
                }
                writer.close();
            } finally {
                try {
                    outputStream.close();
                    inputStream.close();
                    if (// write failed. Most of time because of wrong Marc format
                    tempFile.length() == 0)
                        tempFile.delete();
                    else
                        // only set json if write the temp file successfully:
                        JSONUtilities.safePut(firstFileRecord, "location", JSONUtilities.getString(firstFileRecord, "location", "") + ".xml");
                // file.delete(); // get rid of our original file
                } catch (IOException e) {
                // Just ignore - not much we can do anyway
                }
            }
        } catch (IOException e) {
            logger.error("Failed to create temporary XML file from MARC file", e);
        }
    }
    ObjectNode options = super.createParserUIInitializationData(job, fileRecords, format);
    return options;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) MarcPermissiveStreamReader(org.marc4j.MarcPermissiveStreamReader) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) MarcWriter(org.marc4j.MarcWriter) Record(org.marc4j.marc.Record) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) MarcXmlWriter(org.marc4j.MarcXmlWriter)

Aggregations

File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 MarcPermissiveStreamReader (org.marc4j.MarcPermissiveStreamReader)2 MarcWriter (org.marc4j.MarcWriter)2 MarcXmlWriter (org.marc4j.MarcXmlWriter)2 Record (org.marc4j.marc.Record)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 JSONObject (org.json.JSONObject)1