All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.jscancella.reader.internal.MetadataReader Maven / Gradle / Ivy

Go to download

This is a software library intended to support the creation, manipulation, and validation of "bags" from the bagit specification. It currently supports version 0.93 through 1.0.

There is a newer version: 5.2
Show newest version
package com.github.jscancella.reader.internal;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.AbstractMap.SimpleImmutableEntry;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.jscancella.exceptions.InvalidBagMetadataException;

/**
 * This class is responsible for reading and parsing bagit metadata files from the filesystem
 */
public enum MetadataReader {;//using enum to enforce singleton
  private static final Logger logger = LoggerFactory.getLogger(MetadataReader.class);
  private static final ResourceBundle messages = ResourceBundle.getBundle("MessageBundle");

  /**
   * Reads the bag metadata file (bag-info.txt or package-info.txt) and returns it.
   * 
   * @param rootDir the root directory of the bag
   * @param encoding the encoding of the bagit.txt file
   * @return the bag-info.txt (metadata) information
   * 
   * @throws IOException if there is a problem reading a file
   * @throws InvalidBagMetadataException if the metadata file does not conform to the bagit spec
   */
  public static List> readBagMetadata(final Path rootDir, final Charset encoding) throws IOException{
    logger.info(messages.getString("attempting_read_metadata"));
    List> metadata = new ArrayList<>();
    
    final Path bagInfoFile = rootDir.resolve("bag-info.txt");
    if(Files.exists(bagInfoFile)){
      logger.debug(messages.getString("found_metadata_file"), bagInfoFile);
      metadata = KeyValueReader.readKeyValuesFromFile(bagInfoFile, ":", encoding);
    }
    final Path packageInfoFile = rootDir.resolve("package-info.txt"); //only exists in versions 0.93 - 0.95
    if(Files.exists(packageInfoFile)){
      logger.debug(messages.getString("found_metadata_file"), packageInfoFile);
      metadata = KeyValueReader.readKeyValuesFromFile(packageInfoFile, ":", encoding);
    }
    
    return metadata;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy