1 package org.marc4j.samples;
\r
4 import java.io.FileInputStream;
\r
5 import java.io.FileNotFoundException;
\r
6 import java.io.FileOutputStream;
\r
7 import java.io.InputStream;
\r
8 import java.io.OutputStream;
\r
9 import java.io.PrintStream;
\r
10 import java.util.Iterator;
\r
11 import java.util.List;
\r
13 import org.marc4j.ErrorHandler;
\r
14 import org.marc4j.ErrorHandler.Error;
\r
15 import org.marc4j.MarcException;
\r
16 import org.marc4j.MarcPermissiveStreamReader;
\r
17 import org.marc4j.MarcReader;
\r
18 import org.marc4j.MarcStreamWriter;
\r
19 import org.marc4j.MarcWriter;
\r
20 import org.marc4j.marc.Record;
\r
22 public class PermissiveReaderExample
\r
26 * This test program demonstrates the use of the MarcPermissiveStreamReader
\r
27 * to read Marc records, with the permissive setting turned on. It also
\r
28 * demonstrates the capability of printing out the error messages that are
\r
29 * generated when the MarcPermissiveStreamReader encounters records with
\r
30 * structural error or encoding errors.
\r
32 * When run in verbose mode, (by passing -v as the first parameter) the
\r
33 * program will display the entire record highlighting the lines in the
\r
34 * record that have errors that the permissive reader was able to detect
\r
35 * and make an attempt at correcting. Following that the program will
\r
36 * list all of the errors that it found in the record.
\r
38 * When run in verbose mode as described above, the program is useful for
\r
39 * validating records.
\r
41 * Shown below is the output generated when the program is run on the file
\r
42 * error.mrc found in the resources sub-directory in the samples directory:
\r
44 * Fatal Exception: error parsing data field for tag: 250 with data:
\1fa1st ed.
\r
45 * Typo : Record terminator character not found at end of record length --- [ n/a : n/a ]
\r
46 * Typo : Record terminator appears after stated record length, reading extra bytes --- [ n/a : n/a ]
\r
47 * Minor Error : Field length found in record different from length stated in the directory. --- [ n/a : n/a ]
\r
48 * LEADER 00715cam a2200205 a 4500
\r
50 * 005 20030616111422.0
\r
51 * 008 020805s2002 nyu j 000 1 eng
\r
53 * 020 $a0786816155 (pbk.)
\r
54 * 040 $aDLC$cDLC$dDLC
\r
55 * 100 1 $aChabon, Michael.
\r
56 * 245 10$aSummerland /$cMichael Chabon.
\r
58 * 260 $aNew York :$bMiramax Books/Hyperion Books for Children,$cc2002.
\r
59 * 300 $a500 p. ;$c22 cm.
\r
60 * 520 $aEthan Feld, the worst baseball player in the history of the game, finds himself recruited by a 100-year-old scout to help a band of fairies triumph over an ancient enemy.
\r
62 * 650 1$aBaseball$vFiction.
\r
63 * 650 1$aMagic$vFiction.
\r
65 public static void main(String[] args)
\r
67 PrintStream out = System.out;
\r
68 boolean verbose = Boolean.parseBoolean(System.getProperty("marc.verbose"));
\r
69 boolean veryverbose = Boolean.parseBoolean(System.getProperty("marc.verbose"));
\r
70 if (args[0].equals("-v"))
\r
73 String newArgs[] = new String[args.length-1];
\r
74 System.arraycopy(args, 1, newArgs, 0, args.length-1);
\r
77 if (args[0].equals("-vv"))
\r
81 String newArgs[] = new String[args.length-1];
\r
82 System.arraycopy(args, 1, newArgs, 0, args.length-1);
\r
85 String fileStr = args[0];
\r
86 File file = new File(fileStr);
\r
87 MarcReader readerNormal = null;
\r
88 MarcReader readerPermissive = null;
\r
89 boolean to_utf_8 = true;
\r
93 OutputStream patchedRecStream = null;
\r
94 MarcWriter patchedRecs = null;
\r
95 ErrorHandler errorHandler = new ErrorHandler();
\r
98 inNorm = new FileInputStream(file);
\r
99 readerNormal = new MarcPermissiveStreamReader(inNorm, false, to_utf_8);
\r
100 inPerm = new FileInputStream(file);
\r
101 readerPermissive = new MarcPermissiveStreamReader(inPerm, errorHandler, to_utf_8, "BESTGUESS");
\r
103 catch (FileNotFoundException e)
\r
105 // TODO Auto-generated catch block
\r
106 e.printStackTrace();
\r
108 if (args.length > 1)
\r
112 patchedRecStream = new FileOutputStream(new File(args[1]));
\r
113 patchedRecs = new MarcStreamWriter(patchedRecStream);
\r
115 catch (FileNotFoundException e)
\r
117 // TODO Auto-generated catch block
\r
118 e.printStackTrace();
\r
121 while (readerNormal.hasNext() && readerPermissive.hasNext())
\r
125 recPerm = readerPermissive.next();
\r
126 String strPerm = recPerm.toString();
\r
128 recNorm = readerNormal.next();
\r
130 catch (MarcException me)
\r
134 out.println("Fatal Exception: "+ me.getMessage());
\r
135 dumpErrors(out, errorHandler);
\r
136 showDiffs(out, null, strPerm);
\r
137 out.println("-------------------------------------------------------------------------------------");
\r
141 String strNorm = recNorm.toString();
\r
142 if (!strNorm.equals(strPerm))
\r
146 dumpErrors(out, errorHandler);
\r
147 showDiffs(out, strNorm, strPerm);
\r
148 out.println("-------------------------------------------------------------------------------------");
\r
151 if (patchedRecs != null)
\r
153 patchedRecs.write(recPerm);
\r
156 else if (errorHandler.hasErrors())
\r
160 out.println("Results identical, but errors reported");
\r
161 dumpErrors(out, errorHandler);
\r
162 showDiffs(out, strNorm, strPerm);
\r
163 out.println("-------------------------------------------------------------------------------------");
\r
165 if (patchedRecs != null)
\r
167 patchedRecs.write(recPerm);
\r
170 else if (veryverbose)
\r
172 showDiffs(out, strNorm, strPerm);
\r
178 public static void showDiffs(PrintStream out, String strNorm, String strPerm)
\r
180 if (strNorm != null)
\r
182 String normLines[] = strNorm.split("\n");
\r
183 String permLines[] = strPerm.split("\n");
\r
184 if (normLines.length == permLines.length)
\r
186 for (int i = 0; i < normLines.length; i++)
\r
188 if (normLines[i].equals(permLines[i]))
\r
190 out.println(" " + normLines[i]);
\r
194 out.println(" < " + normLines[i]);
\r
195 out.println(" > " + permLines[i]);
\r
202 String permLines[] = strPerm.split("\n");
\r
203 for (int i = 0; i < permLines.length; i++)
\r
205 out.println(" " + permLines[i]);
\r
211 public static void dumpErrors(PrintStream out, ErrorHandler errorHandler)
\r
213 List<Error> errors = errorHandler.getErrors();
\r
214 if (errors != null)
\r
216 Iterator<Error> iter = errors.iterator();
\r
217 while (iter.hasNext())
\r
219 Object error = iter.next();
\r
220 out.println(error.toString());
\r