1 // $Id: MarcDirStreamReader.java,v 1.2 2008/10/17 06:47:06 haschart Exp $
\r
3 * Copyright (C) 2004 Bas Peters
\r
5 * This file is part of MARC4J
\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
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
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
25 import java.io.File;
\r
26 import java.io.FileInputStream;
\r
27 import java.io.FileNotFoundException;
\r
28 import java.io.FilenameFilter;
\r
30 import org.marc4j.MarcPermissiveStreamReader;
\r
31 import org.marc4j.MarcReader;
\r
32 import org.marc4j.marc.Record;
\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
41 * @author Robert Haschart
\r
42 * @version $Revision: 1.2 $
\r
45 public class MarcDirStreamReader implements MarcReader
\r
48 MarcReader curFileReader;
\r
51 boolean convertToUTF8;
\r
52 String defaultEncoding;
\r
55 * Constructs an instance that traverses the directory specified in the parameter.
\r
57 * @param dirName - The path of the directory from which to read all of the .mrc files
\r
59 public MarcDirStreamReader(String dirName)
\r
61 File dir = new File(dirName);
\r
62 init(dir, false, false, null);
\r
66 * Constructs an instance that traverses the directory specified in the parameter.
\r
68 * @param dir - The path of the directory from which to read all of the .mrc files
\r
70 public MarcDirStreamReader(File dir)
\r
72 init(dir, false, false, null);
\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
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
86 public MarcDirStreamReader(String dirName, boolean permissive, boolean convertToUTF8)
\r
88 File dir = new File(dirName);
\r
89 init(dir, permissive, convertToUTF8, null);
\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
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
103 public MarcDirStreamReader(File dir, boolean permissive, boolean convertToUTF8)
\r
105 init(dir, permissive, convertToUTF8, null);
\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
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
121 public MarcDirStreamReader(String dirName, boolean permissive, boolean convertToUTF8, String defaultEncoding)
\r
123 File dir = new File(dirName);
\r
124 init(dir, permissive, convertToUTF8, defaultEncoding);
\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
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
140 public MarcDirStreamReader(File dir, boolean permissive, boolean convertToUTF8, String defaultEncoding)
\r
142 init(dir, permissive, convertToUTF8, defaultEncoding);
\r
145 private void init(File dir, boolean permissive, boolean convertToUTF8, String defaultEncoding)
\r
147 FilenameFilter filter = new FilenameFilter()
\r
149 public boolean accept(File dir, String name)
\r
151 return(name.endsWith("mrc"));
\r
154 this.permissive = permissive;
\r
155 this.convertToUTF8 = convertToUTF8;
\r
156 list = dir.listFiles(filter);
\r
157 java.util.Arrays.sort(list);
\r
159 curFileReader = null;
\r
160 this.defaultEncoding = defaultEncoding;
\r
164 * Returns true if the iteration has more records, false otherwise.
\r
166 public boolean hasNext()
\r
168 if (curFileReader == null || curFileReader.hasNext() == false)
\r
172 return (curFileReader == null ? false : curFileReader.hasNext());
\r
175 private void nextFile()
\r
177 if (curFileNum != list.length)
\r
181 System.err.println("Switching to input file: "+ list[curFileNum]);
\r
182 if (defaultEncoding != null)
\r
184 curFileReader = new MarcPermissiveStreamReader(new FileInputStream(list[curFileNum++]), permissive, convertToUTF8, defaultEncoding);
\r
188 curFileReader = new MarcPermissiveStreamReader(new FileInputStream(list[curFileNum++]), permissive, convertToUTF8);
\r
191 catch (FileNotFoundException e)
\r
198 curFileReader = null;
\r
203 * Returns the next record in the iteration.
\r
205 * @return Record - the record object
\r
207 public Record next()
\r
209 if (curFileReader == null || curFileReader.hasNext() == false)
\r
213 return (curFileReader == null ? null : curFileReader.next());
\r