KeyValueReader.java

  1. /*
  2.  * Copyright (C) 2023 DANS - Data Archiving and Networked Services (info@dans.knaw.nl)
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  * http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16. package nl.knaw.dans.bagit.reader;

  17. import java.io.BufferedReader;
  18. import java.io.IOException;
  19. import java.nio.charset.Charset;
  20. import java.nio.file.Files;
  21. import java.nio.file.Path;
  22. import java.util.AbstractMap.SimpleImmutableEntry;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. import java.util.ResourceBundle;

  26. import org.slf4j.Logger;
  27. import org.slf4j.LoggerFactory;
  28. import org.slf4j.helpers.MessageFormatter;

  29. import nl.knaw.dans.bagit.exceptions.InvalidBagMetadataException;

  30. /**
  31.  * Convenience class for reading key value pairs from a file
  32.  */
  33. public final class KeyValueReader {
  34.   private static final Logger logger = LoggerFactory.getLogger(KeyValueReader.class);
  35.   private static final String INDENTED_LINE_REGEX = "^\\s+.*";
  36.   private static final ResourceBundle messages = ResourceBundle.getBundle("MessageBundle");

  37.   private KeyValueReader(){
  38.     //intentionall left blank
  39.   }
  40.  

  41.   /**
  42.    * Generic method to read key value pairs from the bagit files, like bagit.txt or bag-info.txt
  43.    *
  44.    * @param file the file to read
  45.    * @param splitRegex how to split the key from the value
  46.    * @param charset the encoding of the file
  47.    *
  48.    * @return a list of key value pairs
  49.    *
  50.    * @throws IOException if there was a problem reading the file
  51.    * @throws InvalidBagMetadataException if the file does not conform to pattern of key value
  52.    */
  53.   @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
  54.   public static List<SimpleImmutableEntry<String, String>> readKeyValuesFromFile(final Path file, final String splitRegex, final Charset charset) throws IOException, InvalidBagMetadataException{
  55.     final List<SimpleImmutableEntry<String, String>> keyValues = new ArrayList<>();
  56.    
  57.     try(final BufferedReader reader = Files.newBufferedReader(file, charset)){
  58.       String line = reader.readLine();
  59.       while(line != null){
  60.         if(line.matches(INDENTED_LINE_REGEX) && !keyValues.isEmpty()){
  61.           mergeIndentedLine(line, keyValues);
  62.         }
  63.         else{
  64.           final String[] parts = checkLineFormat(line, splitRegex);
  65.           final String key = parts[0].trim();
  66.           final String value = parts[1].trim();
  67.           logger.debug(messages.getString("read_key_value_line"), key, value, file, splitRegex);
  68.           keyValues.add(new SimpleImmutableEntry<>(key, value));
  69.         }
  70.          
  71.         line = reader.readLine();
  72.       }
  73.     }
  74.    
  75.     return keyValues;
  76.   }
  77.  
  78.   private static void mergeIndentedLine(final String line, final List<SimpleImmutableEntry<String, String>> keyValues){
  79.     final SimpleImmutableEntry<String, String> oldKeyValue = keyValues.remove(keyValues.size() -1);
  80.     final SimpleImmutableEntry<String, String> newKeyValue = new SimpleImmutableEntry<>(oldKeyValue.getKey(), oldKeyValue.getValue() + System.lineSeparator() +line);
  81.     keyValues.add(newKeyValue);
  82.    
  83.     logger.debug(messages.getString("found_indented_line"), oldKeyValue.getKey());
  84.   }
  85.  
  86.   private static String[] checkLineFormat(final String line, final String splitRegex) throws InvalidBagMetadataException{
  87.     final String[] parts = line.split(splitRegex, 2);
  88.    
  89.     if(parts.length != 2){
  90.       final String formattedMessage = messages.getString("malformed_key_value_line_error");
  91.       throw new InvalidBagMetadataException(MessageFormatter.format(formattedMessage, line, splitRegex).getMessage());
  92.     }
  93.    
  94.     return parts;
  95.   }
  96. }