2929 */
3030public class BagWriter {
3131 private static final Logger logger = LoggerFactory .getLogger (BagWriter .class );
32+ private static final Version VERSION_0_98 = new Version (0 , 98 );
33+ private static final Version VERSION_0_95 = new Version (0 , 95 );
3234
3335 private BagWriter (){}
3436
@@ -47,23 +49,23 @@ public static void write(Bag bag, Path outputDir) throws IOException, NoSuchAlgo
4749 writePayloadManifests (bag .getPayLoadManifests (), bagitDir , bag .getRootDir (), bag .getFileEncoding ());
4850
4951 if (bag .getMetadata ().size () > 0 ){
50- writeBagitInfoFile (bag .getMetadata (), bagitDir , bag .getFileEncoding ());
52+ writeBagitInfoFile (bag .getMetadata (), bag . getVersion (), bagitDir , bag .getFileEncoding ());
5153 }
5254 if (bag .getItemsToFetch ().size () > 0 ){
5355 writeFetchFile (bag .getItemsToFetch (), bagitDir , bag .getFileEncoding ());
5456 }
5557 if (bag .getTagManifests ().size () > 0 ){
56- Set <Manifest > updatedTagManifests = updateTagManifests (bag );
58+ writeAdditionalTagPayloadFiles (bag .getTagManifests (), bagitDir , bag .getRootDir ());
59+ Set <Manifest > updatedTagManifests = updateTagManifests (bag , outputDir );
5760 bag .setTagManifests (updatedTagManifests );
5861 writeTagManifests (updatedTagManifests , bagitDir , bag .getRootDir (), bag .getFileEncoding ());
59- writeAdditionalTagPayloadFiles (updatedTagManifests , bagitDir , bag .getRootDir ());
6062 }
6163 }
6264
6365 protected static Path writeVersionDependentPayloadFiles (Bag bag , Path outputDir ) throws IOException {
6466 Path bagitDir = outputDir ;
6567 //@Incubating
66- if (bag .getVersion (). compareTo ( new Version ( 0 , 98 )) > = 0 ){
68+ if (VERSION_0_98 . compareTo ( bag .getVersion ()) < = 0 ){
6769 bagitDir = outputDir .resolve (".bagit" );
6870 Files .createDirectories (bagitDir );
6971 writePayloadFiles (bag .getPayLoadManifests (), outputDir , bag .getRootDir ());
@@ -85,8 +87,9 @@ protected static Path writeVersionDependentPayloadFiles(Bag bag, Path outputDir)
8587 * @throws IOException if there was a problem writing the file
8688 */
8789 public static void writeBagitFile (Version version , String encoding , Path outputDir ) throws IOException {
88- logger .debug ("Writing bagit.txt file to [{}]" , outputDir );
8990 Path bagitPath = outputDir .resolve ("bagit.txt" );
91+ logger .debug ("Writing bagit.txt file to [{}]" , outputDir );
92+
9093
9194 String firstLine = "BagIt-Version : " + version + System .lineSeparator ();
9295 logger .debug ("Writing line [{}] to [{}]" , firstLine , bagitPath );
@@ -134,29 +137,24 @@ public static void writePayloadManifests(Set<Manifest> manifests, Path outputDir
134137 writeManifests (manifests , outputDir , bagitRootDir , "manifest-" , charsetName );
135138 }
136139
137- protected static Set <Manifest > updateTagManifests (Bag bag ) throws NoSuchAlgorithmException , IOException {
138- Set <Path > tagFilePaths = new HashSet <>();
139-
140- for (Manifest payloadManifest : bag .getPayLoadManifests ()){
141- tagFilePaths .add (bag .getRootDir ().resolve ("manifest-" + payloadManifest .getAlgorithm ().getBagitName () + ".txt" ));
142- }
143-
144- tagFilePaths .add (bag .getRootDir ().resolve ("bag-info.txt" ));
145- tagFilePaths .add (bag .getRootDir ().resolve ("package-info.txt" ));
146- tagFilePaths .add (bag .getRootDir ().resolve ("fetch.txt" ));
147- tagFilePaths .add (bag .getRootDir ().resolve ("bagit.txt" ));
140+ protected static Set <Manifest > updateTagManifests (Bag bag , Path newBagRootDir ) throws NoSuchAlgorithmException , IOException {
141+ Set <Manifest > newManifests = new HashSet <>();
148142
149143 for (Manifest tagManifest : bag .getTagManifests ()){
150- Set <Path > pathsToUpdate = new HashSet <>(tagManifest .getFileToChecksumMap ().keySet ());
151- pathsToUpdate .retainAll (tagFilePaths );
152- for (Path pathToUpdate : pathsToUpdate ){
144+ Manifest newManifest = new Manifest (tagManifest .getAlgorithm ());
145+
146+ for (Path originalPath : tagManifest .getFileToChecksumMap ().keySet ()){
147+ Path relativePath = bag .getRootDir ().relativize (originalPath );
148+ Path pathToUpdate = newBagRootDir .resolve (relativePath );
153149 MessageDigest messageDigest = MessageDigest .getInstance (tagManifest .getAlgorithm ().getMessageDigestName ());
154150 String newChecksum = Hasher .hash (Files .newInputStream (pathToUpdate ), messageDigest );
155- tagManifest .getFileToChecksumMap ().put (pathToUpdate , newChecksum );
151+ newManifest .getFileToChecksumMap ().put (pathToUpdate , newChecksum );
156152 }
153+
154+ newManifests .add (newManifest );
157155 }
158156
159- return bag . getTagManifests () ;
157+ return newManifests ;
160158 }
161159
162160 /**
@@ -208,9 +206,12 @@ protected static void writeAdditionalTagPayloadFiles(Set<Manifest> manifests, Pa
208206 * @param charsetName the name of the encoding for the file
209207 * @throws IOException if there was a problem writing a file
210208 */
211- public static void writeBagitInfoFile (List <Pair <String , String >> metadata , Path outputDir , String charsetName ) throws IOException {
212- logger .debug ("Writing bag-info.txt to [{}]" , outputDir );
209+ public static void writeBagitInfoFile (List <Pair <String , String >> metadata , Version version , Path outputDir , String charsetName ) throws IOException {
213210 Path bagInfoFilePath = outputDir .resolve ("bag-info.txt" );
211+ if (VERSION_0_95 .compareTo (version ) >= 0 ){
212+ bagInfoFilePath = outputDir .resolve ("package-info.txt" );
213+ }
214+ logger .debug ("Writing {} to [{}]" , bagInfoFilePath .getFileName (), outputDir );
214215
215216 Files .deleteIfExists (bagInfoFilePath );
216217
0 commit comments