Original 2.4
[marc4j.git] / src / org / marc4j / MarcDirStreamReader.java
1 // $Id: MarcDirStreamReader.java,v 1.2 2008/10/17 06:47:06 haschart Exp $\r
2 /**\r
3  * Copyright (C) 2004 Bas Peters\r
4  *\r
5  * This file is part of MARC4J\r
6  *\r
7  * MARC4J is free software; you can redistribute it and/or\r
8  * modify it under the terms of the GNU Lesser General Public \r
9  * License as published by the Free Software Foundation; either \r
10  * version 2.1 of the License, or (at your option) any later version.\r
11  *\r
12  * MARC4J is distributed in the hope that it will be useful,\r
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r
15  * Lesser General Public License for more details.\r
16  *\r
17  * You should have received a copy of the GNU Lesser General Public \r
18  * License along with MARC4J; if not, write to the Free Software\r
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
20  *\r
21  */\r
22 \r
23 package org.marc4j;\r
24 \r
25 import java.io.File;\r
26 import java.io.FileInputStream;\r
27 import java.io.FileNotFoundException;\r
28 import java.io.FilenameFilter;\r
29 \r
30 import org.marc4j.MarcPermissiveStreamReader;\r
31 import org.marc4j.MarcReader;\r
32 import org.marc4j.marc.Record;\r
33 \r
34 /**\r
35  * <p>\r
36  * A Marc reader which instead of handling a single file of MARC records\r
37  * it handles a directory, which it will scan for all .mrc files, and \r
38  * iterate through all of them in turn.\r
39  * </p>\r
40  *  \r
41  * @author Robert Haschart\r
42  * @version $Revision: 1.2 $\r
43  */\r
44 \r
45 public class MarcDirStreamReader implements MarcReader\r
46 {\r
47     File list[];\r
48     MarcReader curFileReader;\r
49     int curFileNum;\r
50     boolean permissive;\r
51     boolean convertToUTF8;\r
52     String defaultEncoding;\r
53     \r
54     /**\r
55      * Constructs an instance that traverses the directory specified in the parameter.\r
56      * \r
57      * @param dirName - The path of the directory from which to read all of the .mrc files\r
58      */     \r
59     public MarcDirStreamReader(String dirName)\r
60     {\r
61         File dir = new File(dirName);\r
62         init(dir, false, false, null);\r
63     }\r
64     \r
65     /**\r
66      * Constructs an instance that traverses the directory specified in the parameter.\r
67      * \r
68      * @param dir - The path of the directory from which to read all of the .mrc files\r
69      */     \r
70     public MarcDirStreamReader(File dir)\r
71     {\r
72         init(dir, false, false, null);\r
73     }\r
74 \r
75     /**\r
76      * Constructs an instance that traverses the directory specified in the parameter.\r
77      * Takes the values passed in for permissive and convertToUTF8 and passes them on \r
78      * to each of the MarcPermissiveStreamReader that it creates.\r
79      * \r
80      * @param dirName - The path of the directory from which to read all of the .mrc files\r
81      * @param permissive - Set to true to specify that reader should try to handle and recover \r
82      *                     from errors in the input.\r
83      * @param convertToUTF8 - Set to true to specify that reader should convert the records\r
84      *                                            being read to UTF-8 encoding as they are being read.\r
85      */     \r
86     public MarcDirStreamReader(String dirName, boolean permissive, boolean convertToUTF8)\r
87     {\r
88         File dir = new File(dirName);\r
89         init(dir, permissive, convertToUTF8, null);\r
90     }\r
91     \r
92     /**\r
93      * Constructs an instance that traverses the directory specified in the parameter.\r
94      * Takes the values passed in for permissive and convertToUTF8 and passes them on \r
95      * to each of the MarcPermissiveStreamReader that it creates.\r
96      * \r
97      * @param dir - The path of the directory from which to read all of the .mrc files\r
98      * @param permissive - Set to true to specify that reader should try to handle and recover \r
99      *                     from errors in the input.\r
100      * @param convertToUTF8 - Set to true to specify that reader should convert the records\r
101      *                                            being read to UTF-8 encoding as they are being read.\r
102      */     \r
103     public MarcDirStreamReader(File dir, boolean permissive, boolean convertToUTF8)\r
104     {\r
105         init(dir, permissive, convertToUTF8, null);\r
106     }\r
107 \r
108     /**\r
109      * Constructs an instance that traverses the directory specified in the parameter.\r
110      * Takes the values passed in for permissive and convertToUTF8 and passes them on \r
111      * to each of the MarcPermissiveStreamReader that it creates.\r
112      * \r
113      * @param dirName - The path of the directory from which to read all of the .mrc files\r
114      * @param permissive - Set to true to specify that reader should try to handle and recover \r
115      *                     from errors in the input.\r
116      * @param convertToUTF8 - Set to true to specify that reader should convert the records\r
117      *                                            being read to UTF-8 encoding as they are being read.\r
118      * @param defaultEncoding - Specifies the character encoding that the records being read\r
119      *                                                  are presumed to be in..\r
120      */     \r
121     public MarcDirStreamReader(String dirName, boolean permissive, boolean convertToUTF8, String defaultEncoding)\r
122     {\r
123         File dir = new File(dirName);\r
124         init(dir, permissive, convertToUTF8, defaultEncoding);\r
125     }\r
126     \r
127     /**\r
128      * Constructs an instance that traverses the directory specified in the parameter.\r
129      * Takes the values passed in for permissive and convertToUTF8 and passes them on \r
130      * to each of the MarcPermissiveStreamReader that it creates.\r
131      * \r
132      * @param dir - The path of the directory from which to read all of the .mrc files\r
133      * @param permissive - Set to true to specify that reader should try to handle and recover \r
134      *                     from errors in the input.\r
135      * @param convertToUTF8 - Set to true to specify that reader should convert the records\r
136      *                                            being read to UTF-8 encoding as they are being read.\r
137      * @param defaultEncoding - Specifies the character encoding that the records being read\r
138      *                                                  are presumed to be in..\r
139      */     \r
140     public MarcDirStreamReader(File dir, boolean permissive, boolean convertToUTF8, String defaultEncoding)\r
141     {\r
142         init(dir, permissive, convertToUTF8, defaultEncoding);\r
143     }\r
144 \r
145     private void init(File dir, boolean permissive, boolean convertToUTF8, String defaultEncoding)\r
146     {\r
147         FilenameFilter filter = new FilenameFilter()\r
148         {\r
149             public boolean accept(File dir, String name)\r
150             {\r
151                 return(name.endsWith("mrc"));\r
152             }\r
153         };\r
154         this.permissive = permissive;\r
155         this.convertToUTF8 = convertToUTF8;\r
156         list = dir.listFiles(filter);\r
157         java.util.Arrays.sort(list);\r
158         curFileNum = 0;\r
159         curFileReader = null;\r
160         this.defaultEncoding = defaultEncoding;\r
161     }\r
162     \r
163     /**\r
164      * Returns true if the iteration has more records, false otherwise.\r
165      */\r
166     public boolean hasNext()\r
167     {\r
168         if (curFileReader == null || curFileReader.hasNext() == false)\r
169         {\r
170             nextFile();\r
171         }\r
172         return (curFileReader == null ? false : curFileReader.hasNext());\r
173     }\r
174 \r
175     private void nextFile()\r
176     {\r
177         if (curFileNum != list.length)\r
178         {\r
179             try\r
180             {\r
181                 System.err.println("Switching to input file: "+ list[curFileNum]);\r
182                 if (defaultEncoding != null)\r
183                 {\r
184                     curFileReader = new MarcPermissiveStreamReader(new FileInputStream(list[curFileNum++]), permissive, convertToUTF8, defaultEncoding);\r
185                 }\r
186                 else\r
187                 {\r
188                     curFileReader = new MarcPermissiveStreamReader(new FileInputStream(list[curFileNum++]), permissive, convertToUTF8);\r
189                 }\r
190             }\r
191             catch (FileNotFoundException e)\r
192             {\r
193                 nextFile();\r
194             }\r
195         }\r
196         else \r
197         {\r
198             curFileReader = null;\r
199         }\r
200     }\r
201 \r
202     /**\r
203      * Returns the next record in the iteration.\r
204      * \r
205      * @return Record - the record object\r
206      */\r
207     public Record next()\r
208     {\r
209         if (curFileReader == null || curFileReader.hasNext() == false)\r
210         {\r
211             nextFile();\r
212         }\r
213         return (curFileReader == null ? null : curFileReader.next());\r
214     }\r
215 \r
216 }\r