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.MarcException;
\r
15 import org.marc4j.MarcPermissiveStreamReader;
\r
16 import org.marc4j.MarcReader;
\r
17 import org.marc4j.MarcStreamWriter;
\r
18 import org.marc4j.MarcWriter;
\r
19 import org.marc4j.marc.Record;
\r
21 public class PermissiveReaderExample
\r
25 * This test program demonstrates the use of the MarcPermissiveStreamReader
\r
26 * to read Marc records, with the permissive setting turned on. It also
\r
27 * demonstrates the capability of printing out the error messages that are
\r
28 * generated when the MarcPermissiveStreamReader encounters records with
\r
29 * structural error or encoding errors.
\r
31 * When run in verbose mode, (by passing -v as the first parameter) the
\r
32 * program will display the entire record highlighting the lines in the
\r
33 * record that have errors that the permissive reader was able to detect
\r
34 * and make an attempt at correcting. Following that the program will
\r
35 * list all of the errors that it found in the record.
\r
37 * When run in verbose mode as described above, the program is useful for
\r
38 * validating records.
\r
40 * Shown below is the output generated when the program is run on the file
\r
41 * error.mrc found in the resources sub-directory in the samples directory:
\r
43 * Fatal Exception: error parsing data field for tag: 250 with data:
\1fa1st ed.
\r
44 * Typo : Record terminator character not found at end of record length --- [ n/a : n/a ]
\r
45 * Typo : Record terminator appears after stated record length, reading extra bytes --- [ n/a : n/a ]
\r
46 * Minor Error : Field length found in record different from length stated in the directory. --- [ n/a : n/a ]
\r
47 * LEADER 00715cam a2200205 a 4500
\r
49 * 005 20030616111422.0
\r
50 * 008 020805s2002 nyu j 000 1 eng
\r
52 * 020 $a0786816155 (pbk.)
\r
53 * 040 $aDLC$cDLC$dDLC
\r
54 * 100 1 $aChabon, Michael.
\r
55 * 245 10$aSummerland /$cMichael Chabon.
\r
57 * 260 $aNew York :$bMiramax Books/Hyperion Books for Children,$cc2002.
\r
58 * 300 $a500 p. ;$c22 cm.
\r
59 * 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
61 * 650 1$aBaseball$vFiction.
\r
62 * 650 1$aMagic$vFiction.
\r
64 public static void main(String[] args)
\r
66 PrintStream out = System.out;
\r
67 boolean verbose = Boolean.parseBoolean(System.getProperty("marc.verbose"));
\r
68 boolean veryverbose = Boolean.parseBoolean(System.getProperty("marc.verbose"));
\r
69 if (args[0].equals("-v"))
\r
72 String newArgs[] = new String[args.length-1];
\r
73 System.arraycopy(args, 1, newArgs, 0, args.length-1);
\r
76 if (args[0].equals("-vv"))
\r
80 String newArgs[] = new String[args.length-1];
\r
81 System.arraycopy(args, 1, newArgs, 0, args.length-1);
\r
84 String fileStr = args[0];
\r
85 File file = new File(fileStr);
\r
86 MarcReader readerNormal = null;
\r
87 MarcReader readerPermissive = null;
\r
88 boolean to_utf_8 = true;
\r
92 OutputStream patchedRecStream = null;
\r
93 MarcWriter patchedRecs = null;
\r
94 ErrorHandler errorHandler = new ErrorHandler();
\r
97 inNorm = new FileInputStream(file);
\r
98 readerNormal = new MarcPermissiveStreamReader(inNorm, false, to_utf_8);
\r
99 inPerm = new FileInputStream(file);
\r
100 readerPermissive = new MarcPermissiveStreamReader(inPerm, errorHandler, to_utf_8, "BESTGUESS");
\r
102 catch (FileNotFoundException e)
\r
104 // TODO Auto-generated catch block
\r
105 e.printStackTrace();
\r
107 if (args.length > 1)
\r
111 patchedRecStream = new FileOutputStream(new File(args[1]));
\r
112 patchedRecs = new MarcStreamWriter(patchedRecStream);
\r
114 catch (FileNotFoundException e)
\r
116 // TODO Auto-generated catch block
\r
117 e.printStackTrace();
\r
120 while (readerNormal.hasNext() && readerPermissive.hasNext())
\r
124 recPerm = readerPermissive.next();
\r
125 String strPerm = recPerm.toString();
\r
127 recNorm = readerNormal.next();
\r
129 catch (MarcException me)
\r
133 out.println("Fatal Exception: "+ me.getMessage());
\r
134 dumpErrors(out, errorHandler);
\r
135 showDiffs(out, null, strPerm);
\r
136 out.println("-------------------------------------------------------------------------------------");
\r
140 String strNorm = recNorm.toString();
\r
141 if (!strNorm.equals(strPerm))
\r
145 dumpErrors(out, errorHandler);
\r
146 showDiffs(out, strNorm, strPerm);
\r
147 out.println("-------------------------------------------------------------------------------------");
\r
150 if (patchedRecs != null)
\r
152 patchedRecs.write(recPerm);
\r
155 else if (errorHandler.hasErrors())
\r
159 out.println("Results identical, but errors reported");
\r
160 dumpErrors(out, errorHandler);
\r
161 showDiffs(out, strNorm, strPerm);
\r
162 out.println("-------------------------------------------------------------------------------------");
\r
164 if (patchedRecs != null)
\r
166 patchedRecs.write(recPerm);
\r
169 else if (veryverbose)
\r
171 showDiffs(out, strNorm, strPerm);
\r
177 public static void showDiffs(PrintStream out, String strNorm, String strPerm)
\r
179 if (strNorm != null)
\r
181 String normLines[] = strNorm.split("\n");
\r
182 String permLines[] = strPerm.split("\n");
\r
183 if (normLines.length == permLines.length)
\r
185 for (int i = 0; i < normLines.length; i++)
\r
187 if (normLines[i].equals(permLines[i]))
\r
189 out.println(" " + normLines[i]);
\r
193 out.println(" < " + normLines[i]);
\r
194 out.println(" > " + permLines[i]);
\r
201 String permLines[] = strPerm.split("\n");
\r
202 for (int i = 0; i < permLines.length; i++)
\r
204 out.println(" " + permLines[i]);
\r
210 @SuppressWarnings("unchecked")
\r
211 public static void dumpErrors(PrintStream out, ErrorHandler errorHandler)
\r
213 List<Object> errors = errorHandler.getErrors();
\r
214 if (errors != null)
\r
216 Iterator<Object> iter = errors.iterator();
\r
217 while (iter.hasNext())
\r
219 Object error = iter.next();
\r
220 out.println(error.toString());
\r