11package gov .loc .repository .bagit .reader ;
22
33import java .io .BufferedReader ;
4- import java .io .File ;
5- import java .io .FilenameFilter ;
64import java .io .IOException ;
75import java .net .URL ;
6+ import java .nio .file .DirectoryStream ;
87import java .nio .file .Files ;
9- import java .nio .file .Paths ;
8+ import java .nio .file .Path ;
109import java .util .ArrayList ;
1110import java .util .HashMap ;
1211import java .util .List ;
@@ -48,22 +47,22 @@ public BagReader(BagitAlgorithmNameToSupportedAlgorithmMapping nameMapping){
4847 * @return a {@link Bag} object representing a bag on the filesystem
4948 * @throws UnparsableVersionException If there is a problem parsing the bagit version
5049 */
51- public Bag read (File rootDir ) throws IOException , UnparsableVersionException {
52- File bagitDir = new File ( rootDir , ".bagit" );
53- if (!bagitDir .exists ()){
50+ public Bag read (Path rootDir ) throws IOException , UnparsableVersionException {
51+ Path bagitDir = rootDir . resolve ( ".bagit" );
52+ if (!Files .exists (bagitDir )){
5453 bagitDir = rootDir ;
5554 }
5655
57- File bagitFile = new File ( bagitDir , "bagit.txt" );
56+ Path bagitFile = bagitDir . resolve ( "bagit.txt" );
5857 Bag bag = readBagitTextFile (bagitFile , new Bag ());
5958 bag .setRootDir (rootDir );
6059
6160 bag = readAllManifests (bagitDir , bag );
6261
6362 bag = readBagMetadata (bagitDir , bag );
6463
65- File fetchFile = new File ( bagitDir , "fetch.txt" );
66- if (fetchFile .exists ()){
64+ Path fetchFile = bagitDir . resolve ( "fetch.txt" );
65+ if (Files .exists (fetchFile )){
6766 bag = readFetch (fetchFile , bag );
6867 }
6968
@@ -79,7 +78,7 @@ public Bag read(File rootDir) throws IOException, UnparsableVersionException{
7978 * @throws IOException if there is a problem reading a file
8079 * @throws UnparsableVersionException if there is a problem parsing the bagit version number
8180 */
82- public Bag readBagitTextFile (File bagitFile , Bag bag ) throws IOException , UnparsableVersionException {
81+ public Bag readBagitTextFile (Path bagitFile , Bag bag ) throws IOException , UnparsableVersionException {
8382 logger .debug ("Reading bagit.txt file" );
8483 List <Pair <String , String >> pairs = readKeyValuesFromFile (bagitFile , ":" );
8584
@@ -123,34 +122,33 @@ protected Version parseVersion(String version) throws UnparsableVersionException
123122 * @return a new bag that contains all the manifest(s) information
124123 * @throws IOException if there is a problem reading a file
125124 */
126- public Bag readAllManifests (File rootDir , Bag bag ) throws IOException {
125+ public Bag readAllManifests (Path rootDir , Bag bag ) throws IOException {
127126 logger .info ("Attempting to find and read manifests" );
128127 Bag newBag = new Bag (bag );
129- File [] files = getAllManifestFiles (rootDir );
128+ DirectoryStream < Path > manifests = getAllManifestFiles (rootDir );
130129
131- for ( File file : files ){
132- if (file . getName ().startsWith ("tag " )){
133- logger .debug ("Found tag manifest [{}]" , file );
134- newBag .getTagManifests ().add (readManifest (file , bag .getRootDir ()));
130+ for ( Path path : manifests ){
131+ if (path . getFileName ().toString (). startsWith ("tagmanifest- " )){
132+ logger .debug ("Found tag manifest [{}]" , path );
133+ newBag .getTagManifests ().add (readManifest (path , bag .getRootDir ()));
135134 }
136- else if (file . getName ().startsWith ("manifest" )){
137- logger .debug ("Found payload manifest [{}]" , file );
138- newBag .getPayLoadManifests ().add (readManifest (file , bag .getRootDir ()));
135+ else if (path . getFileName ().toString (). startsWith ("manifest- " )){
136+ logger .debug ("Found payload manifest [{}]" , path );
137+ newBag .getPayLoadManifests ().add (readManifest (path , bag .getRootDir ()));
139138 }
140139 }
141140
142141 return newBag ;
143142 }
144143
145- protected File [] getAllManifestFiles (File rootDir ){
146- File [] files = rootDir .listFiles (new FilenameFilter () {
147- @ Override
148- public boolean accept (File dir , String name ) {
149- return name .matches ("(tag)?manifest\\ -.*\\ .txt" );
144+ protected DirectoryStream <Path > getAllManifestFiles (Path rootDir ) throws IOException {
145+ DirectoryStream .Filter <Path > filter = new DirectoryStream .Filter <Path >() {
146+ public boolean accept (Path file ) throws IOException {
147+ return file .getFileName ().toString ().startsWith ("tagmanifest-" ) || file .getFileName ().toString ().startsWith ("manifest-" );
150148 }
151- }) ;
149+ };
152150
153- return files == null ? new File []{} : files ;
151+ return Files . newDirectoryStream ( rootDir , filter ) ;
154152 }
155153
156154 /**
@@ -160,27 +158,27 @@ public boolean accept(File dir, String name) {
160158 * @return the converted manifest object from the file
161159 * @throws IOException if there is a problem reading a file
162160 */
163- public Manifest readManifest (File manifestFile , File bagRootDir ) throws IOException {
161+ public Manifest readManifest (Path manifestFile , Path bagRootDir ) throws IOException {
164162 logger .debug ("Reading manifest [{}]" , manifestFile );
165- String alg = manifestFile .getName ().split ("[-\\ .]" )[1 ];
163+ String alg = manifestFile .getFileName (). toString ().split ("[-\\ .]" )[1 ];
166164 SupportedAlgorithm algorithm = nameMapping .getMessageDigestName (alg );
167165
168166 Manifest manifest = new Manifest (algorithm );
169167
170- HashMap <File , String > filetToChecksumMap = readChecksumFileMap (manifestFile , bagRootDir );
168+ HashMap <Path , String > filetToChecksumMap = readChecksumFileMap (manifestFile , bagRootDir );
171169 manifest .setFileToChecksumMap (filetToChecksumMap );
172170
173171 return manifest ;
174172 }
175173
176- protected HashMap <File , String > readChecksumFileMap (File manifestFile , File bagRootDir ) throws IOException {
177- HashMap <File , String > map = new HashMap <>();
178- BufferedReader br = Files .newBufferedReader (Paths . get ( manifestFile . toURI ()) );
174+ protected HashMap <Path , String > readChecksumFileMap (Path manifestFile , Path bagRootDir ) throws IOException {
175+ HashMap <Path , String > map = new HashMap <>();
176+ BufferedReader br = Files .newBufferedReader (manifestFile );
179177
180178 String line = br .readLine ();
181179 while (line != null ){
182180 String [] parts = line .split ("\\ s+" , 2 );
183- File file = new File ( bagRootDir , parts [1 ]);
181+ Path file = bagRootDir . resolve ( parts [1 ]);
184182 logger .debug ("Read checksum [{}] and file [{}] from manifest [{}]" , parts [0 ], file , manifestFile );
185183 map .put (file , parts [0 ]);
186184 line = br .readLine ();
@@ -197,18 +195,18 @@ protected HashMap<File, String> readChecksumFileMap(File manifestFile, File bagR
197195 * @return a new bag that contains the bag-info.txt (metadata) information
198196 * @throws IOException if there is a problem reading a file
199197 */
200- public Bag readBagMetadata (File rootDir , Bag bag ) throws IOException {
198+ public Bag readBagMetadata (Path rootDir , Bag bag ) throws IOException {
201199 logger .info ("Attempting to read bag metadata file" );
202200 Bag newBag = new Bag (bag );
203201 List <Pair <String , String >> metadata = new ArrayList <>();
204202
205- File bagInfoFile = new File ( rootDir , "bag-info.txt" );
206- if (bagInfoFile .exists ()){
203+ Path bagInfoFile = rootDir . resolve ( "bag-info.txt" );
204+ if (Files .exists (bagInfoFile )){
207205 logger .debug ("Found [{}] file" , bagInfoFile );
208206 metadata = readKeyValuesFromFile (bagInfoFile , ":" );
209207 }
210- File packageInfoFile = new File ( rootDir , "package-info.txt" ); //onlu exists in versions 0.93 - 0.95
211- if (packageInfoFile .exists ()){
208+ Path packageInfoFile = rootDir . resolve ( "package-info.txt" ); //onlu exists in versions 0.93 - 0.95
209+ if (Files .exists (packageInfoFile )){
212210 logger .debug ("Found [{}] file" , packageInfoFile );
213211 metadata = readKeyValuesFromFile (packageInfoFile , ":" );
214212 }
@@ -226,10 +224,10 @@ public Bag readBagMetadata(File rootDir, Bag bag) throws IOException{
226224 * @return a new bag that contains a list of items to fetch
227225 * @throws IOException if there is a problem reading a file
228226 */
229- public Bag readFetch (File fetchFile , Bag bag ) throws IOException {
227+ public Bag readFetch (Path fetchFile , Bag bag ) throws IOException {
230228 logger .info ("Attempting to read [{}]" , fetchFile );
231229 Bag newBag = new Bag (bag );
232- BufferedReader br = Files .newBufferedReader (Paths . get ( fetchFile . toURI ()) );
230+ BufferedReader br = Files .newBufferedReader (fetchFile );
233231
234232 String line = br .readLine ();
235233 while (line != null ){
@@ -247,9 +245,9 @@ public Bag readFetch(File fetchFile, Bag bag) throws IOException{
247245 return newBag ;
248246 }
249247
250- protected static List <Pair <String , String >> readKeyValuesFromFile (File file , String splitRegex ) throws IOException {
248+ protected static List <Pair <String , String >> readKeyValuesFromFile (Path file , String splitRegex ) throws IOException {
251249 List <Pair <String , String >> keyValues = new ArrayList <>();
252- BufferedReader br = Files .newBufferedReader (Paths . get ( file . toURI ()) );
250+ BufferedReader br = Files .newBufferedReader (file );
253251
254252 String line = br .readLine ();
255253 while (line != null ){
0 commit comments