bs = mdBundle.createBitstream(new PackageUtils.UnclosableInputStream(zip));
bs.setName(fname);
bs.setSource(fname);
// Get magic bitstream format to identify manifest.
BitstreamFormat manifestFormat = null;
manifestFormat = PackageUtils.findOrCreateBitstreamFormat(context,
MANIFEST_BITSTREAM_FORMAT, "application/xml",
MANIFEST_BITSTREAM_FORMAT+" package manifest");
bs.setFormat(manifestFormat);
manifest = METSManifest.create(bs.retrieve(), validate);
}
else
{
manifest = METSManifest.create(new PackageUtils.UnclosableInputStream(zip), validate);
continue;
}
}
else
{
// we need to create the bundle only the first time
if (contentBundle == null)
{
contentBundle = item.createBundle(Constants.CONTENT_BUNDLE_NAME);
}
bs = contentBundle.createBitstream(new PackageUtils.UnclosableInputStream(zip));
bs.setSource(fname);
bs.setName(fname);
}
packageFiles.add(fname);
bs.setSource(fname);
bs.update();
}
zip.close();
if (manifest == null)
throw new PackageValidationException("No METS Manifest found (filename="+MANIFEST_FILE+"). Package is unacceptable.");
// initial sanity checks on manifest (in subclass)
checkManifest(manifest);
/* 2. Grovel a file list out of METS Manifest and compare
* it to the files in package, as an integrity test.
*/
List manifestContentFiles = manifest.getContentFiles();
// Compare manifest files with the ones found in package:
// a. Start with content files (mentioned in <fileGrp>s)
HashSet missingFiles = new HashSet();
for (Iterator mi = manifestContentFiles.iterator(); mi.hasNext(); )
{
// First locate corresponding Bitstream and make
// map of Bitstream to <file> ID.
Element mfile = (Element)mi.next();
String mfileId = mfile.getAttributeValue("ID");
if (mfileId == null)
throw new PackageValidationException("Invalid METS Manifest: file element without ID attribute.");
String path = METSManifest.getFileName(mfile);
Bitstream bs = contentBundle.getBitstreamByName(path);
if (bs == null)
{
log.warn("Cannot find bitstream for filename=\""+path+
"\", skipping it..may cause problems later.");
missingFiles.add(path);
}
else
{
fileIdToBitstream.put(mfileId, bs);
// Now that we're done using Name to match to <file>,
// set default bitstream Name to last path element;
// Zip entries all have '/' pathname separators
// NOTE: set default here, hopefully crosswalk of
// a bitstream techMD section will override it.
String fname = bs.getName();
int lastSlash = fname.lastIndexOf('/');
if (lastSlash >= 0 && lastSlash+1 < fname.length())
bs.setName(fname.substring(lastSlash+1));
// Set Default bitstream format:
// 1. attempt to guess from MIME type
// 2. if that fails, guess from "name" extension.
String mimeType = mfile.getAttributeValue("MIMETYPE");
BitstreamFormat bf = (mimeType == null) ? null :
BitstreamFormat.findByMIMEType(context, mimeType);
if (bf == null)
bf = FormatIdentifier.guessFormat(context, bs);
bs.setFormat(bf);