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