Search in sources :

Example 21 with Version

use of gov.loc.repository.bagit.domain.Version in project bagit-java by LibraryOfCongress.

the class BagReader method read.

/**
 * Read the bag from the filesystem and create a bag object
 *
 * @param rootDir the root directory of the bag
 * @throws IOException if there is a problem reading a file
 * @return a {@link Bag} object representing a bag on the filesystem
 *
 * @throws UnparsableVersionException If there is a problem parsing the bagit version
 * @throws MaliciousPathException if there is path that is referenced in the manifest or fetch file that is outside the bag root directory
 * @throws InvalidBagMetadataException if the metadata or bagit.txt file does not conform to the bagit spec
 * @throws UnsupportedAlgorithmException if the manifest uses a algorithm that isn't supported
 * @throws InvalidBagitFileFormatException if the manifest or fetch file is not formatted properly
 */
public Bag read(final Path rootDir) throws IOException, UnparsableVersionException, MaliciousPathException, InvalidBagMetadataException, UnsupportedAlgorithmException, InvalidBagitFileFormatException {
    final Bag bag = new Bag();
    // @Incubating
    Path bagitDir = rootDir.resolve(".bagit");
    if (!Files.exists(bagitDir)) {
        bagitDir = rootDir;
    }
    bag.setRootDir(rootDir);
    final Path bagitFile = bagitDir.resolve("bagit.txt");
    final SimpleImmutableEntry<Version, Charset> bagitInfo = BagitTextFileReader.readBagitTextFile(bagitFile);
    bag.setVersion(bagitInfo.getKey());
    bag.setFileEncoding(bagitInfo.getValue());
    ManifestReader.readAllManifests(nameMapping, bagitDir, bag);
    bag.getMetadata().addAll(MetadataReader.readBagMetadata(bagitDir, bag.getFileEncoding()));
    final Path fetchFile = bagitDir.resolve("fetch.txt");
    if (Files.exists(fetchFile)) {
        bag.getItemsToFetch().addAll(FetchReader.readFetch(fetchFile, bag.getFileEncoding(), bag.getRootDir()));
    }
    return bag;
}
Also used : Path(java.nio.file.Path) Version(gov.loc.repository.bagit.domain.Version) Bag(gov.loc.repository.bagit.domain.Bag) Charset(java.nio.charset.Charset)

Example 22 with Version

use of gov.loc.repository.bagit.domain.Version in project bagit-java by LibraryOfCongress.

the class BagitSuiteComplanceTest method testTagFileContents.

private void testTagFileContents(final Bag originalBag, final Path newBagDir) throws IOException {
    Path original = originalBag.getRootDir().resolve("bagit.txt");
    Path newFile = newBagDir.resolve("bagit.txt");
    assertTrue("bagit.txt files differ", compareFileContents(original, newFile, StandardCharsets.UTF_8));
    if (originalBag.getVersion().isSameOrOlder(new Version(0, 95))) {
        original = originalBag.getRootDir().resolve("package-info.txt");
        newFile = newBagDir.resolve("package-info.txt");
        assertTrue(original + " differs from " + newFile, compareFileContents(original, newFile, originalBag.getFileEncoding()));
    } else {
        if (Files.exists(originalBag.getRootDir().resolve("bag-info.txt"))) {
            original = originalBag.getRootDir().resolve("bag-info.txt");
            newFile = newBagDir.resolve("bag-info.txt");
            assertTrue(original + " differs from " + newFile, compareFileContents(original, newFile, originalBag.getFileEncoding()));
        }
    }
}
Also used : Path(java.nio.file.Path) Version(gov.loc.repository.bagit.domain.Version)

Example 23 with Version

use of gov.loc.repository.bagit.domain.Version in project bagit-java by LibraryOfCongress.

the class BagLinter method lintBag.

/**
 * The BagIt specification is very flexible in what it allows which leads to situations
 * where something may be technically allowed, but should be discouraged.
 * This method checks a bag for potential problems, or other items that are allowed but discouraged.
 * This <strong>does not</strong> validate a bag. See {@link BagVerifier} instead.
 *
 * @param rootDir the root directory of the bag
 * @param warningsToIgnore any {@link BagitWarning} to ignore when linting
 *
 * @return a set of {@link BagitWarning} detailing all items that should be fixed.
 *
 * @throws InvalidBagMetadataException if the bag metadata does not conform to the bagit specification
 * @throws UnparsableVersionException if there is an error reading the bagit version
 * @throws IOException if there was an error reading a file
 */
public static Set<BagitWarning> lintBag(final Path rootDir, final Collection<BagitWarning> warningsToIgnore) throws IOException, UnparsableVersionException, InvalidBagMetadataException, InvalidBagitFileFormatException {
    final Set<BagitWarning> warnings = new HashSet<>();
    // @Incubating
    Path bagitDir = rootDir.resolve(".bagit");
    if (!Files.exists(bagitDir)) {
        bagitDir = rootDir;
    }
    final Path bagitFile = bagitDir.resolve("bagit.txt");
    checkForExtraLines(bagitFile, warnings, warningsToIgnore);
    final SimpleImmutableEntry<Version, Charset> bagitInfo = BagitTextFileReader.readBagitTextFile(bagitFile);
    logger.info(messages.getString("checking_encoding_problems"));
    EncodingChecker.checkEncoding(bagitInfo.getValue(), warnings, warningsToIgnore);
    logger.info(messages.getString("checking_latest_version"));
    VersionChecker.checkVersion(bagitInfo.getKey(), warnings, warningsToIgnore);
    logger.info(messages.getString("checking_manifest_problems"));
    ManifestChecker.checkManifests(bagitDir, bagitInfo.getValue(), warnings, warningsToIgnore);
    logger.info(messages.getString("checking_metadata_problems"));
    MetadataChecker.checkBagMetadata(bagitDir, bagitInfo.getValue(), warnings, warningsToIgnore);
    return warnings;
}
Also used : Path(java.nio.file.Path) Version(gov.loc.repository.bagit.domain.Version) Charset(java.nio.charset.Charset) HashSet(java.util.HashSet)

Example 24 with Version

use of gov.loc.repository.bagit.domain.Version in project bagit-java by LibraryOfCongress.

the class BagLinter method checkForExtraLines.

private static void checkForExtraLines(final Path bagitFile, final Collection<BagitWarning> warnings, final Collection<BagitWarning> warningsToIgnore) throws InvalidBagMetadataException, IOException, UnparsableVersionException {
    if (warningsToIgnore.contains(BagitWarning.EXTRA_LINES_IN_BAGIT_FILES)) {
        logger.debug(messages.getString("skipping_check_extra_lines"));
        return;
    }
    logger.debug(messages.getString("checking_extra_lines"));
    final List<SimpleImmutableEntry<String, String>> pairs = KeyValueReader.readKeyValuesFromFile(bagitFile, ":", StandardCharsets.UTF_8);
    for (final SimpleImmutableEntry<String, String> pair : pairs) {
        if ("BagIt-Version".equals(pair.getKey())) {
            final Version version = BagitTextFileReader.parseVersion(pair.getValue());
            // versions before 1.0 specified it must be exactly 2 lines
            if (pairs.size() > 2 && version.isOlder(VERSION_1_0)) {
                logger.warn(messages.getString("extra_lines_warning"), pairs.size());
                warnings.add(BagitWarning.EXTRA_LINES_IN_BAGIT_FILES);
            }
        }
    }
}
Also used : SimpleImmutableEntry(java.util.AbstractMap.SimpleImmutableEntry) Version(gov.loc.repository.bagit.domain.Version)

Aggregations

Version (gov.loc.repository.bagit.domain.Version)24 Path (java.nio.file.Path)19 Test (org.junit.Test)17 Bag (gov.loc.repository.bagit.domain.Bag)11 PrivateConstructorTest (gov.loc.repository.bagit.PrivateConstructorTest)7 File (java.io.File)6 Manifest (gov.loc.repository.bagit.domain.Manifest)4 Charset (java.nio.charset.Charset)4 HashSet (java.util.HashSet)3 SimpleImmutableEntry (java.util.AbstractMap.SimpleImmutableEntry)2 Metadata (gov.loc.repository.bagit.domain.Metadata)1 InvalidBagitFileFormatException (gov.loc.repository.bagit.exceptions.InvalidBagitFileFormatException)1 MissingBagitFileException (gov.loc.repository.bagit.exceptions.MissingBagitFileException)1