Original 2.4
[marc4j.git] / src / org / marc4j / samples / PermissiveReaderExample.java
1 package org.marc4j.samples;\r
2 \r
3 import java.io.File;\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
12 \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
20 \r
21 public class PermissiveReaderExample\r
22 {\r
23 \r
24     /**\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
30      * \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
36      *  \r
37      *  When run in verbose mode as described above, the program is useful for\r
38      *  validating records.\r
39      *  \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
42      *  \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
48      *     001 12883376\r
49      *     005 20030616111422.0\r
50      *     008 020805s2002    nyu    j      000 1 eng  \r
51      *     020   $a0786808772\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
56      *     250   $a1st ed.\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
60      *     650  1$aFantasy.\r
61      *     650  1$aBaseball$vFiction.\r
62      *     650  1$aMagic$vFiction.\r
63      */\r
64     public static void main(String[] args)\r
65     {\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
70         {\r
71             verbose = true;\r
72             String newArgs[] = new String[args.length-1];\r
73             System.arraycopy(args, 1, newArgs, 0, args.length-1);\r
74             args = newArgs;\r
75         }\r
76         if (args[0].equals("-vv")) \r
77         {\r
78             verbose = true;\r
79             veryverbose = true;\r
80             String newArgs[] = new String[args.length-1];\r
81             System.arraycopy(args, 1, newArgs, 0, args.length-1);\r
82             args = newArgs;\r
83         }\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
89        \r
90         InputStream inNorm;\r
91         InputStream inPerm;\r
92         OutputStream patchedRecStream = null;\r
93         MarcWriter patchedRecs = null;\r
94         ErrorHandler errorHandler = new ErrorHandler();\r
95         try\r
96         {\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
101         }\r
102         catch (FileNotFoundException e)\r
103         {\r
104             // TODO Auto-generated catch block\r
105             e.printStackTrace();\r
106         }\r
107         if (args.length > 1)\r
108         {\r
109             try\r
110             {\r
111                 patchedRecStream = new FileOutputStream(new File(args[1]));\r
112                 patchedRecs = new MarcStreamWriter(patchedRecStream);\r
113             }\r
114             catch (FileNotFoundException e)\r
115             {\r
116                 // TODO Auto-generated catch block\r
117                 e.printStackTrace();\r
118             }\r
119         }\r
120         while (readerNormal.hasNext() && readerPermissive.hasNext())\r
121         {\r
122             Record recNorm;\r
123             Record recPerm;\r
124             recPerm = readerPermissive.next();\r
125             String strPerm = recPerm.toString();\r
126             try {\r
127                 recNorm = readerNormal.next();\r
128             }\r
129             catch (MarcException me)\r
130             {\r
131                 if (verbose)\r
132                 {\r
133                     out.println("Fatal Exception: "+ me.getMessage());\r
134                     dumpErrors(out, errorHandler);\r
135                     showDiffs(out, null, strPerm);\r
136                     out.println("-------------------------------------------------------------------------------------");\r
137                 }\r
138                 continue;\r
139             }\r
140             String strNorm = recNorm.toString();\r
141             if (!strNorm.equals(strPerm))\r
142             {\r
143                 if (verbose)\r
144                 {\r
145                     dumpErrors(out, errorHandler);\r
146                     showDiffs(out, strNorm, strPerm);\r
147                     out.println("-------------------------------------------------------------------------------------");\r
148                     \r
149                 }\r
150                 if (patchedRecs != null)\r
151                 {\r
152                     patchedRecs.write(recPerm);\r
153                 }\r
154             }\r
155             else if (errorHandler.hasErrors())\r
156             {\r
157                 if (verbose)\r
158                 {\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
163                 }\r
164                 if (patchedRecs != null)\r
165                 {\r
166                     patchedRecs.write(recPerm);\r
167                 }\r
168             }\r
169             else if (veryverbose)\r
170             {\r
171                 showDiffs(out, strNorm, strPerm);                \r
172             }\r
173                 \r
174         }\r
175     }\r
176 \r
177     public static void showDiffs(PrintStream out, String strNorm, String strPerm)\r
178     {\r
179         if (strNorm != null)\r
180         {\r
181             String normLines[] = strNorm.split("\n");\r
182             String permLines[] = strPerm.split("\n");\r
183             if (normLines.length == permLines.length)\r
184             {\r
185                 for (int i = 0; i < normLines.length; i++)\r
186                 {\r
187                     if (normLines[i].equals(permLines[i]))\r
188                     {\r
189                         out.println("   " + normLines[i]);\r
190                     }\r
191                     else\r
192                     {\r
193                         out.println(" < " + normLines[i]);\r
194                         out.println(" > " + permLines[i]);                    \r
195                     }\r
196                 }\r
197             }\r
198         }\r
199         else\r
200         {\r
201             String permLines[] = strPerm.split("\n");\r
202             for (int i = 0; i < permLines.length; i++)\r
203             {\r
204                 out.println("   " + permLines[i]);\r
205             }\r
206         }\r
207 \r
208     }\r
209     \r
210     @SuppressWarnings("unchecked")\r
211         public static void dumpErrors(PrintStream out, ErrorHandler errorHandler)\r
212     {\r
213         List<Object> errors = errorHandler.getErrors();\r
214         if (errors != null) \r
215         {\r
216             Iterator<Object> iter = errors.iterator();\r
217             while (iter.hasNext())\r
218             {\r
219                 Object error = iter.next();\r
220                 out.println(error.toString());\r
221             }\r
222         }\r
223     }\r
224 }\r