001/* 002 * The contents of this file are subject to the license and copyright detailed 003 * in the LICENSE and NOTICE files at the root of the source tree. 004 */ 005package org.duraspace.bagit; 006 007import java.io.File; 008import java.io.FileNotFoundException; 009import java.io.FileReader; 010import java.io.IOException; 011import java.util.Collections; 012import java.util.Map; 013import java.util.Set; 014 015import com.esotericsoftware.yamlbeans.YamlReader; 016 017/** 018 * A convenience class for parsing and storing bagit-config.yml information. The bagit-config.yml represents 019 * user-defined properties to be included in the bag-info.txt. 020 * 021 * @author dbernstein 022 * @since Dec 14, 2016 023 */ 024public class BagConfig { 025 026 public enum AccessTypes { 027 RESTRICTED, INSTITUTION, CONSORTIA; 028 } 029 030 public static final String BAG_INFO_KEY = "bag-info.txt"; 031 032 private static final String APTRUST_INFO_KEY = "aptrust-info.txt"; 033 034 public static final String SOURCE_ORGANIZATION_KEY = "Source-Organization"; 035 036 public static final String ORGANIZATION_ADDRESS_KEY = "Organization-Address"; 037 038 public static final String CONTACT_NAME_KEY = "Contact-Name"; 039 040 public static final String CONTACT_PHONE_KEY = "Contact-Phone"; 041 042 public static final String CONTACT_EMAIL_KEY = "Contact-Email"; 043 044 public static final String EXTERNAL_DESCRIPTION_KEY = "External-Description"; 045 046 public static final String EXTERNAL_IDENTIFIER_KEY = "External-Identifier"; 047 048 public static final String INTERNAL_SENDER_DESCRIPTION_KEY = "Internal-Sender-Description"; 049 050 public static final String INTERNAL_SENDER_IDENTIFIER_KEY = "Internal-Sender-Identifier"; 051 052 public static final String BAGGING_DATE_KEY = "Bagging-Date"; 053 054 public static final String BAG_SIZE_KEY = "Bag-Size"; 055 056 public static final String PAYLOAD_OXUM_KEY = "Payload-Oxum"; 057 058 public static final String BAG_GROUP_IDENTIFIER = "Bag-Group-Identifier"; 059 060 public static final String TITLE_KEY = "Title"; 061 062 public static final String ACCESS_KEY = "Access"; 063 064 private Map<String, Map<String, String>> map; 065 066 /** 067 * Default constructor 068 * 069 * @param bagConfigFile a bagit config yaml file (see src/test/resources/bagit-config.yml) 070 */ 071 @SuppressWarnings("unchecked") 072 public BagConfig(final File bagConfigFile) { 073 final String bagConfigFilePath = bagConfigFile.getAbsolutePath(); 074 075 YamlReader reader = null; 076 try { 077 reader = new YamlReader(new FileReader(bagConfigFile)); 078 map = (Map<String, Map<String, String>>) reader.read(); 079 if (getBagInfo() == null) { 080 throw new RuntimeException("The " + BAG_INFO_KEY + " key is not present in the " + bagConfigFilePath); 081 } 082 083 084 } catch (FileNotFoundException e) { 085 throw new RuntimeException("The specified bag config file does not exist: " + bagConfigFile 086 .getAbsolutePath()); 087 } catch (Exception e) { 088 throw new RuntimeException("The specified bag config file could not be parsed: " + e.getMessage(), e); 089 } finally { 090 if (reader != null) { 091 try { 092 reader.close(); 093 } catch (IOException e) { 094 } 095 } 096 } 097 } 098 099 /** 100 * Returns an immutable map of bag info properties. 101 * 102 * @return a map of bag info properties 103 */ 104 public Map<String, String> getBagInfo() { 105 return Collections.unmodifiableMap(this.map.get(BAG_INFO_KEY)); 106 } 107 108 /** 109 * Returns an immutable map of aptrust info properties. 110 * 111 * @return a map of aptrust info properties 112 */ 113 public Map<String, String> getAPTrustInfo() { 114 return Collections.unmodifiableMap(this.map.get(APTRUST_INFO_KEY)); 115 } 116 117 /** 118 * Returns all the tag files from the config 119 * 120 * @return set of tag filenames 121 */ 122 public Set<String> getTagFiles() { 123 return map.keySet(); 124 } 125 126 /** 127 * Check if a tag file is listed in bag config 128 * 129 * @param tagFile the tag filename 130 * @return true if it is list, false if not 131 */ 132 public boolean hasTagFile(final String tagFile) { 133 return map.containsKey(tagFile); 134 } 135 136 /** 137 * Returns an immutable map of custom tags for a tag file 138 * 139 * @param tagFile name of the tag file to get fields for 140 * @return a map of filenames to key-value property maps 141 */ 142 public Map<String, String> getFieldsForTagFile(final String tagFile) { 143 return map.get(tagFile); 144 } 145}